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 *****************************************************************************/
27 #define LOG_TAG "bt_a2dp_hw"
34 #include <sys/errno.h>
36 #include <sys/socket.h>
42 #include <hardware/audio.h>
43 #include <hardware/hardware.h>
44 #include <system/audio.h>
46 #include "audio_a2dp_hw.h"
48 #include "osi/include/hash_map.h"
49 #include "osi/include/hash_map_utils.h"
50 #include "osi/include/log.h"
51 #include "osi/include/socket_utils/sockets.h"
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() LOG_VERBOSE(LOG_TAG, "%s", __FUNCTION__);
63 #define DEBUG(fmt, ...) LOG_VERBOSE(LOG_TAG, "%s: " fmt,__FUNCTION__, ## __VA_ARGS__)
64 #define INFO(fmt, ...) LOG_INFO(LOG_TAG, "%s: " fmt,__FUNCTION__, ## __VA_ARGS__)
65 #define ERROR(fmt, ...) LOG_ERROR(LOG_TAG, "%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;
111 uint64_t frames_presented; // frames written, never reset
112 uint64_t frames_rendered; // frames written, reset on standby
115 struct a2dp_stream_in {
116 struct audio_stream_in stream;
117 struct a2dp_stream_common common;
120 /*****************************************************************************
122 ******************************************************************************/
124 /*****************************************************************************
126 ******************************************************************************/
128 static size_t out_get_buffer_size(const struct audio_stream *stream);
130 /*****************************************************************************
132 ******************************************************************************/
134 /*****************************************************************************
136 ******************************************************************************/
137 /* Function used only in debug mode */
138 static const char* dump_a2dp_ctrl_event(char event) __attribute__ ((unused));
140 /*****************************************************************************
141 ** Miscellaneous helper functions
142 ******************************************************************************/
144 static const char* dump_a2dp_ctrl_event(char event)
148 CASE_RETURN_STR(A2DP_CTRL_CMD_NONE)
149 CASE_RETURN_STR(A2DP_CTRL_CMD_CHECK_READY)
150 CASE_RETURN_STR(A2DP_CTRL_CMD_START)
151 CASE_RETURN_STR(A2DP_CTRL_CMD_STOP)
152 CASE_RETURN_STR(A2DP_CTRL_CMD_SUSPEND)
154 return "UNKNOWN MSG ID";
158 /* logs timestamp with microsec precision
159 pprev is optional in case a dedicated diff is required */
160 static void ts_log(char *tag, int val, struct timespec *pprev_opt)
163 static struct timespec prev = {0,0};
164 unsigned long long now_us;
165 unsigned long long diff_us;
169 clock_gettime(CLOCK_MONOTONIC, &now);
171 now_us = now.tv_sec*USEC_PER_SEC + now.tv_nsec/1000;
175 diff_us = (now.tv_sec - prev.tv_sec) * USEC_PER_SEC + (now.tv_nsec - prev.tv_nsec)/1000;
177 DEBUG("[%s] ts %08lld, *diff %08lld, val %d", tag, now_us, diff_us, val);
181 diff_us = (now.tv_sec - prev.tv_sec) * USEC_PER_SEC + (now.tv_nsec - prev.tv_nsec)/1000;
183 DEBUG("[%s] ts %08lld, diff %08lld, val %d", tag, now_us, diff_us, val);
187 static int calc_audiotime(struct a2dp_config cfg, int bytes)
189 int chan_count = popcount(cfg.channel_flags);
191 ASSERTC(cfg.format == AUDIO_FORMAT_PCM_16_BIT,
192 "unsupported sample sz", cfg.format);
194 return (int)(((int64_t)bytes * (1000000 / (chan_count * 2))) / cfg.rate);
197 /*****************************************************************************
199 ** bluedroid stack adaptation
201 *****************************************************************************/
203 static int skt_connect(char *path, size_t buffer_sz)
209 INFO("connect to %s (sz %zu)", path, buffer_sz);
211 skt_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
213 if(osi_socket_local_client_connect(skt_fd, path,
214 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM) < 0)
216 ERROR("failed to connect (%s)", strerror(errno));
222 ret = setsockopt(skt_fd, SOL_SOCKET, SO_SNDBUF, (char*)&len, (int)sizeof(len));
224 /* only issue warning if failed */
226 ERROR("setsockopt failed (%s)", strerror(errno));
228 ret = setsockopt(skt_fd, SOL_SOCKET, SO_RCVBUF, (char*)&len, (int)sizeof(len));
230 /* only issue warning if failed */
232 ERROR("setsockopt failed (%s)", strerror(errno));
234 INFO("connected to stack fd = %d", skt_fd);
239 static int skt_read(int fd, void *p, size_t len)
245 ts_log("skt_read recv", len, NULL);
247 if ((read = recv(fd, p, len, MSG_NOSIGNAL)) == -1)
249 ERROR("write failed with errno=%d\n", errno);
256 static int skt_write(int fd, const void *p, size_t len)
264 pfd.events = POLLOUT;
266 /* poll for 500 ms */
269 if (poll(&pfd, 1, 500) == 0)
272 ts_log("skt_write", len, NULL);
274 if ((sent = send(fd, p, len, MSG_NOSIGNAL)) == -1)
276 ERROR("write failed with errno=%d\n", errno);
283 static int skt_disconnect(int fd)
287 if (fd != AUDIO_SKT_DISCONNECTED)
289 shutdown(fd, SHUT_RDWR);
297 /*****************************************************************************
299 ** AUDIO CONTROL PATH
301 *****************************************************************************/
303 static int a2dp_ctrl_receive(struct a2dp_stream_common *common, void* buffer, int length)
305 int ret = recv(common->ctrl_fd, buffer, length, MSG_NOSIGNAL);
308 ERROR("ack failed (%s)", strerror(errno));
312 ret = recv(common->ctrl_fd, buffer, length, MSG_NOSIGNAL);
315 ERROR("ack failed (%s)", strerror(errno));
316 skt_disconnect(common->ctrl_fd);
317 common->ctrl_fd = AUDIO_SKT_DISCONNECTED;
323 skt_disconnect(common->ctrl_fd);
324 common->ctrl_fd = AUDIO_SKT_DISCONNECTED;
332 static int a2dp_command(struct a2dp_stream_common *common, char cmd)
336 DEBUG("A2DP COMMAND %s", dump_a2dp_ctrl_event(cmd));
339 if (send(common->ctrl_fd, &cmd, 1, MSG_NOSIGNAL) == -1)
341 ERROR("cmd failed (%s)", strerror(errno));
342 skt_disconnect(common->ctrl_fd);
343 common->ctrl_fd = AUDIO_SKT_DISCONNECTED;
347 /* wait for ack byte */
348 if (a2dp_ctrl_receive(common, &ack, 1) < 0)
351 DEBUG("A2DP COMMAND %s DONE STATUS %d", dump_a2dp_ctrl_event(cmd), ack);
353 if (ack == A2DP_CTRL_ACK_INCALL_FAILURE)
355 if (ack != A2DP_CTRL_ACK_SUCCESS)
361 static int check_a2dp_ready(struct a2dp_stream_common *common)
363 if (a2dp_command(common, A2DP_CTRL_CMD_CHECK_READY) < 0)
365 ERROR("check a2dp ready failed");
371 static int a2dp_read_audio_config(struct a2dp_stream_common *common)
373 uint32_t sample_rate;
374 uint8_t channel_count;
376 if (a2dp_command(common, A2DP_CTRL_GET_AUDIO_CONFIG) < 0)
378 ERROR("check a2dp ready failed");
382 if (a2dp_ctrl_receive(common, &sample_rate, 4) < 0)
384 if (a2dp_ctrl_receive(common, &channel_count, 1) < 0)
387 common->cfg.channel_flags = (channel_count == 1 ? AUDIO_CHANNEL_IN_MONO : AUDIO_CHANNEL_IN_STEREO);
388 common->cfg.format = AUDIO_STREAM_DEFAULT_FORMAT;
389 common->cfg.rate = sample_rate;
391 INFO("got config %d %d", common->cfg.format, common->cfg.rate);
396 static void a2dp_open_ctrl_path(struct a2dp_stream_common *common)
400 /* retry logic to catch any timing variations on control channel */
401 for (i = 0; i < CTRL_CHAN_RETRY_COUNT; i++)
403 /* connect control channel if not already connected */
404 if ((common->ctrl_fd = skt_connect(A2DP_CTRL_PATH, common->buffer_sz)) > 0)
406 /* success, now check if stack is ready */
407 if (check_a2dp_ready(common) == 0)
410 ERROR("error : a2dp not ready, wait 250 ms and retry");
412 skt_disconnect(common->ctrl_fd);
413 common->ctrl_fd = AUDIO_SKT_DISCONNECTED;
416 /* ctrl channel not ready, wait a bit */
421 /*****************************************************************************
425 *****************************************************************************/
427 static void a2dp_stream_common_init(struct a2dp_stream_common *common)
429 pthread_mutexattr_t lock_attr;
433 pthread_mutexattr_init(&lock_attr);
434 pthread_mutexattr_settype(&lock_attr, PTHREAD_MUTEX_RECURSIVE);
435 pthread_mutex_init(&common->lock, &lock_attr);
437 common->ctrl_fd = AUDIO_SKT_DISCONNECTED;
438 common->audio_fd = AUDIO_SKT_DISCONNECTED;
439 common->state = AUDIO_A2DP_STATE_STOPPED;
441 /* manages max capacity of socket pipe */
442 common->buffer_sz = AUDIO_STREAM_OUTPUT_BUFFER_SZ;
445 static int start_audio_datapath(struct a2dp_stream_common *common)
447 INFO("state %d", common->state);
449 if (common->ctrl_fd == AUDIO_SKT_DISCONNECTED) {
450 INFO("%s AUDIO_SKT_DISCONNECTED", __func__);
454 int oldstate = common->state;
455 common->state = AUDIO_A2DP_STATE_STARTING;
457 int a2dp_status = a2dp_command(common, A2DP_CTRL_CMD_START);
460 ERROR("%s Audiopath start failed (status %d)", __func__, a2dp_status);
462 common->state = oldstate;
465 else if (a2dp_status == A2DP_CTRL_ACK_INCALL_FAILURE)
467 ERROR("%s Audiopath start failed - in call, move to suspended", __func__);
468 common->state = oldstate;
472 /* connect socket if not yet connected */
473 if (common->audio_fd == AUDIO_SKT_DISCONNECTED)
475 common->audio_fd = skt_connect(A2DP_DATA_PATH, common->buffer_sz);
476 if (common->audio_fd < 0)
478 common->state = oldstate;
482 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;
557 DEBUG("write %zu bytes (fd %d)", bytes, out->common.audio_fd);
559 pthread_mutex_lock(&out->common.lock);
561 if (out->common.state == AUDIO_A2DP_STATE_SUSPENDED)
563 DEBUG("stream suspended");
564 pthread_mutex_unlock(&out->common.lock);
568 /* only allow autostarting if we are in stopped or standby */
569 if ((out->common.state == AUDIO_A2DP_STATE_STOPPED) ||
570 (out->common.state == AUDIO_A2DP_STATE_STANDBY))
572 if (start_audio_datapath(&out->common) < 0)
574 pthread_mutex_unlock(&out->common.lock);
578 else if (out->common.state != AUDIO_A2DP_STATE_STARTED)
580 ERROR("stream not in stopped or standby");
581 pthread_mutex_unlock(&out->common.lock);
585 pthread_mutex_unlock(&out->common.lock);
586 sent = skt_write(out->common.audio_fd, buffer, bytes);
589 skt_disconnect(out->common.audio_fd);
590 out->common.audio_fd = AUDIO_SKT_DISCONNECTED;
591 if (out->common.state != AUDIO_A2DP_STATE_SUSPENDED)
592 out->common.state = AUDIO_A2DP_STATE_STOPPED;
594 ERROR("write failed : stream suspended, avoid resetting state");
598 const size_t frames = bytes / audio_stream_out_frame_size(stream);
599 out->frames_rendered += frames;
600 out->frames_presented += frames;
604 us_delay = calc_audiotime(out->common.cfg, bytes);
606 DEBUG("emulate a2dp write delay (%d us)", us_delay);
613 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
615 struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
617 DEBUG("rate %" PRIu32,out->common.cfg.rate);
619 return out->common.cfg.rate;
622 static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
624 struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
626 DEBUG("out_set_sample_rate : %" PRIu32, rate);
628 if (rate != AUDIO_STREAM_DEFAULT_RATE)
630 ERROR("only rate %d supported", AUDIO_STREAM_DEFAULT_RATE);
634 out->common.cfg.rate = rate;
639 static size_t out_get_buffer_size(const struct audio_stream *stream)
641 struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
643 DEBUG("buffer_size : %zu", out->common.buffer_sz);
645 return out->common.buffer_sz;
648 static uint32_t out_get_channels(const struct audio_stream *stream)
650 struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
652 DEBUG("channels 0x%" PRIx32, out->common.cfg.channel_flags);
654 return out->common.cfg.channel_flags;
657 static audio_format_t out_get_format(const struct audio_stream *stream)
659 struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
660 DEBUG("format 0x%x", out->common.cfg.format);
661 return out->common.cfg.format;
664 static int out_set_format(struct audio_stream *stream, audio_format_t format)
668 DEBUG("setting format not yet supported (0x%x)", format);
672 static int out_standby(struct audio_stream *stream)
674 struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
679 pthread_mutex_lock(&out->common.lock);
680 // Do nothing in SUSPENDED state.
681 if (out->common.state != AUDIO_A2DP_STATE_SUSPENDED)
682 retVal = suspend_audio_datapath(&out->common, true);
683 out->frames_rendered = 0; // rendered is reset, presented is not
684 pthread_mutex_unlock (&out->common.lock);
689 static int out_dump(const struct audio_stream *stream, int fd)
697 static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
699 struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
701 INFO("state %d", out->common.state);
703 hash_map_t *params = hash_map_utils_new_from_string_params(kvpairs);
709 pthread_mutex_lock(&out->common.lock);
712 hash_map_utils_dump_string_keys_string_values(params);
714 char *keyval = (char *)hash_map_get(params, "closing");
716 if (keyval && strcmp(keyval, "true") == 0)
718 DEBUG("stream closing, disallow any writes");
719 out->common.state = AUDIO_A2DP_STATE_STOPPING;
722 keyval = (char *)hash_map_get(params, "A2dpSuspended");
724 if (keyval && strcmp(keyval, "true") == 0)
726 if (out->common.state == AUDIO_A2DP_STATE_STARTED)
727 status = suspend_audio_datapath(&out->common, false);
731 /* Do not start the streaming automatically. If the phone was streaming
732 * prior to being suspended, the next out_write shall trigger the
733 * AVDTP start procedure */
734 if (out->common.state == AUDIO_A2DP_STATE_SUSPENDED)
735 out->common.state = AUDIO_A2DP_STATE_STANDBY;
736 /* Irrespective of the state, return 0 */
739 pthread_mutex_unlock(&out->common.lock);
740 hash_map_free(params);
745 static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
751 /* add populating param here */
756 static uint32_t out_get_latency(const struct audio_stream_out *stream)
760 struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
764 latency_us = ((out->common.buffer_sz * 1000 ) /
765 audio_stream_out_frame_size(&out->stream) /
766 out->common.cfg.rate) * 1000;
769 return (latency_us / 1000) + 200;
772 static int out_set_volume(struct audio_stream_out *stream, float left,
781 /* volume controlled in audioflinger mixer (digital) */
786 static int out_get_presentation_position(const struct audio_stream_out *stream,
787 uint64_t *frames, struct timespec *timestamp)
789 struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
792 if (stream == NULL || frames == NULL || timestamp == NULL)
795 int ret = -EWOULDBLOCK;
796 pthread_mutex_lock(&out->common.lock);
797 uint64_t latency_frames = (uint64_t)out_get_latency(stream) * out->common.cfg.rate / 1000;
798 if (out->frames_presented >= latency_frames) {
799 *frames = out->frames_presented - latency_frames;
800 clock_gettime(CLOCK_MONOTONIC, timestamp); // could also be associated with out_write().
803 pthread_mutex_unlock(&out->common.lock);
807 static int out_get_render_position(const struct audio_stream_out *stream,
808 uint32_t *dsp_frames)
810 struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
813 if (stream == NULL || dsp_frames == NULL)
816 pthread_mutex_lock(&out->common.lock);
817 uint64_t latency_frames = (uint64_t)out_get_latency(stream) * out->common.cfg.rate / 1000;
818 if (out->frames_rendered >= latency_frames) {
819 *dsp_frames = (uint32_t)(out->frames_rendered - latency_frames);
823 pthread_mutex_unlock(&out->common.lock);
827 static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
836 static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
849 static uint32_t in_get_sample_rate(const struct audio_stream *stream)
851 struct a2dp_stream_in *in = (struct a2dp_stream_in *)stream;
854 return in->common.cfg.rate;
857 static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
859 struct a2dp_stream_in *in = (struct a2dp_stream_in *)stream;
863 if (in->common.cfg.rate > 0 && in->common.cfg.rate == rate)
869 static size_t in_get_buffer_size(const struct audio_stream *stream)
877 static uint32_t in_get_channels(const struct audio_stream *stream)
879 struct a2dp_stream_in *in = (struct a2dp_stream_in *)stream;
882 return in->common.cfg.channel_flags;
885 static audio_format_t in_get_format(const struct audio_stream *stream)
890 return AUDIO_FORMAT_PCM_16_BIT;
893 static int in_set_format(struct audio_stream *stream, audio_format_t format)
899 if (format == AUDIO_FORMAT_PCM_16_BIT)
905 static int in_standby(struct audio_stream *stream)
913 static int in_dump(const struct audio_stream *stream, int fd)
922 static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
931 static char * in_get_parameters(const struct audio_stream *stream,
941 static int in_set_gain(struct audio_stream_in *stream, float gain)
950 static ssize_t in_read(struct audio_stream_in *stream, void* buffer,
953 struct a2dp_stream_in *in = (struct a2dp_stream_in *)stream;
956 DEBUG("read %zu bytes, state: %d", bytes, in->common.state);
958 if (in->common.state == AUDIO_A2DP_STATE_SUSPENDED)
960 DEBUG("stream suspended");
964 /* only allow autostarting if we are in stopped or standby */
965 if ((in->common.state == AUDIO_A2DP_STATE_STOPPED) ||
966 (in->common.state == AUDIO_A2DP_STATE_STANDBY))
968 pthread_mutex_lock(&in->common.lock);
970 if (start_audio_datapath(&in->common) < 0)
972 /* emulate time this write represents to avoid very fast write
973 failures during transition periods or remote suspend */
975 int us_delay = calc_audiotime(in->common.cfg, bytes);
977 DEBUG("emulate a2dp read delay (%d us)", us_delay);
980 pthread_mutex_unlock(&in->common.lock);
984 pthread_mutex_unlock(&in->common.lock);
986 else if (in->common.state != AUDIO_A2DP_STATE_STARTED)
988 ERROR("stream not in stopped or standby");
992 read = skt_read(in->common.audio_fd, buffer, bytes);
996 skt_disconnect(in->common.audio_fd);
997 in->common.audio_fd = AUDIO_SKT_DISCONNECTED;
998 in->common.state = AUDIO_A2DP_STATE_STOPPED;
999 } else if (read == 0) {
1000 DEBUG("read time out - return zeros");
1001 memset(buffer, 0, bytes);
1005 DEBUG("read %d bytes out of %zu bytes", read, bytes);
1009 static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1017 static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1026 static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1036 static int adev_open_output_stream(struct audio_hw_device *dev,
1037 audio_io_handle_t handle,
1038 audio_devices_t devices,
1039 audio_output_flags_t flags,
1040 struct audio_config *config,
1041 struct audio_stream_out **stream_out,
1042 const char *address)
1045 struct a2dp_audio_device *a2dp_dev = (struct a2dp_audio_device *)dev;
1046 struct a2dp_stream_out *out;
1053 INFO("opening output");
1055 out = (struct a2dp_stream_out *)calloc(1, sizeof(struct a2dp_stream_out));
1060 out->stream.common.get_sample_rate = out_get_sample_rate;
1061 out->stream.common.set_sample_rate = out_set_sample_rate;
1062 out->stream.common.get_buffer_size = out_get_buffer_size;
1063 out->stream.common.get_channels = out_get_channels;
1064 out->stream.common.get_format = out_get_format;
1065 out->stream.common.set_format = out_set_format;
1066 out->stream.common.standby = out_standby;
1067 out->stream.common.dump = out_dump;
1068 out->stream.common.set_parameters = out_set_parameters;
1069 out->stream.common.get_parameters = out_get_parameters;
1070 out->stream.common.add_audio_effect = out_add_audio_effect;
1071 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1072 out->stream.get_latency = out_get_latency;
1073 out->stream.set_volume = out_set_volume;
1074 out->stream.write = out_write;
1075 out->stream.get_render_position = out_get_render_position;
1076 out->stream.get_presentation_position = out_get_presentation_position;
1079 /* initialize a2dp specifics */
1080 a2dp_stream_common_init(&out->common);
1082 out->common.cfg.channel_flags = AUDIO_STREAM_DEFAULT_CHANNEL_FLAG;
1083 out->common.cfg.format = AUDIO_STREAM_DEFAULT_FORMAT;
1084 out->common.cfg.rate = AUDIO_STREAM_DEFAULT_RATE;
1086 /* set output config values */
1089 config->format = out_get_format((const struct audio_stream *)&out->stream);
1090 config->sample_rate = out_get_sample_rate((const struct audio_stream *)&out->stream);
1091 config->channel_mask = out_get_channels((const struct audio_stream *)&out->stream);
1093 *stream_out = &out->stream;
1094 a2dp_dev->output = out;
1096 a2dp_open_ctrl_path(&out->common);
1097 if (out->common.ctrl_fd == AUDIO_SKT_DISCONNECTED)
1099 ERROR("ctrl socket failed to connect (%s)", strerror(errno));
1105 /* Delay to ensure Headset is in proper state when START is initiated
1106 from DUT immediately after the connection due to ongoing music playback. */
1113 a2dp_dev->output = NULL;
1118 static void adev_close_output_stream(struct audio_hw_device *dev,
1119 struct audio_stream_out *stream)
1121 struct a2dp_audio_device *a2dp_dev = (struct a2dp_audio_device *)dev;
1122 struct a2dp_stream_out *out = (struct a2dp_stream_out *)stream;
1124 INFO("closing output (state %d)", out->common.state);
1126 pthread_mutex_lock(&out->common.lock);
1127 if ((out->common.state == AUDIO_A2DP_STATE_STARTED) || (out->common.state == AUDIO_A2DP_STATE_STOPPING))
1128 stop_audio_datapath(&out->common);
1130 skt_disconnect(out->common.ctrl_fd);
1132 a2dp_dev->output = NULL;
1133 pthread_mutex_unlock(&out->common.lock);
1138 static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
1140 struct a2dp_audio_device *a2dp_dev = (struct a2dp_audio_device *)dev;
1141 struct a2dp_stream_out *out = a2dp_dev->output;
1147 INFO("state %d", out->common.state);
1149 retval = out->stream.common.set_parameters((struct audio_stream *)out, kvpairs);
1154 static char * adev_get_parameters(const struct audio_hw_device *dev,
1161 hash_map_t *params = hash_map_utils_new_from_string_params(keys);
1162 hash_map_utils_dump_string_keys_string_values(params);
1163 hash_map_free(params);
1168 static int adev_init_check(const struct audio_hw_device *dev)
1176 static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
1186 static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
1196 static int adev_set_mode(struct audio_hw_device *dev, int mode)
1206 static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
1216 static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1226 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
1227 const struct audio_config *config)
1237 static int adev_open_input_stream(struct audio_hw_device *dev,
1238 audio_io_handle_t handle,
1239 audio_devices_t devices,
1240 struct audio_config *config,
1241 struct audio_stream_in **stream_in,
1242 audio_input_flags_t flags,
1243 const char *address,
1244 audio_source_t source)
1246 struct a2dp_audio_device *a2dp_dev = (struct a2dp_audio_device *)dev;
1247 struct a2dp_stream_in *in;
1258 in = (struct a2dp_stream_in *)calloc(1, sizeof(struct a2dp_stream_in));
1263 in->stream.common.get_sample_rate = in_get_sample_rate;
1264 in->stream.common.set_sample_rate = in_set_sample_rate;
1265 in->stream.common.get_buffer_size = in_get_buffer_size;
1266 in->stream.common.get_channels = in_get_channels;
1267 in->stream.common.get_format = in_get_format;
1268 in->stream.common.set_format = in_set_format;
1269 in->stream.common.standby = in_standby;
1270 in->stream.common.dump = in_dump;
1271 in->stream.common.set_parameters = in_set_parameters;
1272 in->stream.common.get_parameters = in_get_parameters;
1273 in->stream.common.add_audio_effect = in_add_audio_effect;
1274 in->stream.common.remove_audio_effect = in_remove_audio_effect;
1275 in->stream.set_gain = in_set_gain;
1276 in->stream.read = in_read;
1277 in->stream.get_input_frames_lost = in_get_input_frames_lost;
1279 /* initialize a2dp specifics */
1280 a2dp_stream_common_init(&in->common);
1282 *stream_in = &in->stream;
1283 a2dp_dev->input = in;
1285 a2dp_open_ctrl_path(&in->common);
1286 if (in->common.ctrl_fd == AUDIO_SKT_DISCONNECTED)
1288 ERROR("ctrl socket failed to connect (%s)", strerror(errno));
1293 if (a2dp_read_audio_config(&in->common) < 0) {
1294 ERROR("a2dp_read_audio_config failed (%s)", strerror(errno));
1305 a2dp_dev->input = NULL;
1310 static void adev_close_input_stream(struct audio_hw_device *dev,
1311 struct audio_stream_in *stream)
1313 struct a2dp_audio_device *a2dp_dev = (struct a2dp_audio_device *)dev;
1314 struct a2dp_stream_in* in = (struct a2dp_stream_in *)stream;
1315 a2dp_state_t state = in->common.state;
1317 INFO("closing input (state %d)", state);
1319 if ((state == AUDIO_A2DP_STATE_STARTED) || (state == AUDIO_A2DP_STATE_STOPPING))
1320 stop_audio_datapath(&in->common);
1322 skt_disconnect(in->common.ctrl_fd);
1324 a2dp_dev->input = NULL;
1329 static int adev_dump(const audio_hw_device_t *device, int fd)
1339 static int adev_close(hw_device_t *device)
1347 static int adev_open(const hw_module_t* module, const char* name,
1348 hw_device_t** device)
1350 struct a2dp_audio_device *adev;
1352 INFO(" adev_open in A2dp_hw module");
1355 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1357 ERROR("interface %s not matching [%s]", name, AUDIO_HARDWARE_INTERFACE);
1361 adev = calloc(1, sizeof(struct a2dp_audio_device));
1366 adev->device.common.tag = HARDWARE_DEVICE_TAG;
1367 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
1368 adev->device.common.module = (struct hw_module_t *) module;
1369 adev->device.common.close = adev_close;
1371 adev->device.init_check = adev_init_check;
1372 adev->device.set_voice_volume = adev_set_voice_volume;
1373 adev->device.set_master_volume = adev_set_master_volume;
1374 adev->device.set_mode = adev_set_mode;
1375 adev->device.set_mic_mute = adev_set_mic_mute;
1376 adev->device.get_mic_mute = adev_get_mic_mute;
1377 adev->device.set_parameters = adev_set_parameters;
1378 adev->device.get_parameters = adev_get_parameters;
1379 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
1380 adev->device.open_output_stream = adev_open_output_stream;
1381 adev->device.close_output_stream = adev_close_output_stream;
1382 adev->device.open_input_stream = adev_open_input_stream;
1383 adev->device.close_input_stream = adev_close_input_stream;
1384 adev->device.dump = adev_dump;
1386 adev->output = NULL;
1389 *device = &adev->device.common;
1394 static struct hw_module_methods_t hal_module_methods = {
1398 __attribute__ ((visibility ("default")))
1399 struct audio_module HAL_MODULE_INFO_SYM = {
1401 .tag = HARDWARE_MODULE_TAG,
1404 .id = AUDIO_HARDWARE_MODULE_ID,
1405 .name = "A2DP Audio HW HAL",
1406 .author = "The Android Open Source Project",
1407 .methods = &hal_module_methods,