OSDN Git Service

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