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 audio_sk = -1;
35 static bool close_thread = false;
37 static pthread_t ipc_th = 0;
38 static pthread_mutex_t close_mutex = PTHREAD_MUTEX_INITIALIZER;
40 struct a2dp_audio_dev {
41 struct audio_hw_device dev;
42 struct audio_stream_out *out;
45 static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
52 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
58 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
64 static size_t out_get_buffer_size(const struct audio_stream *stream)
70 static uint32_t out_get_channels(const struct audio_stream *stream)
76 static audio_format_t out_get_format(const struct audio_stream *stream)
82 static int out_set_format(struct audio_stream *stream, audio_format_t format)
88 static int out_standby(struct audio_stream *stream)
94 static int out_dump(const struct audio_stream *stream, int fd)
100 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
106 static char *out_get_parameters(const struct audio_stream *stream,
113 static uint32_t out_get_latency(const struct audio_stream_out *stream)
119 static int out_set_volume(struct audio_stream_out *stream, float left,
123 /* volume controlled in audioflinger mixer (digital) */
127 static int out_get_render_position(const struct audio_stream_out *stream,
128 uint32_t *dsp_frames)
134 static int out_add_audio_effect(const struct audio_stream *stream,
135 effect_handle_t effect)
141 static int out_remove_audio_effect(const struct audio_stream *stream,
142 effect_handle_t effect)
148 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
154 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
160 static size_t in_get_buffer_size(const struct audio_stream *stream)
166 static uint32_t in_get_channels(const struct audio_stream *stream)
172 static audio_format_t in_get_format(const struct audio_stream *stream)
178 static int in_set_format(struct audio_stream *stream, audio_format_t format)
184 static int in_standby(struct audio_stream *stream)
190 static int in_dump(const struct audio_stream *stream, int fd)
196 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
202 static char *in_get_parameters(const struct audio_stream *stream,
209 static int in_set_gain(struct audio_stream_in *stream, float gain)
215 static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
222 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
228 static int in_add_audio_effect(const struct audio_stream *stream,
229 effect_handle_t effect)
235 static int in_remove_audio_effect(const struct audio_stream *stream,
236 effect_handle_t effect)
242 static int audio_open_output_stream(struct audio_hw_device *dev,
243 audio_io_handle_t handle,
244 audio_devices_t devices,
245 audio_output_flags_t flags,
246 struct audio_config *config,
247 struct audio_stream_out **stream_out)
250 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
251 struct audio_stream_out *out;
253 out = calloc(1, sizeof(struct audio_stream_out));
259 out->common.get_sample_rate = out_get_sample_rate;
260 out->common.set_sample_rate = out_set_sample_rate;
261 out->common.get_buffer_size = out_get_buffer_size;
262 out->common.get_channels = out_get_channels;
263 out->common.get_format = out_get_format;
264 out->common.set_format = out_set_format;
265 out->common.standby = out_standby;
266 out->common.dump = out_dump;
267 out->common.set_parameters = out_set_parameters;
268 out->common.get_parameters = out_get_parameters;
269 out->common.add_audio_effect = out_add_audio_effect;
270 out->common.remove_audio_effect = out_remove_audio_effect;
271 out->get_latency = out_get_latency;
272 out->set_volume = out_set_volume;
273 out->write = out_write;
274 out->get_render_position = out_get_render_position;
282 static void audio_close_output_stream(struct audio_hw_device *dev,
283 struct audio_stream_out *stream)
286 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *)dev;
289 a2dp_dev->out = NULL;
292 static int audio_set_parameters(struct audio_hw_device *dev,
299 static char *audio_get_parameters(const struct audio_hw_device *dev,
306 static int audio_init_check(const struct audio_hw_device *dev)
312 static int audio_set_voice_volume(struct audio_hw_device *dev, float volume)
318 static int audio_set_master_volume(struct audio_hw_device *dev, float volume)
324 static int audio_set_mode(struct audio_hw_device *dev, int mode)
330 static int audio_set_mic_mute(struct audio_hw_device *dev, bool state)
336 static int audio_get_mic_mute(const struct audio_hw_device *dev, bool *state)
342 static size_t audio_get_input_buffer_size(const struct audio_hw_device *dev,
343 const struct audio_config *config)
349 static int audio_open_input_stream(struct audio_hw_device *dev,
350 audio_io_handle_t handle,
351 audio_devices_t devices,
352 struct audio_config *config,
353 struct audio_stream_in **stream_in)
355 struct audio_stream_in *in;
359 in = calloc(1, sizeof(struct audio_stream_in));
363 in->common.get_sample_rate = in_get_sample_rate;
364 in->common.set_sample_rate = in_set_sample_rate;
365 in->common.get_buffer_size = in_get_buffer_size;
366 in->common.get_channels = in_get_channels;
367 in->common.get_format = in_get_format;
368 in->common.set_format = in_set_format;
369 in->common.standby = in_standby;
370 in->common.dump = in_dump;
371 in->common.set_parameters = in_set_parameters;
372 in->common.get_parameters = in_get_parameters;
373 in->common.add_audio_effect = in_add_audio_effect;
374 in->common.remove_audio_effect = in_remove_audio_effect;
375 in->set_gain = in_set_gain;
377 in->get_input_frames_lost = in_get_input_frames_lost;
384 static void audio_close_input_stream(struct audio_hw_device *dev,
385 struct audio_stream_in *stream_in)
391 static int audio_dump(const audio_hw_device_t *device, int fd)
397 static int audio_close(hw_device_t *device)
399 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *)device;
403 pthread_mutex_lock(&close_mutex);
404 shutdown(audio_sk, SHUT_RDWR);
406 pthread_mutex_unlock(&close_mutex);
408 pthread_join(ipc_th, NULL);
414 static bool create_audio_ipc(void)
416 struct sockaddr_un addr;
422 sk = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
425 error("Failed to create socket: %d (%s)", err, strerror(err));
429 memset(&addr, 0, sizeof(addr));
430 addr.sun_family = AF_UNIX;
432 memcpy(addr.sun_path, BLUEZ_AUDIO_SK_PATH,
433 sizeof(BLUEZ_AUDIO_SK_PATH));
435 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
437 error("Failed to bind socket: %d (%s)", err, strerror(err));
441 if (listen(sk, 1) < 0) {
443 error("Failed to listen on the socket: %d (%s)", err,
448 audio_sk = accept(sk, NULL, NULL);
451 error("Failed to accept socket: %d (%s)", err, strerror(err));
463 static void *ipc_handler(void *data)
471 if(!create_audio_ipc()) {
472 error("Failed to create listening socket");
477 DBG("Audio IPC: Connected");
479 /* TODO: Register ENDPOINT here */
481 memset(&pfd, 0, sizeof(pfd));
483 pfd.events = POLLHUP | POLLERR | POLLNVAL;
485 /* Check if socket is still alive. Empty while loop.*/
486 while (poll(&pfd, 1, -1) < 0 && errno == EINTR);
488 if (pfd.revents & (POLLHUP | POLLERR | POLLNVAL)) {
489 info("Audio HAL: Socket closed");
493 /*Check if audio_dev is closed */
494 pthread_mutex_lock(&close_mutex);
496 close_thread = false;
497 pthread_mutex_unlock(&close_mutex);
500 info("Closing bluetooth_watcher thread");
504 static int audio_open(const hw_module_t *module, const char *name,
505 hw_device_t **device)
507 struct a2dp_audio_dev *a2dp_dev;
512 if (strcmp(name, AUDIO_HARDWARE_INTERFACE)) {
513 error("interface %s not matching [%s]", name,
514 AUDIO_HARDWARE_INTERFACE);
518 a2dp_dev = calloc(1, sizeof(struct a2dp_audio_dev));
522 a2dp_dev->dev.common.version = AUDIO_DEVICE_API_VERSION_CURRENT;
523 a2dp_dev->dev.common.module = (struct hw_module_t *) module;
524 a2dp_dev->dev.common.close = audio_close;
526 a2dp_dev->dev.init_check = audio_init_check;
527 a2dp_dev->dev.set_voice_volume = audio_set_voice_volume;
528 a2dp_dev->dev.set_master_volume = audio_set_master_volume;
529 a2dp_dev->dev.set_mode = audio_set_mode;
530 a2dp_dev->dev.set_mic_mute = audio_set_mic_mute;
531 a2dp_dev->dev.get_mic_mute = audio_get_mic_mute;
532 a2dp_dev->dev.set_parameters = audio_set_parameters;
533 a2dp_dev->dev.get_parameters = audio_get_parameters;
534 a2dp_dev->dev.get_input_buffer_size = audio_get_input_buffer_size;
535 a2dp_dev->dev.open_output_stream = audio_open_output_stream;
536 a2dp_dev->dev.close_output_stream = audio_close_output_stream;
537 a2dp_dev->dev.open_input_stream = audio_open_input_stream;
538 a2dp_dev->dev.close_input_stream = audio_close_input_stream;
539 a2dp_dev->dev.dump = audio_dump;
541 /* Note that &a2dp_dev->dev.common is the same pointer as a2dp_dev.
542 * This results from the structure of following structs:a2dp_audio_dev,
543 * audio_hw_device. We will rely on this later in the code.*/
544 *device = &a2dp_dev->dev.common;
546 err = pthread_create(&ipc_th, NULL, ipc_handler, NULL);
549 error("Failed to start Audio IPC thread: %d (%s)",
550 -err, strerror(-err));
557 static struct hw_module_methods_t hal_module_methods = {
561 struct audio_module HAL_MODULE_INFO_SYM = {
563 .tag = HARDWARE_MODULE_TAG,
566 .id = AUDIO_HARDWARE_MODULE_ID,
567 .name = "A2DP Bluez HW HAL",
568 .author = "Intel Corporation",
569 .methods = &hal_module_methods,