1 /******************************************************************************
3 * Copyright (C) 2009-2012 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
19 /*****************************************************************************
21 * Filename: audio_a2dp_hw.c
23 * Description: Implements hal for bluedroid a2dp audio device
25 *****************************************************************************/
32 #include <sys/socket.h>
35 #include <sys/errno.h>
39 #include <cutils/str_parms.h>
40 #include <cutils/sockets.h>
42 #include <system/audio.h>
43 #include <hardware/audio.h>
45 #include <hardware/hardware.h>
46 #include "audio_a2dp_hw.h"
49 #define LOG_TAG "audio_a2dp_hw"
50 /* #define LOG_NDEBUG 0 */
53 /*****************************************************************************
55 ******************************************************************************/
57 #define CTRL_CHAN_RETRY_COUNT 3
58 #define USEC_PER_SEC 1000000L
60 #define CASE_RETURN_STR(const) case const: return #const;
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__)
67 #define ASSERTC(cond, msg, val) if (!(cond)) {ERROR("### ASSERT : %s line %d %s (%d) ###", __FILE__, __LINE__, msg, val);}
69 /*****************************************************************************
70 ** Local type definitions
71 ******************************************************************************/
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 */
82 struct a2dp_stream_in;
83 struct a2dp_stream_out;
85 struct a2dp_audio_device {
86 struct audio_hw_device device;
87 struct a2dp_stream_in *input;
88 struct a2dp_stream_out *output;
93 uint32_t channel_flags;
97 /* move ctrl_fd outside output stream and keep open until HAL unloaded ? */
99 struct a2dp_stream_common {
100 pthread_mutex_t lock;
104 struct a2dp_config cfg;
108 struct a2dp_stream_out {
109 struct audio_stream_out stream;
110 struct a2dp_stream_common common;
113 struct a2dp_stream_in {
114 struct audio_stream_in stream;
115 struct a2dp_stream_common common;
118 /*****************************************************************************
120 ******************************************************************************/
122 /*****************************************************************************
124 ******************************************************************************/
126 static size_t out_get_buffer_size(const struct audio_stream *stream);
128 /*****************************************************************************
130 ******************************************************************************/
132 /*****************************************************************************
134 ******************************************************************************/
136 /*****************************************************************************
137 ** Miscellaneous helper functions
138 ******************************************************************************/
140 static const char* dump_a2dp_ctrl_event(char event)
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)
150 return "UNKNOWN MSG ID";
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)
159 static struct timespec prev = {0,0};
160 unsigned long long now_us;
161 unsigned long long diff_us;
165 clock_gettime(CLOCK_MONOTONIC, &now);
167 now_us = now.tv_sec*USEC_PER_SEC + now.tv_nsec/1000;
171 diff_us = (now.tv_sec - prev.tv_sec) * USEC_PER_SEC + (now.tv_nsec - prev.tv_nsec)/1000;
173 DEBUG("[%s] ts %08lld, *diff %08lld, val %d", tag, now_us, diff_us, val);
177 diff_us = (now.tv_sec - prev.tv_sec) * USEC_PER_SEC + (now.tv_nsec - prev.tv_nsec)/1000;
179 DEBUG("[%s] ts %08lld, diff %08lld, val %d", tag, now_us, diff_us, val);
183 static int calc_audiotime(struct a2dp_config cfg, int bytes)
185 int chan_count = popcount(cfg.channel_flags);
187 ASSERTC(cfg.format == AUDIO_FORMAT_PCM_16_BIT,
188 "unsupported sample sz", cfg.format);
190 return bytes*(1000000/(chan_count*2))/cfg.rate;
193 /*****************************************************************************
195 ** bluedroid stack adaptation
197 *****************************************************************************/
199 static int skt_connect(char *path, size_t buffer_sz)
203 struct sockaddr_un remote;
206 INFO("connect to %s (sz %zu)", path, buffer_sz);
208 skt_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
210 if(socket_local_client_connect(skt_fd, path,
211 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM) < 0)
213 ERROR("failed to connect (%s)", strerror(errno));
219 ret = setsockopt(skt_fd, SOL_SOCKET, SO_SNDBUF, (char*)&len, (int)sizeof(len));
221 /* only issue warning if failed */
223 ERROR("setsockopt failed (%s)", strerror(errno));
225 INFO("connected to stack fd = %d", skt_fd);
230 static int skt_read(int fd, void *p, size_t len, int us_timeout)
241 ts.tv_sec = us_timeout / 1000000;
242 ts.tv_nsec = (us_timeout % 1000000) * 1000;
244 ts_log("skt_read ppoll", len, NULL);
247 if (ppoll(&pfd, 1, &ts, NULL) == 0) {
251 ts_log("skt_read recv", len, NULL);
253 if ((read = recv(fd, p, len, MSG_NOSIGNAL)) == -1)
255 ERROR("write failed with errno=%d\n", errno);
262 static int skt_write(int fd, const void *p, size_t len)
270 pfd.events = POLLOUT;
272 /* poll for 500 ms */
275 if (poll(&pfd, 1, 500) == 0)
278 ts_log("skt_write", len, NULL);
280 if ((sent = send(fd, p, len, MSG_NOSIGNAL)) == -1)
282 ERROR("write failed with errno=%d\n", errno);
289 static int skt_disconnect(int fd)
293 if (fd != AUDIO_SKT_DISCONNECTED)
295 shutdown(fd, SHUT_RDWR);
303 /*****************************************************************************
305 ** AUDIO CONTROL PATH
307 *****************************************************************************/
309 static int a2dp_ctrl_receive(struct a2dp_stream_common *common, void* buffer, int length)
311 int ret = recv(common->ctrl_fd, buffer, length, MSG_NOSIGNAL);
314 ERROR("ack failed (%s)", strerror(errno));
318 ret = recv(common->ctrl_fd, buffer, length, MSG_NOSIGNAL);
321 ERROR("ack failed (%s)", strerror(errno));
322 skt_disconnect(common->ctrl_fd);
323 common->ctrl_fd = AUDIO_SKT_DISCONNECTED;
329 skt_disconnect(common->ctrl_fd);
330 common->ctrl_fd = AUDIO_SKT_DISCONNECTED;
338 static int a2dp_command(struct a2dp_stream_common *common, char cmd)
342 DEBUG("A2DP COMMAND %s", dump_a2dp_ctrl_event(cmd));
345 if (send(common->ctrl_fd, &cmd, 1, MSG_NOSIGNAL) == -1)
347 ERROR("cmd failed (%s)", strerror(errno));
348 skt_disconnect(common->ctrl_fd);
349 common->ctrl_fd = AUDIO_SKT_DISCONNECTED;
353 /* wait for ack byte */
354 if (a2dp_ctrl_receive(common, &ack, 1) < 0)
357 DEBUG("A2DP COMMAND %s DONE STATUS %d", dump_a2dp_ctrl_event(cmd), ack);
359 if (ack != A2DP_CTRL_ACK_SUCCESS)
365 static int check_a2dp_ready(struct a2dp_stream_common *common)
367 if (a2dp_command(common, A2DP_CTRL_CMD_CHECK_READY) < 0)
369 ERROR("check a2dp ready failed");
375 static int a2dp_read_audio_config(struct a2dp_stream_common *common)
377 char cmd = A2DP_CTRL_GET_AUDIO_CONFIG;
378 uint32_t sample_rate;
379 uint8_t channel_count;
381 if (a2dp_command(common, A2DP_CTRL_GET_AUDIO_CONFIG) < 0)
383 ERROR("check a2dp ready failed");
387 if (a2dp_ctrl_receive(common, &sample_rate, 4) < 0)
389 if (a2dp_ctrl_receive(common, &channel_count, 1) < 0)
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;
396 INFO("got config %d %d", common->cfg.format, common->cfg.rate);
401 static void a2dp_open_ctrl_path(struct a2dp_stream_common *common)
405 /* retry logic to catch any timing variations on control channel */
406 for (i = 0; i < CTRL_CHAN_RETRY_COUNT; i++)
408 /* connect control channel if not already connected */
409 if ((common->ctrl_fd = skt_connect(A2DP_CTRL_PATH, common->buffer_sz)) > 0)
411 /* success, now check if stack is ready */
412 if (check_a2dp_ready(common) == 0)
415 ERROR("error : a2dp not ready, wait 250 ms and retry");
417 skt_disconnect(common->ctrl_fd);
418 common->ctrl_fd = AUDIO_SKT_DISCONNECTED;
421 /* ctrl channel not ready, wait a bit */
426 /*****************************************************************************
430 *****************************************************************************/
432 static void a2dp_stream_common_init(struct a2dp_stream_common *common)
434 pthread_mutexattr_t lock_attr;
438 pthread_mutexattr_init(&lock_attr);
439 pthread_mutexattr_settype(&lock_attr, PTHREAD_MUTEX_RECURSIVE);
440 pthread_mutex_init(&common->lock, &lock_attr);
442 common->ctrl_fd = AUDIO_SKT_DISCONNECTED;
443 common->audio_fd = AUDIO_SKT_DISCONNECTED;
444 common->state = AUDIO_A2DP_STATE_STOPPED;
446 /* manages max capacity of socket pipe */
447 common->buffer_sz = AUDIO_STREAM_OUTPUT_BUFFER_SZ;
450 static int start_audio_datapath(struct a2dp_stream_common *common)
452 int oldstate = common->state;
454 INFO("state %d", common->state);
456 if (common->ctrl_fd == AUDIO_SKT_DISCONNECTED) {
457 INFO("AUDIO_SKT_DISCONNECTED");
461 common->state = AUDIO_A2DP_STATE_STARTING;
463 if (a2dp_command(common, A2DP_CTRL_CMD_START) < 0)
465 ERROR("audiopath start failed");
467 common->state = oldstate;
471 /* connect socket if not yet connected */
472 if (common->audio_fd == AUDIO_SKT_DISCONNECTED)
474 common->audio_fd = skt_connect(A2DP_DATA_PATH, common->buffer_sz);
475 if (common->audio_fd < 0)
477 common->state = oldstate;
481 common->state = AUDIO_A2DP_STATE_STARTED;
488 static int stop_audio_datapath(struct a2dp_stream_common *common)
490 int oldstate = common->state;
492 INFO("state %d", common->state);
494 if (common->ctrl_fd == AUDIO_SKT_DISCONNECTED)
497 /* prevent any stray output writes from autostarting the stream
498 while stopping audiopath */
499 common->state = AUDIO_A2DP_STATE_STOPPING;
501 if (a2dp_command(common, A2DP_CTRL_CMD_STOP) < 0)
503 ERROR("audiopath stop failed");
504 common->state = oldstate;
508 common->state = AUDIO_A2DP_STATE_STOPPED;
510 /* disconnect audio path */
511 skt_disconnect(common->audio_fd);
512 common->audio_fd = AUDIO_SKT_DISCONNECTED;
517 static int suspend_audio_datapath(struct a2dp_stream_common *common, bool standby)
519 INFO("state %d", common->state);
521 if (common->ctrl_fd == AUDIO_SKT_DISCONNECTED)
524 if (common->state == AUDIO_A2DP_STATE_STOPPING)
527 if (a2dp_command(common, A2DP_CTRL_CMD_SUSPEND) < 0)
531 common->state = AUDIO_A2DP_STATE_STANDBY;
533 common->state = AUDIO_A2DP_STATE_SUSPENDED;
535 /* disconnect audio path */
536 skt_disconnect(common->audio_fd);
538 common->audio_fd = AUDIO_SKT_DISCONNECTED;
544 /*****************************************************************************
546 ** audio output callbacks
548 *****************************************************************************/
550 static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
553 struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
556 DEBUG("write %zu bytes (fd %d)", bytes, out->common.audio_fd);
558 if (out->common.state == AUDIO_A2DP_STATE_SUSPENDED)
560 DEBUG("stream suspended");
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))
568 pthread_mutex_lock(&out->common.lock);
570 if (start_audio_datapath(&out->common) < 0)
572 /* emulate time this write represents to avoid very fast write
573 failures during transition periods or remote suspend */
575 int us_delay = calc_audiotime(out->common.cfg, bytes);
577 DEBUG("emulate a2dp write delay (%d us)", us_delay);
580 pthread_mutex_unlock(&out->common.lock);
584 pthread_mutex_unlock(&out->common.lock);
586 else if (out->common.state != AUDIO_A2DP_STATE_STARTED)
588 ERROR("stream not in stopped or standby");
592 sent = skt_write(out->common.audio_fd, buffer, bytes);
596 skt_disconnect(out->common.audio_fd);
597 out->common.audio_fd = AUDIO_SKT_DISCONNECTED;
598 out->common.state = AUDIO_A2DP_STATE_STOPPED;
601 DEBUG("wrote %d bytes out of %zu bytes", sent, bytes);
606 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
608 struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
610 DEBUG("rate %" PRIu32,out->common.cfg.rate);
612 return out->common.cfg.rate;
615 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
617 struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
619 DEBUG("out_set_sample_rate : %" PRIu32, rate);
621 if (rate != AUDIO_STREAM_DEFAULT_RATE)
623 ERROR("only rate %d supported", AUDIO_STREAM_DEFAULT_RATE);
627 out->common.cfg.rate = rate;
632 static size_t out_get_buffer_size(const struct audio_stream *stream)
634 struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
636 DEBUG("buffer_size : %zu", out->common.buffer_sz);
638 return out->common.buffer_sz;
641 static uint32_t out_get_channels(const struct audio_stream *stream)
643 struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
645 DEBUG("channels 0x%" PRIx32, out->common.cfg.channel_flags);
647 return out->common.cfg.channel_flags;
650 static audio_format_t out_get_format(const struct audio_stream *stream)
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;
657 static int out_set_format(struct audio_stream *stream, audio_format_t format)
660 struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
661 DEBUG("setting format not yet supported (0x%x)", format);
665 static int out_standby(struct audio_stream *stream)
667 struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
672 pthread_mutex_lock(&out->common.lock);
674 if (out->common.state == AUDIO_A2DP_STATE_STARTED)
675 retVal = suspend_audio_datapath(&out->common, true);
678 pthread_mutex_unlock(&out->common.lock);
683 static int out_dump(const struct audio_stream *stream, int fd)
686 struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
691 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
693 struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
694 struct str_parms *parms;
699 INFO("state %d", out->common.state);
701 pthread_mutex_lock(&out->common.lock);
703 parms = str_parms_create_str(kvpairs);
706 str_parms_dump(parms);
708 retval = str_parms_get_str(parms, "closing", keyval, sizeof(keyval));
712 if (strcmp(keyval, "true") == 0)
714 DEBUG("stream closing, disallow any writes");
715 out->common.state = AUDIO_A2DP_STATE_STOPPING;
719 retval = str_parms_get_str(parms, "A2dpSuspended", keyval, sizeof(keyval));
723 if (strcmp(keyval, "true") == 0)
725 if (out->common.state == AUDIO_A2DP_STATE_STARTED)
726 status = suspend_audio_datapath(&out->common, false);
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 */
739 pthread_mutex_unlock(&out->common.lock);
740 str_parms_destroy(parms);
745 static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
748 struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
752 /* add populating param here */
757 static uint32_t out_get_latency(const struct audio_stream_out *stream)
761 struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
765 latency_us = ((out->common.buffer_sz * 1000 ) /
766 audio_stream_frame_size(&out->stream.common) /
767 out->common.cfg.rate) * 1000;
770 return (latency_us / 1000) + 200;
773 static int out_set_volume(struct audio_stream_out *stream, float left,
782 /* volume controlled in audioflinger mixer (digital) */
789 static int out_get_render_position(const struct audio_stream_out *stream,
790 uint32_t *dsp_frames)
799 static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
808 static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
821 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
823 struct a2dp_stream_in *in = (struct a2dp_stream_in *)stream;
826 return in->common.cfg.rate;
829 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
831 struct a2dp_stream_in *in = (struct a2dp_stream_in *)stream;
835 if (in->common.cfg.rate > 0 && in->common.cfg.rate == rate)
841 static size_t in_get_buffer_size(const struct audio_stream *stream)
849 static uint32_t in_get_channels(const struct audio_stream *stream)
851 struct a2dp_stream_in *in = (struct a2dp_stream_in *)stream;
854 return in->common.cfg.channel_flags;
857 static audio_format_t in_get_format(const struct audio_stream *stream)
862 return AUDIO_FORMAT_PCM_16_BIT;
865 static int in_set_format(struct audio_stream *stream, audio_format_t format)
871 if (format == AUDIO_FORMAT_PCM_16_BIT)
877 static int in_standby(struct audio_stream *stream)
885 static int in_dump(const struct audio_stream *stream, int fd)
894 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
903 static char * in_get_parameters(const struct audio_stream *stream,
913 static int in_set_gain(struct audio_stream_in *stream, float gain)
922 static ssize_t in_read(struct audio_stream_in *stream, void* buffer,
925 struct a2dp_stream_in *in = (struct a2dp_stream_in *)stream;
928 DEBUG("read %zu bytes, state: %d", bytes, in->common.state);
930 if (in->common.state == AUDIO_A2DP_STATE_SUSPENDED)
932 DEBUG("stream suspended");
936 int us_delay = calc_audiotime(in->common.cfg, bytes);
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))
942 pthread_mutex_lock(&in->common.lock);
944 if (start_audio_datapath(&in->common) < 0)
946 /* emulate time this write represents to avoid very fast write
947 failures during transition periods or remote suspend */
949 DEBUG("emulate a2dp read delay (%d us)", us_delay);
952 pthread_mutex_unlock(&in->common.lock);
956 pthread_mutex_unlock(&in->common.lock);
958 else if (in->common.state != AUDIO_A2DP_STATE_STARTED)
960 ERROR("stream not in stopped or standby");
964 read = skt_read(in->common.audio_fd, buffer, bytes, us_delay);
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);
977 DEBUG("read %d bytes out of %zu bytes", read, bytes);
981 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
989 static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
998 static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
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)
1016 struct a2dp_audio_device *a2dp_dev = (struct a2dp_audio_device *)dev;
1017 struct a2dp_stream_out *out;
1024 INFO("opening output");
1026 out = (struct a2dp_stream_out *)calloc(1, sizeof(struct a2dp_stream_out));
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;
1048 /* initialize a2dp specifics */
1049 a2dp_stream_common_init(&out->common);
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;
1055 /* set output config values */
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);
1062 *stream_out = &out->stream;
1063 a2dp_dev->output = out;
1065 a2dp_open_ctrl_path(&out->common);
1066 if (out->common.ctrl_fd == AUDIO_SKT_DISCONNECTED)
1068 ERROR("ctrl socket failed to connect (%s)", strerror(errno));
1079 a2dp_dev->output = NULL;
1084 static void adev_close_output_stream(struct audio_hw_device *dev,
1085 struct audio_stream_out *stream)
1087 struct a2dp_audio_device *a2dp_dev = (struct a2dp_audio_device *)dev;
1088 struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
1090 INFO("closing output (state %d)", out->common.state);
1092 if ((out->common.state == AUDIO_A2DP_STATE_STARTED) || (out->common.state == AUDIO_A2DP_STATE_STOPPING))
1093 stop_audio_datapath(&out->common);
1095 skt_disconnect(out->common.ctrl_fd);
1097 a2dp_dev->output = NULL;
1102 static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
1104 struct a2dp_audio_device *a2dp_dev = (struct a2dp_audio_device *)dev;
1105 struct a2dp_stream_out *out = a2dp_dev->output;
1110 ERROR("ERROR: set param called even when stream out is null");
1113 INFO("state %d", out->common.state);
1115 retval = out->stream.common.set_parameters((struct audio_stream *)out, kvpairs);
1120 static char * adev_get_parameters(const struct audio_hw_device *dev,
1123 struct str_parms *parms;
1128 parms = str_parms_create_str(keys);
1130 str_parms_dump(parms);
1132 str_parms_destroy(parms);
1137 static int adev_init_check(const struct audio_hw_device *dev)
1139 struct a2dp_audio_device *a2dp_dev = (struct a2dp_audio_device*)dev;
1146 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
1156 static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
1166 static int adev_set_mode(struct audio_hw_device *dev, int mode)
1176 static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
1186 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1196 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
1197 const struct audio_config *config)
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)
1213 struct a2dp_audio_device *a2dp_dev = (struct a2dp_audio_device *)dev;
1214 struct a2dp_stream_in *in;
1222 in = (struct a2dp_stream_in *)calloc(1, sizeof(struct a2dp_stream_in));
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;
1243 /* initialize a2dp specifics */
1244 a2dp_stream_common_init(&in->common);
1246 *stream_in = &in->stream;
1247 a2dp_dev->input = in;
1249 a2dp_open_ctrl_path(&in->common);
1250 if (in->common.ctrl_fd == AUDIO_SKT_DISCONNECTED)
1252 ERROR("ctrl socket failed to connect (%s)", strerror(errno));
1257 if (a2dp_read_audio_config(&in->common) < 0) {
1258 ERROR("a2dp_read_audio_config failed (%s)", strerror(errno));
1269 a2dp_dev->input = NULL;
1274 static void adev_close_input_stream(struct audio_hw_device *dev,
1275 struct audio_stream_in *stream)
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;
1281 INFO("closing input (state %d)", state);
1283 if ((state == AUDIO_A2DP_STATE_STARTED) || (state == AUDIO_A2DP_STATE_STOPPING))
1284 stop_audio_datapath(&in->common);
1286 skt_disconnect(in->common.ctrl_fd);
1288 a2dp_dev->input = NULL;
1293 static int adev_dump(const audio_hw_device_t *device, int fd)
1303 static int adev_close(hw_device_t *device)
1311 static int adev_open(const hw_module_t* module, const char* name,
1312 hw_device_t** device)
1314 struct a2dp_audio_device *adev;
1317 INFO(" adev_open in A2dp_hw module");
1320 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1322 ERROR("interface %s not matching [%s]", name, AUDIO_HARDWARE_INTERFACE);
1326 adev = calloc(1, sizeof(struct a2dp_audio_device));
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;
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;
1351 adev->output = NULL;
1354 *device = &adev->device.common;
1359 static struct hw_module_methods_t hal_module_methods = {
1363 struct audio_module HAL_MODULE_INFO_SYM = {
1365 .tag = HARDWARE_MODULE_TAG,
1368 .id = AUDIO_HARDWARE_MODULE_ID,
1369 .name = "A2DP Audio HW HAL",
1370 .author = "The Android Open Source Project",
1371 .methods = &hal_module_methods,