OSDN Git Service

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