OSDN Git Service

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