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)
285 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
290 a2dp_dev->out = NULL;
293 static int audio_set_parameters(struct audio_hw_device *dev,
300 static char *audio_get_parameters(const struct audio_hw_device *dev,
307 static int audio_init_check(const struct audio_hw_device *dev)
313 static int audio_set_voice_volume(struct audio_hw_device *dev, float volume)
319 static int audio_set_master_volume(struct audio_hw_device *dev, float volume)
325 static int audio_set_mode(struct audio_hw_device *dev, int mode)
331 static int audio_set_mic_mute(struct audio_hw_device *dev, bool state)
337 static int audio_get_mic_mute(const struct audio_hw_device *dev, bool *state)
343 static size_t audio_get_input_buffer_size(const struct audio_hw_device *dev,
344 const struct audio_config *config)
350 static int audio_open_input_stream(struct audio_hw_device *dev,
351 audio_io_handle_t handle,
352 audio_devices_t devices,
353 struct audio_config *config,
354 struct audio_stream_in **stream_in)
356 struct audio_stream_in *in;
360 in = calloc(1, sizeof(struct audio_stream_in));
364 in->common.get_sample_rate = in_get_sample_rate;
365 in->common.set_sample_rate = in_set_sample_rate;
366 in->common.get_buffer_size = in_get_buffer_size;
367 in->common.get_channels = in_get_channels;
368 in->common.get_format = in_get_format;
369 in->common.set_format = in_set_format;
370 in->common.standby = in_standby;
371 in->common.dump = in_dump;
372 in->common.set_parameters = in_set_parameters;
373 in->common.get_parameters = in_get_parameters;
374 in->common.add_audio_effect = in_add_audio_effect;
375 in->common.remove_audio_effect = in_remove_audio_effect;
376 in->set_gain = in_set_gain;
378 in->get_input_frames_lost = in_get_input_frames_lost;
385 static void audio_close_input_stream(struct audio_hw_device *dev,
386 struct audio_stream_in *stream_in)
392 static int audio_dump(const audio_hw_device_t *device, int fd)
398 static int audio_close(hw_device_t *device)
400 struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *)device;
404 pthread_mutex_lock(&close_mutex);
405 shutdown(audio_sk, SHUT_RDWR);
407 pthread_mutex_unlock(&close_mutex);
409 pthread_join(ipc_th, NULL);
415 static bool create_audio_ipc(void)
417 struct sockaddr_un addr;
423 sk = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
426 error("audio: Failed to create socket: %d (%s)", err,
431 memset(&addr, 0, sizeof(addr));
432 addr.sun_family = AF_UNIX;
434 memcpy(addr.sun_path, BLUEZ_AUDIO_SK_PATH,
435 sizeof(BLUEZ_AUDIO_SK_PATH));
437 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
439 error("audio: Failed to bind socket: %d (%s)", err,
444 if (listen(sk, 1) < 0) {
446 error("audio: Failed to listen on the socket: %d (%s)", err,
451 audio_sk = accept(sk, NULL, NULL);
454 error("audio: Failed to accept socket: %d (%s)", err, strerror(err));
466 static void *ipc_handler(void *data)
474 if(!create_audio_ipc()) {
475 error("audio: Failed to create listening socket");
480 DBG("Audio IPC: Connected");
482 /* TODO: Register ENDPOINT here */
484 memset(&pfd, 0, sizeof(pfd));
486 pfd.events = POLLHUP | POLLERR | POLLNVAL;
488 /* Check if socket is still alive. Empty while loop.*/
489 while (poll(&pfd, 1, -1) < 0 && errno == EINTR);
491 if (pfd.revents & (POLLHUP | POLLERR | POLLNVAL)) {
492 info("Audio HAL: Socket closed");
496 /*Check if audio_dev is closed */
497 pthread_mutex_lock(&close_mutex);
499 close_thread = false;
500 pthread_mutex_unlock(&close_mutex);
503 info("Closing bluetooth_watcher thread");
507 static int audio_open(const hw_module_t *module, const char *name,
508 hw_device_t **device)
510 struct a2dp_audio_dev *a2dp_dev;
515 if (strcmp(name, AUDIO_HARDWARE_INTERFACE)) {
516 error("audio: interface %s not matching [%s]", name,
517 AUDIO_HARDWARE_INTERFACE);
521 a2dp_dev = calloc(1, sizeof(struct a2dp_audio_dev));
525 a2dp_dev->dev.common.version = AUDIO_DEVICE_API_VERSION_CURRENT;
526 a2dp_dev->dev.common.module = (struct hw_module_t *) module;
527 a2dp_dev->dev.common.close = audio_close;
529 a2dp_dev->dev.init_check = audio_init_check;
530 a2dp_dev->dev.set_voice_volume = audio_set_voice_volume;
531 a2dp_dev->dev.set_master_volume = audio_set_master_volume;
532 a2dp_dev->dev.set_mode = audio_set_mode;
533 a2dp_dev->dev.set_mic_mute = audio_set_mic_mute;
534 a2dp_dev->dev.get_mic_mute = audio_get_mic_mute;
535 a2dp_dev->dev.set_parameters = audio_set_parameters;
536 a2dp_dev->dev.get_parameters = audio_get_parameters;
537 a2dp_dev->dev.get_input_buffer_size = audio_get_input_buffer_size;
538 a2dp_dev->dev.open_output_stream = audio_open_output_stream;
539 a2dp_dev->dev.close_output_stream = audio_close_output_stream;
540 a2dp_dev->dev.open_input_stream = audio_open_input_stream;
541 a2dp_dev->dev.close_input_stream = audio_close_input_stream;
542 a2dp_dev->dev.dump = audio_dump;
544 /* Note that &a2dp_dev->dev.common is the same pointer as a2dp_dev.
545 * This results from the structure of following structs:a2dp_audio_dev,
546 * audio_hw_device. We will rely on this later in the code.*/
547 *device = &a2dp_dev->dev.common;
549 err = pthread_create(&ipc_th, NULL, ipc_handler, NULL);
552 error("audio: Failed to start Audio IPC thread: %d (%s)",
560 static struct hw_module_methods_t hal_module_methods = {
564 struct audio_module HAL_MODULE_INFO_SYM = {
566 .tag = HARDWARE_MODULE_TAG,
569 .id = AUDIO_HARDWARE_MODULE_ID,
570 .name = "A2DP Bluez HW HAL",
571 .author = "Intel Corporation",
572 .methods = &hal_module_methods,