OSDN Git Service

am 83c5256f: (-s ours) Logging cleanup: HCI and BTM trace macros.
[android-x86/system-bt.git] / audio_a2dp_hw / audio_a2dp_hw.c
1 /******************************************************************************
2  *
3  *  Copyright (C) 2009-2012 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18
19 /*****************************************************************************
20  *
21  *  Filename:      audio_a2dp_hw.c
22  *
23  *  Description:   Implements hal for bluedroid a2dp audio device
24  *
25  *****************************************************************************/
26
27 #include <errno.h>
28 #include <inttypes.h>
29 #include <pthread.h>
30 #include <stdint.h>
31 #include <sys/time.h>
32 #include <sys/socket.h>
33 #include <sys/un.h>
34 #include <sys/poll.h>
35 #include <sys/errno.h>
36 #include <sys/stat.h>
37 #include <unistd.h>
38 #include <fcntl.h>
39 #include <cutils/str_parms.h>
40 #include <cutils/sockets.h>
41
42 #include <system/audio.h>
43 #include <hardware/audio.h>
44
45 #include <hardware/hardware.h>
46 #include "audio_a2dp_hw.h"
47 #include "bt_utils.h"
48
49 #define LOG_TAG "audio_a2dp_hw"
50 /* #define LOG_NDEBUG 0 */
51 #include <log/log.h>
52
53 /*****************************************************************************
54 **  Constants & Macros
55 ******************************************************************************/
56
57 #define CTRL_CHAN_RETRY_COUNT 3
58 #define USEC_PER_SEC 1000000L
59
60 #define CASE_RETURN_STR(const) case const: return #const;
61
62 #define FNLOG()             ALOGV("%s", __FUNCTION__);
63 #define DEBUG(fmt, ...)     ALOGV("%s: " fmt,__FUNCTION__, ## __VA_ARGS__)
64 #define INFO(fmt, ...)      ALOGI("%s: " fmt,__FUNCTION__, ## __VA_ARGS__)
65 #define ERROR(fmt, ...)     ALOGE("%s: " fmt,__FUNCTION__, ## __VA_ARGS__)
66
67 #define ASSERTC(cond, msg, val) if (!(cond)) {ERROR("### ASSERT : %s line %d %s (%d) ###", __FILE__, __LINE__, msg, val);}
68
69 /*****************************************************************************
70 **  Local type definitions
71 ******************************************************************************/
72
73 typedef enum {
74     AUDIO_A2DP_STATE_STARTING,
75     AUDIO_A2DP_STATE_STARTED,
76     AUDIO_A2DP_STATE_STOPPING,
77     AUDIO_A2DP_STATE_STOPPED,
78     AUDIO_A2DP_STATE_SUSPENDED, /* need explicit set param call to resume (suspend=false) */
79     AUDIO_A2DP_STATE_STANDBY    /* allows write to autoresume */
80 } a2dp_state_t;
81
82 struct a2dp_stream_in;
83 struct a2dp_stream_out;
84
85 struct a2dp_audio_device {
86     struct audio_hw_device device;
87     struct a2dp_stream_in  *input;
88     struct a2dp_stream_out *output;
89 };
90
91 struct a2dp_config {
92     uint32_t                rate;
93     uint32_t                channel_flags;
94     int                     format;
95 };
96
97 /* move ctrl_fd outside output stream and keep open until HAL unloaded ? */
98
99 struct a2dp_stream_common {
100     pthread_mutex_t         lock;
101     int                     ctrl_fd;
102     int                     audio_fd;
103     size_t                  buffer_sz;
104     struct a2dp_config      cfg;
105     a2dp_state_t            state;
106 };
107
108 struct a2dp_stream_out {
109     struct audio_stream_out stream;
110     struct a2dp_stream_common common;
111 };
112
113 struct a2dp_stream_in {
114     struct audio_stream_in  stream;
115     struct a2dp_stream_common common;
116 };
117
118 /*****************************************************************************
119 **  Static variables
120 ******************************************************************************/
121
122 /*****************************************************************************
123 **  Static functions
124 ******************************************************************************/
125
126 static size_t out_get_buffer_size(const struct audio_stream *stream);
127
128 /*****************************************************************************
129 **  Externs
130 ******************************************************************************/
131
132 /*****************************************************************************
133 **  Functions
134 ******************************************************************************/
135
136 /*****************************************************************************
137 **   Miscellaneous helper functions
138 ******************************************************************************/
139
140 static const char* dump_a2dp_ctrl_event(char event)
141 {
142     switch(event)
143     {
144         CASE_RETURN_STR(A2DP_CTRL_CMD_NONE)
145         CASE_RETURN_STR(A2DP_CTRL_CMD_CHECK_READY)
146         CASE_RETURN_STR(A2DP_CTRL_CMD_START)
147         CASE_RETURN_STR(A2DP_CTRL_CMD_STOP)
148         CASE_RETURN_STR(A2DP_CTRL_CMD_SUSPEND)
149         default:
150             return "UNKNOWN MSG ID";
151     }
152 }
153
154 /* logs timestamp with microsec precision
155    pprev is optional in case a dedicated diff is required */
156 static void ts_log(char *tag, int val, struct timespec *pprev_opt)
157 {
158     struct timespec now;
159     static struct timespec prev = {0,0};
160     unsigned long long now_us;
161     unsigned long long diff_us;
162     UNUSED(tag);
163     UNUSED(val);
164
165     clock_gettime(CLOCK_MONOTONIC, &now);
166
167     now_us = now.tv_sec*USEC_PER_SEC + now.tv_nsec/1000;
168
169     if (pprev_opt)
170     {
171         diff_us = (now.tv_sec - prev.tv_sec) * USEC_PER_SEC + (now.tv_nsec - prev.tv_nsec)/1000;
172         *pprev_opt = now;
173         DEBUG("[%s] ts %08lld, *diff %08lld, val %d", tag, now_us, diff_us, val);
174     }
175     else
176     {
177         diff_us = (now.tv_sec - prev.tv_sec) * USEC_PER_SEC + (now.tv_nsec - prev.tv_nsec)/1000;
178         prev = now;
179         DEBUG("[%s] ts %08lld, diff %08lld, val %d", tag, now_us, diff_us, val);
180     }
181 }
182
183 static int calc_audiotime(struct a2dp_config cfg, int bytes)
184 {
185     int chan_count = popcount(cfg.channel_flags);
186
187     ASSERTC(cfg.format == AUDIO_FORMAT_PCM_16_BIT,
188             "unsupported sample sz", cfg.format);
189
190     return bytes*(1000000/(chan_count*2))/cfg.rate;
191 }
192
193 /*****************************************************************************
194 **
195 **   bluedroid stack adaptation
196 **
197 *****************************************************************************/
198
199 static int skt_connect(char *path, size_t buffer_sz)
200 {
201     int ret;
202     int skt_fd;
203     struct sockaddr_un remote;
204     int len;
205
206     INFO("connect to %s (sz %zu)", path, buffer_sz);
207
208     skt_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
209
210     if(socket_local_client_connect(skt_fd, path,
211             ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM) < 0)
212     {
213         ERROR("failed to connect (%s)", strerror(errno));
214         close(skt_fd);
215         return -1;
216     }
217
218     len = buffer_sz;
219     ret = setsockopt(skt_fd, SOL_SOCKET, SO_SNDBUF, (char*)&len, (int)sizeof(len));
220
221     /* only issue warning if failed */
222     if (ret < 0)
223         ERROR("setsockopt failed (%s)", strerror(errno));
224
225     INFO("connected to stack fd = %d", skt_fd);
226
227     return skt_fd;
228 }
229
230 static int skt_read(int fd, void *p, size_t len, int us_timeout)
231 {
232     int read;
233     struct pollfd pfd;
234     struct timespec ts;
235
236     FNLOG();
237
238     pfd.fd = fd;
239     pfd.events = POLLIN;
240
241     ts.tv_sec = us_timeout / 1000000;
242     ts.tv_nsec = (us_timeout % 1000000) * 1000;
243
244     ts_log("skt_read ppoll", len, NULL);
245
246     /* read time out */
247     if (ppoll(&pfd, 1, &ts, NULL) == 0) {
248         return 0;
249     }
250
251     ts_log("skt_read recv", len, NULL);
252
253     if ((read = recv(fd, p, len, MSG_NOSIGNAL)) == -1)
254     {
255         ERROR("write failed with errno=%d\n", errno);
256         return -1;
257     }
258
259     return read;
260 }
261
262 static int skt_write(int fd, const void *p, size_t len)
263 {
264     int sent;
265     struct pollfd pfd;
266
267     FNLOG();
268
269     pfd.fd = fd;
270     pfd.events = POLLOUT;
271
272     /* poll for 500 ms */
273
274     /* send time out */
275     if (poll(&pfd, 1, 500) == 0)
276         return 0;
277
278     ts_log("skt_write", len, NULL);
279
280     if ((sent = send(fd, p, len, MSG_NOSIGNAL)) == -1)
281     {
282         ERROR("write failed with errno=%d\n", errno);
283         return -1;
284     }
285
286     return sent;
287 }
288
289 static int skt_disconnect(int fd)
290 {
291     INFO("fd %d", fd);
292
293     if (fd != AUDIO_SKT_DISCONNECTED)
294     {
295         shutdown(fd, SHUT_RDWR);
296         close(fd);
297     }
298     return 0;
299 }
300
301
302
303 /*****************************************************************************
304 **
305 **  AUDIO CONTROL PATH
306 **
307 *****************************************************************************/
308
309 static int a2dp_ctrl_receive(struct a2dp_stream_common *common, void* buffer, int length)
310 {
311     int ret = recv(common->ctrl_fd, buffer, length, MSG_NOSIGNAL);
312     if (ret < 0)
313     {
314         ERROR("ack failed (%s)", strerror(errno));
315         if (errno == EINTR)
316         {
317             /* retry again */
318             ret = recv(common->ctrl_fd, buffer, length, MSG_NOSIGNAL);
319             if (ret < 0)
320             {
321                ERROR("ack failed (%s)", strerror(errno));
322                skt_disconnect(common->ctrl_fd);
323                common->ctrl_fd = AUDIO_SKT_DISCONNECTED;
324                return -1;
325             }
326         }
327         else
328         {
329                skt_disconnect(common->ctrl_fd);
330                common->ctrl_fd = AUDIO_SKT_DISCONNECTED;
331                return -1;
332
333         }
334     }
335     return ret;
336 }
337
338 static int a2dp_command(struct a2dp_stream_common *common, char cmd)
339 {
340     char ack;
341
342     DEBUG("A2DP COMMAND %s", dump_a2dp_ctrl_event(cmd));
343
344     /* send command */
345     if (send(common->ctrl_fd, &cmd, 1, MSG_NOSIGNAL) == -1)
346     {
347         ERROR("cmd failed (%s)", strerror(errno));
348         skt_disconnect(common->ctrl_fd);
349         common->ctrl_fd = AUDIO_SKT_DISCONNECTED;
350         return -1;
351     }
352
353     /* wait for ack byte */
354     if (a2dp_ctrl_receive(common, &ack, 1) < 0)
355         return -1;
356
357     DEBUG("A2DP COMMAND %s DONE STATUS %d", dump_a2dp_ctrl_event(cmd), ack);
358
359     if (ack != A2DP_CTRL_ACK_SUCCESS)
360         return -1;
361
362     return 0;
363 }
364
365 static int check_a2dp_ready(struct a2dp_stream_common *common)
366 {
367     if (a2dp_command(common, A2DP_CTRL_CMD_CHECK_READY) < 0)
368     {
369         ERROR("check a2dp ready failed");
370         return -1;
371     }
372     return 0;
373 }
374
375 static int a2dp_read_audio_config(struct a2dp_stream_common *common)
376 {
377     char cmd = A2DP_CTRL_GET_AUDIO_CONFIG;
378     uint32_t sample_rate;
379     uint8_t channel_count;
380
381     if (a2dp_command(common, A2DP_CTRL_GET_AUDIO_CONFIG) < 0)
382     {
383         ERROR("check a2dp ready failed");
384         return -1;
385     }
386
387     if (a2dp_ctrl_receive(common, &sample_rate, 4) < 0)
388         return -1;
389     if (a2dp_ctrl_receive(common, &channel_count, 1) < 0)
390         return -1;
391
392     common->cfg.channel_flags = (channel_count == 1 ? AUDIO_CHANNEL_IN_MONO : AUDIO_CHANNEL_IN_STEREO);
393     common->cfg.format = AUDIO_STREAM_DEFAULT_FORMAT;
394     common->cfg.rate = sample_rate;
395
396     INFO("got config %d %d", common->cfg.format, common->cfg.rate);
397
398     return 0;
399 }
400
401 static void a2dp_open_ctrl_path(struct a2dp_stream_common *common)
402 {
403     int i;
404
405     /* retry logic to catch any timing variations on control channel */
406     for (i = 0; i < CTRL_CHAN_RETRY_COUNT; i++)
407     {
408         /* connect control channel if not already connected */
409         if ((common->ctrl_fd = skt_connect(A2DP_CTRL_PATH, common->buffer_sz)) > 0)
410         {
411             /* success, now check if stack is ready */
412             if (check_a2dp_ready(common) == 0)
413                 break;
414
415             ERROR("error : a2dp not ready, wait 250 ms and retry");
416             usleep(250000);
417             skt_disconnect(common->ctrl_fd);
418             common->ctrl_fd = AUDIO_SKT_DISCONNECTED;
419         }
420
421         /* ctrl channel not ready, wait a bit */
422         usleep(250000);
423     }
424 }
425
426 /*****************************************************************************
427 **
428 ** AUDIO DATA PATH
429 **
430 *****************************************************************************/
431
432 static void a2dp_stream_common_init(struct a2dp_stream_common *common)
433 {
434     pthread_mutexattr_t lock_attr;
435
436     FNLOG();
437
438     pthread_mutexattr_init(&lock_attr);
439     pthread_mutexattr_settype(&lock_attr, PTHREAD_MUTEX_RECURSIVE);
440     pthread_mutex_init(&common->lock, &lock_attr);
441
442     common->ctrl_fd = AUDIO_SKT_DISCONNECTED;
443     common->audio_fd = AUDIO_SKT_DISCONNECTED;
444     common->state = AUDIO_A2DP_STATE_STOPPED;
445
446     /* manages max capacity of socket pipe */
447     common->buffer_sz = AUDIO_STREAM_OUTPUT_BUFFER_SZ;
448 }
449
450 static int start_audio_datapath(struct a2dp_stream_common *common)
451 {
452     int oldstate = common->state;
453
454     INFO("state %d", common->state);
455
456     if (common->ctrl_fd == AUDIO_SKT_DISCONNECTED) {
457         INFO("AUDIO_SKT_DISCONNECTED");
458         return -1;
459     }
460
461     common->state = AUDIO_A2DP_STATE_STARTING;
462
463     if (a2dp_command(common, A2DP_CTRL_CMD_START) < 0)
464     {
465         ERROR("audiopath start failed");
466
467         common->state = oldstate;
468         return -1;
469     }
470
471     /* connect socket if not yet connected */
472     if (common->audio_fd == AUDIO_SKT_DISCONNECTED)
473     {
474         common->audio_fd = skt_connect(A2DP_DATA_PATH, common->buffer_sz);
475         if (common->audio_fd < 0)
476         {
477             common->state = oldstate;
478             return -1;
479         }
480
481         common->state = AUDIO_A2DP_STATE_STARTED;
482     }
483
484     return 0;
485 }
486
487
488 static int stop_audio_datapath(struct a2dp_stream_common *common)
489 {
490     int oldstate = common->state;
491
492     INFO("state %d", common->state);
493
494     if (common->ctrl_fd == AUDIO_SKT_DISCONNECTED)
495          return -1;
496
497     /* prevent any stray output writes from autostarting the stream
498        while stopping audiopath */
499     common->state = AUDIO_A2DP_STATE_STOPPING;
500
501     if (a2dp_command(common, A2DP_CTRL_CMD_STOP) < 0)
502     {
503         ERROR("audiopath stop failed");
504         common->state = oldstate;
505         return -1;
506     }
507
508     common->state = AUDIO_A2DP_STATE_STOPPED;
509
510     /* disconnect audio path */
511     skt_disconnect(common->audio_fd);
512     common->audio_fd = AUDIO_SKT_DISCONNECTED;
513
514     return 0;
515 }
516
517 static int suspend_audio_datapath(struct a2dp_stream_common *common, bool standby)
518 {
519     INFO("state %d", common->state);
520
521     if (common->ctrl_fd == AUDIO_SKT_DISCONNECTED)
522          return -1;
523
524     if (common->state == AUDIO_A2DP_STATE_STOPPING)
525         return -1;
526
527     if (a2dp_command(common, A2DP_CTRL_CMD_SUSPEND) < 0)
528         return -1;
529
530     if (standby)
531         common->state = AUDIO_A2DP_STATE_STANDBY;
532     else
533         common->state = AUDIO_A2DP_STATE_SUSPENDED;
534
535     /* disconnect audio path */
536     skt_disconnect(common->audio_fd);
537
538     common->audio_fd = AUDIO_SKT_DISCONNECTED;
539
540     return 0;
541 }
542
543
544 /*****************************************************************************
545 **
546 **  audio output callbacks
547 **
548 *****************************************************************************/
549
550 static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
551                          size_t bytes)
552 {
553     struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
554     int sent;
555
556     DEBUG("write %zu bytes (fd %d)", bytes, out->common.audio_fd);
557
558     if (out->common.state == AUDIO_A2DP_STATE_SUSPENDED)
559     {
560         DEBUG("stream suspended");
561         return -1;
562     }
563
564     /* only allow autostarting if we are in stopped or standby */
565     if ((out->common.state == AUDIO_A2DP_STATE_STOPPED) ||
566         (out->common.state == AUDIO_A2DP_STATE_STANDBY))
567     {
568         pthread_mutex_lock(&out->common.lock);
569
570         if (start_audio_datapath(&out->common) < 0)
571         {
572             /* emulate time this write represents to avoid very fast write
573                failures during transition periods or remote suspend */
574
575             int us_delay = calc_audiotime(out->common.cfg, bytes);
576
577             DEBUG("emulate a2dp write delay (%d us)", us_delay);
578
579             usleep(us_delay);
580             pthread_mutex_unlock(&out->common.lock);
581             return -1;
582         }
583
584         pthread_mutex_unlock(&out->common.lock);
585     }
586     else if (out->common.state != AUDIO_A2DP_STATE_STARTED)
587     {
588         ERROR("stream not in stopped or standby");
589         return -1;
590     }
591
592     sent = skt_write(out->common.audio_fd, buffer,  bytes);
593
594     if (sent == -1)
595     {
596         skt_disconnect(out->common.audio_fd);
597         out->common.audio_fd = AUDIO_SKT_DISCONNECTED;
598         out->common.state = AUDIO_A2DP_STATE_STOPPED;
599     }
600
601     DEBUG("wrote %d bytes out of %zu bytes", sent, bytes);
602     return sent;
603 }
604
605
606 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
607 {
608     struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
609
610     DEBUG("rate %" PRIu32,out->common.cfg.rate);
611
612     return out->common.cfg.rate;
613 }
614
615 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
616 {
617     struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
618
619     DEBUG("out_set_sample_rate : %" PRIu32, rate);
620
621     if (rate != AUDIO_STREAM_DEFAULT_RATE)
622     {
623         ERROR("only rate %d supported", AUDIO_STREAM_DEFAULT_RATE);
624         return -1;
625     }
626
627     out->common.cfg.rate = rate;
628
629     return 0;
630 }
631
632 static size_t out_get_buffer_size(const struct audio_stream *stream)
633 {
634     struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
635
636     DEBUG("buffer_size : %zu", out->common.buffer_sz);
637
638     return out->common.buffer_sz;
639 }
640
641 static uint32_t out_get_channels(const struct audio_stream *stream)
642 {
643     struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
644
645     DEBUG("channels 0x%" PRIx32, out->common.cfg.channel_flags);
646
647     return out->common.cfg.channel_flags;
648 }
649
650 static audio_format_t out_get_format(const struct audio_stream *stream)
651 {
652     struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
653     DEBUG("format 0x%x", out->common.cfg.format);
654     return out->common.cfg.format;
655 }
656
657 static int out_set_format(struct audio_stream *stream, audio_format_t format)
658 {
659     UNUSED(format);
660     struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
661     DEBUG("setting format not yet supported (0x%x)", format);
662     return -ENOSYS;
663 }
664
665 static int out_standby(struct audio_stream *stream)
666 {
667     struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
668     int retVal = 0;
669
670     FNLOG();
671
672     pthread_mutex_lock(&out->common.lock);
673
674     if (out->common.state == AUDIO_A2DP_STATE_STARTED)
675         retVal =  suspend_audio_datapath(&out->common, true);
676     else
677         retVal = 0;
678     pthread_mutex_unlock(&out->common.lock);
679
680     return retVal;
681 }
682
683 static int out_dump(const struct audio_stream *stream, int fd)
684 {
685     UNUSED(fd);
686     struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
687     FNLOG();
688     return 0;
689 }
690
691 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
692 {
693     struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
694     struct str_parms *parms;
695     char keyval[16];
696     int retval;
697     int status = 0;
698
699     INFO("state %d", out->common.state);
700
701     pthread_mutex_lock(&out->common.lock);
702
703     parms = str_parms_create_str(kvpairs);
704
705     /* dump params */
706     str_parms_dump(parms);
707
708     retval = str_parms_get_str(parms, "closing", keyval, sizeof(keyval));
709
710     if (retval >= 0)
711     {
712         if (strcmp(keyval, "true") == 0)
713         {
714             DEBUG("stream closing, disallow any writes");
715             out->common.state = AUDIO_A2DP_STATE_STOPPING;
716         }
717     }
718
719     retval = str_parms_get_str(parms, "A2dpSuspended", keyval, sizeof(keyval));
720
721     if (retval >= 0)
722     {
723         if (strcmp(keyval, "true") == 0)
724         {
725             if (out->common.state == AUDIO_A2DP_STATE_STARTED)
726                 status = suspend_audio_datapath(&out->common, false);
727         }
728         else
729         {
730             /* Do not start the streaming automatically. If the phone was streaming
731              * prior to being suspended, the next out_write shall trigger the
732              * AVDTP start procedure */
733             if (out->common.state == AUDIO_A2DP_STATE_SUSPENDED)
734                 out->common.state = AUDIO_A2DP_STATE_STANDBY;
735             /* Irrespective of the state, return 0 */
736         }
737     }
738
739     pthread_mutex_unlock(&out->common.lock);
740     str_parms_destroy(parms);
741
742     return status;
743 }
744
745 static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
746 {
747     UNUSED(keys);
748     struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
749
750     FNLOG();
751
752     /* add populating param here */
753
754     return strdup("");
755 }
756
757 static uint32_t out_get_latency(const struct audio_stream_out *stream)
758 {
759     int latency_us;
760
761     struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
762
763     FNLOG();
764
765     latency_us = ((out->common.buffer_sz * 1000 ) /
766                     audio_stream_frame_size(&out->stream.common) /
767                     out->common.cfg.rate) * 1000;
768
769
770     return (latency_us / 1000) + 200;
771 }
772
773 static int out_set_volume(struct audio_stream_out *stream, float left,
774                           float right)
775 {
776     UNUSED(stream);
777     UNUSED(left);
778     UNUSED(right);
779
780     FNLOG();
781
782     /* volume controlled in audioflinger mixer (digital) */
783
784     return -ENOSYS;
785 }
786
787
788
789 static int out_get_render_position(const struct audio_stream_out *stream,
790                                    uint32_t *dsp_frames)
791 {
792     UNUSED(stream);
793     UNUSED(dsp_frames);
794
795     FNLOG();
796     return -EINVAL;
797 }
798
799 static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
800 {
801     UNUSED(stream);
802     UNUSED(effect);
803
804     FNLOG();
805     return 0;
806 }
807
808 static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
809 {
810     UNUSED(stream);
811     UNUSED(effect);
812
813     FNLOG();
814     return 0;
815 }
816
817 /*
818  * AUDIO INPUT STREAM
819  */
820
821 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
822 {
823     struct a2dp_stream_in *in = (struct a2dp_stream_in *)stream;
824
825     FNLOG();
826     return in->common.cfg.rate;
827 }
828
829 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
830 {
831     struct a2dp_stream_in *in = (struct a2dp_stream_in *)stream;
832
833     FNLOG();
834
835     if (in->common.cfg.rate > 0 && in->common.cfg.rate == rate)
836         return 0;
837     else
838         return -1;
839 }
840
841 static size_t in_get_buffer_size(const struct audio_stream *stream)
842 {
843     UNUSED(stream);
844
845     FNLOG();
846     return 320;
847 }
848
849 static uint32_t in_get_channels(const struct audio_stream *stream)
850 {
851     struct a2dp_stream_in *in = (struct a2dp_stream_in *)stream;
852
853     FNLOG();
854     return in->common.cfg.channel_flags;
855 }
856
857 static audio_format_t in_get_format(const struct audio_stream *stream)
858 {
859     UNUSED(stream);
860
861     FNLOG();
862     return AUDIO_FORMAT_PCM_16_BIT;
863 }
864
865 static int in_set_format(struct audio_stream *stream, audio_format_t format)
866 {
867     UNUSED(stream);
868     UNUSED(format);
869
870     FNLOG();
871     if (format == AUDIO_FORMAT_PCM_16_BIT)
872         return 0;
873     else
874         return -1;
875 }
876
877 static int in_standby(struct audio_stream *stream)
878 {
879     UNUSED(stream);
880
881     FNLOG();
882     return 0;
883 }
884
885 static int in_dump(const struct audio_stream *stream, int fd)
886 {
887     UNUSED(stream);
888     UNUSED(fd);
889
890     FNLOG();
891     return 0;
892 }
893
894 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
895 {
896     UNUSED(stream);
897     UNUSED(kvpairs);
898
899     FNLOG();
900     return 0;
901 }
902
903 static char * in_get_parameters(const struct audio_stream *stream,
904                                 const char *keys)
905 {
906     UNUSED(stream);
907     UNUSED(keys);
908
909     FNLOG();
910     return strdup("");
911 }
912
913 static int in_set_gain(struct audio_stream_in *stream, float gain)
914 {
915     UNUSED(stream);
916     UNUSED(gain);
917
918     FNLOG();
919     return 0;
920 }
921
922 static ssize_t in_read(struct audio_stream_in *stream, void* buffer,
923                        size_t bytes)
924 {
925     struct a2dp_stream_in *in = (struct a2dp_stream_in *)stream;
926     int read;
927
928     DEBUG("read %zu bytes, state: %d", bytes, in->common.state);
929
930     if (in->common.state == AUDIO_A2DP_STATE_SUSPENDED)
931     {
932         DEBUG("stream suspended");
933         return -1;
934     }
935
936     int us_delay = calc_audiotime(in->common.cfg, bytes);
937
938     /* only allow autostarting if we are in stopped or standby */
939     if ((in->common.state == AUDIO_A2DP_STATE_STOPPED) ||
940         (in->common.state == AUDIO_A2DP_STATE_STANDBY))
941     {
942         pthread_mutex_lock(&in->common.lock);
943
944         if (start_audio_datapath(&in->common) < 0)
945         {
946             /* emulate time this write represents to avoid very fast write
947                failures during transition periods or remote suspend */
948
949             DEBUG("emulate a2dp read delay (%d us)", us_delay);
950
951             usleep(us_delay);
952             pthread_mutex_unlock(&in->common.lock);
953             return -1;
954         }
955
956         pthread_mutex_unlock(&in->common.lock);
957     }
958     else if (in->common.state != AUDIO_A2DP_STATE_STARTED)
959     {
960         ERROR("stream not in stopped or standby");
961         return -1;
962     }
963
964     read = skt_read(in->common.audio_fd, buffer, bytes, us_delay);
965
966     if (read == -1)
967     {
968         skt_disconnect(in->common.audio_fd);
969         in->common.audio_fd = AUDIO_SKT_DISCONNECTED;
970         in->common.state = AUDIO_A2DP_STATE_STOPPED;
971     } else if (read == 0) {
972         DEBUG("read time out - return zeros");
973         memset(buffer, 0, bytes);
974         read = bytes;
975     }
976
977     DEBUG("read %d bytes out of %zu bytes", read, bytes);
978     return read;
979 }
980
981 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
982 {
983     UNUSED(stream);
984
985     FNLOG();
986     return 0;
987 }
988
989 static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
990 {
991     UNUSED(stream);
992     UNUSED(effect);
993
994     FNLOG();
995     return 0;
996 }
997
998 static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
999 {
1000     UNUSED(stream);
1001     UNUSED(effect);
1002
1003     FNLOG();
1004
1005     return 0;
1006 }
1007
1008 static int adev_open_output_stream(struct audio_hw_device *dev,
1009                                    audio_io_handle_t handle,
1010                                    audio_devices_t devices,
1011                                    audio_output_flags_t flags,
1012                                    struct audio_config *config,
1013                                    struct audio_stream_out **stream_out)
1014
1015 {
1016     struct a2dp_audio_device *a2dp_dev = (struct a2dp_audio_device *)dev;
1017     struct a2dp_stream_out *out;
1018     int ret = 0;
1019     int i;
1020     UNUSED(handle);
1021     UNUSED(devices);
1022     UNUSED(flags);
1023
1024     INFO("opening output");
1025
1026     out = (struct a2dp_stream_out *)calloc(1, sizeof(struct a2dp_stream_out));
1027
1028     if (!out)
1029         return -ENOMEM;
1030
1031     out->stream.common.get_sample_rate = out_get_sample_rate;
1032     out->stream.common.set_sample_rate = out_set_sample_rate;
1033     out->stream.common.get_buffer_size = out_get_buffer_size;
1034     out->stream.common.get_channels = out_get_channels;
1035     out->stream.common.get_format = out_get_format;
1036     out->stream.common.set_format = out_set_format;
1037     out->stream.common.standby = out_standby;
1038     out->stream.common.dump = out_dump;
1039     out->stream.common.set_parameters = out_set_parameters;
1040     out->stream.common.get_parameters = out_get_parameters;
1041     out->stream.common.add_audio_effect = out_add_audio_effect;
1042     out->stream.common.remove_audio_effect = out_remove_audio_effect;
1043     out->stream.get_latency = out_get_latency;
1044     out->stream.set_volume = out_set_volume;
1045     out->stream.write = out_write;
1046     out->stream.get_render_position = out_get_render_position;
1047
1048     /* initialize a2dp specifics */
1049     a2dp_stream_common_init(&out->common);
1050
1051     out->common.cfg.channel_flags = AUDIO_STREAM_DEFAULT_CHANNEL_FLAG;
1052     out->common.cfg.format = AUDIO_STREAM_DEFAULT_FORMAT;
1053     out->common.cfg.rate = AUDIO_STREAM_DEFAULT_RATE;
1054
1055    /* set output config values */
1056    if (config)
1057    {
1058       config->format = out_get_format((const struct audio_stream *)&out->stream);
1059       config->sample_rate = out_get_sample_rate((const struct audio_stream *)&out->stream);
1060       config->channel_mask = out_get_channels((const struct audio_stream *)&out->stream);
1061    }
1062     *stream_out = &out->stream;
1063     a2dp_dev->output = out;
1064
1065     a2dp_open_ctrl_path(&out->common);
1066     if (out->common.ctrl_fd == AUDIO_SKT_DISCONNECTED)
1067     {
1068         ERROR("ctrl socket failed to connect (%s)", strerror(errno));
1069         ret = -1;
1070         goto err_open;
1071     }
1072
1073     DEBUG("success");
1074     return 0;
1075
1076 err_open:
1077     free(out);
1078     *stream_out = NULL;
1079     a2dp_dev->output = NULL;
1080     ERROR("failed");
1081     return ret;
1082 }
1083
1084 static void adev_close_output_stream(struct audio_hw_device *dev,
1085                                      struct audio_stream_out *stream)
1086 {
1087     struct a2dp_audio_device *a2dp_dev = (struct a2dp_audio_device *)dev;
1088     struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
1089
1090     INFO("closing output (state %d)", out->common.state);
1091
1092     if ((out->common.state == AUDIO_A2DP_STATE_STARTED) || (out->common.state == AUDIO_A2DP_STATE_STOPPING))
1093         stop_audio_datapath(&out->common);
1094
1095     skt_disconnect(out->common.ctrl_fd);
1096     free(stream);
1097     a2dp_dev->output = NULL;
1098
1099     DEBUG("done");
1100 }
1101
1102 static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
1103 {
1104     struct a2dp_audio_device *a2dp_dev = (struct a2dp_audio_device *)dev;
1105     struct a2dp_stream_out *out = a2dp_dev->output;
1106     int retval = 0;
1107
1108     if (out == NULL)
1109     {
1110         ERROR("ERROR: set param called even when stream out is null");
1111         return retval;
1112     }
1113     INFO("state %d", out->common.state);
1114
1115     retval = out->stream.common.set_parameters((struct audio_stream *)out, kvpairs);
1116
1117     return retval;
1118 }
1119
1120 static char * adev_get_parameters(const struct audio_hw_device *dev,
1121                                   const char *keys)
1122 {
1123     struct str_parms *parms;
1124     UNUSED(dev);
1125
1126     FNLOG();
1127
1128     parms = str_parms_create_str(keys);
1129
1130     str_parms_dump(parms);
1131
1132     str_parms_destroy(parms);
1133
1134     return strdup("");
1135 }
1136
1137 static int adev_init_check(const struct audio_hw_device *dev)
1138 {
1139     struct a2dp_audio_device *a2dp_dev = (struct a2dp_audio_device*)dev;
1140
1141     FNLOG();
1142
1143     return 0;
1144 }
1145
1146 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
1147 {
1148     UNUSED(dev);
1149     UNUSED(volume);
1150
1151     FNLOG();
1152
1153     return -ENOSYS;
1154 }
1155
1156 static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
1157 {
1158     UNUSED(dev);
1159     UNUSED(volume);
1160
1161     FNLOG();
1162
1163     return -ENOSYS;
1164 }
1165
1166 static int adev_set_mode(struct audio_hw_device *dev, int mode)
1167 {
1168     UNUSED(dev);
1169     UNUSED(mode);
1170
1171     FNLOG();
1172
1173     return 0;
1174 }
1175
1176 static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
1177 {
1178     UNUSED(dev);
1179     UNUSED(state);
1180
1181     FNLOG();
1182
1183     return -ENOSYS;
1184 }
1185
1186 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1187 {
1188     UNUSED(dev);
1189     UNUSED(state);
1190
1191     FNLOG();
1192
1193     return -ENOSYS;
1194 }
1195
1196 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
1197                                          const struct audio_config *config)
1198 {
1199     UNUSED(dev);
1200     UNUSED(config);
1201
1202     FNLOG();
1203
1204     return 320;
1205 }
1206
1207 static int adev_open_input_stream(struct audio_hw_device *dev,
1208                                   audio_io_handle_t handle,
1209                                   audio_devices_t devices,
1210                                   struct audio_config *config,
1211                                   struct audio_stream_in **stream_in)
1212 {
1213     struct a2dp_audio_device *a2dp_dev = (struct a2dp_audio_device *)dev;
1214     struct a2dp_stream_in *in;
1215     int ret;
1216     UNUSED(handle);
1217     UNUSED(devices);
1218     UNUSED(config);
1219
1220     FNLOG();
1221
1222     in = (struct a2dp_stream_in *)calloc(1, sizeof(struct a2dp_stream_in));
1223
1224     if (!in)
1225         return -ENOMEM;
1226
1227     in->stream.common.get_sample_rate = in_get_sample_rate;
1228     in->stream.common.set_sample_rate = in_set_sample_rate;
1229     in->stream.common.get_buffer_size = in_get_buffer_size;
1230     in->stream.common.get_channels = in_get_channels;
1231     in->stream.common.get_format = in_get_format;
1232     in->stream.common.set_format = in_set_format;
1233     in->stream.common.standby = in_standby;
1234     in->stream.common.dump = in_dump;
1235     in->stream.common.set_parameters = in_set_parameters;
1236     in->stream.common.get_parameters = in_get_parameters;
1237     in->stream.common.add_audio_effect = in_add_audio_effect;
1238     in->stream.common.remove_audio_effect = in_remove_audio_effect;
1239     in->stream.set_gain = in_set_gain;
1240     in->stream.read = in_read;
1241     in->stream.get_input_frames_lost = in_get_input_frames_lost;
1242
1243     /* initialize a2dp specifics */
1244     a2dp_stream_common_init(&in->common);
1245
1246     *stream_in = &in->stream;
1247     a2dp_dev->input = in;
1248
1249     a2dp_open_ctrl_path(&in->common);
1250     if (in->common.ctrl_fd == AUDIO_SKT_DISCONNECTED)
1251     {
1252         ERROR("ctrl socket failed to connect (%s)", strerror(errno));
1253         ret = -1;
1254         goto err_open;
1255     }
1256
1257     if (a2dp_read_audio_config(&in->common) < 0) {
1258         ERROR("a2dp_read_audio_config failed (%s)", strerror(errno));
1259         ret = -1;
1260         goto err_open;
1261     }
1262
1263     DEBUG("success");
1264     return 0;
1265
1266 err_open:
1267     free(in);
1268     *stream_in = NULL;
1269     a2dp_dev->input = NULL;
1270     ERROR("failed");
1271     return ret;
1272 }
1273
1274 static void adev_close_input_stream(struct audio_hw_device *dev,
1275                                    struct audio_stream_in *stream)
1276 {
1277     struct a2dp_audio_device *a2dp_dev = (struct a2dp_audio_device *)dev;
1278     struct a2dp_stream_in* in = (struct a2dp_stream_in *)stream;
1279     a2dp_state_t state = in->common.state;
1280
1281     INFO("closing input (state %d)", state);
1282
1283     if ((state == AUDIO_A2DP_STATE_STARTED) || (state == AUDIO_A2DP_STATE_STOPPING))
1284         stop_audio_datapath(&in->common);
1285
1286     skt_disconnect(in->common.ctrl_fd);
1287     free(stream);
1288     a2dp_dev->input = NULL;
1289
1290     DEBUG("done");
1291 }
1292
1293 static int adev_dump(const audio_hw_device_t *device, int fd)
1294 {
1295     UNUSED(device);
1296     UNUSED(fd);
1297
1298     FNLOG();
1299
1300     return 0;
1301 }
1302
1303 static int adev_close(hw_device_t *device)
1304 {
1305     FNLOG();
1306
1307     free(device);
1308     return 0;
1309 }
1310
1311 static int adev_open(const hw_module_t* module, const char* name,
1312                      hw_device_t** device)
1313 {
1314     struct a2dp_audio_device *adev;
1315     int ret;
1316
1317     INFO(" adev_open in A2dp_hw module");
1318     FNLOG();
1319
1320     if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1321     {
1322         ERROR("interface %s not matching [%s]", name, AUDIO_HARDWARE_INTERFACE);
1323         return -EINVAL;
1324     }
1325
1326     adev = calloc(1, sizeof(struct a2dp_audio_device));
1327
1328     if (!adev)
1329         return -ENOMEM;
1330
1331     adev->device.common.tag = HARDWARE_DEVICE_TAG;
1332     adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
1333     adev->device.common.module = (struct hw_module_t *) module;
1334     adev->device.common.close = adev_close;
1335
1336     adev->device.init_check = adev_init_check;
1337     adev->device.set_voice_volume = adev_set_voice_volume;
1338     adev->device.set_master_volume = adev_set_master_volume;
1339     adev->device.set_mode = adev_set_mode;
1340     adev->device.set_mic_mute = adev_set_mic_mute;
1341     adev->device.get_mic_mute = adev_get_mic_mute;
1342     adev->device.set_parameters = adev_set_parameters;
1343     adev->device.get_parameters = adev_get_parameters;
1344     adev->device.get_input_buffer_size = adev_get_input_buffer_size;
1345     adev->device.open_output_stream = adev_open_output_stream;
1346     adev->device.close_output_stream = adev_close_output_stream;
1347     adev->device.open_input_stream = adev_open_input_stream;
1348     adev->device.close_input_stream = adev_close_input_stream;
1349     adev->device.dump = adev_dump;
1350
1351     adev->output = NULL;
1352
1353
1354     *device = &adev->device.common;
1355
1356     return 0;
1357 }
1358
1359 static struct hw_module_methods_t hal_module_methods = {
1360     .open = adev_open,
1361 };
1362
1363 struct audio_module HAL_MODULE_INFO_SYM = {
1364     .common = {
1365         .tag = HARDWARE_MODULE_TAG,
1366         .version_major = 1,
1367         .version_minor = 0,
1368         .id = AUDIO_HARDWARE_MODULE_ID,
1369         .name = "A2DP Audio HW HAL",
1370         .author = "The Android Open Source Project",
1371         .methods = &hal_module_methods,
1372     },
1373 };
1374