OSDN Git Service

android/hal-audio: Add support to resume 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 ipc_close_stream_cmd(uint8_t endpoint_id)
404 {
405         struct audio_cmd_close_stream cmd;
406         int result;
407
408         DBG("");
409
410         cmd.id = endpoint_id;
411
412         result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_CLOSE_STREAM,
413                                 sizeof(cmd), &cmd, NULL, NULL, NULL);
414
415         return result;
416 }
417
418 static int ipc_resume_stream_cmd(uint8_t endpoint_id)
419 {
420         struct audio_cmd_resume_stream cmd;
421         int result;
422
423         DBG("");
424
425         cmd.id = endpoint_id;
426
427         result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_RESUME_STREAM,
428                                 sizeof(cmd), &cmd, NULL, NULL, NULL);
429
430         return result;
431 }
432
433 static int register_endpoints(void)
434 {
435         struct audio_endpoint *ep = &audio_endpoints[0];
436         size_t i;
437
438         for (i = 0; i < NUM_CODECS; i++, ep++) {
439                 const struct audio_codec *codec = &audio_codecs[i];
440
441                 ep->id = ipc_open_cmd(codec);
442
443                 if (!ep->id)
444                         return AUDIO_STATUS_FAILED;
445
446                 ep->codec = codec;
447                 ep->codec_data = NULL;
448                 ep->fd = -1;
449         }
450
451         return AUDIO_STATUS_SUCCESS;
452 }
453
454 static void unregister_endpoints(void)
455 {
456         size_t i;
457
458         for (i = 0; i < MAX_AUDIO_ENDPOINTS; i++) {
459                 struct audio_endpoint *ep = &audio_endpoints[i];
460
461                 if (ep->id) {
462                         ipc_close_cmd(ep->id);
463                         memset(ep, 0, sizeof(*ep));
464                 }
465         }
466 }
467
468 static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
469                                                                 size_t bytes)
470 {
471         struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
472
473         /* We can auto-start only from standby */
474         if (out->audio_state == AUDIO_A2DP_STATE_STANDBY) {
475                 DBG("stream in standby, auto-start");
476
477                 if (ipc_resume_stream_cmd(out->ep->id) != AUDIO_STATUS_SUCCESS)
478                         return -1;
479
480                 out->audio_state = AUDIO_A2DP_STATE_STARTED;
481         }
482
483         if (out->audio_state != AUDIO_A2DP_STATE_STARTED) {
484                 DBG("stream not started");
485                 return -1;
486         }
487
488         /* TODO: encode data using codec */
489
490         return bytes;
491 }
492
493 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
494 {
495         DBG("");
496         return -ENOSYS;
497 }
498
499 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
500 {
501         DBG("");
502         return -ENOSYS;
503 }
504
505 static size_t out_get_buffer_size(const struct audio_stream *stream)
506 {
507         DBG("");
508         return -ENOSYS;
509 }
510
511 static uint32_t out_get_channels(const struct audio_stream *stream)
512 {
513         DBG("");
514         return -ENOSYS;
515 }
516
517 static audio_format_t out_get_format(const struct audio_stream *stream)
518 {
519         DBG("");
520         return -ENOSYS;
521 }
522
523 static int out_set_format(struct audio_stream *stream, audio_format_t format)
524 {
525         DBG("");
526         return -ENOSYS;
527 }
528
529 static int out_standby(struct audio_stream *stream)
530 {
531         DBG("");
532         return -ENOSYS;
533 }
534
535 static int out_dump(const struct audio_stream *stream, int fd)
536 {
537         DBG("");
538         return -ENOSYS;
539 }
540
541 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
542 {
543         DBG("");
544         return -ENOSYS;
545 }
546
547 static char *out_get_parameters(const struct audio_stream *stream,
548                                                         const char *keys)
549 {
550         DBG("");
551         return strdup("");
552 }
553
554 static uint32_t out_get_latency(const struct audio_stream_out *stream)
555 {
556         DBG("");
557         return -ENOSYS;
558 }
559
560 static int out_set_volume(struct audio_stream_out *stream, float left,
561                                                                 float right)
562 {
563         DBG("");
564         /* volume controlled in audioflinger mixer (digital) */
565         return -ENOSYS;
566 }
567
568 static int out_get_render_position(const struct audio_stream_out *stream,
569                                                         uint32_t *dsp_frames)
570 {
571         DBG("");
572         return -ENOSYS;
573 }
574
575 static int out_add_audio_effect(const struct audio_stream *stream,
576                                                         effect_handle_t effect)
577 {
578         DBG("");
579         return -ENOSYS;
580 }
581
582 static int out_remove_audio_effect(const struct audio_stream *stream,
583                                                         effect_handle_t effect)
584 {
585         DBG("");
586         return -ENOSYS;
587 }
588
589 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
590 {
591         DBG("");
592         return -ENOSYS;
593 }
594
595 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
596 {
597         DBG("");
598         return -ENOSYS;
599 }
600
601 static size_t in_get_buffer_size(const struct audio_stream *stream)
602 {
603         DBG("");
604         return -ENOSYS;
605 }
606
607 static uint32_t in_get_channels(const struct audio_stream *stream)
608 {
609         DBG("");
610         return -ENOSYS;
611 }
612
613 static audio_format_t in_get_format(const struct audio_stream *stream)
614 {
615         DBG("");
616         return -ENOSYS;
617 }
618
619 static int in_set_format(struct audio_stream *stream, audio_format_t format)
620 {
621         DBG("");
622         return -ENOSYS;
623 }
624
625 static int in_standby(struct audio_stream *stream)
626 {
627         DBG("");
628         return -ENOSYS;
629 }
630
631 static int in_dump(const struct audio_stream *stream, int fd)
632 {
633         DBG("");
634         return -ENOSYS;
635 }
636
637 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
638 {
639         DBG("");
640         return -ENOSYS;
641 }
642
643 static char *in_get_parameters(const struct audio_stream *stream,
644                                                         const char *keys)
645 {
646         DBG("");
647         return strdup("");
648 }
649
650 static int in_set_gain(struct audio_stream_in *stream, float gain)
651 {
652         DBG("");
653         return -ENOSYS;
654 }
655
656 static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
657                                                                 size_t bytes)
658 {
659         DBG("");
660         return -ENOSYS;
661 }
662
663 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
664 {
665         DBG("");
666         return -ENOSYS;
667 }
668
669 static int in_add_audio_effect(const struct audio_stream *stream,
670                                                         effect_handle_t effect)
671 {
672         DBG("");
673         return -ENOSYS;
674 }
675
676 static int in_remove_audio_effect(const struct audio_stream *stream,
677                                                         effect_handle_t effect)
678 {
679         DBG("");
680         return -ENOSYS;
681 }
682
683 static int audio_open_output_stream(struct audio_hw_device *dev,
684                                         audio_io_handle_t handle,
685                                         audio_devices_t devices,
686                                         audio_output_flags_t flags,
687                                         struct audio_config *config,
688                                         struct audio_stream_out **stream_out)
689
690 {
691         struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
692         struct a2dp_stream_out *out;
693         struct audio_preset *preset;
694
695         out = calloc(1, sizeof(struct a2dp_stream_out));
696         if (!out)
697                 return -ENOMEM;
698
699         DBG("");
700
701         out->stream.common.get_sample_rate = out_get_sample_rate;
702         out->stream.common.set_sample_rate = out_set_sample_rate;
703         out->stream.common.get_buffer_size = out_get_buffer_size;
704         out->stream.common.get_channels = out_get_channels;
705         out->stream.common.get_format = out_get_format;
706         out->stream.common.set_format = out_set_format;
707         out->stream.common.standby = out_standby;
708         out->stream.common.dump = out_dump;
709         out->stream.common.set_parameters = out_set_parameters;
710         out->stream.common.get_parameters = out_get_parameters;
711         out->stream.common.add_audio_effect = out_add_audio_effect;
712         out->stream.common.remove_audio_effect = out_remove_audio_effect;
713         out->stream.get_latency = out_get_latency;
714         out->stream.set_volume = out_set_volume;
715         out->stream.write = out_write;
716         out->stream.get_render_position = out_get_render_position;
717
718         /* TODO: for now we always use endpoint 0 */
719         out->ep = &audio_endpoints[0];
720
721         if (ipc_open_stream_cmd(out->ep->id, &preset) != AUDIO_STATUS_SUCCESS)
722                 goto fail;
723
724         if (!preset)
725                 goto fail;
726
727         /* TODO: initialize codec using received audio_preset */
728
729         free(preset);
730
731         *stream_out = &out->stream;
732         a2dp_dev->out = out;
733
734         out->audio_state = AUDIO_A2DP_STATE_STANDBY;
735
736         return 0;
737
738 fail:
739         free(out);
740         *stream_out = NULL;
741         return -EIO;
742 }
743
744 static void audio_close_output_stream(struct audio_hw_device *dev,
745                                         struct audio_stream_out *stream)
746 {
747         struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
748         struct audio_endpoint *ep = a2dp_dev->out->ep;
749
750         DBG("");
751
752         ipc_close_stream_cmd(ep->id);
753
754         /* TODO: cleanup codec */
755
756         free(stream);
757         a2dp_dev->out = NULL;
758 }
759
760 static int audio_set_parameters(struct audio_hw_device *dev,
761                                                         const char *kvpairs)
762 {
763         DBG("");
764         return -ENOSYS;
765 }
766
767 static char *audio_get_parameters(const struct audio_hw_device *dev,
768                                                         const char *keys)
769 {
770         DBG("");
771         return strdup("");
772 }
773
774 static int audio_init_check(const struct audio_hw_device *dev)
775 {
776         DBG("");
777         return -ENOSYS;
778 }
779
780 static int audio_set_voice_volume(struct audio_hw_device *dev, float volume)
781 {
782         DBG("");
783         return -ENOSYS;
784 }
785
786 static int audio_set_master_volume(struct audio_hw_device *dev, float volume)
787 {
788         DBG("");
789         return -ENOSYS;
790 }
791
792 static int audio_set_mode(struct audio_hw_device *dev, int mode)
793 {
794         DBG("");
795         return -ENOSYS;
796 }
797
798 static int audio_set_mic_mute(struct audio_hw_device *dev, bool state)
799 {
800         DBG("");
801         return -ENOSYS;
802 }
803
804 static int audio_get_mic_mute(const struct audio_hw_device *dev, bool *state)
805 {
806         DBG("");
807         return -ENOSYS;
808 }
809
810 static size_t audio_get_input_buffer_size(const struct audio_hw_device *dev,
811                                         const struct audio_config *config)
812 {
813         DBG("");
814         return -ENOSYS;
815 }
816
817 static int audio_open_input_stream(struct audio_hw_device *dev,
818                                         audio_io_handle_t handle,
819                                         audio_devices_t devices,
820                                         struct audio_config *config,
821                                         struct audio_stream_in **stream_in)
822 {
823         struct audio_stream_in *in;
824
825         DBG("");
826
827         in = calloc(1, sizeof(struct audio_stream_in));
828         if (!in)
829                 return -ENOMEM;
830
831         in->common.get_sample_rate = in_get_sample_rate;
832         in->common.set_sample_rate = in_set_sample_rate;
833         in->common.get_buffer_size = in_get_buffer_size;
834         in->common.get_channels = in_get_channels;
835         in->common.get_format = in_get_format;
836         in->common.set_format = in_set_format;
837         in->common.standby = in_standby;
838         in->common.dump = in_dump;
839         in->common.set_parameters = in_set_parameters;
840         in->common.get_parameters = in_get_parameters;
841         in->common.add_audio_effect = in_add_audio_effect;
842         in->common.remove_audio_effect = in_remove_audio_effect;
843         in->set_gain = in_set_gain;
844         in->read = in_read;
845         in->get_input_frames_lost = in_get_input_frames_lost;
846
847         *stream_in = in;
848
849         return 0;
850 }
851
852 static void audio_close_input_stream(struct audio_hw_device *dev,
853                                         struct audio_stream_in *stream_in)
854 {
855         DBG("");
856         free(stream_in);
857 }
858
859 static int audio_dump(const audio_hw_device_t *device, int fd)
860 {
861         DBG("");
862         return -ENOSYS;
863 }
864
865 static int audio_close(hw_device_t *device)
866 {
867         struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *)device;
868
869         DBG("");
870
871         pthread_mutex_lock(&close_mutex);
872         audio_ipc_cleanup();
873         close_thread = true;
874         pthread_mutex_unlock(&close_mutex);
875
876         pthread_join(ipc_th, NULL);
877
878         close(listen_sk);
879         listen_sk = -1;
880
881         free(a2dp_dev);
882         return 0;
883 }
884
885 static void *ipc_handler(void *data)
886 {
887         bool done = false;
888         struct pollfd pfd;
889
890         DBG("");
891
892         while (!done) {
893                 DBG("Waiting for connection ...");
894                 audio_sk = accept(listen_sk, NULL, NULL);
895                 if (audio_sk < 0) {
896                         int err = errno;
897                         error("audio: Failed to accept socket: %d (%s)", err,
898                                                                 strerror(err));
899                         continue;
900                 }
901
902                 DBG("Audio IPC: Connected");
903
904                 if (register_endpoints() != AUDIO_STATUS_SUCCESS) {
905                         error("audio: Failed to register endpoints");
906
907                         unregister_endpoints();
908
909                         shutdown(audio_sk, SHUT_RDWR);
910                         continue;
911                 }
912
913                 memset(&pfd, 0, sizeof(pfd));
914                 pfd.fd = audio_sk;
915                 pfd.events = POLLHUP | POLLERR | POLLNVAL;
916
917                 /* Check if socket is still alive. Empty while loop.*/
918                 while (poll(&pfd, 1, -1) < 0 && errno == EINTR);
919
920                 if (pfd.revents & (POLLHUP | POLLERR | POLLNVAL)) {
921                         info("Audio HAL: Socket closed");
922                         audio_sk = -1;
923                 }
924
925                 /*Check if audio_dev is closed */
926                 pthread_mutex_lock(&close_mutex);
927                 done = close_thread;
928                 close_thread = false;
929                 pthread_mutex_unlock(&close_mutex);
930         }
931
932         unregister_endpoints();
933
934         info("Closing Audio IPC thread");
935         return NULL;
936 }
937
938 static int audio_ipc_init(void)
939 {
940         struct sockaddr_un addr;
941         int err;
942         int sk;
943
944         DBG("");
945
946         sk = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
947         if (sk < 0) {
948                 err = errno;
949                 error("audio: Failed to create socket: %d (%s)", err,
950                                                                 strerror(err));
951                 return err;
952         }
953
954         memset(&addr, 0, sizeof(addr));
955         addr.sun_family = AF_UNIX;
956
957         memcpy(addr.sun_path, BLUEZ_AUDIO_SK_PATH,
958                                         sizeof(BLUEZ_AUDIO_SK_PATH));
959
960         if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
961                 err = errno;
962                 error("audio: Failed to bind socket: %d (%s)", err,
963                                                                 strerror(err));
964                 goto failed;
965         }
966
967         if (listen(sk, 1) < 0) {
968                 err = errno;
969                 error("audio: Failed to listen on the socket: %d (%s)", err,
970                                                                 strerror(err));
971                 goto failed;
972         }
973
974         listen_sk = sk;
975
976         err = pthread_create(&ipc_th, NULL, ipc_handler, NULL);
977         if (err) {
978                 err = -err;
979                 ipc_th = 0;
980                 error("audio: Failed to start Audio IPC thread: %d (%s)",
981                                                         err, strerror(err));
982                 goto failed;
983         }
984
985         return 0;
986
987 failed:
988         close(sk);
989         return err;
990 }
991
992 static int audio_open(const hw_module_t *module, const char *name,
993                                                         hw_device_t **device)
994 {
995         struct a2dp_audio_dev *a2dp_dev;
996         int err;
997
998         DBG("");
999
1000         if (strcmp(name, AUDIO_HARDWARE_INTERFACE)) {
1001                 error("audio: interface %s not matching [%s]", name,
1002                                                 AUDIO_HARDWARE_INTERFACE);
1003                 return -EINVAL;
1004         }
1005
1006         err = audio_ipc_init();
1007         if (err)
1008                 return -err;
1009
1010         a2dp_dev = calloc(1, sizeof(struct a2dp_audio_dev));
1011         if (!a2dp_dev)
1012                 return -ENOMEM;
1013
1014         a2dp_dev->dev.common.version = AUDIO_DEVICE_API_VERSION_CURRENT;
1015         a2dp_dev->dev.common.module = (struct hw_module_t *) module;
1016         a2dp_dev->dev.common.close = audio_close;
1017
1018         a2dp_dev->dev.init_check = audio_init_check;
1019         a2dp_dev->dev.set_voice_volume = audio_set_voice_volume;
1020         a2dp_dev->dev.set_master_volume = audio_set_master_volume;
1021         a2dp_dev->dev.set_mode = audio_set_mode;
1022         a2dp_dev->dev.set_mic_mute = audio_set_mic_mute;
1023         a2dp_dev->dev.get_mic_mute = audio_get_mic_mute;
1024         a2dp_dev->dev.set_parameters = audio_set_parameters;
1025         a2dp_dev->dev.get_parameters = audio_get_parameters;
1026         a2dp_dev->dev.get_input_buffer_size = audio_get_input_buffer_size;
1027         a2dp_dev->dev.open_output_stream = audio_open_output_stream;
1028         a2dp_dev->dev.close_output_stream = audio_close_output_stream;
1029         a2dp_dev->dev.open_input_stream = audio_open_input_stream;
1030         a2dp_dev->dev.close_input_stream = audio_close_input_stream;
1031         a2dp_dev->dev.dump = audio_dump;
1032
1033         /* Note that &a2dp_dev->dev.common is the same pointer as a2dp_dev.
1034          * This results from the structure of following structs:a2dp_audio_dev,
1035          * audio_hw_device. We will rely on this later in the code.*/
1036         *device = &a2dp_dev->dev.common;
1037
1038         return 0;
1039 }
1040
1041 static struct hw_module_methods_t hal_module_methods = {
1042         .open = audio_open,
1043 };
1044
1045 struct audio_module HAL_MODULE_INFO_SYM = {
1046         .common = {
1047         .tag = HARDWARE_MODULE_TAG,
1048         .version_major = 1,
1049         .version_minor = 0,
1050         .id = AUDIO_HARDWARE_MODULE_ID,
1051         .name = "A2DP Bluez HW HAL",
1052         .author = "Intel Corporation",
1053         .methods = &hal_module_methods,
1054         },
1055 };