OSDN Git Service

207101fa80216f6697eda0c1cae547787e524198
[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 "audio-msg.h"
34 #include "ipc-common.h"
35 #include "hal-log.h"
36 #include "hal-msg.h"
37 #include "hal-audio.h"
38 #include "hal-utils.h"
39 #include "hal.h"
40
41 #define FIXED_A2DP_PLAYBACK_LATENCY_MS 25
42
43 #define FIXED_BUFFER_SIZE (20 * 512)
44
45 #define MAX_DELAY       100000 /* 100ms */
46
47 static const uint8_t a2dp_src_uuid[] = {
48                 0x00, 0x00, 0x11, 0x0a, 0x00, 0x00, 0x10, 0x00,
49                 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb };
50
51 static int listen_sk = -1;
52 static int audio_sk = -1;
53
54 static pthread_t ipc_th = 0;
55 static pthread_mutex_t sk_mutex = PTHREAD_MUTEX_INITIALIZER;
56
57 static void timespec_add(struct timespec *base, uint64_t time_us,
58                                                         struct timespec *res)
59 {
60         res->tv_sec = base->tv_sec + time_us / 1000000;
61         res->tv_nsec = base->tv_nsec + (time_us % 1000000) * 1000;
62
63         if (res->tv_nsec >= 1000000000) {
64                 res->tv_sec++;
65                 res->tv_nsec -= 1000000000;
66         }
67 }
68
69 static void timespec_diff(struct timespec *a, struct timespec *b,
70                                                         struct timespec *res)
71 {
72         res->tv_sec = a->tv_sec - b->tv_sec;
73         res->tv_nsec = a->tv_nsec - b->tv_nsec;
74
75         if (res->tv_nsec < 0) {
76                 res->tv_sec--;
77                 res->tv_nsec += 1000000000; /* 1sec */
78         }
79 }
80
81 static uint64_t timespec_diff_us(struct timespec *a, struct timespec *b)
82 {
83         struct timespec res;
84
85         timespec_diff(a, b, &res);
86
87         return res.tv_sec * 1000000ll + res.tv_nsec / 1000ll;
88 }
89
90 #if defined(ANDROID)
91 /*
92  * Bionic does not have clock_nanosleep() prototype in time.h even though
93  * it provides its implementation.
94  */
95 extern int clock_nanosleep(clockid_t clock_id, int flags,
96                                         const struct timespec *request,
97                                         struct timespec *remain);
98 #endif
99
100 static struct {
101         const audio_codec_get_t get_codec;
102         bool loaded;
103 } audio_codecs[] = {
104                 { .get_codec = codec_aptx, .loaded = false },
105                 { .get_codec = codec_sbc, .loaded = false },
106 };
107
108 #define NUM_CODECS (sizeof(audio_codecs) / sizeof(audio_codecs[0]))
109
110 #define MAX_AUDIO_ENDPOINTS NUM_CODECS
111
112 struct audio_endpoint {
113         uint8_t id;
114         const struct audio_codec *codec;
115         void *codec_data;
116         int fd;
117
118         struct media_packet *mp;
119         size_t mp_data_len;
120
121         uint16_t seq;
122         uint32_t samples;
123         struct timespec start;
124
125         bool resync;
126 };
127
128 static struct audio_endpoint audio_endpoints[MAX_AUDIO_ENDPOINTS];
129
130 enum a2dp_state_t {
131         AUDIO_A2DP_STATE_NONE,
132         AUDIO_A2DP_STATE_STANDBY,
133         AUDIO_A2DP_STATE_SUSPENDED,
134         AUDIO_A2DP_STATE_STARTED
135 };
136
137 struct a2dp_stream_out {
138         struct audio_stream_out stream;
139
140         struct audio_endpoint *ep;
141         enum a2dp_state_t audio_state;
142         struct audio_input_config cfg;
143
144         uint8_t *downmix_buf;
145 };
146
147 struct a2dp_audio_dev {
148         struct audio_hw_device dev;
149         struct a2dp_stream_out *out;
150 };
151
152 static int audio_ipc_cmd(uint8_t service_id, uint8_t opcode, uint16_t len,
153                         void *param, size_t *rsp_len, void *rsp, int *fd)
154 {
155         ssize_t ret;
156         struct msghdr msg;
157         struct iovec iv[2];
158         struct ipc_hdr cmd;
159         char cmsgbuf[CMSG_SPACE(sizeof(int))];
160         struct ipc_status s;
161         size_t s_len = sizeof(s);
162
163         pthread_mutex_lock(&sk_mutex);
164
165         if (audio_sk < 0) {
166                 error("audio: Invalid cmd socket passed to audio_ipc_cmd");
167                 goto failed;
168         }
169
170         if (!rsp || !rsp_len) {
171                 memset(&s, 0, s_len);
172                 rsp_len = &s_len;
173                 rsp = &s;
174         }
175
176         memset(&msg, 0, sizeof(msg));
177         memset(&cmd, 0, sizeof(cmd));
178
179         cmd.service_id = service_id;
180         cmd.opcode = opcode;
181         cmd.len = len;
182
183         iv[0].iov_base = &cmd;
184         iv[0].iov_len = sizeof(cmd);
185
186         iv[1].iov_base = param;
187         iv[1].iov_len = len;
188
189         msg.msg_iov = iv;
190         msg.msg_iovlen = 2;
191
192         ret = sendmsg(audio_sk, &msg, 0);
193         if (ret < 0) {
194                 error("audio: Sending command failed:%s", strerror(errno));
195                 goto failed;
196         }
197
198         /* socket was shutdown */
199         if (ret == 0) {
200                 error("audio: Command socket closed");
201                 goto failed;
202         }
203
204         memset(&msg, 0, sizeof(msg));
205         memset(&cmd, 0, sizeof(cmd));
206
207         iv[0].iov_base = &cmd;
208         iv[0].iov_len = sizeof(cmd);
209
210         iv[1].iov_base = rsp;
211         iv[1].iov_len = *rsp_len;
212
213         msg.msg_iov = iv;
214         msg.msg_iovlen = 2;
215
216         if (fd) {
217                 memset(cmsgbuf, 0, sizeof(cmsgbuf));
218                 msg.msg_control = cmsgbuf;
219                 msg.msg_controllen = sizeof(cmsgbuf);
220         }
221
222         ret = recvmsg(audio_sk, &msg, 0);
223         if (ret < 0) {
224                 error("audio: Receiving command response failed:%s",
225                                                         strerror(errno));
226                 goto failed;
227         }
228
229         if (ret < (ssize_t) sizeof(cmd)) {
230                 error("audio: Too small response received(%zd bytes)", ret);
231                 goto failed;
232         }
233
234         if (cmd.service_id != service_id) {
235                 error("audio: Invalid service id (%u vs %u)", cmd.service_id,
236                                                                 service_id);
237                 goto failed;
238         }
239
240         if (ret != (ssize_t) (sizeof(cmd) + cmd.len)) {
241                 error("audio: Malformed response received(%zd bytes)", ret);
242                 goto failed;
243         }
244
245         if (cmd.opcode != opcode && cmd.opcode != AUDIO_OP_STATUS) {
246                 error("audio: Invalid opcode received (%u vs %u)",
247                                                 cmd.opcode, opcode);
248                 goto failed;
249         }
250
251         if (cmd.opcode == AUDIO_OP_STATUS) {
252                 struct ipc_status *s = rsp;
253
254                 if (sizeof(*s) != cmd.len) {
255                         error("audio: Invalid status length");
256                         goto failed;
257                 }
258
259                 if (s->code == AUDIO_STATUS_SUCCESS) {
260                         error("audio: Invalid success status response");
261                         goto failed;
262                 }
263
264                 pthread_mutex_unlock(&sk_mutex);
265
266                 return s->code;
267         }
268
269         pthread_mutex_unlock(&sk_mutex);
270
271         /* Receive auxiliary data in msg */
272         if (fd) {
273                 struct cmsghdr *cmsg;
274
275                 *fd = -1;
276
277                 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
278                                         cmsg = CMSG_NXTHDR(&msg, cmsg)) {
279                         if (cmsg->cmsg_level == SOL_SOCKET
280                                         && cmsg->cmsg_type == SCM_RIGHTS) {
281                                 memcpy(fd, CMSG_DATA(cmsg), sizeof(int));
282                                 break;
283                         }
284                 }
285
286                 if (*fd < 0)
287                         goto failed;
288         }
289
290         *rsp_len = cmd.len;
291
292         return AUDIO_STATUS_SUCCESS;
293
294 failed:
295         /* Some serious issue happen on IPC - recover */
296         shutdown(audio_sk, SHUT_RDWR);
297         pthread_mutex_unlock(&sk_mutex);
298
299         return AUDIO_STATUS_FAILED;
300 }
301
302 static int ipc_open_cmd(const struct audio_codec *codec)
303 {
304         uint8_t buf[BLUEZ_AUDIO_MTU];
305         struct audio_cmd_open *cmd = (struct audio_cmd_open *) buf;
306         struct audio_rsp_open rsp;
307         size_t cmd_len = sizeof(buf) - sizeof(*cmd);
308         size_t rsp_len = sizeof(rsp);
309         int result;
310
311         DBG("");
312
313         memcpy(cmd->uuid, a2dp_src_uuid, sizeof(a2dp_src_uuid));
314
315         cmd->codec = codec->type;
316         cmd->presets = codec->get_presets(cmd->preset, &cmd_len);
317
318         cmd_len += sizeof(*cmd);
319
320         result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_OPEN, cmd_len, cmd,
321                                 &rsp_len, &rsp, NULL);
322
323         if (result != AUDIO_STATUS_SUCCESS)
324                 return 0;
325
326         return rsp.id;
327 }
328
329 static int ipc_close_cmd(uint8_t endpoint_id)
330 {
331         struct audio_cmd_close cmd;
332         int result;
333
334         DBG("");
335
336         cmd.id = endpoint_id;
337
338         result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_CLOSE,
339                                 sizeof(cmd), &cmd, NULL, NULL, NULL);
340
341         return result;
342 }
343
344 static int ipc_open_stream_cmd(uint8_t *endpoint_id, uint16_t *mtu, int *fd,
345                                                 struct audio_preset **caps)
346 {
347         char buf[BLUEZ_AUDIO_MTU];
348         struct audio_cmd_open_stream cmd;
349         struct audio_rsp_open_stream *rsp =
350                                         (struct audio_rsp_open_stream *) &buf;
351         size_t rsp_len = sizeof(buf);
352         int result;
353
354         DBG("");
355
356         if (!caps)
357                 return AUDIO_STATUS_FAILED;
358
359         cmd.id = *endpoint_id;
360
361         result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_OPEN_STREAM,
362                                 sizeof(cmd), &cmd, &rsp_len, rsp, fd);
363         if (result == AUDIO_STATUS_SUCCESS) {
364                 size_t buf_len = sizeof(struct audio_preset) +
365                                         rsp->preset[0].len;
366                 *endpoint_id = rsp->id;
367                 *mtu = rsp->mtu;
368                 *caps = malloc(buf_len);
369                 memcpy(*caps, &rsp->preset, buf_len);
370         } else {
371                 *caps = NULL;
372         }
373
374         return result;
375 }
376
377 static int ipc_close_stream_cmd(uint8_t endpoint_id)
378 {
379         struct audio_cmd_close_stream cmd;
380         int result;
381
382         DBG("");
383
384         cmd.id = endpoint_id;
385
386         result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_CLOSE_STREAM,
387                                         sizeof(cmd), &cmd, NULL, NULL, NULL);
388
389         return result;
390 }
391
392 static int ipc_resume_stream_cmd(uint8_t endpoint_id)
393 {
394         struct audio_cmd_resume_stream cmd;
395         int result;
396
397         DBG("");
398
399         cmd.id = endpoint_id;
400
401         result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_RESUME_STREAM,
402                                         sizeof(cmd), &cmd, NULL, NULL, NULL);
403
404         return result;
405 }
406
407 static int ipc_suspend_stream_cmd(uint8_t endpoint_id)
408 {
409         struct audio_cmd_suspend_stream cmd;
410         int result;
411
412         DBG("");
413
414         cmd.id = endpoint_id;
415
416         result = audio_ipc_cmd(AUDIO_SERVICE_ID, AUDIO_OP_SUSPEND_STREAM,
417                                         sizeof(cmd), &cmd, NULL, NULL, NULL);
418
419         return result;
420 }
421
422 struct register_state {
423         struct audio_endpoint *ep;
424         bool error;
425 };
426
427 static void register_endpoint(const struct audio_codec *codec,
428                                                 struct register_state *state)
429 {
430         struct audio_endpoint *ep = state->ep;
431
432         /* don't even try to register more endpoints if one failed */
433         if (state->error)
434                 return;
435
436         ep->id = ipc_open_cmd(codec);
437
438         if (!ep->id) {
439                 state->error = true;
440                 error("Failed to register endpoint");
441                 return;
442         }
443
444         ep->codec = codec;
445         ep->codec_data = NULL;
446         ep->fd = -1;
447
448         state->ep++;
449 }
450
451 static int register_endpoints(void)
452 {
453         struct register_state state;
454         unsigned int i;
455
456         state.ep = &audio_endpoints[0];
457         state.error = false;
458
459         for (i = 0; i < NUM_CODECS; i++) {
460                 const struct audio_codec *codec = audio_codecs[i].get_codec();
461
462                 if (!audio_codecs[i].loaded)
463                         continue;
464
465                 register_endpoint(codec, &state);
466         }
467
468         return state.error ? AUDIO_STATUS_FAILED : AUDIO_STATUS_SUCCESS;
469 }
470
471 static void unregister_endpoints(void)
472 {
473         size_t i;
474
475         for (i = 0; i < MAX_AUDIO_ENDPOINTS; i++) {
476                 struct audio_endpoint *ep = &audio_endpoints[i];
477
478                 if (ep->id) {
479                         ipc_close_cmd(ep->id);
480                         memset(ep, 0, sizeof(*ep));
481                 }
482         }
483 }
484
485 static bool open_endpoint(struct audio_endpoint **epp,
486                                                 struct audio_input_config *cfg)
487 {
488         struct audio_preset *preset;
489         struct audio_endpoint *ep = *epp;
490         const struct audio_codec *codec;
491         uint16_t mtu;
492         uint16_t payload_len;
493         int fd;
494         size_t i;
495         uint8_t ep_id = 0;
496
497         if (ep)
498                 ep_id = ep->id;
499
500         if (ipc_open_stream_cmd(&ep_id, &mtu, &fd, &preset) !=
501                                                         AUDIO_STATUS_SUCCESS)
502                 return false;
503
504         DBG("ep_id=%d mtu=%u", ep_id, mtu);
505
506         for (i = 0; i < MAX_AUDIO_ENDPOINTS; i++)
507                 if (audio_endpoints[i].id == ep_id) {
508                         ep = &audio_endpoints[i];
509                         break;
510                 }
511
512         if (!ep) {
513                 error("Cound not find opened endpoint");
514                 goto failed;
515         }
516
517         *epp = ep;
518
519         payload_len = mtu;
520         if (ep->codec->use_rtp)
521                 payload_len -= sizeof(struct rtp_header);
522
523         ep->fd = fd;
524
525         codec = ep->codec;
526         codec->init(preset, payload_len, &ep->codec_data);
527         codec->get_config(ep->codec_data, cfg);
528
529         ep->mp = calloc(mtu, 1);
530         if (!ep->mp)
531                 goto failed;
532
533         if (ep->codec->use_rtp) {
534                 struct media_packet_rtp *mp_rtp =
535                                         (struct media_packet_rtp *) ep->mp;
536                 mp_rtp->hdr.v = 2;
537                 mp_rtp->hdr.pt = 0x60;
538                 mp_rtp->hdr.ssrc = htonl(1);
539         }
540
541         ep->mp_data_len = payload_len;
542
543         free(preset);
544
545         return true;
546
547 failed:
548         close(fd);
549         free(preset);
550
551         return false;
552 }
553
554 static void close_endpoint(struct audio_endpoint *ep)
555 {
556         ipc_close_stream_cmd(ep->id);
557         if (ep->fd >= 0) {
558                 close(ep->fd);
559                 ep->fd = -1;
560         }
561
562         free(ep->mp);
563
564         ep->codec->cleanup(ep->codec_data);
565         ep->codec_data = NULL;
566 }
567
568 static bool resume_endpoint(struct audio_endpoint *ep)
569 {
570         if (ipc_resume_stream_cmd(ep->id) != AUDIO_STATUS_SUCCESS)
571                 return false;
572
573         ep->samples = 0;
574         ep->resync = false;
575
576         ep->codec->update_qos(ep->codec_data, QOS_POLICY_DEFAULT);
577
578         return true;
579 }
580
581 static void downmix_to_mono(struct a2dp_stream_out *out, const uint8_t *buffer,
582                                                                 size_t bytes)
583 {
584         const int16_t *input = (const void *) buffer;
585         int16_t *output = (void *) out->downmix_buf;
586         size_t i, frames;
587
588         /* PCM 16bit stereo */
589         frames = bytes / (2 * sizeof(int16_t));
590
591         for (i = 0; i < frames; i++) {
592                 int16_t l = get_le16(&input[i * 2]);
593                 int16_t r = get_le16(&input[i * 2 + 1]);
594
595                 put_le16((l + r) / 2, &output[i]);
596         }
597 }
598
599 static bool wait_for_endpoint(struct audio_endpoint *ep, bool *writable)
600 {
601         int ret;
602
603         while (true) {
604                 struct pollfd pollfd;
605
606                 pollfd.fd = ep->fd;
607                 pollfd.events = POLLOUT;
608                 pollfd.revents = 0;
609
610                 ret = poll(&pollfd, 1, 500);
611
612                 if (ret >= 0) {
613                         *writable = !!(pollfd.revents & POLLOUT);
614                         break;
615                 }
616
617                 if (errno != EINTR) {
618                         ret = errno;
619                         error("poll failed (%d)", ret);
620                         return false;
621                 }
622         }
623
624         return true;
625 }
626
627 static bool write_to_endpoint(struct audio_endpoint *ep, size_t bytes)
628 {
629         struct media_packet *mp = (struct media_packet *) ep->mp;
630         int ret;
631
632         while (true) {
633                 ret = write(ep->fd, mp, bytes);
634
635                 if (ret >= 0)
636                         break;
637
638                 /*
639                  * this should not happen so let's issue warning, but do not
640                  * fail, we can try to write next packet
641                  */
642                 if (errno == EAGAIN) {
643                         ret = errno;
644                         warn("write failed (%d)", ret);
645                         break;
646                 }
647
648                 if (errno != EINTR) {
649                         ret = errno;
650                         error("write failed (%d)", ret);
651                         return false;
652                 }
653         }
654
655         return true;
656 }
657
658 static bool write_data(struct a2dp_stream_out *out, const void *buffer,
659                                                                 size_t bytes)
660 {
661         struct audio_endpoint *ep = out->ep;
662         struct media_packet *mp = (struct media_packet *) ep->mp;
663         struct media_packet_rtp *mp_rtp = (struct media_packet_rtp *) ep->mp;
664         size_t free_space = ep->mp_data_len;
665         size_t consumed = 0;
666
667         while (consumed < bytes) {
668                 size_t written = 0;
669                 ssize_t read;
670                 uint32_t samples;
671                 int ret;
672                 struct timespec current;
673                 uint64_t audio_sent, audio_passed;
674                 bool do_write = false;
675
676                 /*
677                  * prepare media packet in advance so we don't waste time after
678                  * wakeup
679                  */
680                 if (ep->codec->use_rtp) {
681                         mp_rtp->hdr.sequence_number = htons(ep->seq++);
682                         mp_rtp->hdr.timestamp = htonl(ep->samples);
683                 }
684                 read = ep->codec->encode_mediapacket(ep->codec_data,
685                                                 buffer + consumed,
686                                                 bytes - consumed, mp,
687                                                 free_space, &written);
688
689                 /*
690                  * not much we can do here, let's just ignore remaining
691                  * data and continue
692                  */
693                 if (read <= 0)
694                         return true;
695
696                 /* calculate where are we and where we should be */
697                 clock_gettime(CLOCK_MONOTONIC, &current);
698                 if (!ep->samples)
699                         memcpy(&ep->start, &current, sizeof(ep->start));
700                 audio_sent = ep->samples * 1000000ll / out->cfg.rate;
701                 audio_passed = timespec_diff_us(&current, &ep->start);
702
703                 /*
704                  * if we're ahead of stream then wait for next write point,
705                  * if we're lagging more than 100ms then stop writing and just
706                  * skip data until we're back in sync
707                  */
708                 if (audio_sent > audio_passed) {
709                         struct timespec anchor;
710
711                         ep->resync = false;
712
713                         timespec_add(&ep->start, audio_sent, &anchor);
714
715                         while (true) {
716                                 ret = clock_nanosleep(CLOCK_MONOTONIC,
717                                                         TIMER_ABSTIME, &anchor,
718                                                         NULL);
719
720                                 if (!ret)
721                                         break;
722
723                                 if (ret != EINTR) {
724                                         error("clock_nanosleep failed (%d)",
725                                                                         ret);
726                                         return false;
727                                 }
728                         }
729                 } else if (!ep->resync) {
730                         uint64_t diff = audio_passed - audio_sent;
731
732                         if (diff > MAX_DELAY) {
733                                 warn("lag is %jums, resyncing", diff / 1000);
734
735                                 ep->codec->update_qos(ep->codec_data,
736                                                         QOS_POLICY_DECREASE);
737                                 ep->resync = true;
738                         }
739                 }
740
741                 /* we send data only in case codec encoded some data, i.e. some
742                  * codecs do internal buffering and output data only if full
743                  * frame can be encoded
744                  * in resync mode we'll just drop mediapackets
745                  */
746                 if (written > 0 && !ep->resync) {
747                         /* wait some time for socket to be ready for write,
748                          * but we'll just skip writing data if timeout occurs
749                          */
750                         if (!wait_for_endpoint(ep, &do_write))
751                                 return false;
752
753                         if (do_write) {
754                                 if (ep->codec->use_rtp)
755                                         written += sizeof(struct rtp_header);
756
757                                 if (!write_to_endpoint(ep, written))
758                                         return false;
759                         }
760                 }
761
762                 /*
763                  * AudioFlinger provides 16bit PCM, so sample size is 2 bytes
764                  * multiplied by number of channels. Number of channels is
765                  * simply number of bits set in channels mask.
766                  */
767                 samples = read / (2 * popcount(out->cfg.channels));
768                 ep->samples += samples;
769                 consumed += read;
770         }
771
772         return true;
773 }
774
775 static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
776                                                                 size_t bytes)
777 {
778         struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
779         const void *in_buf = buffer;
780         size_t in_len = bytes;
781
782         /* just return in case we're closing */
783         if (out->audio_state == AUDIO_A2DP_STATE_NONE)
784                 return -1;
785
786         /* We can auto-start only from standby */
787         if (out->audio_state == AUDIO_A2DP_STATE_STANDBY) {
788                 DBG("stream in standby, auto-start");
789
790                 if (!resume_endpoint(out->ep))
791                         return -1;
792
793                 out->audio_state = AUDIO_A2DP_STATE_STARTED;
794         }
795
796         if (out->audio_state != AUDIO_A2DP_STATE_STARTED) {
797                 error("audio: stream not started");
798                 return -1;
799         }
800
801         if (out->ep->fd < 0) {
802                 error("audio: no transport socket");
803                 return -1;
804         }
805
806         /*
807          * currently Android audioflinger is not able to provide mono stream on
808          * A2DP output so down mixing needs to be done in hal-audio plugin.
809          *
810          * for reference see
811          * AudioFlinger::PlaybackThread::readOutputParameters()
812          * frameworks/av/services/audioflinger/Threads.cpp:1631
813          */
814         if (out->cfg.channels == AUDIO_CHANNEL_OUT_MONO) {
815                 if (!out->downmix_buf) {
816                         error("audio: downmix buffer not initialized");
817                         return -1;
818                 }
819
820                 downmix_to_mono(out, buffer, bytes);
821
822                 in_buf = out->downmix_buf;
823                 in_len = bytes / 2;
824         }
825
826         if (!write_data(out, in_buf, in_len))
827                 return -1;
828
829         return bytes;
830 }
831
832 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
833 {
834         struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
835
836         DBG("");
837
838         return out->cfg.rate;
839 }
840
841 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
842 {
843         struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
844
845         DBG("");
846
847         if (rate != out->cfg.rate) {
848                 warn("audio: cannot set sample rate to %d", rate);
849                 return -1;
850         }
851
852         return 0;
853 }
854
855 static size_t out_get_buffer_size(const struct audio_stream *stream)
856 {
857         DBG("");
858
859         /*
860          * We should return proper buffer size calculated by codec (so each
861          * input buffer is encoded into single media packed) but this does not
862          * work well with AudioFlinger and causes problems. For this reason we
863          * use magic value here and out_write code takes care of splitting
864          * input buffer into multiple media packets.
865          */
866         return FIXED_BUFFER_SIZE;
867 }
868
869 static uint32_t out_get_channels(const struct audio_stream *stream)
870 {
871         DBG("");
872
873         /*
874          * AudioFlinger can only provide stereo stream, so we return it here and
875          * later we'll downmix this to mono in case codec requires it
876          */
877
878         return AUDIO_CHANNEL_OUT_STEREO;
879 }
880
881 static audio_format_t out_get_format(const struct audio_stream *stream)
882 {
883         struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
884
885         DBG("");
886
887         return out->cfg.format;
888 }
889
890 static int out_set_format(struct audio_stream *stream, audio_format_t format)
891 {
892         DBG("");
893         return -ENOSYS;
894 }
895
896 static int out_standby(struct audio_stream *stream)
897 {
898         struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
899
900         DBG("");
901
902         if (out->audio_state == AUDIO_A2DP_STATE_STARTED) {
903                 if (ipc_suspend_stream_cmd(out->ep->id) != AUDIO_STATUS_SUCCESS)
904                         return -1;
905                 out->audio_state = AUDIO_A2DP_STATE_STANDBY;
906         }
907
908         return 0;
909 }
910
911 static int out_dump(const struct audio_stream *stream, int fd)
912 {
913         DBG("");
914         return -ENOSYS;
915 }
916
917 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
918 {
919         struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
920         char *kvpair;
921         char *str;
922         char *saveptr;
923         bool enter_suspend = false;
924         bool exit_suspend = false;
925
926         DBG("%s", kvpairs);
927
928         str = strdup(kvpairs);
929         if (!str)
930                 return -ENOMEM;
931
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 audio_open_output_stream_real(struct audio_hw_device *dev,
1113                                         audio_io_handle_t handle,
1114                                         audio_devices_t devices,
1115                                         audio_output_flags_t flags,
1116                                         struct audio_config *config,
1117                                         struct audio_stream_out **stream_out,
1118                                         const char *address)
1119 {
1120         struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
1121         struct a2dp_stream_out *out;
1122
1123         out = calloc(1, sizeof(struct a2dp_stream_out));
1124         if (!out)
1125                 return -ENOMEM;
1126
1127         DBG("");
1128
1129         out->stream.common.get_sample_rate = out_get_sample_rate;
1130         out->stream.common.set_sample_rate = out_set_sample_rate;
1131         out->stream.common.get_buffer_size = out_get_buffer_size;
1132         out->stream.common.get_channels = out_get_channels;
1133         out->stream.common.get_format = out_get_format;
1134         out->stream.common.set_format = out_set_format;
1135         out->stream.common.standby = out_standby;
1136         out->stream.common.dump = out_dump;
1137         out->stream.common.set_parameters = out_set_parameters;
1138         out->stream.common.get_parameters = out_get_parameters;
1139         out->stream.common.add_audio_effect = out_add_audio_effect;
1140         out->stream.common.remove_audio_effect = out_remove_audio_effect;
1141         out->stream.get_latency = out_get_latency;
1142         out->stream.set_volume = out_set_volume;
1143         out->stream.write = out_write;
1144         out->stream.get_render_position = out_get_render_position;
1145
1146         /* We want to autoselect opened endpoint */
1147         out->ep = NULL;
1148
1149         if (!open_endpoint(&out->ep, &out->cfg))
1150                 goto fail;
1151
1152         DBG("rate=%d channels=%d format=%d", out->cfg.rate,
1153                                         out->cfg.channels, out->cfg.format);
1154
1155         if (out->cfg.channels == AUDIO_CHANNEL_OUT_MONO) {
1156                 out->downmix_buf = malloc(FIXED_BUFFER_SIZE / 2);
1157                 if (!out->downmix_buf)
1158                         goto fail;
1159         }
1160
1161         *stream_out = &out->stream;
1162         a2dp_dev->out = out;
1163
1164         out->audio_state = AUDIO_A2DP_STATE_STANDBY;
1165
1166         return 0;
1167
1168 fail:
1169         error("audio: cannot open output stream");
1170         free(out);
1171         *stream_out = NULL;
1172         return -EIO;
1173 }
1174
1175 #if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
1176 static int audio_open_output_stream(struct audio_hw_device *dev,
1177                                         audio_io_handle_t handle,
1178                                         audio_devices_t devices,
1179                                         audio_output_flags_t flags,
1180                                         struct audio_config *config,
1181                                         struct audio_stream_out **stream_out,
1182                                         const char *address)
1183 {
1184         return audio_open_output_stream_real(dev, handle, devices, flags,
1185                                                 config, stream_out, address);
1186 }
1187 #else
1188 static int audio_open_output_stream(struct audio_hw_device *dev,
1189                                         audio_io_handle_t handle,
1190                                         audio_devices_t devices,
1191                                         audio_output_flags_t flags,
1192                                         struct audio_config *config,
1193                                         struct audio_stream_out **stream_out)
1194 {
1195         return audio_open_output_stream_real(dev, handle, devices, flags,
1196                                                 config, stream_out, NULL);
1197 }
1198 #endif
1199
1200 static void audio_close_output_stream(struct audio_hw_device *dev,
1201                                         struct audio_stream_out *stream)
1202 {
1203         struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
1204         struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
1205
1206         DBG("");
1207
1208         close_endpoint(a2dp_dev->out->ep);
1209
1210         free(out->downmix_buf);
1211
1212         free(stream);
1213         a2dp_dev->out = NULL;
1214 }
1215
1216 static int audio_set_parameters(struct audio_hw_device *dev,
1217                                                         const char *kvpairs)
1218 {
1219         struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
1220         struct a2dp_stream_out *out = a2dp_dev->out;
1221
1222         DBG("");
1223
1224         if (!out)
1225                 return 0;
1226
1227         return out->stream.common.set_parameters((struct audio_stream *) out,
1228                                                                 kvpairs);
1229 }
1230
1231 static char *audio_get_parameters(const struct audio_hw_device *dev,
1232                                                         const char *keys)
1233 {
1234         DBG("");
1235         return strdup("");
1236 }
1237
1238 static int audio_init_check(const struct audio_hw_device *dev)
1239 {
1240         DBG("");
1241         return 0;
1242 }
1243
1244 static int audio_set_voice_volume(struct audio_hw_device *dev, float volume)
1245 {
1246         DBG("");
1247         return -ENOSYS;
1248 }
1249
1250 static int audio_set_master_volume(struct audio_hw_device *dev, float volume)
1251 {
1252         DBG("");
1253         return -ENOSYS;
1254 }
1255
1256 static int audio_set_mode(struct audio_hw_device *dev, int mode)
1257 {
1258         DBG("");
1259         return -ENOSYS;
1260 }
1261
1262 static int audio_set_mic_mute(struct audio_hw_device *dev, bool state)
1263 {
1264         DBG("");
1265         return -ENOSYS;
1266 }
1267
1268 static int audio_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1269 {
1270         DBG("");
1271         return -ENOSYS;
1272 }
1273
1274 static size_t audio_get_input_buffer_size(const struct audio_hw_device *dev,
1275                                         const struct audio_config *config)
1276 {
1277         DBG("");
1278         return -ENOSYS;
1279 }
1280
1281 static int audio_open_input_stream_real(struct audio_hw_device *dev,
1282                                         audio_io_handle_t handle,
1283                                         audio_devices_t devices,
1284                                         struct audio_config *config,
1285                                         struct audio_stream_in **stream_in,
1286                                         audio_input_flags_t flags,
1287                                         const char *address,
1288                                         audio_source_t source)
1289 {
1290         struct audio_stream_in *in;
1291
1292         DBG("");
1293
1294         in = calloc(1, sizeof(struct audio_stream_in));
1295         if (!in)
1296                 return -ENOMEM;
1297
1298         in->common.get_sample_rate = in_get_sample_rate;
1299         in->common.set_sample_rate = in_set_sample_rate;
1300         in->common.get_buffer_size = in_get_buffer_size;
1301         in->common.get_channels = in_get_channels;
1302         in->common.get_format = in_get_format;
1303         in->common.set_format = in_set_format;
1304         in->common.standby = in_standby;
1305         in->common.dump = in_dump;
1306         in->common.set_parameters = in_set_parameters;
1307         in->common.get_parameters = in_get_parameters;
1308         in->common.add_audio_effect = in_add_audio_effect;
1309         in->common.remove_audio_effect = in_remove_audio_effect;
1310         in->set_gain = in_set_gain;
1311         in->read = in_read;
1312         in->get_input_frames_lost = in_get_input_frames_lost;
1313
1314         *stream_in = in;
1315
1316         return 0;
1317 }
1318
1319 #if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
1320 static int audio_open_input_stream(struct audio_hw_device *dev,
1321                                         audio_io_handle_t handle,
1322                                         audio_devices_t devices,
1323                                         struct audio_config *config,
1324                                         struct audio_stream_in **stream_in,
1325                                         audio_input_flags_t flags,
1326                                         const char *address,
1327                                         audio_source_t source)
1328 {
1329         return audio_open_input_stream_real(dev, handle, devices, config,
1330                                                 stream_in, flags, address,
1331                                                 source);
1332 }
1333 #else
1334 static int audio_open_input_stream(struct audio_hw_device *dev,
1335                                         audio_io_handle_t handle,
1336                                         audio_devices_t devices,
1337                                         struct audio_config *config,
1338                                         struct audio_stream_in **stream_in)
1339 {
1340         return audio_open_input_stream_real(dev, handle, devices, config,
1341                                                 stream_in, 0, NULL, 0);
1342 }
1343 #endif
1344
1345 static void audio_close_input_stream(struct audio_hw_device *dev,
1346                                         struct audio_stream_in *stream_in)
1347 {
1348         DBG("");
1349         free(stream_in);
1350 }
1351
1352 static int audio_dump(const audio_hw_device_t *device, int fd)
1353 {
1354         DBG("");
1355         return -ENOSYS;
1356 }
1357
1358 #if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
1359 static int set_master_mute(struct audio_hw_device *dev, bool mute)
1360 {
1361         DBG("");
1362         return -ENOSYS;
1363 }
1364
1365 static int get_master_mute(struct audio_hw_device *dev, bool *mute)
1366 {
1367         DBG("");
1368         return -ENOSYS;
1369 }
1370
1371 static int create_audio_patch(struct audio_hw_device *dev,
1372                                         unsigned int num_sources,
1373                                         const struct audio_port_config *sources,
1374                                         unsigned int num_sinks,
1375                                         const struct audio_port_config *sinks,
1376                                         audio_patch_handle_t *handle)
1377 {
1378         DBG("");
1379         return -ENOSYS;
1380 }
1381
1382 static int release_audio_patch(struct audio_hw_device *dev,
1383                                         audio_patch_handle_t handle)
1384 {
1385         DBG("");
1386         return -ENOSYS;
1387 }
1388
1389 static int get_audio_port(struct audio_hw_device *dev, struct audio_port *port)
1390 {
1391         DBG("");
1392         return -ENOSYS;
1393 }
1394
1395 static int set_audio_port_config(struct audio_hw_device *dev,
1396                                         const struct audio_port_config *config)
1397 {
1398         DBG("");
1399         return -ENOSYS;
1400 }
1401 #endif
1402
1403 static int audio_close(hw_device_t *device)
1404 {
1405         struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *)device;
1406         unsigned int i;
1407
1408         DBG("");
1409
1410         unregister_endpoints();
1411
1412         for (i = 0; i < NUM_CODECS; i++) {
1413                 const struct audio_codec *codec = audio_codecs[i].get_codec();
1414
1415                 if (!audio_codecs[i].loaded)
1416                         continue;
1417
1418                 if (codec->unload)
1419                         codec->unload();
1420
1421                 audio_codecs[i].loaded = false;
1422         }
1423
1424         shutdown(listen_sk, SHUT_RDWR);
1425         shutdown(audio_sk, SHUT_RDWR);
1426
1427         pthread_join(ipc_th, NULL);
1428
1429         close(listen_sk);
1430         listen_sk = -1;
1431
1432         free(a2dp_dev);
1433         return 0;
1434 }
1435
1436 static void *ipc_handler(void *data)
1437 {
1438         bool done = false;
1439         struct pollfd pfd;
1440         int sk;
1441
1442         DBG("");
1443
1444         while (!done) {
1445                 DBG("Waiting for connection ...");
1446
1447                 sk = accept(listen_sk, NULL, NULL);
1448                 if (sk < 0) {
1449                         int err = errno;
1450
1451                         if (err == EINTR)
1452                                 continue;
1453
1454                         if (err != ECONNABORTED && err != EINVAL)
1455                                 error("audio: Failed to accept socket: %d (%s)",
1456                                                         err, strerror(err));
1457
1458                         break;
1459                 }
1460
1461                 pthread_mutex_lock(&sk_mutex);
1462                 audio_sk = sk;
1463                 pthread_mutex_unlock(&sk_mutex);
1464
1465                 DBG("Audio IPC: Connected");
1466
1467                 if (register_endpoints() != AUDIO_STATUS_SUCCESS) {
1468                         error("audio: Failed to register endpoints");
1469
1470                         unregister_endpoints();
1471
1472                         pthread_mutex_lock(&sk_mutex);
1473                         shutdown(audio_sk, SHUT_RDWR);
1474                         close(audio_sk);
1475                         audio_sk = -1;
1476                         pthread_mutex_unlock(&sk_mutex);
1477
1478                         continue;
1479                 }
1480
1481                 memset(&pfd, 0, sizeof(pfd));
1482                 pfd.fd = audio_sk;
1483                 pfd.events = POLLHUP | POLLERR | POLLNVAL;
1484
1485                 /* Check if socket is still alive. Empty while loop.*/
1486                 while (poll(&pfd, 1, -1) < 0 && errno == EINTR);
1487
1488                 info("Audio HAL: Socket closed");
1489
1490                 pthread_mutex_lock(&sk_mutex);
1491                 close(audio_sk);
1492                 audio_sk = -1;
1493                 pthread_mutex_unlock(&sk_mutex);
1494         }
1495
1496         /* audio_sk is closed at this point, just cleanup endpoints states */
1497         memset(audio_endpoints, 0, sizeof(audio_endpoints));
1498
1499         info("Closing Audio IPC thread");
1500         return NULL;
1501 }
1502
1503 static int audio_ipc_init(void)
1504 {
1505         struct sockaddr_un addr;
1506         int err;
1507         int sk;
1508
1509         DBG("");
1510
1511         sk = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
1512         if (sk < 0) {
1513                 err = -errno;
1514                 error("audio: Failed to create socket: %d (%s)", -err,
1515                                                                 strerror(-err));
1516                 return err;
1517         }
1518
1519         memset(&addr, 0, sizeof(addr));
1520         addr.sun_family = AF_UNIX;
1521
1522         memcpy(addr.sun_path, BLUEZ_AUDIO_SK_PATH,
1523                                         sizeof(BLUEZ_AUDIO_SK_PATH));
1524
1525         if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1526                 err = -errno;
1527                 error("audio: Failed to bind socket: %d (%s)", -err,
1528                                                                 strerror(-err));
1529                 goto failed;
1530         }
1531
1532         if (listen(sk, 1) < 0) {
1533                 err = -errno;
1534                 error("audio: Failed to listen on the socket: %d (%s)", -err,
1535                                                                 strerror(-err));
1536                 goto failed;
1537         }
1538
1539         listen_sk = sk;
1540
1541         err = pthread_create(&ipc_th, NULL, ipc_handler, NULL);
1542         if (err) {
1543                 err = -err;
1544                 ipc_th = 0;
1545                 error("audio: Failed to start Audio IPC thread: %d (%s)",
1546                                                         -err, strerror(-err));
1547                 goto failed;
1548         }
1549
1550         return 0;
1551
1552 failed:
1553         close(sk);
1554         return err;
1555 }
1556
1557 static int audio_open(const hw_module_t *module, const char *name,
1558                                                         hw_device_t **device)
1559 {
1560         struct a2dp_audio_dev *a2dp_dev;
1561         size_t i;
1562         int err;
1563
1564         DBG("");
1565
1566         if (strcmp(name, AUDIO_HARDWARE_INTERFACE)) {
1567                 error("audio: interface %s not matching [%s]", name,
1568                                                 AUDIO_HARDWARE_INTERFACE);
1569                 return -EINVAL;
1570         }
1571
1572         err = audio_ipc_init();
1573         if (err < 0)
1574                 return err;
1575
1576         a2dp_dev = calloc(1, sizeof(struct a2dp_audio_dev));
1577         if (!a2dp_dev)
1578                 return -ENOMEM;
1579
1580         a2dp_dev->dev.common.tag = HARDWARE_DEVICE_TAG;
1581         a2dp_dev->dev.common.version = AUDIO_DEVICE_API_VERSION_CURRENT;
1582         a2dp_dev->dev.common.module = (struct hw_module_t *) module;
1583         a2dp_dev->dev.common.close = audio_close;
1584
1585         a2dp_dev->dev.init_check = audio_init_check;
1586         a2dp_dev->dev.set_voice_volume = audio_set_voice_volume;
1587         a2dp_dev->dev.set_master_volume = audio_set_master_volume;
1588         a2dp_dev->dev.set_mode = audio_set_mode;
1589         a2dp_dev->dev.set_mic_mute = audio_set_mic_mute;
1590         a2dp_dev->dev.get_mic_mute = audio_get_mic_mute;
1591         a2dp_dev->dev.set_parameters = audio_set_parameters;
1592         a2dp_dev->dev.get_parameters = audio_get_parameters;
1593         a2dp_dev->dev.get_input_buffer_size = audio_get_input_buffer_size;
1594         a2dp_dev->dev.open_output_stream = audio_open_output_stream;
1595         a2dp_dev->dev.close_output_stream = audio_close_output_stream;
1596         a2dp_dev->dev.open_input_stream = audio_open_input_stream;
1597         a2dp_dev->dev.close_input_stream = audio_close_input_stream;
1598         a2dp_dev->dev.dump = audio_dump;
1599 #if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
1600         a2dp_dev->dev.set_master_mute = set_master_mute;
1601         a2dp_dev->dev.get_master_mute = get_master_mute;
1602         a2dp_dev->dev.create_audio_patch = create_audio_patch;
1603         a2dp_dev->dev.release_audio_patch = release_audio_patch;
1604         a2dp_dev->dev.get_audio_port = get_audio_port;
1605         a2dp_dev->dev.set_audio_port_config = set_audio_port_config;
1606 #endif
1607
1608         for (i = 0; i < NUM_CODECS; i++) {
1609                 const struct audio_codec *codec = audio_codecs[i].get_codec();
1610
1611                 if (codec->load && !codec->load())
1612                         continue;
1613
1614                 audio_codecs[i].loaded = true;
1615         }
1616
1617         /*
1618          * Note that &a2dp_dev->dev.common is the same pointer as a2dp_dev.
1619          * This results from the structure of following structs:a2dp_audio_dev,
1620          * audio_hw_device. We will rely on this later in the code.
1621          */
1622         *device = &a2dp_dev->dev.common;
1623
1624         return 0;
1625 }
1626
1627 static struct hw_module_methods_t hal_module_methods = {
1628         .open = audio_open,
1629 };
1630
1631 struct audio_module HAL_MODULE_INFO_SYM = {
1632         .common = {
1633                 .tag = HARDWARE_MODULE_TAG,
1634                 .version_major = 1,
1635                 .version_minor = 0,
1636                 .id = AUDIO_HARDWARE_MODULE_ID,
1637                 .name = "A2DP Bluez HW HAL",
1638                 .author = "Intel Corporation",
1639                 .methods = &hal_module_methods,
1640         },
1641 };