OSDN Git Service

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