OSDN Git Service

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