OSDN Git Service

android/hal-audio: Add support to open output stream
[android-x86/external-bluetooth-bluez.git] / android / hal-audio.c
1 /*
2  * Copyright (C) 2013 Intel Corporation
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <errno.h>
19 #include <pthread.h>
20 #include <poll.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/socket.h>
25 #include <sys/un.h>
26 #include <unistd.h>
27
28 #include <hardware/audio.h>
29 #include <hardware/hardware.h>
30
31 #include "audio-msg.h"
32 #include "hal-log.h"
33 #include "hal-msg.h"
34 #include "../profiles/audio/a2dp-codecs.h"
35
36 static const uint8_t a2dp_src_uuid[] = {
37                 0x00, 0x00, 0x11, 0x0a, 0x00, 0x00, 0x10, 0x00,
38                 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb };
39
40 static int listen_sk = -1;
41 static int audio_sk = -1;
42 static bool close_thread = false;
43
44 static pthread_t ipc_th = 0;
45 static pthread_mutex_t close_mutex = PTHREAD_MUTEX_INITIALIZER;
46 static pthread_mutex_t sk_mutex = PTHREAD_MUTEX_INITIALIZER;
47
48 struct audio_input_config {
49         uint32_t rate;
50         uint32_t channels;
51         audio_format_t format;
52 };
53
54 static int sbc_get_presets(struct audio_preset *preset, size_t *len);
55
56 struct audio_codec {
57         uint8_t type;
58
59         int (*get_presets) (struct audio_preset *preset, size_t *len);
60
61         int (*init) (struct audio_preset *preset, void **codec_data);
62         int (*cleanup) (void *codec_data);
63         int (*get_config) (void *codec_data,
64                                         struct audio_input_config *config);
65         ssize_t (*write_data) (void *codec_data, const void *buffer,
66                                 size_t bytes);
67 };
68
69 static const struct audio_codec audio_codecs[] = {
70         {
71                 .type = A2DP_CODEC_SBC,
72
73                 .get_presets = sbc_get_presets,
74         }
75 };
76
77 #define NUM_CODECS (sizeof(audio_codecs) / sizeof(audio_codecs[0]))
78
79 #define MAX_AUDIO_ENDPOINTS NUM_CODECS
80
81 struct audio_endpoint {
82         uint8_t id;
83         const struct audio_codec *codec;
84         void *codec_data;
85         int fd;
86 };
87
88 static struct audio_endpoint audio_endpoints[MAX_AUDIO_ENDPOINTS];
89
90 enum a2dp_state_t {
91         AUDIO_A2DP_STATE_NONE,
92         AUDIO_A2DP_STATE_STANDBY,
93         AUDIO_A2DP_STATE_SUSPENDED,
94         AUDIO_A2DP_STATE_STARTED
95 };
96
97 struct a2dp_stream_out {
98         struct audio_stream_out stream;
99
100         struct audio_endpoint *ep;
101         enum a2dp_state_t audio_state;
102 };
103
104 struct a2dp_audio_dev {
105         struct audio_hw_device dev;
106         struct a2dp_stream_out *out;
107 };
108
109 static const a2dp_sbc_t sbc_presets[] = {
110         {
111                 .frequency = SBC_SAMPLING_FREQ_44100 | SBC_SAMPLING_FREQ_48000,
112                 .channel_mode = SBC_CHANNEL_MODE_MONO |
113                                 SBC_CHANNEL_MODE_DUAL_CHANNEL |
114                                 SBC_CHANNEL_MODE_STEREO |
115                                 SBC_CHANNEL_MODE_JOINT_STEREO,
116                 .subbands = SBC_SUBBANDS_4 | SBC_SUBBANDS_8,
117                 .allocation_method = SBC_ALLOCATION_SNR |
118                                         SBC_ALLOCATION_LOUDNESS,
119                 .block_length = SBC_BLOCK_LENGTH_4 | SBC_BLOCK_LENGTH_8 |
120                                 SBC_BLOCK_LENGTH_12 | SBC_BLOCK_LENGTH_16,
121                 .min_bitpool = MIN_BITPOOL,
122                 .max_bitpool = MAX_BITPOOL
123         },
124         {
125                 .frequency = SBC_SAMPLING_FREQ_44100,
126                 .channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO,
127                 .subbands = SBC_SUBBANDS_8,
128                 .allocation_method = SBC_ALLOCATION_LOUDNESS,
129                 .block_length = SBC_BLOCK_LENGTH_16,
130                 .min_bitpool = MIN_BITPOOL,
131                 .max_bitpool = MAX_BITPOOL
132         },
133         {
134                 .frequency = SBC_SAMPLING_FREQ_48000,
135                 .channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO,
136                 .subbands = SBC_SUBBANDS_8,
137                 .allocation_method = SBC_ALLOCATION_LOUDNESS,
138                 .block_length = SBC_BLOCK_LENGTH_16,
139                 .min_bitpool = MIN_BITPOOL,
140                 .max_bitpool = MAX_BITPOOL
141         },
142 };
143
144 static int sbc_get_presets(struct audio_preset *preset, size_t *len)
145 {
146         int i;
147         int count;
148         size_t new_len = 0;
149         uint8_t *ptr = (uint8_t *) preset;
150         size_t preset_size = sizeof(*preset) + sizeof(a2dp_sbc_t);
151
152         DBG("");
153
154         count = sizeof(sbc_presets) / sizeof(sbc_presets[0]);
155
156         for (i = 0; i < count; i++) {
157                 preset = (struct audio_preset *) ptr;
158
159                 if (new_len + preset_size > *len)
160                         break;
161
162                 preset->len = sizeof(a2dp_sbc_t);
163                 memcpy(preset->data, &sbc_presets[i], preset->len);
164
165                 new_len += preset_size;
166                 ptr += preset_size;
167         }
168
169         *len = new_len;
170
171         return i;
172 }
173
174 static void audio_ipc_cleanup(void)
175 {
176         if (audio_sk >= 0) {
177                 shutdown(audio_sk, SHUT_RDWR);
178                 audio_sk = -1;
179         }
180 }
181
182 static int audio_ipc_cmd(uint8_t service_id, uint8_t opcode, uint16_t len,
183                         void *param, size_t *rsp_len, void *rsp, int *fd)
184 {
185         ssize_t ret;
186         struct msghdr msg;
187         struct iovec iv[2];
188         struct hal_hdr cmd;
189         char cmsgbuf[CMSG_SPACE(sizeof(int))];
190         struct hal_status s;
191         size_t s_len = sizeof(s);
192
193         if (audio_sk < 0) {
194                 error("audio: Invalid cmd socket passed to audio_ipc_cmd");
195                 goto failed;
196         }
197
198         if (!rsp || !rsp_len) {
199                 memset(&s, 0, s_len);
200                 rsp_len = &s_len;
201                 rsp = &s;
202         }
203
204         memset(&msg, 0, sizeof(msg));
205         memset(&cmd, 0, sizeof(cmd));
206
207         cmd.service_id = service_id;
208         cmd.opcode = opcode;
209         cmd.len = len;
210
211         iv[0].iov_base = &cmd;
212         iv[0].iov_len = sizeof(cmd);
213
214         iv[1].iov_base = param;
215         iv[1].iov_len = len;
216
217         msg.msg_iov = iv;
218         msg.msg_iovlen = 2;
219
220         pthread_mutex_lock(&sk_mutex);
221
222         ret = sendmsg(audio_sk, &msg, 0);
223         if (ret < 0) {
224                 error("audio: Sending command failed:%s", strerror(errno));
225                 pthread_mutex_unlock(&sk_mutex);
226                 goto failed;
227         }
228
229         /* socket was shutdown */
230         if (ret == 0) {
231                 error("audio: Command socket closed");
232                 goto failed;
233         }
234
235         memset(&msg, 0, sizeof(msg));
236         memset(&cmd, 0, sizeof(cmd));
237
238         iv[0].iov_base = &cmd;
239         iv[0].iov_len = sizeof(cmd);
240
241         iv[1].iov_base = rsp;
242         iv[1].iov_len = *rsp_len;
243
244         msg.msg_iov = iv;
245         msg.msg_iovlen = 2;
246
247         if (fd) {
248                 memset(cmsgbuf, 0, sizeof(cmsgbuf));
249                 msg.msg_control = cmsgbuf;
250                 msg.msg_controllen = sizeof(cmsgbuf);
251         }
252
253         ret = recvmsg(audio_sk, &msg, 0);
254         if (ret < 0) {
255                 error("audio: Receiving command response failed:%s",
256                                                         strerror(errno));
257                 pthread_mutex_unlock(&sk_mutex);
258                 goto failed;
259         }
260
261         pthread_mutex_unlock(&sk_mutex);
262
263         if (ret < (ssize_t) sizeof(cmd)) {
264                 error("audio: Too small response received(%zd bytes)", ret);
265                 goto failed;
266         }
267
268         if (cmd.service_id != service_id) {
269                 error("audio: Invalid service id (%u vs %u)", cmd.service_id,
270                                                                 service_id);
271                 goto failed;
272         }
273
274         if (ret != (ssize_t) (sizeof(cmd) + cmd.len)) {
275                 error("audio: Malformed response received(%zd bytes)", ret);
276                 goto failed;
277         }
278
279         if (cmd.opcode != opcode && cmd.opcode != AUDIO_OP_STATUS) {
280                 error("audio: Invalid opcode received (%u vs %u)",
281                                                 cmd.opcode, opcode);
282                 goto failed;
283         }
284
285         if (cmd.opcode == AUDIO_OP_STATUS) {
286                 struct hal_status *s = rsp;
287
288                 if (sizeof(*s) != cmd.len) {
289                         error("audio: Invalid status length");
290                         goto failed;
291                 }
292
293                 if (s->code == AUDIO_STATUS_SUCCESS) {
294                         error("audio: Invalid success status response");
295                         goto failed;
296                 }
297
298                 return s->code;
299         }
300
301         /* Receive auxiliary data in msg */
302         if (fd) {
303                 struct cmsghdr *cmsg;
304
305                 *fd = -1;
306
307                 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
308                                         cmsg = CMSG_NXTHDR(&msg, cmsg)) {
309                         if (cmsg->cmsg_level == SOL_SOCKET
310                                         && cmsg->cmsg_type == SCM_RIGHTS) {
311                                 memcpy(fd, CMSG_DATA(cmsg), sizeof(int));
312                                 break;
313                         }
314                 }
315         }
316
317         if (rsp_len)
318                 *rsp_len = cmd.len;
319
320         return AUDIO_STATUS_SUCCESS;
321
322 failed:
323         /* Some serious issue happen on IPC - recover */
324         shutdown(audio_sk, SHUT_RDWR);
325         audio_sk = -1;
326         return AUDIO_STATUS_FAILED;
327 }
328
329 static int ipc_open_cmd(const struct audio_codec *codec)
330 {
331         uint8_t buf[BLUEZ_AUDIO_MTU];
332         struct audio_cmd_open *cmd = (struct audio_cmd_open *) buf;
333         struct audio_rsp_open rsp;
334         size_t cmd_len = sizeof(buf) - sizeof(*cmd);
335         size_t rsp_len = sizeof(rsp);
336         int result;
337
338         DBG("");
339
340         memcpy(cmd->uuid, a2dp_src_uuid, sizeof(a2dp_src_uuid));
341
342         cmd->codec = codec->type;
343         cmd->presets = codec->get_presets(cmd->preset, &cmd_len);
344
345         cmd_len += sizeof(*cmd);
346
347         result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_OPEN, cmd_len, cmd,
348                                 &rsp_len, &rsp, NULL);
349
350         if (result != AUDIO_STATUS_SUCCESS)
351                 return 0;
352
353         return rsp.id;
354 }
355
356 static int ipc_close_cmd(uint8_t endpoint_id)
357 {
358         struct audio_cmd_close cmd;
359         int result;
360
361         DBG("");
362
363         cmd.id = endpoint_id;
364
365         result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_CLOSE,
366                                 sizeof(cmd), &cmd, NULL, NULL, NULL);
367
368         return result;
369 }
370
371 static int ipc_open_stream_cmd(uint8_t endpoint_id,
372                                         struct audio_preset **caps)
373 {
374         char buf[BLUEZ_AUDIO_MTU];
375         struct audio_cmd_open_stream cmd;
376         struct audio_rsp_open_stream *rsp =
377                                         (struct audio_rsp_open_stream *) &buf;
378         size_t rsp_len = sizeof(buf);
379         int result;
380
381         DBG("");
382
383         if (!caps)
384                 return AUDIO_STATUS_FAILED;
385
386         cmd.id = endpoint_id;
387
388         result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_OPEN_STREAM,
389                                 sizeof(cmd), &cmd, &rsp_len, rsp, NULL);
390
391         if (result == AUDIO_STATUS_SUCCESS) {
392                 size_t buf_len = sizeof(struct audio_preset) +
393                                         rsp->preset[0].len;
394                 *caps = malloc(buf_len);
395                 memcpy(*caps, &rsp->preset, buf_len);
396         } else {
397                 *caps = NULL;
398         }
399
400         return result;
401 }
402
403 static int register_endpoints(void)
404 {
405         struct audio_endpoint *ep = &audio_endpoints[0];
406         size_t i;
407
408         for (i = 0; i < NUM_CODECS; i++, ep++) {
409                 const struct audio_codec *codec = &audio_codecs[i];
410
411                 ep->id = ipc_open_cmd(codec);
412
413                 if (!ep->id)
414                         return AUDIO_STATUS_FAILED;
415
416                 ep->codec = codec;
417                 ep->codec_data = NULL;
418                 ep->fd = -1;
419         }
420
421         return AUDIO_STATUS_SUCCESS;
422 }
423
424 static void unregister_endpoints(void)
425 {
426         size_t i;
427
428         for (i = 0; i < MAX_AUDIO_ENDPOINTS; i++) {
429                 struct audio_endpoint *ep = &audio_endpoints[i];
430
431                 if (ep->id) {
432                         ipc_close_cmd(ep->id);
433                         memset(ep, 0, sizeof(*ep));
434                 }
435         }
436 }
437
438 static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
439                                                                 size_t bytes)
440 {
441         DBG("");
442         return -ENOSYS;
443 }
444
445 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
446 {
447         DBG("");
448         return -ENOSYS;
449 }
450
451 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
452 {
453         DBG("");
454         return -ENOSYS;
455 }
456
457 static size_t out_get_buffer_size(const struct audio_stream *stream)
458 {
459         DBG("");
460         return -ENOSYS;
461 }
462
463 static uint32_t out_get_channels(const struct audio_stream *stream)
464 {
465         DBG("");
466         return -ENOSYS;
467 }
468
469 static audio_format_t out_get_format(const struct audio_stream *stream)
470 {
471         DBG("");
472         return -ENOSYS;
473 }
474
475 static int out_set_format(struct audio_stream *stream, audio_format_t format)
476 {
477         DBG("");
478         return -ENOSYS;
479 }
480
481 static int out_standby(struct audio_stream *stream)
482 {
483         DBG("");
484         return -ENOSYS;
485 }
486
487 static int out_dump(const struct audio_stream *stream, int fd)
488 {
489         DBG("");
490         return -ENOSYS;
491 }
492
493 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
494 {
495         DBG("");
496         return -ENOSYS;
497 }
498
499 static char *out_get_parameters(const struct audio_stream *stream,
500                                                         const char *keys)
501 {
502         DBG("");
503         return strdup("");
504 }
505
506 static uint32_t out_get_latency(const struct audio_stream_out *stream)
507 {
508         DBG("");
509         return -ENOSYS;
510 }
511
512 static int out_set_volume(struct audio_stream_out *stream, float left,
513                                                                 float right)
514 {
515         DBG("");
516         /* volume controlled in audioflinger mixer (digital) */
517         return -ENOSYS;
518 }
519
520 static int out_get_render_position(const struct audio_stream_out *stream,
521                                                         uint32_t *dsp_frames)
522 {
523         DBG("");
524         return -ENOSYS;
525 }
526
527 static int out_add_audio_effect(const struct audio_stream *stream,
528                                                         effect_handle_t effect)
529 {
530         DBG("");
531         return -ENOSYS;
532 }
533
534 static int out_remove_audio_effect(const struct audio_stream *stream,
535                                                         effect_handle_t effect)
536 {
537         DBG("");
538         return -ENOSYS;
539 }
540
541 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
542 {
543         DBG("");
544         return -ENOSYS;
545 }
546
547 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
548 {
549         DBG("");
550         return -ENOSYS;
551 }
552
553 static size_t in_get_buffer_size(const struct audio_stream *stream)
554 {
555         DBG("");
556         return -ENOSYS;
557 }
558
559 static uint32_t in_get_channels(const struct audio_stream *stream)
560 {
561         DBG("");
562         return -ENOSYS;
563 }
564
565 static audio_format_t in_get_format(const struct audio_stream *stream)
566 {
567         DBG("");
568         return -ENOSYS;
569 }
570
571 static int in_set_format(struct audio_stream *stream, audio_format_t format)
572 {
573         DBG("");
574         return -ENOSYS;
575 }
576
577 static int in_standby(struct audio_stream *stream)
578 {
579         DBG("");
580         return -ENOSYS;
581 }
582
583 static int in_dump(const struct audio_stream *stream, int fd)
584 {
585         DBG("");
586         return -ENOSYS;
587 }
588
589 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
590 {
591         DBG("");
592         return -ENOSYS;
593 }
594
595 static char *in_get_parameters(const struct audio_stream *stream,
596                                                         const char *keys)
597 {
598         DBG("");
599         return strdup("");
600 }
601
602 static int in_set_gain(struct audio_stream_in *stream, float gain)
603 {
604         DBG("");
605         return -ENOSYS;
606 }
607
608 static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
609                                                                 size_t bytes)
610 {
611         DBG("");
612         return -ENOSYS;
613 }
614
615 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
616 {
617         DBG("");
618         return -ENOSYS;
619 }
620
621 static int in_add_audio_effect(const struct audio_stream *stream,
622                                                         effect_handle_t effect)
623 {
624         DBG("");
625         return -ENOSYS;
626 }
627
628 static int in_remove_audio_effect(const struct audio_stream *stream,
629                                                         effect_handle_t effect)
630 {
631         DBG("");
632         return -ENOSYS;
633 }
634
635 static int audio_open_output_stream(struct audio_hw_device *dev,
636                                         audio_io_handle_t handle,
637                                         audio_devices_t devices,
638                                         audio_output_flags_t flags,
639                                         struct audio_config *config,
640                                         struct audio_stream_out **stream_out)
641
642 {
643         struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
644         struct a2dp_stream_out *out;
645         struct audio_preset *preset;
646
647         out = calloc(1, sizeof(struct a2dp_stream_out));
648         if (!out)
649                 return -ENOMEM;
650
651         DBG("");
652
653         out->stream.common.get_sample_rate = out_get_sample_rate;
654         out->stream.common.set_sample_rate = out_set_sample_rate;
655         out->stream.common.get_buffer_size = out_get_buffer_size;
656         out->stream.common.get_channels = out_get_channels;
657         out->stream.common.get_format = out_get_format;
658         out->stream.common.set_format = out_set_format;
659         out->stream.common.standby = out_standby;
660         out->stream.common.dump = out_dump;
661         out->stream.common.set_parameters = out_set_parameters;
662         out->stream.common.get_parameters = out_get_parameters;
663         out->stream.common.add_audio_effect = out_add_audio_effect;
664         out->stream.common.remove_audio_effect = out_remove_audio_effect;
665         out->stream.get_latency = out_get_latency;
666         out->stream.set_volume = out_set_volume;
667         out->stream.write = out_write;
668         out->stream.get_render_position = out_get_render_position;
669
670         /* TODO: for now we always use endpoint 0 */
671         out->ep = &audio_endpoints[0];
672
673         if (ipc_open_stream_cmd(out->ep->id, &preset) != AUDIO_STATUS_SUCCESS)
674                 goto fail;
675
676         if (!preset)
677                 goto fail;
678
679         /* TODO: initialize codec using received audio_preset */
680
681         free(preset);
682
683         *stream_out = &out->stream;
684         a2dp_dev->out = out;
685
686         out->audio_state = AUDIO_A2DP_STATE_STANDBY;
687
688         return 0;
689
690 fail:
691         free(out);
692         *stream_out = NULL;
693         return -EIO;
694 }
695
696 static void audio_close_output_stream(struct audio_hw_device *dev,
697                                         struct audio_stream_out *stream)
698 {
699         struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
700
701         DBG("");
702
703         free(stream);
704         a2dp_dev->out = NULL;
705 }
706
707 static int audio_set_parameters(struct audio_hw_device *dev,
708                                                         const char *kvpairs)
709 {
710         DBG("");
711         return -ENOSYS;
712 }
713
714 static char *audio_get_parameters(const struct audio_hw_device *dev,
715                                                         const char *keys)
716 {
717         DBG("");
718         return strdup("");
719 }
720
721 static int audio_init_check(const struct audio_hw_device *dev)
722 {
723         DBG("");
724         return -ENOSYS;
725 }
726
727 static int audio_set_voice_volume(struct audio_hw_device *dev, float volume)
728 {
729         DBG("");
730         return -ENOSYS;
731 }
732
733 static int audio_set_master_volume(struct audio_hw_device *dev, float volume)
734 {
735         DBG("");
736         return -ENOSYS;
737 }
738
739 static int audio_set_mode(struct audio_hw_device *dev, int mode)
740 {
741         DBG("");
742         return -ENOSYS;
743 }
744
745 static int audio_set_mic_mute(struct audio_hw_device *dev, bool state)
746 {
747         DBG("");
748         return -ENOSYS;
749 }
750
751 static int audio_get_mic_mute(const struct audio_hw_device *dev, bool *state)
752 {
753         DBG("");
754         return -ENOSYS;
755 }
756
757 static size_t audio_get_input_buffer_size(const struct audio_hw_device *dev,
758                                         const struct audio_config *config)
759 {
760         DBG("");
761         return -ENOSYS;
762 }
763
764 static int audio_open_input_stream(struct audio_hw_device *dev,
765                                         audio_io_handle_t handle,
766                                         audio_devices_t devices,
767                                         struct audio_config *config,
768                                         struct audio_stream_in **stream_in)
769 {
770         struct audio_stream_in *in;
771
772         DBG("");
773
774         in = calloc(1, sizeof(struct audio_stream_in));
775         if (!in)
776                 return -ENOMEM;
777
778         in->common.get_sample_rate = in_get_sample_rate;
779         in->common.set_sample_rate = in_set_sample_rate;
780         in->common.get_buffer_size = in_get_buffer_size;
781         in->common.get_channels = in_get_channels;
782         in->common.get_format = in_get_format;
783         in->common.set_format = in_set_format;
784         in->common.standby = in_standby;
785         in->common.dump = in_dump;
786         in->common.set_parameters = in_set_parameters;
787         in->common.get_parameters = in_get_parameters;
788         in->common.add_audio_effect = in_add_audio_effect;
789         in->common.remove_audio_effect = in_remove_audio_effect;
790         in->set_gain = in_set_gain;
791         in->read = in_read;
792         in->get_input_frames_lost = in_get_input_frames_lost;
793
794         *stream_in = in;
795
796         return 0;
797 }
798
799 static void audio_close_input_stream(struct audio_hw_device *dev,
800                                         struct audio_stream_in *stream_in)
801 {
802         DBG("");
803         free(stream_in);
804 }
805
806 static int audio_dump(const audio_hw_device_t *device, int fd)
807 {
808         DBG("");
809         return -ENOSYS;
810 }
811
812 static int audio_close(hw_device_t *device)
813 {
814         struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *)device;
815
816         DBG("");
817
818         pthread_mutex_lock(&close_mutex);
819         audio_ipc_cleanup();
820         close_thread = true;
821         pthread_mutex_unlock(&close_mutex);
822
823         pthread_join(ipc_th, NULL);
824
825         close(listen_sk);
826         listen_sk = -1;
827
828         free(a2dp_dev);
829         return 0;
830 }
831
832 static void *ipc_handler(void *data)
833 {
834         bool done = false;
835         struct pollfd pfd;
836
837         DBG("");
838
839         while (!done) {
840                 DBG("Waiting for connection ...");
841                 audio_sk = accept(listen_sk, NULL, NULL);
842                 if (audio_sk < 0) {
843                         int err = errno;
844                         error("audio: Failed to accept socket: %d (%s)", err,
845                                                                 strerror(err));
846                         continue;
847                 }
848
849                 DBG("Audio IPC: Connected");
850
851                 if (register_endpoints() != AUDIO_STATUS_SUCCESS) {
852                         error("audio: Failed to register endpoints");
853
854                         unregister_endpoints();
855
856                         shutdown(audio_sk, SHUT_RDWR);
857                         continue;
858                 }
859
860                 memset(&pfd, 0, sizeof(pfd));
861                 pfd.fd = audio_sk;
862                 pfd.events = POLLHUP | POLLERR | POLLNVAL;
863
864                 /* Check if socket is still alive. Empty while loop.*/
865                 while (poll(&pfd, 1, -1) < 0 && errno == EINTR);
866
867                 if (pfd.revents & (POLLHUP | POLLERR | POLLNVAL)) {
868                         info("Audio HAL: Socket closed");
869                         audio_sk = -1;
870                 }
871
872                 /*Check if audio_dev is closed */
873                 pthread_mutex_lock(&close_mutex);
874                 done = close_thread;
875                 close_thread = false;
876                 pthread_mutex_unlock(&close_mutex);
877         }
878
879         unregister_endpoints();
880
881         info("Closing Audio IPC thread");
882         return NULL;
883 }
884
885 static int audio_ipc_init(void)
886 {
887         struct sockaddr_un addr;
888         int err;
889         int sk;
890
891         DBG("");
892
893         sk = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
894         if (sk < 0) {
895                 err = errno;
896                 error("audio: Failed to create socket: %d (%s)", err,
897                                                                 strerror(err));
898                 return err;
899         }
900
901         memset(&addr, 0, sizeof(addr));
902         addr.sun_family = AF_UNIX;
903
904         memcpy(addr.sun_path, BLUEZ_AUDIO_SK_PATH,
905                                         sizeof(BLUEZ_AUDIO_SK_PATH));
906
907         if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
908                 err = errno;
909                 error("audio: Failed to bind socket: %d (%s)", err,
910                                                                 strerror(err));
911                 goto failed;
912         }
913
914         if (listen(sk, 1) < 0) {
915                 err = errno;
916                 error("audio: Failed to listen on the socket: %d (%s)", err,
917                                                                 strerror(err));
918                 goto failed;
919         }
920
921         listen_sk = sk;
922
923         err = pthread_create(&ipc_th, NULL, ipc_handler, NULL);
924         if (err) {
925                 err = -err;
926                 ipc_th = 0;
927                 error("audio: Failed to start Audio IPC thread: %d (%s)",
928                                                         err, strerror(err));
929                 goto failed;
930         }
931
932         return 0;
933
934 failed:
935         close(sk);
936         return err;
937 }
938
939 static int audio_open(const hw_module_t *module, const char *name,
940                                                         hw_device_t **device)
941 {
942         struct a2dp_audio_dev *a2dp_dev;
943         int err;
944
945         DBG("");
946
947         if (strcmp(name, AUDIO_HARDWARE_INTERFACE)) {
948                 error("audio: interface %s not matching [%s]", name,
949                                                 AUDIO_HARDWARE_INTERFACE);
950                 return -EINVAL;
951         }
952
953         err = audio_ipc_init();
954         if (err)
955                 return -err;
956
957         a2dp_dev = calloc(1, sizeof(struct a2dp_audio_dev));
958         if (!a2dp_dev)
959                 return -ENOMEM;
960
961         a2dp_dev->dev.common.version = AUDIO_DEVICE_API_VERSION_CURRENT;
962         a2dp_dev->dev.common.module = (struct hw_module_t *) module;
963         a2dp_dev->dev.common.close = audio_close;
964
965         a2dp_dev->dev.init_check = audio_init_check;
966         a2dp_dev->dev.set_voice_volume = audio_set_voice_volume;
967         a2dp_dev->dev.set_master_volume = audio_set_master_volume;
968         a2dp_dev->dev.set_mode = audio_set_mode;
969         a2dp_dev->dev.set_mic_mute = audio_set_mic_mute;
970         a2dp_dev->dev.get_mic_mute = audio_get_mic_mute;
971         a2dp_dev->dev.set_parameters = audio_set_parameters;
972         a2dp_dev->dev.get_parameters = audio_get_parameters;
973         a2dp_dev->dev.get_input_buffer_size = audio_get_input_buffer_size;
974         a2dp_dev->dev.open_output_stream = audio_open_output_stream;
975         a2dp_dev->dev.close_output_stream = audio_close_output_stream;
976         a2dp_dev->dev.open_input_stream = audio_open_input_stream;
977         a2dp_dev->dev.close_input_stream = audio_close_input_stream;
978         a2dp_dev->dev.dump = audio_dump;
979
980         /* Note that &a2dp_dev->dev.common is the same pointer as a2dp_dev.
981          * This results from the structure of following structs:a2dp_audio_dev,
982          * audio_hw_device. We will rely on this later in the code.*/
983         *device = &a2dp_dev->dev.common;
984
985         return 0;
986 }
987
988 static struct hw_module_methods_t hal_module_methods = {
989         .open = audio_open,
990 };
991
992 struct audio_module HAL_MODULE_INFO_SYM = {
993         .common = {
994         .tag = HARDWARE_MODULE_TAG,
995         .version_major = 1,
996         .version_minor = 0,
997         .id = AUDIO_HARDWARE_MODULE_ID,
998         .name = "A2DP Bluez HW HAL",
999         .author = "Intel Corporation",
1000         .methods = &hal_module_methods,
1001         },
1002 };