OSDN Git Service

android/hal-audio: Workaround AudioFlinger issues
[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 #include <arpa/inet.h>
28
29 #include <hardware/audio.h>
30 #include <hardware/hardware.h>
31
32 #include <sbc/sbc.h>
33
34 #include "audio-msg.h"
35 #include "hal-log.h"
36 #include "hal-msg.h"
37 #include "../profiles/audio/a2dp-codecs.h"
38
39 #define FIXED_A2DP_PLAYBACK_LATENCY_MS 25
40
41 static const uint8_t a2dp_src_uuid[] = {
42                 0x00, 0x00, 0x11, 0x0a, 0x00, 0x00, 0x10, 0x00,
43                 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb };
44
45 static int listen_sk = -1;
46 static int audio_sk = -1;
47
48 static pthread_t ipc_th = 0;
49 static pthread_mutex_t sk_mutex = PTHREAD_MUTEX_INITIALIZER;
50
51 #if __BYTE_ORDER == __LITTLE_ENDIAN
52
53 struct rtp_header {
54         unsigned cc:4;
55         unsigned x:1;
56         unsigned p:1;
57         unsigned v:2;
58
59         unsigned pt:7;
60         unsigned m:1;
61
62         uint16_t sequence_number;
63         uint32_t timestamp;
64         uint32_t ssrc;
65         uint32_t csrc[0];
66 } __attribute__ ((packed));
67
68 struct rtp_payload {
69         unsigned frame_count:4;
70         unsigned rfa0:1;
71         unsigned is_last_fragment:1;
72         unsigned is_first_fragment:1;
73         unsigned is_fragmented:1;
74 } __attribute__ ((packed));
75
76 #elif __BYTE_ORDER == __BIG_ENDIAN
77
78 struct rtp_header {
79         unsigned v:2;
80         unsigned p:1;
81         unsigned x:1;
82         unsigned cc:4;
83
84         unsigned m:1;
85         unsigned pt:7;
86
87         uint16_t sequence_number;
88         uint32_t timestamp;
89         uint32_t ssrc;
90         uint32_t csrc[0];
91 } __attribute__ ((packed));
92
93 struct rtp_payload {
94         unsigned is_fragmented:1;
95         unsigned is_first_fragment:1;
96         unsigned is_last_fragment:1;
97         unsigned rfa0:1;
98         unsigned frame_count:4;
99 } __attribute__ ((packed));
100
101 #else
102 #error "Unknown byte order"
103 #endif
104
105 struct media_packet {
106         struct rtp_header hdr;
107         struct rtp_payload payload;
108         uint8_t data[0];
109 };
110
111 struct audio_input_config {
112         uint32_t rate;
113         uint32_t channels;
114         audio_format_t format;
115 };
116
117 struct sbc_data {
118         a2dp_sbc_t sbc;
119
120         sbc_t enc;
121
122         size_t in_frame_len;
123         size_t in_buf_size;
124
125         size_t out_buf_size;
126         uint8_t *out_buf;
127
128         unsigned frame_duration;
129         unsigned frames_per_packet;
130
131         struct timespec start;
132         unsigned frames_sent;
133
134         uint16_t seq;
135 };
136
137 static inline void timespec_diff(struct timespec *a, struct timespec *b,
138                                         struct timespec *res)
139 {
140         res->tv_sec = a->tv_sec - b->tv_sec;
141         res->tv_nsec = a->tv_nsec - b->tv_nsec;
142
143         if (res->tv_nsec < 0) {
144                 res->tv_sec--;
145                 res->tv_nsec += 1000000000; /* 1sec */
146         }
147 }
148
149 static int sbc_get_presets(struct audio_preset *preset, size_t *len);
150 static int sbc_codec_init(struct audio_preset *preset, uint16_t mtu,
151                                 void **codec_data);
152 static int sbc_cleanup(void *codec_data);
153 static int sbc_get_config(void *codec_data, struct audio_input_config *config);
154 static size_t sbc_get_buffer_size(void *codec_data);
155 static size_t sbc_get_mediapacket_duration(void *codec_data);
156 static void sbc_resume(void *codec_data);
157 static ssize_t sbc_write_data(void *codec_data, const void *buffer,
158                                         size_t bytes, int fd);
159
160 struct audio_codec {
161         uint8_t type;
162
163         int (*get_presets) (struct audio_preset *preset, size_t *len);
164
165         int (*init) (struct audio_preset *preset, uint16_t mtu,
166                                 void **codec_data);
167         int (*cleanup) (void *codec_data);
168         int (*get_config) (void *codec_data,
169                                         struct audio_input_config *config);
170         size_t (*get_buffer_size) (void *codec_data);
171         size_t (*get_mediapacket_duration) (void *codec_data);
172         void (*resume) (void *codec_data);
173         ssize_t (*write_data) (void *codec_data, const void *buffer,
174                                 size_t bytes, int fd);
175 };
176
177 static const struct audio_codec audio_codecs[] = {
178         {
179                 .type = A2DP_CODEC_SBC,
180
181                 .get_presets = sbc_get_presets,
182
183                 .init = sbc_codec_init,
184                 .cleanup = sbc_cleanup,
185                 .get_config = sbc_get_config,
186                 .get_buffer_size = sbc_get_buffer_size,
187                 .get_mediapacket_duration = sbc_get_mediapacket_duration,
188                 .resume = sbc_resume,
189                 .write_data = sbc_write_data,
190         }
191 };
192
193 #define NUM_CODECS (sizeof(audio_codecs) / sizeof(audio_codecs[0]))
194
195 #define MAX_AUDIO_ENDPOINTS NUM_CODECS
196
197 struct audio_endpoint {
198         uint8_t id;
199         const struct audio_codec *codec;
200         void *codec_data;
201         int fd;
202 };
203
204 static struct audio_endpoint audio_endpoints[MAX_AUDIO_ENDPOINTS];
205
206 enum a2dp_state_t {
207         AUDIO_A2DP_STATE_NONE,
208         AUDIO_A2DP_STATE_STANDBY,
209         AUDIO_A2DP_STATE_SUSPENDED,
210         AUDIO_A2DP_STATE_STARTED
211 };
212
213 struct a2dp_stream_out {
214         struct audio_stream_out stream;
215
216         struct audio_endpoint *ep;
217         enum a2dp_state_t audio_state;
218         struct audio_input_config cfg;
219 };
220
221 struct a2dp_audio_dev {
222         struct audio_hw_device dev;
223         struct a2dp_stream_out *out;
224 };
225
226 static const a2dp_sbc_t sbc_presets[] = {
227         {
228                 .frequency = SBC_SAMPLING_FREQ_44100 | SBC_SAMPLING_FREQ_48000,
229                 .channel_mode = SBC_CHANNEL_MODE_MONO |
230                                 SBC_CHANNEL_MODE_DUAL_CHANNEL |
231                                 SBC_CHANNEL_MODE_STEREO |
232                                 SBC_CHANNEL_MODE_JOINT_STEREO,
233                 .subbands = SBC_SUBBANDS_4 | SBC_SUBBANDS_8,
234                 .allocation_method = SBC_ALLOCATION_SNR |
235                                         SBC_ALLOCATION_LOUDNESS,
236                 .block_length = SBC_BLOCK_LENGTH_4 | SBC_BLOCK_LENGTH_8 |
237                                 SBC_BLOCK_LENGTH_12 | SBC_BLOCK_LENGTH_16,
238                 .min_bitpool = MIN_BITPOOL,
239                 .max_bitpool = MAX_BITPOOL
240         },
241         {
242                 .frequency = SBC_SAMPLING_FREQ_44100,
243                 .channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO,
244                 .subbands = SBC_SUBBANDS_8,
245                 .allocation_method = SBC_ALLOCATION_LOUDNESS,
246                 .block_length = SBC_BLOCK_LENGTH_16,
247                 .min_bitpool = MIN_BITPOOL,
248                 .max_bitpool = MAX_BITPOOL
249         },
250         {
251                 .frequency = SBC_SAMPLING_FREQ_48000,
252                 .channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO,
253                 .subbands = SBC_SUBBANDS_8,
254                 .allocation_method = SBC_ALLOCATION_LOUDNESS,
255                 .block_length = SBC_BLOCK_LENGTH_16,
256                 .min_bitpool = MIN_BITPOOL,
257                 .max_bitpool = MAX_BITPOOL
258         },
259 };
260
261 static int sbc_get_presets(struct audio_preset *preset, size_t *len)
262 {
263         int i;
264         int count;
265         size_t new_len = 0;
266         uint8_t *ptr = (uint8_t *) preset;
267         size_t preset_size = sizeof(*preset) + sizeof(a2dp_sbc_t);
268
269         DBG("");
270
271         count = sizeof(sbc_presets) / sizeof(sbc_presets[0]);
272
273         for (i = 0; i < count; i++) {
274                 preset = (struct audio_preset *) ptr;
275
276                 if (new_len + preset_size > *len)
277                         break;
278
279                 preset->len = sizeof(a2dp_sbc_t);
280                 memcpy(preset->data, &sbc_presets[i], preset->len);
281
282                 new_len += preset_size;
283                 ptr += preset_size;
284         }
285
286         *len = new_len;
287
288         return i;
289 }
290
291 static void sbc_init_encoder(struct sbc_data *sbc_data)
292 {
293         a2dp_sbc_t *in = &sbc_data->sbc;
294         sbc_t *out = &sbc_data->enc;
295
296         DBG("");
297
298         sbc_init_a2dp(out, 0L, in, sizeof(*in));
299
300         out->endian = SBC_LE;
301         out->bitpool = in->max_bitpool;
302 }
303
304 static int sbc_codec_init(struct audio_preset *preset, uint16_t mtu,
305                                 void **codec_data)
306 {
307         struct sbc_data *sbc_data;
308         size_t hdr_len = sizeof(struct media_packet);
309         size_t in_frame_len;
310         size_t out_frame_len;
311         size_t num_frames;
312
313         DBG("");
314
315         if (preset->len != sizeof(a2dp_sbc_t)) {
316                 DBG("preset size mismatch");
317                 return AUDIO_STATUS_FAILED;
318         }
319
320         sbc_data = calloc(sizeof(struct sbc_data), 1);
321
322         memcpy(&sbc_data->sbc, preset->data, preset->len);
323
324         sbc_init_encoder(sbc_data);
325
326         in_frame_len = sbc_get_codesize(&sbc_data->enc);
327         out_frame_len = sbc_get_frame_length(&sbc_data->enc);
328         num_frames = (mtu - hdr_len) / out_frame_len;
329
330         sbc_data->in_frame_len = in_frame_len;
331         sbc_data->in_buf_size = num_frames * in_frame_len;
332
333         sbc_data->out_buf_size = hdr_len + num_frames * out_frame_len;
334         sbc_data->out_buf = calloc(1, sbc_data->out_buf_size);
335
336         sbc_data->frame_duration = sbc_get_frame_duration(&sbc_data->enc);
337         sbc_data->frames_per_packet = num_frames;
338
339         *codec_data = sbc_data;
340
341         return AUDIO_STATUS_SUCCESS;
342 }
343
344 static int sbc_cleanup(void *codec_data)
345 {
346         struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
347
348         DBG("");
349
350         sbc_finish(&sbc_data->enc);
351         free(sbc_data->out_buf);
352         free(codec_data);
353
354         return AUDIO_STATUS_SUCCESS;
355 }
356
357 static int sbc_get_config(void *codec_data, struct audio_input_config *config)
358 {
359         struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
360
361         switch (sbc_data->sbc.frequency) {
362         case SBC_SAMPLING_FREQ_16000:
363                 config->rate = 16000;
364                 break;
365         case SBC_SAMPLING_FREQ_32000:
366                 config->rate = 32000;
367                 break;
368         case SBC_SAMPLING_FREQ_44100:
369                 config->rate = 44100;
370                 break;
371         case SBC_SAMPLING_FREQ_48000:
372                 config->rate = 48000;
373                 break;
374         default:
375                 return AUDIO_STATUS_FAILED;
376         }
377         config->channels = sbc_data->sbc.channel_mode == SBC_CHANNEL_MODE_MONO ?
378                                 AUDIO_CHANNEL_OUT_MONO :
379                                 AUDIO_CHANNEL_OUT_STEREO;
380         config->format = AUDIO_FORMAT_PCM_16_BIT;
381
382         return AUDIO_STATUS_SUCCESS;
383 }
384
385 static size_t sbc_get_buffer_size(void *codec_data)
386 {
387         struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
388
389         DBG("");
390
391         return sbc_data->in_buf_size;
392 }
393
394 static size_t sbc_get_mediapacket_duration(void *codec_data)
395 {
396         struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
397
398         DBG("");
399
400         return sbc_data->frame_duration * sbc_data->frames_per_packet;
401 }
402
403 static void sbc_resume(void *codec_data)
404 {
405         struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
406
407         DBG("");
408
409         clock_gettime(CLOCK_MONOTONIC, &sbc_data->start);
410
411         sbc_data->frames_sent = 0;
412 }
413
414 static void write_media_packet(int fd, struct sbc_data *sbc_data,
415                                 struct media_packet *mp, size_t data_len)
416 {
417         struct timespec cur;
418         struct timespec diff;
419         unsigned expected_frames;
420         int ret;
421
422         ret = write(fd, mp, sizeof(*mp) + data_len);
423         if (ret < 0) {
424                 int err = errno;
425                 DBG("error writing data: %d (%s)", err,
426                                                 strerror(err));
427         }
428
429         sbc_data->frames_sent += mp->payload.frame_count;
430
431         clock_gettime(CLOCK_MONOTONIC, &cur);
432         timespec_diff(&cur, &sbc_data->start, &diff);
433         expected_frames = (diff.tv_sec * 1000000 + diff.tv_nsec / 1000) /
434                                                 sbc_data->frame_duration;
435
436         /* AudioFlinger does not seem to provide any *working*
437          * API to provide data in some interval and will just
438          * send another buffer as soon as we process current
439          * one. To prevent overflowing L2CAP socket, we need to
440          * introduce some artificial delay here base on how many
441          * audio frames were sent so far, i.e. if we're not
442          * lagging behind audio stream, we can sleep for
443          * duration of single media packet.
444          */
445         if (sbc_data->frames_sent >= expected_frames)
446                 usleep(sbc_data->frame_duration *
447                                 mp->payload.frame_count);
448 }
449
450 static ssize_t sbc_write_data(void *codec_data, const void *buffer,
451                                 size_t bytes, int fd)
452 {
453         struct sbc_data *sbc_data = (struct sbc_data *) codec_data;
454         size_t consumed = 0;
455         size_t encoded = 0;
456         struct media_packet *mp = (struct media_packet *) sbc_data->out_buf;
457         size_t free_space = sbc_data->out_buf_size - sizeof(*mp);
458         int ret;
459
460         mp->hdr.v = 2;
461         mp->hdr.pt = 1;
462         mp->hdr.sequence_number = htons(sbc_data->seq++);
463         mp->hdr.ssrc = htonl(1);
464         mp->payload.frame_count = 0;
465
466         while (bytes - consumed >= sbc_data->in_frame_len) {
467                 ssize_t written = 0;
468
469                 ret = sbc_encode(&sbc_data->enc, buffer + consumed,
470                                         sbc_data->in_frame_len,
471                                         mp->data + encoded, free_space,
472                                         &written);
473
474                 if (ret < 0) {
475                         DBG("failed to encode block");
476                         break;
477                 }
478
479                 mp->payload.frame_count++;
480
481                 consumed += ret;
482                 encoded += written;
483                 free_space -= written;
484
485                 /* write data if we either filled media packed or encoded all
486                  * input data
487                  */
488                 if (mp->payload.frame_count == sbc_data->frames_per_packet ||
489                                 bytes == consumed) {
490                         write_media_packet(fd, sbc_data, mp, encoded);
491
492                         encoded = 0;
493                         free_space = sbc_data->out_buf_size - sizeof(*mp);
494                         mp->payload.frame_count = 0;
495                 }
496         }
497
498         if (consumed != bytes) {
499                 /* we should encode all input data
500                  * if we did not, something went wrong but we can't really
501                  * handle this so this is just sanity check
502                  */
503                 DBG("some data were not encoded");
504         }
505
506         /* we always assume that all data was processed and sent */
507         return bytes;
508 }
509
510 static int audio_ipc_cmd(uint8_t service_id, uint8_t opcode, uint16_t len,
511                         void *param, size_t *rsp_len, void *rsp, int *fd)
512 {
513         ssize_t ret;
514         struct msghdr msg;
515         struct iovec iv[2];
516         struct hal_hdr cmd;
517         char cmsgbuf[CMSG_SPACE(sizeof(int))];
518         struct hal_status s;
519         size_t s_len = sizeof(s);
520
521         pthread_mutex_lock(&sk_mutex);
522
523         if (audio_sk < 0) {
524                 error("audio: Invalid cmd socket passed to audio_ipc_cmd");
525                 goto failed;
526         }
527
528         if (!rsp || !rsp_len) {
529                 memset(&s, 0, s_len);
530                 rsp_len = &s_len;
531                 rsp = &s;
532         }
533
534         memset(&msg, 0, sizeof(msg));
535         memset(&cmd, 0, sizeof(cmd));
536
537         cmd.service_id = service_id;
538         cmd.opcode = opcode;
539         cmd.len = len;
540
541         iv[0].iov_base = &cmd;
542         iv[0].iov_len = sizeof(cmd);
543
544         iv[1].iov_base = param;
545         iv[1].iov_len = len;
546
547         msg.msg_iov = iv;
548         msg.msg_iovlen = 2;
549
550         ret = sendmsg(audio_sk, &msg, 0);
551         if (ret < 0) {
552                 error("audio: Sending command failed:%s", strerror(errno));
553                 goto failed;
554         }
555
556         /* socket was shutdown */
557         if (ret == 0) {
558                 error("audio: Command socket closed");
559                 goto failed;
560         }
561
562         memset(&msg, 0, sizeof(msg));
563         memset(&cmd, 0, sizeof(cmd));
564
565         iv[0].iov_base = &cmd;
566         iv[0].iov_len = sizeof(cmd);
567
568         iv[1].iov_base = rsp;
569         iv[1].iov_len = *rsp_len;
570
571         msg.msg_iov = iv;
572         msg.msg_iovlen = 2;
573
574         if (fd) {
575                 memset(cmsgbuf, 0, sizeof(cmsgbuf));
576                 msg.msg_control = cmsgbuf;
577                 msg.msg_controllen = sizeof(cmsgbuf);
578         }
579
580         ret = recvmsg(audio_sk, &msg, 0);
581         if (ret < 0) {
582                 error("audio: Receiving command response failed:%s",
583                                                         strerror(errno));
584                 goto failed;
585         }
586
587         if (ret < (ssize_t) sizeof(cmd)) {
588                 error("audio: Too small response received(%zd bytes)", ret);
589                 goto failed;
590         }
591
592         if (cmd.service_id != service_id) {
593                 error("audio: Invalid service id (%u vs %u)", cmd.service_id,
594                                                                 service_id);
595                 goto failed;
596         }
597
598         if (ret != (ssize_t) (sizeof(cmd) + cmd.len)) {
599                 error("audio: Malformed response received(%zd bytes)", ret);
600                 goto failed;
601         }
602
603         if (cmd.opcode != opcode && cmd.opcode != AUDIO_OP_STATUS) {
604                 error("audio: Invalid opcode received (%u vs %u)",
605                                                 cmd.opcode, opcode);
606                 goto failed;
607         }
608
609         if (cmd.opcode == AUDIO_OP_STATUS) {
610                 struct hal_status *s = rsp;
611
612                 if (sizeof(*s) != cmd.len) {
613                         error("audio: Invalid status length");
614                         goto failed;
615                 }
616
617                 if (s->code == AUDIO_STATUS_SUCCESS) {
618                         error("audio: Invalid success status response");
619                         goto failed;
620                 }
621
622                 pthread_mutex_unlock(&sk_mutex);
623
624                 return s->code;
625         }
626
627         pthread_mutex_unlock(&sk_mutex);
628
629         /* Receive auxiliary data in msg */
630         if (fd) {
631                 struct cmsghdr *cmsg;
632
633                 *fd = -1;
634
635                 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
636                                         cmsg = CMSG_NXTHDR(&msg, cmsg)) {
637                         if (cmsg->cmsg_level == SOL_SOCKET
638                                         && cmsg->cmsg_type == SCM_RIGHTS) {
639                                 memcpy(fd, CMSG_DATA(cmsg), sizeof(int));
640                                 break;
641                         }
642                 }
643         }
644
645         if (rsp_len)
646                 *rsp_len = cmd.len;
647
648         return AUDIO_STATUS_SUCCESS;
649
650 failed:
651         /* Some serious issue happen on IPC - recover */
652         shutdown(audio_sk, SHUT_RDWR);
653         pthread_mutex_unlock(&sk_mutex);
654
655         return AUDIO_STATUS_FAILED;
656 }
657
658 static int ipc_open_cmd(const struct audio_codec *codec)
659 {
660         uint8_t buf[BLUEZ_AUDIO_MTU];
661         struct audio_cmd_open *cmd = (struct audio_cmd_open *) buf;
662         struct audio_rsp_open rsp;
663         size_t cmd_len = sizeof(buf) - sizeof(*cmd);
664         size_t rsp_len = sizeof(rsp);
665         int result;
666
667         DBG("");
668
669         memcpy(cmd->uuid, a2dp_src_uuid, sizeof(a2dp_src_uuid));
670
671         cmd->codec = codec->type;
672         cmd->presets = codec->get_presets(cmd->preset, &cmd_len);
673
674         cmd_len += sizeof(*cmd);
675
676         result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_OPEN, cmd_len, cmd,
677                                 &rsp_len, &rsp, NULL);
678
679         if (result != AUDIO_STATUS_SUCCESS)
680                 return 0;
681
682         return rsp.id;
683 }
684
685 static int ipc_close_cmd(uint8_t endpoint_id)
686 {
687         struct audio_cmd_close cmd;
688         int result;
689
690         DBG("");
691
692         cmd.id = endpoint_id;
693
694         result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_CLOSE,
695                                 sizeof(cmd), &cmd, NULL, NULL, NULL);
696
697         return result;
698 }
699
700 static int ipc_open_stream_cmd(uint8_t endpoint_id, uint16_t *mtu, int *fd,
701                                         struct audio_preset **caps)
702 {
703         char buf[BLUEZ_AUDIO_MTU];
704         struct audio_cmd_open_stream cmd;
705         struct audio_rsp_open_stream *rsp =
706                                         (struct audio_rsp_open_stream *) &buf;
707         size_t rsp_len = sizeof(buf);
708         int result;
709
710         DBG("");
711
712         if (!caps)
713                 return AUDIO_STATUS_FAILED;
714
715         cmd.id = endpoint_id;
716
717         result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_OPEN_STREAM,
718                                 sizeof(cmd), &cmd, &rsp_len, rsp, fd);
719
720         if (result == AUDIO_STATUS_SUCCESS) {
721                 size_t buf_len = sizeof(struct audio_preset) +
722                                         rsp->preset[0].len;
723                 *mtu = rsp->mtu;
724                 *caps = malloc(buf_len);
725                 memcpy(*caps, &rsp->preset, buf_len);
726         } else {
727                 *caps = NULL;
728         }
729
730         return result;
731 }
732
733 static int ipc_close_stream_cmd(uint8_t endpoint_id)
734 {
735         struct audio_cmd_close_stream cmd;
736         int result;
737
738         DBG("");
739
740         cmd.id = endpoint_id;
741
742         result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_CLOSE_STREAM,
743                                 sizeof(cmd), &cmd, NULL, NULL, NULL);
744
745         return result;
746 }
747
748 static int ipc_resume_stream_cmd(uint8_t endpoint_id)
749 {
750         struct audio_cmd_resume_stream cmd;
751         int result;
752
753         DBG("");
754
755         cmd.id = endpoint_id;
756
757         result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_RESUME_STREAM,
758                                 sizeof(cmd), &cmd, NULL, NULL, NULL);
759
760         return result;
761 }
762
763 static int ipc_suspend_stream_cmd(uint8_t endpoint_id)
764 {
765         struct audio_cmd_suspend_stream cmd;
766         int result;
767
768         DBG("");
769
770         cmd.id = endpoint_id;
771
772         result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_SUSPEND_STREAM,
773                                 sizeof(cmd), &cmd, NULL, NULL, NULL);
774
775         return result;
776 }
777
778 static int register_endpoints(void)
779 {
780         struct audio_endpoint *ep = &audio_endpoints[0];
781         size_t i;
782
783         for (i = 0; i < NUM_CODECS; i++, ep++) {
784                 const struct audio_codec *codec = &audio_codecs[i];
785
786                 ep->id = ipc_open_cmd(codec);
787
788                 if (!ep->id)
789                         return AUDIO_STATUS_FAILED;
790
791                 ep->codec = codec;
792                 ep->codec_data = NULL;
793                 ep->fd = -1;
794         }
795
796         return AUDIO_STATUS_SUCCESS;
797 }
798
799 static void unregister_endpoints(void)
800 {
801         size_t i;
802
803         for (i = 0; i < MAX_AUDIO_ENDPOINTS; i++) {
804                 struct audio_endpoint *ep = &audio_endpoints[i];
805
806                 if (ep->id) {
807                         ipc_close_cmd(ep->id);
808                         memset(ep, 0, sizeof(*ep));
809                 }
810         }
811 }
812
813 static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
814                                                                 size_t bytes)
815 {
816         struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
817
818         /* We can auto-start only from standby */
819         if (out->audio_state == AUDIO_A2DP_STATE_STANDBY) {
820                 DBG("stream in standby, auto-start");
821
822                 if (ipc_resume_stream_cmd(out->ep->id) != AUDIO_STATUS_SUCCESS)
823                         return -1;
824
825                 out->ep->codec->resume(out->ep->codec_data);
826
827                 out->audio_state = AUDIO_A2DP_STATE_STARTED;
828         }
829
830         if (out->audio_state != AUDIO_A2DP_STATE_STARTED) {
831                 DBG("stream not started");
832                 return -1;
833         }
834
835         if (out->ep->fd < 0) {
836                 DBG("no transport");
837                 return -1;
838         }
839
840         return out->ep->codec->write_data(out->ep->codec_data, buffer,
841                                                 bytes, out->ep->fd);
842 }
843
844 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
845 {
846         struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
847
848         DBG("");
849
850         return out->cfg.rate;
851 }
852
853 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
854 {
855         struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
856
857         DBG("");
858
859         if (rate != out->cfg.rate) {
860                 DBG("cannot set sample rate to %d", rate);
861                 return -1;
862         }
863
864         return 0;
865 }
866
867 static size_t out_get_buffer_size(const struct audio_stream *stream)
868 {
869         DBG("");
870
871         /* We should return proper buffer size calculated by codec (so each
872          * input buffer is encoded into single media packed) but this does not
873          * work well with AudioFlinger and causes problems. For this reason we
874          * use magic value here and out_write code takes care of splitting
875          * input buffer into multiple media packets.
876          */
877         return 20 * 512;
878 }
879
880 static uint32_t out_get_channels(const struct audio_stream *stream)
881 {
882         struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
883
884         DBG("");
885
886         return out->cfg.channels;
887 }
888
889 static audio_format_t out_get_format(const struct audio_stream *stream)
890 {
891         struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
892
893         DBG("");
894
895         return out->cfg.format;
896 }
897
898 static int out_set_format(struct audio_stream *stream, audio_format_t format)
899 {
900         DBG("");
901         return -ENOSYS;
902 }
903
904 static int out_standby(struct audio_stream *stream)
905 {
906         struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
907
908         DBG("");
909
910         if (out->audio_state == AUDIO_A2DP_STATE_STARTED) {
911                 if (ipc_suspend_stream_cmd(out->ep->id) != AUDIO_STATUS_SUCCESS)
912                         return -1;
913                 out->audio_state = AUDIO_A2DP_STATE_STANDBY;
914         }
915
916         return 0;
917 }
918
919 static int out_dump(const struct audio_stream *stream, int fd)
920 {
921         DBG("");
922         return -ENOSYS;
923 }
924
925 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
926 {
927         struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
928         char *kvpair;
929         char *str;
930         char *saveptr;
931         bool enter_suspend = false;
932         bool exit_suspend = false;
933
934         DBG("%s", kvpairs);
935
936         str = strdup(kvpairs);
937         kvpair = strtok_r(str, ";", &saveptr);
938
939         for (; kvpair && *kvpair; kvpair = strtok_r(NULL, ";", &saveptr)) {
940                 char *keyval;
941
942                 keyval = strchr(kvpair, '=');
943                 if (!keyval)
944                         continue;
945
946                 *keyval = '\0';
947                 keyval++;
948
949                 if (!strcmp(kvpair, "closing")) {
950                         if (!strcmp(keyval, "true"))
951                                 out->audio_state = AUDIO_A2DP_STATE_NONE;
952                 } else if (!strcmp(kvpair, "A2dpSuspended")) {
953                         if (!strcmp(keyval, "true"))
954                                 enter_suspend = true;
955                         else
956                                 exit_suspend = true;
957                 }
958         }
959
960         free(str);
961
962         if (enter_suspend && out->audio_state == AUDIO_A2DP_STATE_STARTED) {
963                 if (ipc_suspend_stream_cmd(out->ep->id) != AUDIO_STATUS_SUCCESS)
964                         return -1;
965                 out->audio_state = AUDIO_A2DP_STATE_SUSPENDED;
966         }
967
968         if (exit_suspend && out->audio_state == AUDIO_A2DP_STATE_SUSPENDED)
969                 out->audio_state = AUDIO_A2DP_STATE_STANDBY;
970
971         return 0;
972 }
973
974 static char *out_get_parameters(const struct audio_stream *stream,
975                                                         const char *keys)
976 {
977         DBG("");
978         return strdup("");
979 }
980
981 static uint32_t out_get_latency(const struct audio_stream_out *stream)
982 {
983         struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
984         struct audio_endpoint *ep = out->ep;
985         size_t pkt_duration;
986
987         DBG("");
988
989         pkt_duration = ep->codec->get_mediapacket_duration(ep->codec_data);
990
991         return FIXED_A2DP_PLAYBACK_LATENCY_MS + pkt_duration / 1000;
992 }
993
994 static int out_set_volume(struct audio_stream_out *stream, float left,
995                                                                 float right)
996 {
997         DBG("");
998         /* volume controlled in audioflinger mixer (digital) */
999         return -ENOSYS;
1000 }
1001
1002 static int out_get_render_position(const struct audio_stream_out *stream,
1003                                                         uint32_t *dsp_frames)
1004 {
1005         DBG("");
1006         return -ENOSYS;
1007 }
1008
1009 static int out_add_audio_effect(const struct audio_stream *stream,
1010                                                         effect_handle_t effect)
1011 {
1012         DBG("");
1013         return -ENOSYS;
1014 }
1015
1016 static int out_remove_audio_effect(const struct audio_stream *stream,
1017                                                         effect_handle_t effect)
1018 {
1019         DBG("");
1020         return -ENOSYS;
1021 }
1022
1023 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1024 {
1025         DBG("");
1026         return -ENOSYS;
1027 }
1028
1029 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1030 {
1031         DBG("");
1032         return -ENOSYS;
1033 }
1034
1035 static size_t in_get_buffer_size(const struct audio_stream *stream)
1036 {
1037         DBG("");
1038         return -ENOSYS;
1039 }
1040
1041 static uint32_t in_get_channels(const struct audio_stream *stream)
1042 {
1043         DBG("");
1044         return -ENOSYS;
1045 }
1046
1047 static audio_format_t in_get_format(const struct audio_stream *stream)
1048 {
1049         DBG("");
1050         return -ENOSYS;
1051 }
1052
1053 static int in_set_format(struct audio_stream *stream, audio_format_t format)
1054 {
1055         DBG("");
1056         return -ENOSYS;
1057 }
1058
1059 static int in_standby(struct audio_stream *stream)
1060 {
1061         DBG("");
1062         return -ENOSYS;
1063 }
1064
1065 static int in_dump(const struct audio_stream *stream, int fd)
1066 {
1067         DBG("");
1068         return -ENOSYS;
1069 }
1070
1071 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1072 {
1073         DBG("");
1074         return -ENOSYS;
1075 }
1076
1077 static char *in_get_parameters(const struct audio_stream *stream,
1078                                                         const char *keys)
1079 {
1080         DBG("");
1081         return strdup("");
1082 }
1083
1084 static int in_set_gain(struct audio_stream_in *stream, float gain)
1085 {
1086         DBG("");
1087         return -ENOSYS;
1088 }
1089
1090 static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1091                                                                 size_t bytes)
1092 {
1093         DBG("");
1094         return -ENOSYS;
1095 }
1096
1097 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1098 {
1099         DBG("");
1100         return -ENOSYS;
1101 }
1102
1103 static int in_add_audio_effect(const struct audio_stream *stream,
1104                                                         effect_handle_t effect)
1105 {
1106         DBG("");
1107         return -ENOSYS;
1108 }
1109
1110 static int in_remove_audio_effect(const struct audio_stream *stream,
1111                                                         effect_handle_t effect)
1112 {
1113         DBG("");
1114         return -ENOSYS;
1115 }
1116
1117 static int audio_open_output_stream(struct audio_hw_device *dev,
1118                                         audio_io_handle_t handle,
1119                                         audio_devices_t devices,
1120                                         audio_output_flags_t flags,
1121                                         struct audio_config *config,
1122                                         struct audio_stream_out **stream_out)
1123
1124 {
1125         struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
1126         struct a2dp_stream_out *out;
1127         struct audio_preset *preset;
1128         const struct audio_codec *codec;
1129         uint16_t mtu;
1130         int fd;
1131
1132         out = calloc(1, sizeof(struct a2dp_stream_out));
1133         if (!out)
1134                 return -ENOMEM;
1135
1136         DBG("");
1137
1138         out->stream.common.get_sample_rate = out_get_sample_rate;
1139         out->stream.common.set_sample_rate = out_set_sample_rate;
1140         out->stream.common.get_buffer_size = out_get_buffer_size;
1141         out->stream.common.get_channels = out_get_channels;
1142         out->stream.common.get_format = out_get_format;
1143         out->stream.common.set_format = out_set_format;
1144         out->stream.common.standby = out_standby;
1145         out->stream.common.dump = out_dump;
1146         out->stream.common.set_parameters = out_set_parameters;
1147         out->stream.common.get_parameters = out_get_parameters;
1148         out->stream.common.add_audio_effect = out_add_audio_effect;
1149         out->stream.common.remove_audio_effect = out_remove_audio_effect;
1150         out->stream.get_latency = out_get_latency;
1151         out->stream.set_volume = out_set_volume;
1152         out->stream.write = out_write;
1153         out->stream.get_render_position = out_get_render_position;
1154
1155         /* TODO: for now we always use endpoint 0 */
1156         out->ep = &audio_endpoints[0];
1157
1158         if (ipc_open_stream_cmd(out->ep->id, &mtu, &fd, &preset) !=
1159                         AUDIO_STATUS_SUCCESS)
1160                 goto fail;
1161
1162         if (!preset || fd < 0)
1163                 goto fail;
1164
1165         out->ep->fd = fd;
1166
1167         codec = out->ep->codec;
1168
1169         codec->init(preset, mtu, &out->ep->codec_data);
1170         codec->get_config(out->ep->codec_data, &out->cfg);
1171
1172         DBG("rate=%d channels=%d format=%d", out->cfg.rate,
1173                         out->cfg.channels, out->cfg.format);
1174
1175         free(preset);
1176
1177         *stream_out = &out->stream;
1178         a2dp_dev->out = out;
1179
1180         out->audio_state = AUDIO_A2DP_STATE_STANDBY;
1181
1182         return 0;
1183
1184 fail:
1185         free(out);
1186         *stream_out = NULL;
1187         return -EIO;
1188 }
1189
1190 static void audio_close_output_stream(struct audio_hw_device *dev,
1191                                         struct audio_stream_out *stream)
1192 {
1193         struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
1194         struct audio_endpoint *ep = a2dp_dev->out->ep;
1195
1196         DBG("");
1197
1198         ipc_close_stream_cmd(ep->id);
1199
1200         if (ep->fd >= 0) {
1201                 close(ep->fd);
1202                 ep->fd = -1;
1203         }
1204
1205         ep->codec->cleanup(ep->codec_data);
1206         ep->codec_data = NULL;
1207
1208         free(stream);
1209         a2dp_dev->out = NULL;
1210 }
1211
1212 static int audio_set_parameters(struct audio_hw_device *dev,
1213                                                         const char *kvpairs)
1214 {
1215         struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
1216         struct a2dp_stream_out *out = a2dp_dev->out;
1217
1218         DBG("");
1219
1220         if (!out)
1221                 return 0;
1222
1223         return out->stream.common.set_parameters((struct audio_stream *) out,
1224                                                         kvpairs);
1225 }
1226
1227 static char *audio_get_parameters(const struct audio_hw_device *dev,
1228                                                         const char *keys)
1229 {
1230         DBG("");
1231         return strdup("");
1232 }
1233
1234 static int audio_init_check(const struct audio_hw_device *dev)
1235 {
1236         DBG("");
1237         return 0;
1238 }
1239
1240 static int audio_set_voice_volume(struct audio_hw_device *dev, float volume)
1241 {
1242         DBG("");
1243         return -ENOSYS;
1244 }
1245
1246 static int audio_set_master_volume(struct audio_hw_device *dev, float volume)
1247 {
1248         DBG("");
1249         return -ENOSYS;
1250 }
1251
1252 static int audio_set_mode(struct audio_hw_device *dev, int mode)
1253 {
1254         DBG("");
1255         return -ENOSYS;
1256 }
1257
1258 static int audio_set_mic_mute(struct audio_hw_device *dev, bool state)
1259 {
1260         DBG("");
1261         return -ENOSYS;
1262 }
1263
1264 static int audio_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1265 {
1266         DBG("");
1267         return -ENOSYS;
1268 }
1269
1270 static size_t audio_get_input_buffer_size(const struct audio_hw_device *dev,
1271                                         const struct audio_config *config)
1272 {
1273         DBG("");
1274         return -ENOSYS;
1275 }
1276
1277 static int audio_open_input_stream(struct audio_hw_device *dev,
1278                                         audio_io_handle_t handle,
1279                                         audio_devices_t devices,
1280                                         struct audio_config *config,
1281                                         struct audio_stream_in **stream_in)
1282 {
1283         struct audio_stream_in *in;
1284
1285         DBG("");
1286
1287         in = calloc(1, sizeof(struct audio_stream_in));
1288         if (!in)
1289                 return -ENOMEM;
1290
1291         in->common.get_sample_rate = in_get_sample_rate;
1292         in->common.set_sample_rate = in_set_sample_rate;
1293         in->common.get_buffer_size = in_get_buffer_size;
1294         in->common.get_channels = in_get_channels;
1295         in->common.get_format = in_get_format;
1296         in->common.set_format = in_set_format;
1297         in->common.standby = in_standby;
1298         in->common.dump = in_dump;
1299         in->common.set_parameters = in_set_parameters;
1300         in->common.get_parameters = in_get_parameters;
1301         in->common.add_audio_effect = in_add_audio_effect;
1302         in->common.remove_audio_effect = in_remove_audio_effect;
1303         in->set_gain = in_set_gain;
1304         in->read = in_read;
1305         in->get_input_frames_lost = in_get_input_frames_lost;
1306
1307         *stream_in = in;
1308
1309         return 0;
1310 }
1311
1312 static void audio_close_input_stream(struct audio_hw_device *dev,
1313                                         struct audio_stream_in *stream_in)
1314 {
1315         DBG("");
1316         free(stream_in);
1317 }
1318
1319 static int audio_dump(const audio_hw_device_t *device, int fd)
1320 {
1321         DBG("");
1322         return -ENOSYS;
1323 }
1324
1325 static int audio_close(hw_device_t *device)
1326 {
1327         struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *)device;
1328
1329         DBG("");
1330
1331         unregister_endpoints();
1332
1333         shutdown(listen_sk, SHUT_RDWR);
1334         shutdown(audio_sk, SHUT_RDWR);
1335
1336         pthread_join(ipc_th, NULL);
1337
1338         close(listen_sk);
1339         listen_sk = -1;
1340
1341         free(a2dp_dev);
1342         return 0;
1343 }
1344
1345 static void *ipc_handler(void *data)
1346 {
1347         bool done = false;
1348         struct pollfd pfd;
1349         int sk;
1350
1351         DBG("");
1352
1353         while (!done) {
1354                 DBG("Waiting for connection ...");
1355
1356                 sk = accept(listen_sk, NULL, NULL);
1357                 if (sk < 0) {
1358                         int err = errno;
1359
1360                         if (err == EINTR)
1361                                 continue;
1362
1363                         if (err != ECONNABORTED && err != EINVAL)
1364                                 error("audio: Failed to accept socket: %d (%s)",
1365                                                         err, strerror(err));
1366
1367                         break;
1368                 }
1369
1370                 pthread_mutex_lock(&sk_mutex);
1371                 audio_sk = sk;
1372                 pthread_mutex_unlock(&sk_mutex);
1373
1374                 DBG("Audio IPC: Connected");
1375
1376                 if (register_endpoints() != AUDIO_STATUS_SUCCESS) {
1377                         error("audio: Failed to register endpoints");
1378
1379                         unregister_endpoints();
1380
1381                         pthread_mutex_lock(&sk_mutex);
1382                         shutdown(audio_sk, SHUT_RDWR);
1383                         close(audio_sk);
1384                         audio_sk = -1;
1385                         pthread_mutex_unlock(&sk_mutex);
1386
1387                         continue;
1388                 }
1389
1390                 memset(&pfd, 0, sizeof(pfd));
1391                 pfd.fd = audio_sk;
1392                 pfd.events = POLLHUP | POLLERR | POLLNVAL;
1393
1394                 /* Check if socket is still alive. Empty while loop.*/
1395                 while (poll(&pfd, 1, -1) < 0 && errno == EINTR);
1396
1397                 if (pfd.revents & (POLLHUP | POLLERR | POLLNVAL)) {
1398                         info("Audio HAL: Socket closed");
1399
1400                         pthread_mutex_lock(&sk_mutex);
1401                         close(audio_sk);
1402                         audio_sk = -1;
1403                         pthread_mutex_unlock(&sk_mutex);
1404                 }
1405         }
1406
1407         /* audio_sk is closed at this point, just cleanup endpoints states */
1408         memset(audio_endpoints, 0, sizeof(audio_endpoints));
1409
1410         info("Closing Audio IPC thread");
1411         return NULL;
1412 }
1413
1414 static int audio_ipc_init(void)
1415 {
1416         struct sockaddr_un addr;
1417         int err;
1418         int sk;
1419
1420         DBG("");
1421
1422         sk = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
1423         if (sk < 0) {
1424                 err = errno;
1425                 error("audio: Failed to create socket: %d (%s)", err,
1426                                                                 strerror(err));
1427                 return err;
1428         }
1429
1430         memset(&addr, 0, sizeof(addr));
1431         addr.sun_family = AF_UNIX;
1432
1433         memcpy(addr.sun_path, BLUEZ_AUDIO_SK_PATH,
1434                                         sizeof(BLUEZ_AUDIO_SK_PATH));
1435
1436         if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1437                 err = errno;
1438                 error("audio: Failed to bind socket: %d (%s)", err,
1439                                                                 strerror(err));
1440                 goto failed;
1441         }
1442
1443         if (listen(sk, 1) < 0) {
1444                 err = errno;
1445                 error("audio: Failed to listen on the socket: %d (%s)", err,
1446                                                                 strerror(err));
1447                 goto failed;
1448         }
1449
1450         listen_sk = sk;
1451
1452         err = pthread_create(&ipc_th, NULL, ipc_handler, NULL);
1453         if (err) {
1454                 err = -err;
1455                 ipc_th = 0;
1456                 error("audio: Failed to start Audio IPC thread: %d (%s)",
1457                                                         err, strerror(err));
1458                 goto failed;
1459         }
1460
1461         return 0;
1462
1463 failed:
1464         close(sk);
1465         return err;
1466 }
1467
1468 static int audio_open(const hw_module_t *module, const char *name,
1469                                                         hw_device_t **device)
1470 {
1471         struct a2dp_audio_dev *a2dp_dev;
1472         int err;
1473
1474         DBG("");
1475
1476         if (strcmp(name, AUDIO_HARDWARE_INTERFACE)) {
1477                 error("audio: interface %s not matching [%s]", name,
1478                                                 AUDIO_HARDWARE_INTERFACE);
1479                 return -EINVAL;
1480         }
1481
1482         err = audio_ipc_init();
1483         if (err)
1484                 return -err;
1485
1486         a2dp_dev = calloc(1, sizeof(struct a2dp_audio_dev));
1487         if (!a2dp_dev)
1488                 return -ENOMEM;
1489
1490         a2dp_dev->dev.common.tag = HARDWARE_DEVICE_TAG;
1491         a2dp_dev->dev.common.version = AUDIO_DEVICE_API_VERSION_CURRENT;
1492         a2dp_dev->dev.common.module = (struct hw_module_t *) module;
1493         a2dp_dev->dev.common.close = audio_close;
1494
1495         a2dp_dev->dev.init_check = audio_init_check;
1496         a2dp_dev->dev.set_voice_volume = audio_set_voice_volume;
1497         a2dp_dev->dev.set_master_volume = audio_set_master_volume;
1498         a2dp_dev->dev.set_mode = audio_set_mode;
1499         a2dp_dev->dev.set_mic_mute = audio_set_mic_mute;
1500         a2dp_dev->dev.get_mic_mute = audio_get_mic_mute;
1501         a2dp_dev->dev.set_parameters = audio_set_parameters;
1502         a2dp_dev->dev.get_parameters = audio_get_parameters;
1503         a2dp_dev->dev.get_input_buffer_size = audio_get_input_buffer_size;
1504         a2dp_dev->dev.open_output_stream = audio_open_output_stream;
1505         a2dp_dev->dev.close_output_stream = audio_close_output_stream;
1506         a2dp_dev->dev.open_input_stream = audio_open_input_stream;
1507         a2dp_dev->dev.close_input_stream = audio_close_input_stream;
1508         a2dp_dev->dev.dump = audio_dump;
1509
1510         /* Note that &a2dp_dev->dev.common is the same pointer as a2dp_dev.
1511          * This results from the structure of following structs:a2dp_audio_dev,
1512          * audio_hw_device. We will rely on this later in the code.*/
1513         *device = &a2dp_dev->dev.common;
1514
1515         return 0;
1516 }
1517
1518 static struct hw_module_methods_t hal_module_methods = {
1519         .open = audio_open,
1520 };
1521
1522 struct audio_module HAL_MODULE_INFO_SYM = {
1523         .common = {
1524         .tag = HARDWARE_MODULE_TAG,
1525         .version_major = 1,
1526         .version_minor = 0,
1527         .id = AUDIO_HARDWARE_MODULE_ID,
1528         .name = "A2DP Bluez HW HAL",
1529         .author = "Intel Corporation",
1530         .methods = &hal_module_methods,
1531         },
1532 };