The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
Jakub Pawlowski | 5b790fe | 2017-09-18 09:00:20 -0700 | [diff] [blame] | 3 | * Copyright 2009-2012 Broadcom Corporation |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 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 | |
Marie Janssen | 49120dc | 2015-07-07 16:47:20 -0700 | [diff] [blame] | 27 | #define LOG_TAG "bt_a2dp_hw" |
| 28 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 29 | #include <errno.h> |
Miao Chou | 1096912 | 2015-06-09 17:39:46 -0700 | [diff] [blame] | 30 | #include <fcntl.h> |
Mark Salyzyn | 40421cb | 2014-04-17 13:45:20 -0700 | [diff] [blame] | 31 | #include <inttypes.h> |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 32 | #include <stdint.h> |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 33 | #include <sys/errno.h> |
Miao Chou | 1096912 | 2015-06-09 17:39:46 -0700 | [diff] [blame] | 34 | #include <sys/socket.h> |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 35 | #include <sys/stat.h> |
Miao Chou | 1096912 | 2015-06-09 17:39:46 -0700 | [diff] [blame] | 36 | #include <sys/time.h> |
| 37 | #include <sys/un.h> |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 38 | #include <unistd.h> |
Miao Chou | 1096912 | 2015-06-09 17:39:46 -0700 | [diff] [blame] | 39 | |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 40 | #include <mutex> |
| 41 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 42 | #include <hardware/audio.h> |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 43 | #include <hardware/hardware.h> |
Miao Chou | 1096912 | 2015-06-09 17:39:46 -0700 | [diff] [blame] | 44 | #include <system/audio.h> |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 45 | |
Miao Chou | 98a9641 | 2015-07-08 14:50:32 -0700 | [diff] [blame] | 46 | #include "osi/include/hash_map_utils.h" |
Sharvil Nanavati | 4480276 | 2014-12-23 23:08:58 -0800 | [diff] [blame] | 47 | #include "osi/include/log.h" |
Pavlin Radoslavov | d6121a3 | 2016-05-12 11:36:44 -0700 | [diff] [blame] | 48 | #include "osi/include/osi.h" |
Miao Chou | 1096912 | 2015-06-09 17:39:46 -0700 | [diff] [blame] | 49 | #include "osi/include/socket_utils/sockets.h" |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 50 | |
Jack He | e2eeff4 | 2016-12-07 18:25:17 -0800 | [diff] [blame] | 51 | #include "audio_a2dp_hw.h" |
| 52 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 53 | /***************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 54 | * Constants & Macros |
| 55 | *****************************************************************************/ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 56 | |
| 57 | #define CTRL_CHAN_RETRY_COUNT 3 |
| 58 | #define USEC_PER_SEC 1000000L |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 59 | #define SOCK_SEND_TIMEOUT_MS 2000 /* Timeout for sending */ |
| 60 | #define SOCK_RECV_TIMEOUT_MS 5000 /* Timeout for receiving */ |
Ajay Panicker | ef673c5 | 2016-12-21 15:37:34 -0800 | [diff] [blame] | 61 | #define SEC_TO_MS 1000 |
| 62 | #define SEC_TO_NS 1000000000 |
| 63 | #define MS_TO_NS 1000000 |
| 64 | #define DELAY_TO_NS 100000 |
| 65 | |
| 66 | #define MIN_DELAY_MS 100 |
| 67 | #define MAX_DELAY_MS 1000 |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 68 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 69 | // set WRITE_POLL_MS to 0 for blocking sockets, nonzero for polled non-blocking |
| 70 | // sockets |
Andy Hung | 32e7c20 | 2016-06-14 19:58:58 -0700 | [diff] [blame] | 71 | #define WRITE_POLL_MS 20 |
| 72 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 73 | #define FNLOG() LOG_VERBOSE(LOG_TAG, "%s", __func__); |
| 74 | #define DEBUG(fmt, ...) \ |
| 75 | LOG_VERBOSE(LOG_TAG, "%s: " fmt, __func__, ##__VA_ARGS__) |
| 76 | #define INFO(fmt, ...) LOG_INFO(LOG_TAG, "%s: " fmt, __func__, ##__VA_ARGS__) |
| 77 | #define WARN(fmt, ...) LOG_WARN(LOG_TAG, "%s: " fmt, __func__, ##__VA_ARGS__) |
| 78 | #define ERROR(fmt, ...) LOG_ERROR(LOG_TAG, "%s: " fmt, __func__, ##__VA_ARGS__) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 79 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 80 | #define ASSERTC(cond, msg, val) \ |
| 81 | if (!(cond)) { \ |
| 82 | ERROR("### ASSERT : %s line %d %s (%d) ###", __FILE__, __LINE__, msg, \ |
| 83 | val); \ |
| 84 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 85 | |
| 86 | /***************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 87 | * Local type definitions |
| 88 | *****************************************************************************/ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 89 | |
| 90 | typedef enum { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 91 | AUDIO_A2DP_STATE_STARTING, |
| 92 | AUDIO_A2DP_STATE_STARTED, |
| 93 | AUDIO_A2DP_STATE_STOPPING, |
| 94 | AUDIO_A2DP_STATE_STOPPED, |
| 95 | /* need explicit set param call to resume (suspend=false) */ |
| 96 | AUDIO_A2DP_STATE_SUSPENDED, |
| 97 | AUDIO_A2DP_STATE_STANDBY /* allows write to autoresume */ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 98 | } a2dp_state_t; |
| 99 | |
Mike Lockwood | 3e8a242f | 2014-05-23 12:42:24 -0700 | [diff] [blame] | 100 | struct a2dp_stream_in; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 101 | struct a2dp_stream_out; |
| 102 | |
| 103 | struct a2dp_audio_device { |
Andy Hung | b1a900f | 2017-03-30 16:28:46 -0700 | [diff] [blame] | 104 | // Important: device must be first as an audio_hw_device* may be cast to |
| 105 | // a2dp_audio_device* when the type is implicitly known. |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 106 | struct audio_hw_device device; |
Andy Hung | b1a900f | 2017-03-30 16:28:46 -0700 | [diff] [blame] | 107 | std::recursive_mutex* mutex; // See note below on mutex acquisition order. |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 108 | struct a2dp_stream_in* input; |
| 109 | struct a2dp_stream_out* output; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | struct a2dp_config { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 113 | uint32_t rate; |
| 114 | uint32_t channel_mask; |
Pavlin Radoslavov | 95e81d0 | 2017-10-29 11:35:22 -0700 | [diff] [blame] | 115 | bool is_stereo_to_mono; // True if fetching Stereo and mixing into Mono |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 116 | int format; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | /* move ctrl_fd outside output stream and keep open until HAL unloaded ? */ |
| 120 | |
Mike Lockwood | 3e8a242f | 2014-05-23 12:42:24 -0700 | [diff] [blame] | 121 | struct a2dp_stream_common { |
Andy Hung | b1a900f | 2017-03-30 16:28:46 -0700 | [diff] [blame] | 122 | std::recursive_mutex* mutex; // See note below on mutex acquisition order. |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 123 | int ctrl_fd; |
| 124 | int audio_fd; |
| 125 | size_t buffer_sz; |
| 126 | struct a2dp_config cfg; |
| 127 | a2dp_state_t state; |
Mike Lockwood | 3e8a242f | 2014-05-23 12:42:24 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | struct a2dp_stream_out { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 131 | struct audio_stream_out stream; |
| 132 | struct a2dp_stream_common common; |
| 133 | uint64_t frames_presented; // frames written, never reset |
| 134 | uint64_t frames_rendered; // frames written, reset on standby |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | struct a2dp_stream_in { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 138 | struct audio_stream_in stream; |
| 139 | struct a2dp_stream_common common; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 140 | }; |
| 141 | |
Andy Hung | b1a900f | 2017-03-30 16:28:46 -0700 | [diff] [blame] | 142 | /* |
| 143 | * Mutex acquisition order: |
| 144 | * |
| 145 | * The a2dp_audio_device (adev) mutex must be acquired before |
| 146 | * the a2dp_stream_common (out or in) mutex. |
| 147 | * |
| 148 | * This may differ from other audio HALs. |
| 149 | */ |
| 150 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 151 | /***************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 152 | * Static variables |
| 153 | *****************************************************************************/ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 154 | |
Ajay Panicker | ef673c5 | 2016-12-21 15:37:34 -0800 | [diff] [blame] | 155 | static bool enable_delay_reporting = false; |
| 156 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 157 | /***************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 158 | * Static functions |
| 159 | *****************************************************************************/ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 160 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 161 | static size_t out_get_buffer_size(const struct audio_stream* stream); |
Ajay Panicker | ef673c5 | 2016-12-21 15:37:34 -0800 | [diff] [blame] | 162 | static uint32_t out_get_latency(const struct audio_stream_out* stream); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 163 | |
| 164 | /***************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 165 | * Externs |
| 166 | *****************************************************************************/ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 167 | |
| 168 | /***************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 169 | * Functions |
| 170 | *****************************************************************************/ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 171 | static void a2dp_open_ctrl_path(struct a2dp_stream_common* common); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 172 | |
| 173 | /***************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 174 | * Miscellaneous helper functions |
| 175 | *****************************************************************************/ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 176 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 177 | /* logs timestamp with microsec precision |
| 178 | pprev is optional in case a dedicated diff is required */ |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 179 | static void ts_log(UNUSED_ATTR const char* tag, UNUSED_ATTR int val, |
| 180 | struct timespec* pprev_opt) { |
| 181 | struct timespec now; |
| 182 | static struct timespec prev = {0, 0}; |
| 183 | unsigned long long now_us; |
| 184 | unsigned long long diff_us; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 185 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 186 | clock_gettime(CLOCK_MONOTONIC, &now); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 187 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 188 | now_us = now.tv_sec * USEC_PER_SEC + now.tv_nsec / 1000; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 189 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 190 | if (pprev_opt) { |
| 191 | diff_us = (now.tv_sec - prev.tv_sec) * USEC_PER_SEC + |
| 192 | (now.tv_nsec - prev.tv_nsec) / 1000; |
| 193 | *pprev_opt = now; |
| 194 | DEBUG("[%s] ts %08lld, *diff %08lld, val %d", tag, now_us, diff_us, val); |
| 195 | } else { |
| 196 | diff_us = (now.tv_sec - prev.tv_sec) * USEC_PER_SEC + |
| 197 | (now.tv_nsec - prev.tv_nsec) / 1000; |
| 198 | prev = now; |
| 199 | DEBUG("[%s] ts %08lld, diff %08lld, val %d", tag, now_us, diff_us, val); |
| 200 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 203 | static int calc_audiotime_usec(struct a2dp_config cfg, int bytes) { |
| 204 | int chan_count = audio_channel_count_from_out_mask(cfg.channel_mask); |
| 205 | int bytes_per_sample; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 206 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 207 | switch (cfg.format) { |
Pavlin Radoslavov | 4ebaa86 | 2016-11-22 11:42:11 -0800 | [diff] [blame] | 208 | case AUDIO_FORMAT_PCM_8_BIT: |
| 209 | bytes_per_sample = 1; |
| 210 | break; |
| 211 | case AUDIO_FORMAT_PCM_16_BIT: |
| 212 | bytes_per_sample = 2; |
| 213 | break; |
| 214 | case AUDIO_FORMAT_PCM_24_BIT_PACKED: |
| 215 | bytes_per_sample = 3; |
| 216 | break; |
| 217 | case AUDIO_FORMAT_PCM_8_24_BIT: |
| 218 | bytes_per_sample = 4; |
| 219 | break; |
| 220 | case AUDIO_FORMAT_PCM_32_BIT: |
| 221 | bytes_per_sample = 4; |
| 222 | break; |
| 223 | default: |
| 224 | ASSERTC(false, "unsupported sample format", cfg.format); |
| 225 | bytes_per_sample = 2; |
| 226 | break; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 227 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 228 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 229 | return ( |
| 230 | int)(((int64_t)bytes * (USEC_PER_SEC / (chan_count * bytes_per_sample))) / |
| 231 | cfg.rate); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /***************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 235 | * |
| 236 | * bluedroid stack adaptation |
| 237 | * |
| 238 | ****************************************************************************/ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 239 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 240 | static int skt_connect(const char* path, size_t buffer_sz) { |
| 241 | int ret; |
| 242 | int skt_fd; |
| 243 | int len; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 244 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 245 | INFO("connect to %s (sz %zu)", path, buffer_sz); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 246 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 247 | skt_fd = socket(AF_LOCAL, SOCK_STREAM, 0); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 248 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 249 | if (osi_socket_local_client_connect( |
| 250 | skt_fd, path, ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM) < 0) { |
| 251 | ERROR("failed to connect (%s)", strerror(errno)); |
| 252 | close(skt_fd); |
| 253 | return -1; |
| 254 | } |
| 255 | |
| 256 | len = buffer_sz; |
| 257 | ret = |
| 258 | setsockopt(skt_fd, SOL_SOCKET, SO_SNDBUF, (char*)&len, (int)sizeof(len)); |
| 259 | if (ret < 0) ERROR("setsockopt failed (%s)", strerror(errno)); |
| 260 | |
| 261 | ret = |
| 262 | setsockopt(skt_fd, SOL_SOCKET, SO_RCVBUF, (char*)&len, (int)sizeof(len)); |
| 263 | if (ret < 0) ERROR("setsockopt failed (%s)", strerror(errno)); |
| 264 | |
| 265 | /* Socket send/receive timeout value */ |
| 266 | struct timeval tv; |
| 267 | tv.tv_sec = SOCK_SEND_TIMEOUT_MS / 1000; |
| 268 | tv.tv_usec = (SOCK_SEND_TIMEOUT_MS % 1000) * 1000; |
| 269 | |
| 270 | ret = setsockopt(skt_fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); |
| 271 | if (ret < 0) ERROR("setsockopt failed (%s)", strerror(errno)); |
| 272 | |
| 273 | tv.tv_sec = SOCK_RECV_TIMEOUT_MS / 1000; |
| 274 | tv.tv_usec = (SOCK_RECV_TIMEOUT_MS % 1000) * 1000; |
| 275 | |
| 276 | ret = setsockopt(skt_fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); |
| 277 | if (ret < 0) ERROR("setsockopt failed (%s)", strerror(errno)); |
| 278 | |
| 279 | INFO("connected to stack fd = %d", skt_fd); |
| 280 | |
| 281 | return skt_fd; |
| 282 | } |
| 283 | |
| 284 | static int skt_read(int fd, void* p, size_t len) { |
| 285 | ssize_t read; |
| 286 | |
| 287 | FNLOG(); |
| 288 | |
| 289 | ts_log("skt_read recv", len, NULL); |
| 290 | |
| 291 | OSI_NO_INTR(read = recv(fd, p, len, MSG_NOSIGNAL)); |
| 292 | if (read == -1) ERROR("read failed with errno=%d\n", errno); |
| 293 | |
| 294 | return (int)read; |
| 295 | } |
| 296 | |
| 297 | static int skt_write(int fd, const void* p, size_t len) { |
| 298 | ssize_t sent; |
| 299 | FNLOG(); |
| 300 | |
| 301 | ts_log("skt_write", len, NULL); |
| 302 | |
| 303 | if (WRITE_POLL_MS == 0) { |
| 304 | // do not poll, use blocking send |
| 305 | OSI_NO_INTR(sent = send(fd, p, len, MSG_NOSIGNAL)); |
| 306 | if (sent == -1) ERROR("write failed with error(%s)", strerror(errno)); |
| 307 | |
| 308 | return (int)sent; |
| 309 | } |
| 310 | |
| 311 | // use non-blocking send, poll |
| 312 | int ms_timeout = SOCK_SEND_TIMEOUT_MS; |
| 313 | size_t count = 0; |
| 314 | while (count < len) { |
| 315 | OSI_NO_INTR(sent = send(fd, p, len - count, MSG_NOSIGNAL | MSG_DONTWAIT)); |
| 316 | if (sent == -1) { |
| 317 | if (errno != EAGAIN && errno != EWOULDBLOCK) { |
| 318 | ERROR("write failed with error(%s)", strerror(errno)); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 319 | return -1; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 320 | } |
| 321 | if (ms_timeout >= WRITE_POLL_MS) { |
| 322 | usleep(WRITE_POLL_MS * 1000); |
| 323 | ms_timeout -= WRITE_POLL_MS; |
| 324 | continue; |
| 325 | } |
| 326 | WARN("write timeout exceeded, sent %zu bytes", count); |
| 327 | return -1; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 328 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 329 | count += sent; |
| 330 | p = (const uint8_t*)p + sent; |
| 331 | } |
| 332 | return (int)count; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 333 | } |
| 334 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 335 | static int skt_disconnect(int fd) { |
| 336 | INFO("fd %d", fd); |
Mike Lockwood | 3e8a242f | 2014-05-23 12:42:24 -0700 | [diff] [blame] | 337 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 338 | if (fd != AUDIO_SKT_DISCONNECTED) { |
| 339 | shutdown(fd, SHUT_RDWR); |
| 340 | close(fd); |
| 341 | } |
| 342 | return 0; |
Mike Lockwood | 3e8a242f | 2014-05-23 12:42:24 -0700 | [diff] [blame] | 343 | } |
| 344 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 345 | /***************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 346 | * |
| 347 | * AUDIO CONTROL PATH |
| 348 | * |
| 349 | ****************************************************************************/ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 350 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 351 | static int a2dp_ctrl_receive(struct a2dp_stream_common* common, void* buffer, |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 352 | size_t length) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 353 | ssize_t ret; |
| 354 | int i; |
Pavlin Radoslavov | d6121a3 | 2016-05-12 11:36:44 -0700 | [diff] [blame] | 355 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 356 | for (i = 0;; i++) { |
| 357 | OSI_NO_INTR(ret = recv(common->ctrl_fd, buffer, length, MSG_NOSIGNAL)); |
| 358 | if (ret > 0) { |
| 359 | break; |
Eric Laurent | 7a599b4 | 2016-05-11 18:18:54 -0700 | [diff] [blame] | 360 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 361 | if (ret == 0) { |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 362 | ERROR("receive control data failed: peer closed"); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 363 | break; |
Mike Lockwood | 3e8a242f | 2014-05-23 12:42:24 -0700 | [diff] [blame] | 364 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 365 | if (errno != EWOULDBLOCK && errno != EAGAIN) { |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 366 | ERROR("receive control data failed: error(%s)", strerror(errno)); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 367 | break; |
| 368 | } |
| 369 | if (i == (CTRL_CHAN_RETRY_COUNT - 1)) { |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 370 | ERROR("receive control data failed: max retry count"); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 371 | break; |
| 372 | } |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 373 | INFO("receive control data failed (%s), retrying", strerror(errno)); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 374 | } |
| 375 | if (ret <= 0) { |
| 376 | skt_disconnect(common->ctrl_fd); |
| 377 | common->ctrl_fd = AUDIO_SKT_DISCONNECTED; |
| 378 | } |
| 379 | return ret; |
Mike Lockwood | 3e8a242f | 2014-05-23 12:42:24 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 382 | // Sends control info for stream |common|. The data to send is stored in |
| 383 | // |buffer| and has size |length|. |
| 384 | // On success, returns the number of octets sent, otherwise -1. |
| 385 | static int a2dp_ctrl_send(struct a2dp_stream_common* common, const void* buffer, |
| 386 | size_t length) { |
| 387 | ssize_t sent; |
| 388 | size_t remaining = length; |
| 389 | int i; |
| 390 | |
| 391 | if (length == 0) return 0; // Nothing to do |
| 392 | |
| 393 | for (i = 0;; i++) { |
| 394 | OSI_NO_INTR(sent = send(common->ctrl_fd, buffer, remaining, MSG_NOSIGNAL)); |
| 395 | if (sent == static_cast<ssize_t>(remaining)) { |
| 396 | remaining = 0; |
| 397 | break; |
| 398 | } |
| 399 | if (sent > 0) { |
| 400 | buffer = (static_cast<const char*>(buffer) + sent); |
| 401 | remaining -= sent; |
| 402 | continue; |
| 403 | } |
| 404 | if (sent < 0) { |
| 405 | if (errno != EWOULDBLOCK && errno != EAGAIN) { |
| 406 | ERROR("send control data failed: error(%s)", strerror(errno)); |
| 407 | break; |
| 408 | } |
| 409 | INFO("send control data failed (%s), retrying", strerror(errno)); |
| 410 | } |
| 411 | if (i >= (CTRL_CHAN_RETRY_COUNT - 1)) { |
| 412 | ERROR("send control data failed: max retry count"); |
| 413 | break; |
| 414 | } |
| 415 | } |
| 416 | if (remaining > 0) { |
| 417 | skt_disconnect(common->ctrl_fd); |
| 418 | common->ctrl_fd = AUDIO_SKT_DISCONNECTED; |
| 419 | return -1; |
| 420 | } |
| 421 | return length; |
| 422 | } |
| 423 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 424 | static int a2dp_command(struct a2dp_stream_common* common, tA2DP_CTRL_CMD cmd) { |
| 425 | char ack; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 426 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 427 | DEBUG("A2DP COMMAND %s", audio_a2dp_hw_dump_ctrl_event(cmd)); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 428 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 429 | if (common->ctrl_fd == AUDIO_SKT_DISCONNECTED) { |
Pavlin Radoslavov | 2a3b9b5 | 2018-04-30 13:39:20 -0700 | [diff] [blame] | 430 | INFO("starting up or recovering from previous error: command=%s", |
| 431 | audio_a2dp_hw_dump_ctrl_event(cmd)); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 432 | a2dp_open_ctrl_path(common); |
Eric Laurent | 7a599b4 | 2016-05-11 18:18:54 -0700 | [diff] [blame] | 433 | if (common->ctrl_fd == AUDIO_SKT_DISCONNECTED) { |
Pavlin Radoslavov | 2a3b9b5 | 2018-04-30 13:39:20 -0700 | [diff] [blame] | 434 | ERROR("failure to open ctrl path: command=%s", |
| 435 | audio_a2dp_hw_dump_ctrl_event(cmd)); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 436 | return -1; |
Eric Laurent | 7a599b4 | 2016-05-11 18:18:54 -0700 | [diff] [blame] | 437 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 438 | } |
Eric Laurent | 7a599b4 | 2016-05-11 18:18:54 -0700 | [diff] [blame] | 439 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 440 | /* send command */ |
| 441 | ssize_t sent; |
| 442 | OSI_NO_INTR(sent = send(common->ctrl_fd, &cmd, 1, MSG_NOSIGNAL)); |
| 443 | if (sent == -1) { |
Pavlin Radoslavov | 2a3b9b5 | 2018-04-30 13:39:20 -0700 | [diff] [blame] | 444 | ERROR("cmd failed (%s): command=%s", strerror(errno), |
| 445 | audio_a2dp_hw_dump_ctrl_event(cmd)); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 446 | skt_disconnect(common->ctrl_fd); |
| 447 | common->ctrl_fd = AUDIO_SKT_DISCONNECTED; |
| 448 | return -1; |
| 449 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 450 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 451 | /* wait for ack byte */ |
| 452 | if (a2dp_ctrl_receive(common, &ack, 1) < 0) { |
| 453 | ERROR("A2DP COMMAND %s: no ACK", audio_a2dp_hw_dump_ctrl_event(cmd)); |
| 454 | return -1; |
| 455 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 456 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 457 | DEBUG("A2DP COMMAND %s DONE STATUS %d", audio_a2dp_hw_dump_ctrl_event(cmd), |
| 458 | ack); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 459 | |
Pavlin Radoslavov | 2a3b9b5 | 2018-04-30 13:39:20 -0700 | [diff] [blame] | 460 | if (ack == A2DP_CTRL_ACK_INCALL_FAILURE) { |
| 461 | ERROR("A2DP COMMAND %s error %d", audio_a2dp_hw_dump_ctrl_event(cmd), ack); |
| 462 | return ack; |
| 463 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 464 | if (ack != A2DP_CTRL_ACK_SUCCESS) { |
| 465 | ERROR("A2DP COMMAND %s error %d", audio_a2dp_hw_dump_ctrl_event(cmd), ack); |
| 466 | return -1; |
| 467 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 468 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 469 | return 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 470 | } |
| 471 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 472 | static int check_a2dp_ready(struct a2dp_stream_common* common) { |
| 473 | if (a2dp_command(common, A2DP_CTRL_CMD_CHECK_READY) < 0) { |
| 474 | ERROR("check a2dp ready failed"); |
| 475 | return -1; |
| 476 | } |
| 477 | return 0; |
Mike Lockwood | 3e8a242f | 2014-05-23 12:42:24 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 480 | static int a2dp_read_input_audio_config(struct a2dp_stream_common* common) { |
| 481 | tA2DP_SAMPLE_RATE sample_rate; |
| 482 | tA2DP_CHANNEL_COUNT channel_count; |
Mike Lockwood | 3e8a242f | 2014-05-23 12:42:24 -0700 | [diff] [blame] | 483 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 484 | if (a2dp_command(common, A2DP_CTRL_GET_INPUT_AUDIO_CONFIG) < 0) { |
| 485 | ERROR("get a2dp input audio config failed"); |
| 486 | return -1; |
| 487 | } |
Mike Lockwood | 3e8a242f | 2014-05-23 12:42:24 -0700 | [diff] [blame] | 488 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 489 | if (a2dp_ctrl_receive(common, &sample_rate, sizeof(tA2DP_SAMPLE_RATE)) < 0) |
| 490 | return -1; |
| 491 | if (a2dp_ctrl_receive(common, &channel_count, sizeof(tA2DP_CHANNEL_COUNT)) < |
| 492 | 0) { |
| 493 | return -1; |
| 494 | } |
Mike Lockwood | 3e8a242f | 2014-05-23 12:42:24 -0700 | [diff] [blame] | 495 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 496 | switch (sample_rate) { |
Pavlin Radoslavov | 4ebaa86 | 2016-11-22 11:42:11 -0800 | [diff] [blame] | 497 | case 44100: |
| 498 | case 48000: |
| 499 | common->cfg.rate = sample_rate; |
| 500 | break; |
| 501 | default: |
| 502 | ERROR("Invalid sample rate: %" PRIu32, sample_rate); |
| 503 | return -1; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 504 | } |
Mike Lockwood | 3e8a242f | 2014-05-23 12:42:24 -0700 | [diff] [blame] | 505 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 506 | switch (channel_count) { |
Pavlin Radoslavov | 4ebaa86 | 2016-11-22 11:42:11 -0800 | [diff] [blame] | 507 | case 1: |
| 508 | common->cfg.channel_mask = AUDIO_CHANNEL_IN_MONO; |
| 509 | break; |
| 510 | case 2: |
| 511 | common->cfg.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
| 512 | break; |
| 513 | default: |
| 514 | ERROR("Invalid channel count: %" PRIu32, channel_count); |
| 515 | return -1; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 516 | } |
Pavlin Radoslavov | 4ebaa86 | 2016-11-22 11:42:11 -0800 | [diff] [blame] | 517 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 518 | // TODO: For now input audio format is always hard-coded as PCM 16-bit |
| 519 | common->cfg.format = AUDIO_FORMAT_PCM_16_BIT; |
Pavlin Radoslavov | 4ebaa86 | 2016-11-22 11:42:11 -0800 | [diff] [blame] | 520 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 521 | INFO("got input audio config %d %d", common->cfg.format, common->cfg.rate); |
Pavlin Radoslavov | 4ebaa86 | 2016-11-22 11:42:11 -0800 | [diff] [blame] | 522 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 523 | return 0; |
Pavlin Radoslavov | 4ebaa86 | 2016-11-22 11:42:11 -0800 | [diff] [blame] | 524 | } |
| 525 | |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 526 | static int a2dp_read_output_audio_config( |
| 527 | struct a2dp_stream_common* common, btav_a2dp_codec_config_t* codec_config, |
| 528 | btav_a2dp_codec_config_t* codec_capability, bool update_stream_config) { |
| 529 | struct a2dp_config stream_config; |
Pavlin Radoslavov | 4ebaa86 | 2016-11-22 11:42:11 -0800 | [diff] [blame] | 530 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 531 | if (a2dp_command(common, A2DP_CTRL_GET_OUTPUT_AUDIO_CONFIG) < 0) { |
| 532 | ERROR("get a2dp output audio config failed"); |
| 533 | return -1; |
| 534 | } |
Pavlin Radoslavov | 4ebaa86 | 2016-11-22 11:42:11 -0800 | [diff] [blame] | 535 | |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 536 | // Receive the current codec config |
| 537 | if (a2dp_ctrl_receive(common, &codec_config->sample_rate, |
| 538 | sizeof(btav_a2dp_codec_sample_rate_t)) < 0) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 539 | return -1; |
| 540 | } |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 541 | if (a2dp_ctrl_receive(common, &codec_config->bits_per_sample, |
| 542 | sizeof(btav_a2dp_codec_bits_per_sample_t)) < 0) { |
| 543 | return -1; |
| 544 | } |
| 545 | if (a2dp_ctrl_receive(common, &codec_config->channel_mode, |
| 546 | sizeof(btav_a2dp_codec_channel_mode_t)) < 0) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 547 | return -1; |
| 548 | } |
Pavlin Radoslavov | 4ebaa86 | 2016-11-22 11:42:11 -0800 | [diff] [blame] | 549 | |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 550 | // Receive the current codec capability |
| 551 | if (a2dp_ctrl_receive(common, &codec_capability->sample_rate, |
| 552 | sizeof(btav_a2dp_codec_sample_rate_t)) < 0) { |
| 553 | return -1; |
| 554 | } |
| 555 | if (a2dp_ctrl_receive(common, &codec_capability->bits_per_sample, |
| 556 | sizeof(btav_a2dp_codec_bits_per_sample_t)) < 0) { |
| 557 | return -1; |
| 558 | } |
| 559 | if (a2dp_ctrl_receive(common, &codec_capability->channel_mode, |
| 560 | sizeof(btav_a2dp_codec_channel_mode_t)) < 0) { |
| 561 | return -1; |
| 562 | } |
| 563 | |
| 564 | // Check the codec config sample rate |
| 565 | switch (codec_config->sample_rate) { |
| 566 | case BTAV_A2DP_CODEC_SAMPLE_RATE_44100: |
| 567 | stream_config.rate = 44100; |
Pavlin Radoslavov | 4ebaa86 | 2016-11-22 11:42:11 -0800 | [diff] [blame] | 568 | break; |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 569 | case BTAV_A2DP_CODEC_SAMPLE_RATE_48000: |
| 570 | stream_config.rate = 48000; |
| 571 | break; |
| 572 | case BTAV_A2DP_CODEC_SAMPLE_RATE_88200: |
| 573 | stream_config.rate = 88200; |
| 574 | break; |
| 575 | case BTAV_A2DP_CODEC_SAMPLE_RATE_96000: |
| 576 | stream_config.rate = 96000; |
| 577 | break; |
| 578 | case BTAV_A2DP_CODEC_SAMPLE_RATE_176400: |
| 579 | stream_config.rate = 176400; |
| 580 | break; |
| 581 | case BTAV_A2DP_CODEC_SAMPLE_RATE_192000: |
| 582 | stream_config.rate = 192000; |
| 583 | break; |
| 584 | case BTAV_A2DP_CODEC_SAMPLE_RATE_NONE: |
Pavlin Radoslavov | 4ebaa86 | 2016-11-22 11:42:11 -0800 | [diff] [blame] | 585 | default: |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 586 | ERROR("Invalid sample rate: 0x%x", codec_config->sample_rate); |
Pavlin Radoslavov | 4ebaa86 | 2016-11-22 11:42:11 -0800 | [diff] [blame] | 587 | return -1; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 588 | } |
Pavlin Radoslavov | 4ebaa86 | 2016-11-22 11:42:11 -0800 | [diff] [blame] | 589 | |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 590 | // Check the codec config bits per sample |
| 591 | switch (codec_config->bits_per_sample) { |
| 592 | case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16: |
| 593 | stream_config.format = AUDIO_FORMAT_PCM_16_BIT; |
Pavlin Radoslavov | 4ebaa86 | 2016-11-22 11:42:11 -0800 | [diff] [blame] | 594 | break; |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 595 | case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24: |
| 596 | stream_config.format = AUDIO_FORMAT_PCM_24_BIT_PACKED; |
Pavlin Radoslavov | 4ebaa86 | 2016-11-22 11:42:11 -0800 | [diff] [blame] | 597 | break; |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 598 | case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32: |
| 599 | stream_config.format = AUDIO_FORMAT_PCM_32_BIT; |
| 600 | break; |
| 601 | case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE: |
Pavlin Radoslavov | 4ebaa86 | 2016-11-22 11:42:11 -0800 | [diff] [blame] | 602 | default: |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 603 | ERROR("Invalid bits per sample: 0x%x", codec_config->bits_per_sample); |
Pavlin Radoslavov | 4ebaa86 | 2016-11-22 11:42:11 -0800 | [diff] [blame] | 604 | return -1; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 605 | } |
Pavlin Radoslavov | 4ebaa86 | 2016-11-22 11:42:11 -0800 | [diff] [blame] | 606 | |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 607 | // Check the codec config channel mode |
| 608 | switch (codec_config->channel_mode) { |
| 609 | case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO: |
| 610 | stream_config.channel_mask = AUDIO_CHANNEL_OUT_MONO; |
Pavlin Radoslavov | 95e81d0 | 2017-10-29 11:35:22 -0700 | [diff] [blame] | 611 | stream_config.is_stereo_to_mono = true; |
Pavlin Radoslavov | 4ebaa86 | 2016-11-22 11:42:11 -0800 | [diff] [blame] | 612 | break; |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 613 | case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO: |
| 614 | stream_config.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
Pavlin Radoslavov | 95e81d0 | 2017-10-29 11:35:22 -0700 | [diff] [blame] | 615 | stream_config.is_stereo_to_mono = false; |
Pavlin Radoslavov | 4ebaa86 | 2016-11-22 11:42:11 -0800 | [diff] [blame] | 616 | break; |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 617 | case BTAV_A2DP_CODEC_CHANNEL_MODE_NONE: |
Pavlin Radoslavov | 4ebaa86 | 2016-11-22 11:42:11 -0800 | [diff] [blame] | 618 | default: |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 619 | ERROR("Invalid channel mode: 0x%x", codec_config->channel_mode); |
Pavlin Radoslavov | 4ebaa86 | 2016-11-22 11:42:11 -0800 | [diff] [blame] | 620 | return -1; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 621 | } |
Pavlin Radoslavov | 95e81d0 | 2017-10-29 11:35:22 -0700 | [diff] [blame] | 622 | if (stream_config.is_stereo_to_mono) { |
| 623 | stream_config.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 624 | } |
Pavlin Radoslavov | 4ebaa86 | 2016-11-22 11:42:11 -0800 | [diff] [blame] | 625 | |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 626 | // Update the output stream configuration |
| 627 | if (update_stream_config) { |
| 628 | common->cfg.rate = stream_config.rate; |
| 629 | common->cfg.channel_mask = stream_config.channel_mask; |
Pavlin Radoslavov | 95e81d0 | 2017-10-29 11:35:22 -0700 | [diff] [blame] | 630 | common->cfg.is_stereo_to_mono = stream_config.is_stereo_to_mono; |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 631 | common->cfg.format = stream_config.format; |
Pavlin Radoslavov | bdd34d2 | 2017-04-05 16:18:26 -0700 | [diff] [blame] | 632 | common->buffer_sz = audio_a2dp_hw_stream_compute_buffer_size( |
Pavlin Radoslavov | ae9db0f | 2017-04-04 11:31:29 -0700 | [diff] [blame] | 633 | codec_config->sample_rate, codec_config->bits_per_sample, |
| 634 | codec_config->channel_mode); |
Pavlin Radoslavov | 95e81d0 | 2017-10-29 11:35:22 -0700 | [diff] [blame] | 635 | if (common->cfg.is_stereo_to_mono) { |
| 636 | // We need to fetch twice as much data from the Audio framework |
| 637 | common->buffer_sz *= 2; |
| 638 | } |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 639 | } |
| 640 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 641 | INFO( |
Pavlin Radoslavov | 7a69fa4 | 2017-12-14 16:35:28 -0800 | [diff] [blame] | 642 | "got output codec config (update_stream_config=%s): " |
| 643 | "sample_rate=0x%x bits_per_sample=0x%x channel_mode=0x%x", |
| 644 | update_stream_config ? "true" : "false", codec_config->sample_rate, |
| 645 | codec_config->bits_per_sample, codec_config->channel_mode); |
| 646 | |
| 647 | INFO( |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 648 | "got output codec capability: sample_rate=0x%x bits_per_sample=0x%x " |
| 649 | "channel_mode=0x%x", |
| 650 | codec_capability->sample_rate, codec_capability->bits_per_sample, |
| 651 | codec_capability->channel_mode); |
| 652 | |
| 653 | return 0; |
| 654 | } |
| 655 | |
| 656 | static int a2dp_write_output_audio_config(struct a2dp_stream_common* common) { |
| 657 | btav_a2dp_codec_config_t codec_config; |
| 658 | |
| 659 | if (a2dp_command(common, A2DP_CTRL_SET_OUTPUT_AUDIO_CONFIG) < 0) { |
| 660 | ERROR("set a2dp output audio config failed"); |
| 661 | return -1; |
| 662 | } |
| 663 | |
| 664 | codec_config.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE; |
| 665 | codec_config.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE; |
| 666 | codec_config.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE; |
| 667 | |
| 668 | switch (common->cfg.rate) { |
| 669 | case 44100: |
| 670 | codec_config.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_44100; |
| 671 | break; |
| 672 | case 48000: |
| 673 | codec_config.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_48000; |
| 674 | break; |
| 675 | case 88200: |
| 676 | codec_config.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_88200; |
| 677 | break; |
| 678 | case 96000: |
| 679 | codec_config.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_96000; |
| 680 | break; |
| 681 | case 176400: |
| 682 | codec_config.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_176400; |
| 683 | break; |
| 684 | case 192000: |
| 685 | codec_config.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_192000; |
| 686 | break; |
| 687 | default: |
| 688 | ERROR("Invalid sample rate: %" PRIu32, common->cfg.rate); |
| 689 | return -1; |
| 690 | } |
| 691 | |
| 692 | switch (common->cfg.format) { |
| 693 | case AUDIO_FORMAT_PCM_16_BIT: |
| 694 | codec_config.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16; |
| 695 | break; |
| 696 | case AUDIO_FORMAT_PCM_24_BIT_PACKED: |
| 697 | codec_config.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24; |
| 698 | break; |
| 699 | case AUDIO_FORMAT_PCM_32_BIT: |
| 700 | codec_config.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32; |
| 701 | break; |
| 702 | case AUDIO_FORMAT_PCM_8_24_BIT: |
Chih-Hung Hsieh | d464658 | 2018-09-12 15:20:43 -0700 | [diff] [blame] | 703 | // All 24-bit audio is expected in AUDIO_FORMAT_PCM_24_BIT_PACKED format |
| 704 | FALLTHROUGH_INTENDED; /* FALLTHROUGH */ |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 705 | default: |
| 706 | ERROR("Invalid audio format: 0x%x", common->cfg.format); |
| 707 | return -1; |
| 708 | } |
| 709 | |
| 710 | switch (common->cfg.channel_mask) { |
| 711 | case AUDIO_CHANNEL_OUT_MONO: |
| 712 | codec_config.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_MONO; |
| 713 | break; |
| 714 | case AUDIO_CHANNEL_OUT_STEREO: |
Pavlin Radoslavov | 95e81d0 | 2017-10-29 11:35:22 -0700 | [diff] [blame] | 715 | if (common->cfg.is_stereo_to_mono) { |
| 716 | codec_config.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_MONO; |
| 717 | } else { |
| 718 | codec_config.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO; |
| 719 | } |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 720 | break; |
| 721 | default: |
| 722 | ERROR("Invalid channel mask: 0x%x", common->cfg.channel_mask); |
| 723 | return -1; |
| 724 | } |
| 725 | |
| 726 | // Send the current codec config that has been selected by us |
| 727 | if (a2dp_ctrl_send(common, &codec_config.sample_rate, |
| 728 | sizeof(btav_a2dp_codec_sample_rate_t)) < 0) |
| 729 | return -1; |
| 730 | if (a2dp_ctrl_send(common, &codec_config.bits_per_sample, |
| 731 | sizeof(btav_a2dp_codec_bits_per_sample_t)) < 0) { |
| 732 | return -1; |
| 733 | } |
| 734 | if (a2dp_ctrl_send(common, &codec_config.channel_mode, |
| 735 | sizeof(btav_a2dp_codec_channel_mode_t)) < 0) { |
| 736 | return -1; |
| 737 | } |
| 738 | |
| 739 | INFO( |
| 740 | "sent output codec config: sample_rate=0x%x bits_per_sample=0x%x " |
| 741 | "channel_mode=0x%x", |
| 742 | codec_config.sample_rate, codec_config.bits_per_sample, |
| 743 | codec_config.channel_mode); |
Mike Lockwood | 3e8a242f | 2014-05-23 12:42:24 -0700 | [diff] [blame] | 744 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 745 | return 0; |
Mike Lockwood | 3e8a242f | 2014-05-23 12:42:24 -0700 | [diff] [blame] | 746 | } |
| 747 | |
Ajay Panicker | ef673c5 | 2016-12-21 15:37:34 -0800 | [diff] [blame] | 748 | static int a2dp_get_presentation_position_cmd(struct a2dp_stream_common* common, |
| 749 | uint64_t* bytes, uint16_t* delay, |
| 750 | struct timespec* timestamp) { |
Pavlin Radoslavov | 61d1e86 | 2018-05-08 18:00:24 -0700 | [diff] [blame] | 751 | if ((common->ctrl_fd == AUDIO_SKT_DISCONNECTED) || |
| 752 | (common->state != AUDIO_A2DP_STATE_STARTED)) { // Audio is not streaming |
Ajay Panicker | d4f2566 | 2018-04-05 09:14:51 -0700 | [diff] [blame] | 753 | return -1; |
| 754 | } |
| 755 | |
Ajay Panicker | ef673c5 | 2016-12-21 15:37:34 -0800 | [diff] [blame] | 756 | if (a2dp_command(common, A2DP_CTRL_GET_PRESENTATION_POSITION) < 0) { |
| 757 | return -1; |
| 758 | } |
| 759 | |
| 760 | if (a2dp_ctrl_receive(common, bytes, sizeof(*bytes)) < 0) { |
| 761 | return -1; |
| 762 | } |
| 763 | |
| 764 | if (a2dp_ctrl_receive(common, delay, sizeof(*delay)) < 0) { |
| 765 | return -1; |
| 766 | } |
| 767 | |
| 768 | uint32_t seconds; |
| 769 | if (a2dp_ctrl_receive(common, &seconds, sizeof(seconds)) < 0) { |
| 770 | return -1; |
| 771 | } |
| 772 | |
| 773 | uint32_t nsec; |
| 774 | if (a2dp_ctrl_receive(common, &nsec, sizeof(nsec)) < 0) { |
| 775 | return -1; |
| 776 | } |
| 777 | |
| 778 | timestamp->tv_sec = seconds; |
| 779 | timestamp->tv_nsec = nsec; |
| 780 | return 0; |
| 781 | } |
| 782 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 783 | static void a2dp_open_ctrl_path(struct a2dp_stream_common* common) { |
| 784 | int i; |
Mike Lockwood | 3e8a242f | 2014-05-23 12:42:24 -0700 | [diff] [blame] | 785 | |
Andy Hung | f95d27b | 2017-05-01 15:04:57 -0700 | [diff] [blame] | 786 | if (common->ctrl_fd != AUDIO_SKT_DISCONNECTED) return; // already connected |
| 787 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 788 | /* retry logic to catch any timing variations on control channel */ |
| 789 | for (i = 0; i < CTRL_CHAN_RETRY_COUNT; i++) { |
| 790 | /* connect control channel if not already connected */ |
Pavlin Radoslavov | ae9db0f | 2017-04-04 11:31:29 -0700 | [diff] [blame] | 791 | if ((common->ctrl_fd = skt_connect( |
Andy Hung | f95d27b | 2017-05-01 15:04:57 -0700 | [diff] [blame] | 792 | A2DP_CTRL_PATH, AUDIO_STREAM_CONTROL_OUTPUT_BUFFER_SZ)) >= 0) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 793 | /* success, now check if stack is ready */ |
| 794 | if (check_a2dp_ready(common) == 0) break; |
Mike Lockwood | 3e8a242f | 2014-05-23 12:42:24 -0700 | [diff] [blame] | 795 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 796 | ERROR("error : a2dp not ready, wait 250 ms and retry"); |
| 797 | usleep(250000); |
| 798 | skt_disconnect(common->ctrl_fd); |
| 799 | common->ctrl_fd = AUDIO_SKT_DISCONNECTED; |
Mike Lockwood | 3e8a242f | 2014-05-23 12:42:24 -0700 | [diff] [blame] | 800 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 801 | |
| 802 | /* ctrl channel not ready, wait a bit */ |
| 803 | usleep(250000); |
| 804 | } |
Mike Lockwood | 3e8a242f | 2014-05-23 12:42:24 -0700 | [diff] [blame] | 805 | } |
| 806 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 807 | /***************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 808 | * |
| 809 | * AUDIO DATA PATH |
| 810 | * |
| 811 | ****************************************************************************/ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 812 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 813 | static void a2dp_stream_common_init(struct a2dp_stream_common* common) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 814 | FNLOG(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 815 | |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 816 | common->mutex = new std::recursive_mutex; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 817 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 818 | common->ctrl_fd = AUDIO_SKT_DISCONNECTED; |
| 819 | common->audio_fd = AUDIO_SKT_DISCONNECTED; |
| 820 | common->state = AUDIO_A2DP_STATE_STOPPED; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 821 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 822 | /* manages max capacity of socket pipe */ |
| 823 | common->buffer_sz = AUDIO_STREAM_OUTPUT_BUFFER_SZ; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 824 | } |
| 825 | |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 826 | static void a2dp_stream_common_destroy(struct a2dp_stream_common* common) { |
| 827 | FNLOG(); |
| 828 | |
| 829 | delete common->mutex; |
| 830 | common->mutex = NULL; |
| 831 | } |
| 832 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 833 | static int start_audio_datapath(struct a2dp_stream_common* common) { |
| 834 | INFO("state %d", common->state); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 835 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 836 | int oldstate = common->state; |
| 837 | common->state = AUDIO_A2DP_STATE_STARTING; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 838 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 839 | int a2dp_status = a2dp_command(common, A2DP_CTRL_CMD_START); |
| 840 | if (a2dp_status < 0) { |
| 841 | ERROR("Audiopath start failed (status %d)", a2dp_status); |
| 842 | goto error; |
| 843 | } else if (a2dp_status == A2DP_CTRL_ACK_INCALL_FAILURE) { |
| 844 | ERROR("Audiopath start failed - in call, move to suspended"); |
| 845 | goto error; |
| 846 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 847 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 848 | /* connect socket if not yet connected */ |
| 849 | if (common->audio_fd == AUDIO_SKT_DISCONNECTED) { |
| 850 | common->audio_fd = skt_connect(A2DP_DATA_PATH, common->buffer_sz); |
| 851 | if (common->audio_fd < 0) { |
| 852 | ERROR("Audiopath start failed - error opening data socket"); |
| 853 | goto error; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 854 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 855 | } |
| 856 | common->state = (a2dp_state_t)AUDIO_A2DP_STATE_STARTED; |
Ajay Panicker | ef673c5 | 2016-12-21 15:37:34 -0800 | [diff] [blame] | 857 | |
| 858 | /* check to see if delay reporting is enabled */ |
| 859 | enable_delay_reporting = delay_reporting_enabled(); |
| 860 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 861 | return 0; |
Eric Laurent | 7a599b4 | 2016-05-11 18:18:54 -0700 | [diff] [blame] | 862 | |
| 863 | error: |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 864 | common->state = (a2dp_state_t)oldstate; |
| 865 | return -1; |
| 866 | } |
| 867 | |
| 868 | static int stop_audio_datapath(struct a2dp_stream_common* common) { |
| 869 | int oldstate = common->state; |
| 870 | |
| 871 | INFO("state %d", common->state); |
| 872 | |
| 873 | /* prevent any stray output writes from autostarting the stream |
| 874 | while stopping audiopath */ |
| 875 | common->state = AUDIO_A2DP_STATE_STOPPING; |
| 876 | |
| 877 | if (a2dp_command(common, A2DP_CTRL_CMD_STOP) < 0) { |
| 878 | ERROR("audiopath stop failed"); |
Jakub Pawlowski | 6a05951 | 2016-05-25 17:49:32 -0700 | [diff] [blame] | 879 | common->state = (a2dp_state_t)oldstate; |
Eric Laurent | 7a599b4 | 2016-05-11 18:18:54 -0700 | [diff] [blame] | 880 | return -1; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | common->state = (a2dp_state_t)AUDIO_A2DP_STATE_STOPPED; |
| 884 | |
| 885 | /* disconnect audio path */ |
| 886 | skt_disconnect(common->audio_fd); |
| 887 | common->audio_fd = AUDIO_SKT_DISCONNECTED; |
| 888 | |
| 889 | return 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 890 | } |
| 891 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 892 | static int suspend_audio_datapath(struct a2dp_stream_common* common, |
| 893 | bool standby) { |
| 894 | INFO("state %d", common->state); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 895 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 896 | if (common->state == AUDIO_A2DP_STATE_STOPPING) return -1; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 897 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 898 | if (a2dp_command(common, A2DP_CTRL_CMD_SUSPEND) < 0) return -1; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 899 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 900 | if (standby) |
| 901 | common->state = AUDIO_A2DP_STATE_STANDBY; |
| 902 | else |
| 903 | common->state = AUDIO_A2DP_STATE_SUSPENDED; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 904 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 905 | /* disconnect audio path */ |
| 906 | skt_disconnect(common->audio_fd); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 907 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 908 | common->audio_fd = AUDIO_SKT_DISCONNECTED; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 909 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 910 | return 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 911 | } |
| 912 | |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 913 | /***************************************************************************** |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 914 | * |
| 915 | * audio output callbacks |
| 916 | * |
| 917 | ****************************************************************************/ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 918 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 919 | static ssize_t out_write(struct audio_stream_out* stream, const void* buffer, |
| 920 | size_t bytes) { |
| 921 | struct a2dp_stream_out* out = (struct a2dp_stream_out*)stream; |
| 922 | int sent = -1; |
Pavlin Radoslavov | 95e81d0 | 2017-10-29 11:35:22 -0700 | [diff] [blame] | 923 | size_t write_bytes = bytes; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 924 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 925 | DEBUG("write %zu bytes (fd %d)", bytes, out->common.audio_fd); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 926 | |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 927 | std::unique_lock<std::recursive_mutex> lock(*out->common.mutex); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 928 | if (out->common.state == AUDIO_A2DP_STATE_SUSPENDED || |
| 929 | out->common.state == AUDIO_A2DP_STATE_STOPPING) { |
| 930 | DEBUG("stream suspended or closing"); |
| 931 | goto finish; |
| 932 | } |
| 933 | |
| 934 | /* only allow autostarting if we are in stopped or standby */ |
| 935 | if ((out->common.state == AUDIO_A2DP_STATE_STOPPED) || |
| 936 | (out->common.state == AUDIO_A2DP_STATE_STANDBY)) { |
| 937 | if (start_audio_datapath(&out->common) < 0) { |
| 938 | goto finish; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 939 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 940 | } else if (out->common.state != AUDIO_A2DP_STATE_STARTED) { |
| 941 | ERROR("stream not in stopped or standby"); |
| 942 | goto finish; |
| 943 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 944 | |
Pavlin Radoslavov | 95e81d0 | 2017-10-29 11:35:22 -0700 | [diff] [blame] | 945 | // Mix the stereo into mono if necessary |
| 946 | if (out->common.cfg.is_stereo_to_mono) { |
| 947 | const size_t frames = bytes / audio_stream_out_frame_size(stream); |
| 948 | int16_t* src = (int16_t*)buffer; |
| 949 | int16_t* dst = (int16_t*)buffer; |
| 950 | for (size_t i = 0; i < frames; i++, dst++, src += 2) { |
| 951 | *dst = (int16_t)(((int32_t)src[0] + (int32_t)src[1]) >> 1); |
| 952 | } |
| 953 | write_bytes /= 2; |
| 954 | DEBUG("stereo-to-mono mixing: write %zu bytes (fd %d)", write_bytes, |
| 955 | out->common.audio_fd); |
| 956 | } |
| 957 | |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 958 | lock.unlock(); |
Pavlin Radoslavov | 95e81d0 | 2017-10-29 11:35:22 -0700 | [diff] [blame] | 959 | sent = skt_write(out->common.audio_fd, buffer, write_bytes); |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 960 | lock.lock(); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 961 | |
| 962 | if (sent == -1) { |
| 963 | skt_disconnect(out->common.audio_fd); |
| 964 | out->common.audio_fd = AUDIO_SKT_DISCONNECTED; |
| 965 | if ((out->common.state != AUDIO_A2DP_STATE_SUSPENDED) && |
| 966 | (out->common.state != AUDIO_A2DP_STATE_STOPPING)) { |
| 967 | out->common.state = AUDIO_A2DP_STATE_STOPPED; |
| 968 | } else { |
| 969 | ERROR("write failed : stream suspended, avoid resetting state"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 970 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 971 | goto finish; |
| 972 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 973 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 974 | finish:; |
| 975 | const size_t frames = bytes / audio_stream_out_frame_size(stream); |
| 976 | out->frames_rendered += frames; |
| 977 | out->frames_presented += frames; |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 978 | lock.unlock(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 979 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 980 | // If send didn't work out, sleep to emulate write delay. |
| 981 | if (sent == -1) { |
| 982 | const int us_delay = calc_audiotime_usec(out->common.cfg, bytes); |
| 983 | DEBUG("emulate a2dp write delay (%d us)", us_delay); |
| 984 | usleep(us_delay); |
| 985 | } |
| 986 | return bytes; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 987 | } |
| 988 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 989 | static uint32_t out_get_sample_rate(const struct audio_stream* stream) { |
| 990 | struct a2dp_stream_out* out = (struct a2dp_stream_out*)stream; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 991 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 992 | DEBUG("rate %" PRIu32, out->common.cfg.rate); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 993 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 994 | return out->common.cfg.rate; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 995 | } |
| 996 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 997 | static int out_set_sample_rate(struct audio_stream* stream, uint32_t rate) { |
| 998 | struct a2dp_stream_out* out = (struct a2dp_stream_out*)stream; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 999 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1000 | DEBUG("out_set_sample_rate : %" PRIu32, rate); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1001 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1002 | out->common.cfg.rate = rate; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1003 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1004 | return 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1005 | } |
| 1006 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1007 | static size_t out_get_buffer_size(const struct audio_stream* stream) { |
| 1008 | struct a2dp_stream_out* out = (struct a2dp_stream_out*)stream; |
| 1009 | // period_size is the AudioFlinger mixer buffer size. |
| 1010 | const size_t period_size = |
| 1011 | out->common.buffer_sz / AUDIO_STREAM_OUTPUT_BUFFER_PERIODS; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1012 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1013 | DEBUG("socket buffer size: %zu period size: %zu", out->common.buffer_sz, |
| 1014 | period_size); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1015 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1016 | return period_size; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1017 | } |
| 1018 | |
Pavlin Radoslavov | bdd34d2 | 2017-04-05 16:18:26 -0700 | [diff] [blame] | 1019 | size_t audio_a2dp_hw_stream_compute_buffer_size( |
Pavlin Radoslavov | ae9db0f | 2017-04-04 11:31:29 -0700 | [diff] [blame] | 1020 | btav_a2dp_codec_sample_rate_t codec_sample_rate, |
| 1021 | btav_a2dp_codec_bits_per_sample_t codec_bits_per_sample, |
| 1022 | btav_a2dp_codec_channel_mode_t codec_channel_mode) { |
| 1023 | size_t buffer_sz = AUDIO_STREAM_OUTPUT_BUFFER_SZ; // Default value |
Pavlin Radoslavov | bdd34d2 | 2017-04-05 16:18:26 -0700 | [diff] [blame] | 1024 | const uint64_t time_period_ms = 20; // Conservative 20ms |
Pavlin Radoslavov | ae9db0f | 2017-04-04 11:31:29 -0700 | [diff] [blame] | 1025 | uint32_t sample_rate; |
| 1026 | uint32_t bits_per_sample; |
| 1027 | uint32_t number_of_channels; |
| 1028 | |
| 1029 | // Check the codec config sample rate |
| 1030 | switch (codec_sample_rate) { |
| 1031 | case BTAV_A2DP_CODEC_SAMPLE_RATE_44100: |
| 1032 | sample_rate = 44100; |
| 1033 | break; |
| 1034 | case BTAV_A2DP_CODEC_SAMPLE_RATE_48000: |
| 1035 | sample_rate = 48000; |
| 1036 | break; |
| 1037 | case BTAV_A2DP_CODEC_SAMPLE_RATE_88200: |
| 1038 | sample_rate = 88200; |
| 1039 | break; |
| 1040 | case BTAV_A2DP_CODEC_SAMPLE_RATE_96000: |
| 1041 | sample_rate = 96000; |
| 1042 | break; |
| 1043 | case BTAV_A2DP_CODEC_SAMPLE_RATE_176400: |
| 1044 | sample_rate = 176400; |
| 1045 | break; |
| 1046 | case BTAV_A2DP_CODEC_SAMPLE_RATE_192000: |
| 1047 | sample_rate = 192000; |
| 1048 | break; |
| 1049 | case BTAV_A2DP_CODEC_SAMPLE_RATE_NONE: |
| 1050 | default: |
| 1051 | ERROR("Invalid sample rate: 0x%x", codec_sample_rate); |
| 1052 | return buffer_sz; |
| 1053 | } |
| 1054 | |
| 1055 | // Check the codec config bits per sample |
| 1056 | switch (codec_bits_per_sample) { |
| 1057 | case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16: |
| 1058 | bits_per_sample = 16; |
| 1059 | break; |
| 1060 | case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24: |
| 1061 | bits_per_sample = 24; |
| 1062 | break; |
| 1063 | case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32: |
| 1064 | bits_per_sample = 32; |
| 1065 | break; |
| 1066 | case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE: |
| 1067 | default: |
| 1068 | ERROR("Invalid bits per sample: 0x%x", codec_bits_per_sample); |
| 1069 | return buffer_sz; |
| 1070 | } |
| 1071 | |
| 1072 | // Check the codec config channel mode |
| 1073 | switch (codec_channel_mode) { |
| 1074 | case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO: |
| 1075 | number_of_channels = 1; |
| 1076 | break; |
| 1077 | case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO: |
| 1078 | number_of_channels = 2; |
| 1079 | break; |
| 1080 | case BTAV_A2DP_CODEC_CHANNEL_MODE_NONE: |
| 1081 | default: |
| 1082 | ERROR("Invalid channel mode: 0x%x", codec_channel_mode); |
| 1083 | return buffer_sz; |
| 1084 | } |
| 1085 | |
| 1086 | // |
| 1087 | // The buffer size is computed by using the following formula: |
| 1088 | // |
| 1089 | // AUDIO_STREAM_OUTPUT_BUFFER_SIZE = |
| 1090 | // (TIME_PERIOD_MS * AUDIO_STREAM_OUTPUT_BUFFER_PERIODS * |
| 1091 | // SAMPLE_RATE_HZ * NUMBER_OF_CHANNELS * (BITS_PER_SAMPLE / 8)) / 1000 |
| 1092 | // |
| 1093 | // AUDIO_STREAM_OUTPUT_BUFFER_PERIODS controls how the socket buffer is |
| 1094 | // divided for AudioFlinger data delivery. The AudioFlinger mixer delivers |
| 1095 | // data in chunks of |
| 1096 | // (AUDIO_STREAM_OUTPUT_BUFFER_SIZE / AUDIO_STREAM_OUTPUT_BUFFER_PERIODS) . |
| 1097 | // If the number of periods is 2, the socket buffer represents "double |
| 1098 | // buffering" of the AudioFlinger mixer buffer. |
| 1099 | // |
| 1100 | // Furthermore, the AudioFlinger expects the buffer size to be a multiple |
| 1101 | // of 16 frames. |
| 1102 | const size_t divisor = (AUDIO_STREAM_OUTPUT_BUFFER_PERIODS * 16 * |
| 1103 | number_of_channels * bits_per_sample) / |
| 1104 | 8; |
| 1105 | |
| 1106 | buffer_sz = (time_period_ms * AUDIO_STREAM_OUTPUT_BUFFER_PERIODS * |
| 1107 | sample_rate * number_of_channels * (bits_per_sample / 8)) / |
| 1108 | 1000; |
| 1109 | |
| 1110 | // Adjust the buffer size so it can be divided by the divisor |
| 1111 | const size_t remainder = buffer_sz % divisor; |
| 1112 | if (remainder != 0) { |
| 1113 | buffer_sz += divisor - remainder; |
| 1114 | } |
| 1115 | |
| 1116 | return buffer_sz; |
| 1117 | } |
| 1118 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1119 | static uint32_t out_get_channels(const struct audio_stream* stream) { |
| 1120 | struct a2dp_stream_out* out = (struct a2dp_stream_out*)stream; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1121 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1122 | DEBUG("channels 0x%" PRIx32, out->common.cfg.channel_mask); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1123 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1124 | return out->common.cfg.channel_mask; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1125 | } |
| 1126 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1127 | static audio_format_t out_get_format(const struct audio_stream* stream) { |
| 1128 | struct a2dp_stream_out* out = (struct a2dp_stream_out*)stream; |
| 1129 | DEBUG("format 0x%x", out->common.cfg.format); |
| 1130 | return (audio_format_t)out->common.cfg.format; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1131 | } |
| 1132 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1133 | static int out_set_format(UNUSED_ATTR struct audio_stream* stream, |
| 1134 | UNUSED_ATTR audio_format_t format) { |
| 1135 | DEBUG("setting format not yet supported (0x%x)", format); |
| 1136 | return -ENOSYS; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1137 | } |
| 1138 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1139 | static int out_standby(struct audio_stream* stream) { |
| 1140 | struct a2dp_stream_out* out = (struct a2dp_stream_out*)stream; |
| 1141 | int retVal = 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1142 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1143 | FNLOG(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1144 | |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 1145 | std::lock_guard<std::recursive_mutex> lock(*out->common.mutex); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1146 | // Do nothing in SUSPENDED state. |
| 1147 | if (out->common.state != AUDIO_A2DP_STATE_SUSPENDED) |
| 1148 | retVal = suspend_audio_datapath(&out->common, true); |
| 1149 | out->frames_rendered = 0; // rendered is reset, presented is not |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1150 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1151 | return retVal; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1152 | } |
| 1153 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1154 | static int out_dump(UNUSED_ATTR const struct audio_stream* stream, |
| 1155 | UNUSED_ATTR int fd) { |
| 1156 | FNLOG(); |
| 1157 | return 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1158 | } |
| 1159 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1160 | static int out_set_parameters(struct audio_stream* stream, |
| 1161 | const char* kvpairs) { |
| 1162 | struct a2dp_stream_out* out = (struct a2dp_stream_out*)stream; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1163 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1164 | INFO("state %d kvpairs %s", out->common.state, kvpairs); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1165 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1166 | std::unordered_map<std::string, std::string> params = |
| 1167 | hash_map_utils_new_from_string_params(kvpairs); |
| 1168 | int status = 0; |
Miao Chou | 118242b | 2015-07-29 14:12:11 -0700 | [diff] [blame] | 1169 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1170 | if (params.empty()) return status; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1171 | |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 1172 | std::lock_guard<std::recursive_mutex> lock(*out->common.mutex); |
Miao Chou | 118242b | 2015-07-29 14:12:11 -0700 | [diff] [blame] | 1173 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1174 | /* dump params */ |
| 1175 | hash_map_utils_dump_string_keys_string_values(params); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1176 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1177 | if (params["closing"].compare("true") == 0) { |
| 1178 | DEBUG("stream closing, disallow any writes"); |
| 1179 | out->common.state = AUDIO_A2DP_STATE_STOPPING; |
| 1180 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1181 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1182 | if (params["A2dpSuspended"].compare("true") == 0) { |
| 1183 | if (out->common.state == AUDIO_A2DP_STATE_STARTED) |
| 1184 | status = suspend_audio_datapath(&out->common, false); |
| 1185 | } else { |
| 1186 | /* Do not start the streaming automatically. If the phone was streaming |
| 1187 | * prior to being suspended, the next out_write shall trigger the |
| 1188 | * AVDTP start procedure */ |
| 1189 | if (out->common.state == AUDIO_A2DP_STATE_SUSPENDED) |
| 1190 | out->common.state = AUDIO_A2DP_STATE_STANDBY; |
| 1191 | /* Irrespective of the state, return 0 */ |
| 1192 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1193 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1194 | return status; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1195 | } |
| 1196 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1197 | static char* out_get_parameters(const struct audio_stream* stream, |
| 1198 | const char* keys) { |
| 1199 | FNLOG(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1200 | |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 1201 | btav_a2dp_codec_config_t codec_config; |
| 1202 | btav_a2dp_codec_config_t codec_capability; |
| 1203 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1204 | struct a2dp_stream_out* out = (struct a2dp_stream_out*)stream; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1205 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1206 | std::unordered_map<std::string, std::string> params = |
| 1207 | hash_map_utils_new_from_string_params(keys); |
| 1208 | std::unordered_map<std::string, std::string> return_params; |
Pavlin Radoslavov | 4ebaa86 | 2016-11-22 11:42:11 -0800 | [diff] [blame] | 1209 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1210 | if (params.empty()) return strdup(""); |
Pavlin Radoslavov | 4ebaa86 | 2016-11-22 11:42:11 -0800 | [diff] [blame] | 1211 | |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 1212 | std::lock_guard<std::recursive_mutex> lock(*out->common.mutex); |
Pavlin Radoslavov | 4ebaa86 | 2016-11-22 11:42:11 -0800 | [diff] [blame] | 1213 | |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 1214 | if (a2dp_read_output_audio_config(&out->common, &codec_config, |
| 1215 | &codec_capability, |
| 1216 | false /* update_stream_config */) < 0) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1217 | ERROR("a2dp_read_output_audio_config failed"); |
| 1218 | goto done; |
| 1219 | } |
Pavlin Radoslavov | 4ebaa86 | 2016-11-22 11:42:11 -0800 | [diff] [blame] | 1220 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1221 | // Add the format |
| 1222 | if (params.find(AUDIO_PARAMETER_STREAM_SUP_FORMATS) != params.end()) { |
| 1223 | std::string param; |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 1224 | if (codec_capability.bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16) { |
| 1225 | if (!param.empty()) param += "|"; |
| 1226 | param += "AUDIO_FORMAT_PCM_16_BIT"; |
Pavlin Radoslavov | 4ebaa86 | 2016-11-22 11:42:11 -0800 | [diff] [blame] | 1227 | } |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 1228 | if (codec_capability.bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24) { |
| 1229 | if (!param.empty()) param += "|"; |
| 1230 | param += "AUDIO_FORMAT_PCM_24_BIT_PACKED"; |
| 1231 | } |
| 1232 | if (codec_capability.bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32) { |
| 1233 | if (!param.empty()) param += "|"; |
| 1234 | param += "AUDIO_FORMAT_PCM_32_BIT"; |
| 1235 | } |
| 1236 | if (param.empty()) { |
| 1237 | ERROR("Invalid codec capability bits_per_sample=0x%x", |
| 1238 | codec_capability.bits_per_sample); |
| 1239 | goto done; |
| 1240 | } else { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1241 | return_params[AUDIO_PARAMETER_STREAM_SUP_FORMATS] = param; |
| 1242 | } |
| 1243 | } |
Pavlin Radoslavov | 4ebaa86 | 2016-11-22 11:42:11 -0800 | [diff] [blame] | 1244 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1245 | // Add the sample rate |
| 1246 | if (params.find(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES) != params.end()) { |
| 1247 | std::string param; |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 1248 | if (codec_capability.sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_44100) { |
| 1249 | if (!param.empty()) param += "|"; |
| 1250 | param += "44100"; |
Pavlin Radoslavov | 4ebaa86 | 2016-11-22 11:42:11 -0800 | [diff] [blame] | 1251 | } |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 1252 | if (codec_capability.sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_48000) { |
| 1253 | if (!param.empty()) param += "|"; |
| 1254 | param += "48000"; |
| 1255 | } |
| 1256 | if (codec_capability.sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_88200) { |
| 1257 | if (!param.empty()) param += "|"; |
| 1258 | param += "88200"; |
| 1259 | } |
| 1260 | if (codec_capability.sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_96000) { |
| 1261 | if (!param.empty()) param += "|"; |
| 1262 | param += "96000"; |
| 1263 | } |
| 1264 | if (codec_capability.sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_176400) { |
| 1265 | if (!param.empty()) param += "|"; |
| 1266 | param += "176400"; |
| 1267 | } |
| 1268 | if (codec_capability.sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_192000) { |
| 1269 | if (!param.empty()) param += "|"; |
| 1270 | param += "192000"; |
| 1271 | } |
| 1272 | if (param.empty()) { |
| 1273 | ERROR("Invalid codec capability sample_rate=0x%x", |
| 1274 | codec_capability.sample_rate); |
| 1275 | goto done; |
| 1276 | } else { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1277 | return_params[AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES] = param; |
| 1278 | } |
| 1279 | } |
Pavlin Radoslavov | 4ebaa86 | 2016-11-22 11:42:11 -0800 | [diff] [blame] | 1280 | |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 1281 | // Add the channel mask |
| 1282 | if (params.find(AUDIO_PARAMETER_STREAM_SUP_CHANNELS) != params.end()) { |
| 1283 | std::string param; |
| 1284 | if (codec_capability.channel_mode & BTAV_A2DP_CODEC_CHANNEL_MODE_MONO) { |
| 1285 | if (!param.empty()) param += "|"; |
| 1286 | param += "AUDIO_CHANNEL_OUT_MONO"; |
| 1287 | } |
| 1288 | if (codec_capability.channel_mode & BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO) { |
| 1289 | if (!param.empty()) param += "|"; |
| 1290 | param += "AUDIO_CHANNEL_OUT_STEREO"; |
| 1291 | } |
| 1292 | if (param.empty()) { |
| 1293 | ERROR("Invalid codec capability channel_mode=0x%x", |
| 1294 | codec_capability.channel_mode); |
| 1295 | goto done; |
| 1296 | } else { |
| 1297 | return_params[AUDIO_PARAMETER_STREAM_SUP_CHANNELS] = param; |
| 1298 | } |
| 1299 | } |
| 1300 | |
Pavlin Radoslavov | 4ebaa86 | 2016-11-22 11:42:11 -0800 | [diff] [blame] | 1301 | done: |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1302 | std::string result; |
| 1303 | for (const auto& ptr : return_params) { |
| 1304 | result += ptr.first + "=" + ptr.second + ";"; |
| 1305 | } |
Pavlin Radoslavov | 4ebaa86 | 2016-11-22 11:42:11 -0800 | [diff] [blame] | 1306 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1307 | INFO("get parameters result = %s", result.c_str()); |
Pavlin Radoslavov | 4ebaa86 | 2016-11-22 11:42:11 -0800 | [diff] [blame] | 1308 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1309 | return strdup(result.c_str()); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1310 | } |
| 1311 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1312 | static uint32_t out_get_latency(const struct audio_stream_out* stream) { |
| 1313 | int latency_us; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1314 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1315 | struct a2dp_stream_out* out = (struct a2dp_stream_out*)stream; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1316 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1317 | FNLOG(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1318 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1319 | latency_us = |
| 1320 | ((out->common.buffer_sz * 1000) / |
| 1321 | audio_stream_out_frame_size(&out->stream) / out->common.cfg.rate) * |
| 1322 | 1000; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1323 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1324 | return (latency_us / 1000) + 200; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1325 | } |
| 1326 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1327 | static int out_set_volume(UNUSED_ATTR struct audio_stream_out* stream, |
| 1328 | UNUSED_ATTR float left, UNUSED_ATTR float right) { |
| 1329 | FNLOG(); |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 1330 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1331 | /* volume controlled in audioflinger mixer (digital) */ |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1332 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1333 | return -ENOSYS; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1334 | } |
| 1335 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1336 | static int out_get_presentation_position(const struct audio_stream_out* stream, |
| 1337 | uint64_t* frames, |
| 1338 | struct timespec* timestamp) { |
| 1339 | struct a2dp_stream_out* out = (struct a2dp_stream_out*)stream; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1340 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1341 | FNLOG(); |
| 1342 | if (stream == NULL || frames == NULL || timestamp == NULL) return -EINVAL; |
Eric Laurent | e1eb6c4 | 2015-05-26 15:50:29 -0700 | [diff] [blame] | 1343 | |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 1344 | std::lock_guard<std::recursive_mutex> lock(*out->common.mutex); |
Ajay Panicker | ef673c5 | 2016-12-21 15:37:34 -0800 | [diff] [blame] | 1345 | |
| 1346 | // bytes is the total number of bytes sent by the Bluetooth stack to a |
| 1347 | // remote headset |
| 1348 | uint64_t bytes = 0; |
| 1349 | |
| 1350 | // delay_report is the audio delay from the remote headset receiving data to |
| 1351 | // the headset playing sound in units of 1/10ms |
| 1352 | uint16_t delay_report = 0; |
| 1353 | |
| 1354 | // If for some reason getting a delay fails or delay reports are disabled, |
| 1355 | // default to old delay |
| 1356 | if (enable_delay_reporting && |
| 1357 | a2dp_get_presentation_position_cmd(&out->common, &bytes, &delay_report, |
| 1358 | timestamp) == 0) { |
| 1359 | uint64_t delay_ns = delay_report * DELAY_TO_NS; |
| 1360 | if (delay_ns > MIN_DELAY_MS * MS_TO_NS && |
| 1361 | delay_ns < MAX_DELAY_MS * MS_TO_NS) { |
| 1362 | *frames = bytes / audio_stream_out_frame_size(stream); |
| 1363 | |
| 1364 | timestamp->tv_nsec += delay_ns; |
| 1365 | if (timestamp->tv_nsec > 1 * SEC_TO_NS) { |
| 1366 | timestamp->tv_sec++; |
| 1367 | timestamp->tv_nsec -= SEC_TO_NS; |
| 1368 | } |
| 1369 | return 0; |
| 1370 | } |
| 1371 | } |
| 1372 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1373 | uint64_t latency_frames = |
| 1374 | (uint64_t)out_get_latency(stream) * out->common.cfg.rate / 1000; |
| 1375 | if (out->frames_presented >= latency_frames) { |
Ajay Panicker | ef673c5 | 2016-12-21 15:37:34 -0800 | [diff] [blame] | 1376 | clock_gettime(CLOCK_MONOTONIC, timestamp); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1377 | *frames = out->frames_presented - latency_frames; |
Ajay Panicker | ef673c5 | 2016-12-21 15:37:34 -0800 | [diff] [blame] | 1378 | return 0; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1379 | } |
Ajay Panicker | ef673c5 | 2016-12-21 15:37:34 -0800 | [diff] [blame] | 1380 | |
| 1381 | return -EWOULDBLOCK; |
Eric Laurent | e1eb6c4 | 2015-05-26 15:50:29 -0700 | [diff] [blame] | 1382 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1383 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1384 | static int out_get_render_position(const struct audio_stream_out* stream, |
| 1385 | uint32_t* dsp_frames) { |
| 1386 | struct a2dp_stream_out* out = (struct a2dp_stream_out*)stream; |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 1387 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1388 | FNLOG(); |
| 1389 | if (stream == NULL || dsp_frames == NULL) return -EINVAL; |
Eric Laurent | e1eb6c4 | 2015-05-26 15:50:29 -0700 | [diff] [blame] | 1390 | |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 1391 | std::lock_guard<std::recursive_mutex> lock(*out->common.mutex); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1392 | uint64_t latency_frames = |
| 1393 | (uint64_t)out_get_latency(stream) * out->common.cfg.rate / 1000; |
| 1394 | if (out->frames_rendered >= latency_frames) { |
| 1395 | *dsp_frames = (uint32_t)(out->frames_rendered - latency_frames); |
| 1396 | } else { |
| 1397 | *dsp_frames = 0; |
| 1398 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1399 | return 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1400 | } |
| 1401 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1402 | static int out_add_audio_effect(UNUSED_ATTR const struct audio_stream* stream, |
| 1403 | UNUSED_ATTR effect_handle_t effect) { |
| 1404 | FNLOG(); |
| 1405 | return 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1406 | } |
| 1407 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1408 | static int out_remove_audio_effect( |
| 1409 | UNUSED_ATTR const struct audio_stream* stream, |
| 1410 | UNUSED_ATTR effect_handle_t effect) { |
| 1411 | FNLOG(); |
| 1412 | return 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | /* |
| 1416 | * AUDIO INPUT STREAM |
| 1417 | */ |
| 1418 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1419 | static uint32_t in_get_sample_rate(const struct audio_stream* stream) { |
| 1420 | struct a2dp_stream_in* in = (struct a2dp_stream_in*)stream; |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 1421 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1422 | FNLOG(); |
| 1423 | return in->common.cfg.rate; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1424 | } |
| 1425 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1426 | static int in_set_sample_rate(struct audio_stream* stream, uint32_t rate) { |
| 1427 | struct a2dp_stream_in* in = (struct a2dp_stream_in*)stream; |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 1428 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1429 | FNLOG(); |
Mike Lockwood | 3e8a242f | 2014-05-23 12:42:24 -0700 | [diff] [blame] | 1430 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1431 | if (in->common.cfg.rate > 0 && in->common.cfg.rate == rate) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1432 | return 0; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1433 | else |
| 1434 | return -1; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1435 | } |
| 1436 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1437 | static size_t in_get_buffer_size( |
| 1438 | UNUSED_ATTR const struct audio_stream* stream) { |
| 1439 | FNLOG(); |
| 1440 | return 320; |
| 1441 | } |
| 1442 | |
| 1443 | static uint32_t in_get_channels(const struct audio_stream* stream) { |
| 1444 | struct a2dp_stream_in* in = (struct a2dp_stream_in*)stream; |
| 1445 | |
| 1446 | FNLOG(); |
| 1447 | return in->common.cfg.channel_mask; |
| 1448 | } |
| 1449 | |
| 1450 | static audio_format_t in_get_format( |
| 1451 | UNUSED_ATTR const struct audio_stream* stream) { |
| 1452 | FNLOG(); |
| 1453 | return AUDIO_FORMAT_PCM_16_BIT; |
| 1454 | } |
| 1455 | |
| 1456 | static int in_set_format(UNUSED_ATTR struct audio_stream* stream, |
| 1457 | UNUSED_ATTR audio_format_t format) { |
| 1458 | FNLOG(); |
| 1459 | if (format == AUDIO_FORMAT_PCM_16_BIT) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1460 | return 0; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1461 | else |
| 1462 | return -1; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1463 | } |
| 1464 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1465 | static int in_standby(UNUSED_ATTR struct audio_stream* stream) { |
| 1466 | FNLOG(); |
| 1467 | return 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1468 | } |
| 1469 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1470 | static int in_dump(UNUSED_ATTR const struct audio_stream* stream, |
| 1471 | UNUSED_ATTR int fd) { |
| 1472 | FNLOG(); |
| 1473 | return 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1474 | } |
| 1475 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1476 | static int in_set_parameters(UNUSED_ATTR struct audio_stream* stream, |
| 1477 | UNUSED_ATTR const char* kvpairs) { |
| 1478 | FNLOG(); |
| 1479 | return 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1480 | } |
| 1481 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1482 | static char* in_get_parameters(UNUSED_ATTR const struct audio_stream* stream, |
| 1483 | UNUSED_ATTR const char* keys) { |
| 1484 | FNLOG(); |
| 1485 | return strdup(""); |
| 1486 | } |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 1487 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1488 | static int in_set_gain(UNUSED_ATTR struct audio_stream_in* stream, |
| 1489 | UNUSED_ATTR float gain) { |
| 1490 | FNLOG(); |
| 1491 | return 0; |
| 1492 | } |
Mike Lockwood | 3e8a242f | 2014-05-23 12:42:24 -0700 | [diff] [blame] | 1493 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1494 | static ssize_t in_read(struct audio_stream_in* stream, void* buffer, |
| 1495 | size_t bytes) { |
| 1496 | struct a2dp_stream_in* in = (struct a2dp_stream_in*)stream; |
| 1497 | int read; |
| 1498 | int us_delay; |
| 1499 | |
| 1500 | DEBUG("read %zu bytes, state: %d", bytes, in->common.state); |
| 1501 | |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 1502 | std::unique_lock<std::recursive_mutex> lock(*in->common.mutex); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1503 | if (in->common.state == AUDIO_A2DP_STATE_SUSPENDED || |
| 1504 | in->common.state == AUDIO_A2DP_STATE_STOPPING) { |
| 1505 | DEBUG("stream suspended"); |
| 1506 | goto error; |
| 1507 | } |
| 1508 | |
| 1509 | /* only allow autostarting if we are in stopped or standby */ |
| 1510 | if ((in->common.state == AUDIO_A2DP_STATE_STOPPED) || |
| 1511 | (in->common.state == AUDIO_A2DP_STATE_STANDBY)) { |
| 1512 | if (start_audio_datapath(&in->common) < 0) { |
| 1513 | goto error; |
Mike Lockwood | 3e8a242f | 2014-05-23 12:42:24 -0700 | [diff] [blame] | 1514 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1515 | } else if (in->common.state != AUDIO_A2DP_STATE_STARTED) { |
| 1516 | ERROR("stream not in stopped or standby"); |
| 1517 | goto error; |
| 1518 | } |
Mike Lockwood | 3e8a242f | 2014-05-23 12:42:24 -0700 | [diff] [blame] | 1519 | |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 1520 | lock.unlock(); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1521 | read = skt_read(in->common.audio_fd, buffer, bytes); |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 1522 | lock.lock(); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1523 | if (read == -1) { |
| 1524 | skt_disconnect(in->common.audio_fd); |
| 1525 | in->common.audio_fd = AUDIO_SKT_DISCONNECTED; |
| 1526 | if ((in->common.state != AUDIO_A2DP_STATE_SUSPENDED) && |
| 1527 | (in->common.state != AUDIO_A2DP_STATE_STOPPING)) { |
| 1528 | in->common.state = AUDIO_A2DP_STATE_STOPPED; |
| 1529 | } else { |
| 1530 | ERROR("read failed : stream suspended, avoid resetting state"); |
Mike Lockwood | 3e8a242f | 2014-05-23 12:42:24 -0700 | [diff] [blame] | 1531 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1532 | goto error; |
| 1533 | } else if (read == 0) { |
| 1534 | DEBUG("read time out - return zeros"); |
| 1535 | memset(buffer, 0, bytes); |
| 1536 | read = bytes; |
| 1537 | } |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 1538 | lock.unlock(); |
Mike Lockwood | 3e8a242f | 2014-05-23 12:42:24 -0700 | [diff] [blame] | 1539 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1540 | DEBUG("read %d bytes out of %zu bytes", read, bytes); |
| 1541 | return read; |
Eric Laurent | 7a599b4 | 2016-05-11 18:18:54 -0700 | [diff] [blame] | 1542 | |
| 1543 | error: |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1544 | memset(buffer, 0, bytes); |
| 1545 | us_delay = calc_audiotime_usec(in->common.cfg, bytes); |
| 1546 | DEBUG("emulate a2dp read delay (%d us)", us_delay); |
Eric Laurent | 7a599b4 | 2016-05-11 18:18:54 -0700 | [diff] [blame] | 1547 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1548 | usleep(us_delay); |
| 1549 | return bytes; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1550 | } |
| 1551 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1552 | static uint32_t in_get_input_frames_lost( |
| 1553 | UNUSED_ATTR struct audio_stream_in* stream) { |
| 1554 | FNLOG(); |
| 1555 | return 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1556 | } |
| 1557 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1558 | static int in_add_audio_effect(UNUSED_ATTR const struct audio_stream* stream, |
| 1559 | UNUSED_ATTR effect_handle_t effect) { |
| 1560 | FNLOG(); |
| 1561 | return 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1562 | } |
| 1563 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1564 | static int in_remove_audio_effect(UNUSED_ATTR const struct audio_stream* stream, |
| 1565 | UNUSED_ATTR effect_handle_t effect) { |
| 1566 | FNLOG(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1567 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1568 | return 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1569 | } |
| 1570 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1571 | static int adev_open_output_stream(struct audio_hw_device* dev, |
Myles Watson | d35a648 | 2016-10-27 08:52:16 -0700 | [diff] [blame] | 1572 | UNUSED_ATTR audio_io_handle_t handle, |
| 1573 | UNUSED_ATTR audio_devices_t devices, |
| 1574 | UNUSED_ATTR audio_output_flags_t flags, |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1575 | struct audio_config* config, |
| 1576 | struct audio_stream_out** stream_out, |
| 1577 | UNUSED_ATTR const char* address) |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1578 | |
| 1579 | { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1580 | struct a2dp_audio_device* a2dp_dev = (struct a2dp_audio_device*)dev; |
| 1581 | struct a2dp_stream_out* out; |
| 1582 | int ret = 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1583 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1584 | INFO("opening output"); |
Andy Hung | b1a900f | 2017-03-30 16:28:46 -0700 | [diff] [blame] | 1585 | // protect against adev->output and stream_out from being inconsistent |
| 1586 | std::lock_guard<std::recursive_mutex> lock(*a2dp_dev->mutex); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1587 | out = (struct a2dp_stream_out*)calloc(1, sizeof(struct a2dp_stream_out)); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1588 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1589 | if (!out) return -ENOMEM; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1590 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1591 | out->stream.common.get_sample_rate = out_get_sample_rate; |
| 1592 | out->stream.common.set_sample_rate = out_set_sample_rate; |
| 1593 | out->stream.common.get_buffer_size = out_get_buffer_size; |
| 1594 | out->stream.common.get_channels = out_get_channels; |
| 1595 | out->stream.common.get_format = out_get_format; |
| 1596 | out->stream.common.set_format = out_set_format; |
| 1597 | out->stream.common.standby = out_standby; |
| 1598 | out->stream.common.dump = out_dump; |
| 1599 | out->stream.common.set_parameters = out_set_parameters; |
| 1600 | out->stream.common.get_parameters = out_get_parameters; |
| 1601 | out->stream.common.add_audio_effect = out_add_audio_effect; |
| 1602 | out->stream.common.remove_audio_effect = out_remove_audio_effect; |
| 1603 | out->stream.get_latency = out_get_latency; |
| 1604 | out->stream.set_volume = out_set_volume; |
| 1605 | out->stream.write = out_write; |
| 1606 | out->stream.get_render_position = out_get_render_position; |
| 1607 | out->stream.get_presentation_position = out_get_presentation_position; |
Eric Laurent | e1eb6c4 | 2015-05-26 15:50:29 -0700 | [diff] [blame] | 1608 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1609 | /* initialize a2dp specifics */ |
| 1610 | a2dp_stream_common_init(&out->common); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1611 | |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 1612 | // Make sure we always have the feeding parameters configured |
| 1613 | btav_a2dp_codec_config_t codec_config; |
| 1614 | btav_a2dp_codec_config_t codec_capability; |
| 1615 | if (a2dp_read_output_audio_config(&out->common, &codec_config, |
| 1616 | &codec_capability, |
| 1617 | true /* update_stream_config */) < 0) { |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1618 | ERROR("a2dp_read_output_audio_config failed"); |
| 1619 | ret = -1; |
| 1620 | goto err_open; |
| 1621 | } |
Andy Hung | f95d27b | 2017-05-01 15:04:57 -0700 | [diff] [blame] | 1622 | // a2dp_read_output_audio_config() opens the socket control path (or fails) |
Mike Lockwood | 3e8a242f | 2014-05-23 12:42:24 -0700 | [diff] [blame] | 1623 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1624 | /* set output config values */ |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 1625 | if (config != nullptr) { |
| 1626 | // Try to use the config parameters and send it to the remote side |
| 1627 | // TODO: Shall we use out_set_format() and similar? |
| 1628 | if (config->format != 0) out->common.cfg.format = config->format; |
| 1629 | if (config->sample_rate != 0) out->common.cfg.rate = config->sample_rate; |
| 1630 | if (config->channel_mask != 0) |
| 1631 | out->common.cfg.channel_mask = config->channel_mask; |
| 1632 | if ((out->common.cfg.format != 0) || (out->common.cfg.rate != 0) || |
| 1633 | (out->common.cfg.channel_mask != 0)) { |
| 1634 | if (a2dp_write_output_audio_config(&out->common) < 0) { |
| 1635 | ERROR("a2dp_write_output_audio_config failed"); |
| 1636 | ret = -1; |
| 1637 | goto err_open; |
| 1638 | } |
| 1639 | // Read again and make sure we use the same parameters as the remote side |
| 1640 | if (a2dp_read_output_audio_config(&out->common, &codec_config, |
| 1641 | &codec_capability, |
| 1642 | true /* update_stream_config */) < 0) { |
| 1643 | ERROR("a2dp_read_output_audio_config failed"); |
| 1644 | ret = -1; |
| 1645 | goto err_open; |
| 1646 | } |
| 1647 | } |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1648 | config->format = out_get_format((const struct audio_stream*)&out->stream); |
| 1649 | config->sample_rate = |
| 1650 | out_get_sample_rate((const struct audio_stream*)&out->stream); |
| 1651 | config->channel_mask = |
| 1652 | out_get_channels((const struct audio_stream*)&out->stream); |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 1653 | |
Pavlin Radoslavov | ae9db0f | 2017-04-04 11:31:29 -0700 | [diff] [blame] | 1654 | INFO( |
| 1655 | "Output stream config: format=0x%x sample_rate=%d channel_mask=0x%x " |
| 1656 | "buffer_sz=%zu", |
| 1657 | config->format, config->sample_rate, config->channel_mask, |
| 1658 | out->common.buffer_sz); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1659 | } |
| 1660 | *stream_out = &out->stream; |
| 1661 | a2dp_dev->output = out; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1662 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1663 | DEBUG("success"); |
| 1664 | /* Delay to ensure Headset is in proper state when START is initiated from |
| 1665 | * DUT immediately after the connection due to ongoing music playback. */ |
| 1666 | usleep(250000); |
| 1667 | return 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1668 | |
| 1669 | err_open: |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 1670 | a2dp_stream_common_destroy(&out->common); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1671 | free(out); |
| 1672 | *stream_out = NULL; |
| 1673 | a2dp_dev->output = NULL; |
| 1674 | ERROR("failed"); |
| 1675 | return ret; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1676 | } |
| 1677 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1678 | static void adev_close_output_stream(struct audio_hw_device* dev, |
| 1679 | struct audio_stream_out* stream) { |
| 1680 | struct a2dp_audio_device* a2dp_dev = (struct a2dp_audio_device*)dev; |
| 1681 | struct a2dp_stream_out* out = (struct a2dp_stream_out*)stream; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1682 | |
Stanley Tng | 05cc6c7 | 2018-10-10 18:57:29 -0700 | [diff] [blame] | 1683 | INFO("%s: state %d", __func__, out->common.state); |
| 1684 | |
Andy Hung | b1a900f | 2017-03-30 16:28:46 -0700 | [diff] [blame] | 1685 | // prevent interference with adev_set_parameters. |
| 1686 | std::lock_guard<std::recursive_mutex> lock(*a2dp_dev->mutex); |
| 1687 | { |
| 1688 | std::lock_guard<std::recursive_mutex> lock(*out->common.mutex); |
| 1689 | const a2dp_state_t state = out->common.state; |
| 1690 | INFO("closing output (state %d)", (int)state); |
| 1691 | if ((state == AUDIO_A2DP_STATE_STARTED) || |
| 1692 | (state == AUDIO_A2DP_STATE_STOPPING)) { |
| 1693 | stop_audio_datapath(&out->common); |
| 1694 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1695 | |
Andy Hung | b1a900f | 2017-03-30 16:28:46 -0700 | [diff] [blame] | 1696 | skt_disconnect(out->common.ctrl_fd); |
| 1697 | out->common.ctrl_fd = AUDIO_SKT_DISCONNECTED; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1698 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1699 | |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 1700 | a2dp_stream_common_destroy(&out->common); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1701 | free(stream); |
| 1702 | a2dp_dev->output = NULL; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1703 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1704 | DEBUG("done"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1705 | } |
| 1706 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1707 | static int adev_set_parameters(struct audio_hw_device* dev, |
| 1708 | const char* kvpairs) { |
| 1709 | struct a2dp_audio_device* a2dp_dev = (struct a2dp_audio_device*)dev; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1710 | int retval = 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1711 | |
Andy Hung | b1a900f | 2017-03-30 16:28:46 -0700 | [diff] [blame] | 1712 | // prevent interference with adev_close_output_stream |
| 1713 | std::lock_guard<std::recursive_mutex> lock(*a2dp_dev->mutex); |
| 1714 | struct a2dp_stream_out* out = a2dp_dev->output; |
| 1715 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1716 | if (out == NULL) return retval; |
Eric Laurent | 71ac7a7 | 2014-10-29 17:17:31 -0700 | [diff] [blame] | 1717 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1718 | INFO("state %d", out->common.state); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1719 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1720 | retval = |
| 1721 | out->stream.common.set_parameters((struct audio_stream*)out, kvpairs); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1722 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1723 | return retval; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1724 | } |
| 1725 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1726 | static char* adev_get_parameters(UNUSED_ATTR const struct audio_hw_device* dev, |
| 1727 | const char* keys) { |
| 1728 | FNLOG(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1729 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1730 | std::unordered_map<std::string, std::string> params = |
| 1731 | hash_map_utils_new_from_string_params(keys); |
| 1732 | hash_map_utils_dump_string_keys_string_values(params); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1733 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1734 | return strdup(""); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1735 | } |
| 1736 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1737 | static int adev_init_check(UNUSED_ATTR const struct audio_hw_device* dev) { |
| 1738 | FNLOG(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1739 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1740 | return 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1741 | } |
| 1742 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1743 | static int adev_set_voice_volume(UNUSED_ATTR struct audio_hw_device* dev, |
| 1744 | UNUSED_ATTR float volume) { |
| 1745 | FNLOG(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1746 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1747 | return -ENOSYS; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1748 | } |
| 1749 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1750 | static int adev_set_master_volume(UNUSED_ATTR struct audio_hw_device* dev, |
| 1751 | UNUSED_ATTR float volume) { |
| 1752 | FNLOG(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1753 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1754 | return -ENOSYS; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1755 | } |
| 1756 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1757 | static int adev_set_mode(UNUSED_ATTR struct audio_hw_device* dev, |
| 1758 | UNUSED_ATTR audio_mode_t mode) { |
| 1759 | FNLOG(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1760 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1761 | return 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1762 | } |
| 1763 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1764 | static int adev_set_mic_mute(UNUSED_ATTR struct audio_hw_device* dev, |
| 1765 | UNUSED_ATTR bool state) { |
| 1766 | FNLOG(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1767 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1768 | return -ENOSYS; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1769 | } |
| 1770 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1771 | static int adev_get_mic_mute(UNUSED_ATTR const struct audio_hw_device* dev, |
| 1772 | UNUSED_ATTR bool* state) { |
| 1773 | FNLOG(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1774 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1775 | return -ENOSYS; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1776 | } |
| 1777 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1778 | static size_t adev_get_input_buffer_size( |
| 1779 | UNUSED_ATTR const struct audio_hw_device* dev, |
| 1780 | UNUSED_ATTR const struct audio_config* config) { |
| 1781 | FNLOG(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1782 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1783 | return 320; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1784 | } |
| 1785 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1786 | static int adev_open_input_stream(struct audio_hw_device* dev, |
Myles Watson | d35a648 | 2016-10-27 08:52:16 -0700 | [diff] [blame] | 1787 | UNUSED_ATTR audio_io_handle_t handle, |
| 1788 | UNUSED_ATTR audio_devices_t devices, |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1789 | UNUSED_ATTR struct audio_config* config, |
| 1790 | struct audio_stream_in** stream_in, |
Myles Watson | d35a648 | 2016-10-27 08:52:16 -0700 | [diff] [blame] | 1791 | UNUSED_ATTR audio_input_flags_t flags, |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1792 | UNUSED_ATTR const char* address, |
| 1793 | UNUSED_ATTR audio_source_t source) { |
| 1794 | struct a2dp_audio_device* a2dp_dev = (struct a2dp_audio_device*)dev; |
| 1795 | struct a2dp_stream_in* in; |
| 1796 | int ret; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1797 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1798 | FNLOG(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1799 | |
Andy Hung | b1a900f | 2017-03-30 16:28:46 -0700 | [diff] [blame] | 1800 | // protect against adev->input and stream_in from being inconsistent |
| 1801 | std::lock_guard<std::recursive_mutex> lock(*a2dp_dev->mutex); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1802 | in = (struct a2dp_stream_in*)calloc(1, sizeof(struct a2dp_stream_in)); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1803 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1804 | if (!in) return -ENOMEM; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1805 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1806 | in->stream.common.get_sample_rate = in_get_sample_rate; |
| 1807 | in->stream.common.set_sample_rate = in_set_sample_rate; |
| 1808 | in->stream.common.get_buffer_size = in_get_buffer_size; |
| 1809 | in->stream.common.get_channels = in_get_channels; |
| 1810 | in->stream.common.get_format = in_get_format; |
| 1811 | in->stream.common.set_format = in_set_format; |
| 1812 | in->stream.common.standby = in_standby; |
| 1813 | in->stream.common.dump = in_dump; |
| 1814 | in->stream.common.set_parameters = in_set_parameters; |
| 1815 | in->stream.common.get_parameters = in_get_parameters; |
| 1816 | in->stream.common.add_audio_effect = in_add_audio_effect; |
| 1817 | in->stream.common.remove_audio_effect = in_remove_audio_effect; |
| 1818 | in->stream.set_gain = in_set_gain; |
| 1819 | in->stream.read = in_read; |
| 1820 | in->stream.get_input_frames_lost = in_get_input_frames_lost; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1821 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1822 | /* initialize a2dp specifics */ |
| 1823 | a2dp_stream_common_init(&in->common); |
Mike Lockwood | 3e8a242f | 2014-05-23 12:42:24 -0700 | [diff] [blame] | 1824 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1825 | *stream_in = &in->stream; |
| 1826 | a2dp_dev->input = in; |
Mike Lockwood | 3e8a242f | 2014-05-23 12:42:24 -0700 | [diff] [blame] | 1827 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1828 | if (a2dp_read_input_audio_config(&in->common) < 0) { |
| 1829 | ERROR("a2dp_read_input_audio_config failed (%s)", strerror(errno)); |
| 1830 | ret = -1; |
| 1831 | goto err_open; |
| 1832 | } |
Andy Hung | f95d27b | 2017-05-01 15:04:57 -0700 | [diff] [blame] | 1833 | // a2dp_read_input_audio_config() opens socket control path (or fails) |
Mike Lockwood | 3e8a242f | 2014-05-23 12:42:24 -0700 | [diff] [blame] | 1834 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1835 | DEBUG("success"); |
| 1836 | return 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1837 | |
| 1838 | err_open: |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 1839 | a2dp_stream_common_destroy(&in->common); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1840 | free(in); |
| 1841 | *stream_in = NULL; |
| 1842 | a2dp_dev->input = NULL; |
| 1843 | ERROR("failed"); |
| 1844 | return ret; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1845 | } |
| 1846 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1847 | static void adev_close_input_stream(struct audio_hw_device* dev, |
| 1848 | struct audio_stream_in* stream) { |
| 1849 | struct a2dp_audio_device* a2dp_dev = (struct a2dp_audio_device*)dev; |
| 1850 | struct a2dp_stream_in* in = (struct a2dp_stream_in*)stream; |
Mike J. Chen | 5cd8bff | 2014-01-31 18:16:59 -0800 | [diff] [blame] | 1851 | |
Andy Hung | b1a900f | 2017-03-30 16:28:46 -0700 | [diff] [blame] | 1852 | std::lock_guard<std::recursive_mutex> lock(*a2dp_dev->mutex); |
| 1853 | { |
| 1854 | std::lock_guard<std::recursive_mutex> lock(*in->common.mutex); |
| 1855 | const a2dp_state_t state = in->common.state; |
| 1856 | INFO("closing input (state %d)", (int)state); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1857 | |
Andy Hung | b1a900f | 2017-03-30 16:28:46 -0700 | [diff] [blame] | 1858 | if ((state == AUDIO_A2DP_STATE_STARTED) || |
| 1859 | (state == AUDIO_A2DP_STATE_STOPPING)) |
| 1860 | stop_audio_datapath(&in->common); |
Mike Lockwood | 3e8a242f | 2014-05-23 12:42:24 -0700 | [diff] [blame] | 1861 | |
Andy Hung | b1a900f | 2017-03-30 16:28:46 -0700 | [diff] [blame] | 1862 | skt_disconnect(in->common.ctrl_fd); |
| 1863 | in->common.ctrl_fd = AUDIO_SKT_DISCONNECTED; |
| 1864 | } |
Marie Janssen | 21da637 | 2016-11-02 18:31:55 -0700 | [diff] [blame] | 1865 | a2dp_stream_common_destroy(&in->common); |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1866 | free(stream); |
| 1867 | a2dp_dev->input = NULL; |
Mike Lockwood | 3e8a242f | 2014-05-23 12:42:24 -0700 | [diff] [blame] | 1868 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1869 | DEBUG("done"); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1870 | } |
| 1871 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1872 | static int adev_dump(UNUSED_ATTR const audio_hw_device_t* device, |
| 1873 | UNUSED_ATTR int fd) { |
| 1874 | FNLOG(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1875 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1876 | return 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1877 | } |
| 1878 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1879 | static int adev_close(hw_device_t* device) { |
Andy Hung | b1a900f | 2017-03-30 16:28:46 -0700 | [diff] [blame] | 1880 | struct a2dp_audio_device* a2dp_dev = (struct a2dp_audio_device*)device; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1881 | FNLOG(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1882 | |
Andy Hung | b1a900f | 2017-03-30 16:28:46 -0700 | [diff] [blame] | 1883 | delete a2dp_dev->mutex; |
| 1884 | a2dp_dev->mutex = nullptr; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1885 | free(device); |
| 1886 | return 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1887 | } |
| 1888 | |
| 1889 | static int adev_open(const hw_module_t* module, const char* name, |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1890 | hw_device_t** device) { |
| 1891 | struct a2dp_audio_device* adev; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1892 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1893 | INFO(" adev_open in A2dp_hw module"); |
| 1894 | FNLOG(); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1895 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1896 | if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) { |
| 1897 | ERROR("interface %s not matching [%s]", name, AUDIO_HARDWARE_INTERFACE); |
| 1898 | return -EINVAL; |
| 1899 | } |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1900 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1901 | adev = (struct a2dp_audio_device*)calloc(1, sizeof(struct a2dp_audio_device)); |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1902 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1903 | if (!adev) return -ENOMEM; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1904 | |
Andy Hung | b1a900f | 2017-03-30 16:28:46 -0700 | [diff] [blame] | 1905 | adev->mutex = new std::recursive_mutex; |
| 1906 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1907 | adev->device.common.tag = HARDWARE_DEVICE_TAG; |
| 1908 | adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0; |
| 1909 | adev->device.common.module = (struct hw_module_t*)module; |
| 1910 | adev->device.common.close = adev_close; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1911 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1912 | adev->device.init_check = adev_init_check; |
| 1913 | adev->device.set_voice_volume = adev_set_voice_volume; |
| 1914 | adev->device.set_master_volume = adev_set_master_volume; |
| 1915 | adev->device.set_mode = adev_set_mode; |
| 1916 | adev->device.set_mic_mute = adev_set_mic_mute; |
| 1917 | adev->device.get_mic_mute = adev_get_mic_mute; |
| 1918 | adev->device.set_parameters = adev_set_parameters; |
| 1919 | adev->device.get_parameters = adev_get_parameters; |
| 1920 | adev->device.get_input_buffer_size = adev_get_input_buffer_size; |
| 1921 | adev->device.open_output_stream = adev_open_output_stream; |
| 1922 | adev->device.close_output_stream = adev_close_output_stream; |
| 1923 | adev->device.open_input_stream = adev_open_input_stream; |
| 1924 | adev->device.close_input_stream = adev_close_input_stream; |
| 1925 | adev->device.dump = adev_dump; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1926 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1927 | adev->output = NULL; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1928 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1929 | *device = &adev->device.common; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1930 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1931 | return 0; |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1932 | } |
| 1933 | |
| 1934 | static struct hw_module_methods_t hal_module_methods = { |
| 1935 | .open = adev_open, |
| 1936 | }; |
| 1937 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 1938 | __attribute__(( |
| 1939 | visibility("default"))) struct audio_module HAL_MODULE_INFO_SYM = { |
| 1940 | .common = |
| 1941 | { |
| 1942 | .tag = HARDWARE_MODULE_TAG, |
| 1943 | .version_major = 1, |
| 1944 | .version_minor = 0, |
| 1945 | .id = AUDIO_HARDWARE_MODULE_ID, |
| 1946 | .name = "A2DP Audio HW HAL", |
| 1947 | .author = "The Android Open Source Project", |
| 1948 | .methods = &hal_module_methods, |
| 1949 | }, |
The Android Open Source Project | 5738f83 | 2012-12-12 16:00:35 -0800 | [diff] [blame] | 1950 | }; |