1 /******************************************************************************
2 *
3 * Copyright 2016 The Android Open Source Project
4 * Copyright 2009-2012 Broadcom Corporation
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 ******************************************************************************/
19
20 #define LOG_TAG "bt_btif_a2dp_control"
21
22 #include <base/logging.h>
23 #include <stdbool.h>
24 #include <stdint.h>
25
26 #include "audio_a2dp_hw/include/audio_a2dp_hw.h"
27 #include "bt_common.h"
28 #include "btif_a2dp.h"
29 #include "btif_a2dp_control.h"
30 #include "btif_a2dp_sink.h"
31 #include "btif_a2dp_source.h"
32 #include "btif_av.h"
33 #include "btif_av_co.h"
34 #include "btif_hf.h"
35 #include "osi/include/osi.h"
36 #include "uipc.h"
37
38 #define A2DP_DATA_READ_POLL_MS 10
39
40 struct {
41 uint64_t total_bytes_read = 0;
42 uint16_t audio_delay = 0;
43 struct timespec timestamp = {};
44 } delay_report_stats;
45
46 static void btif_a2dp_data_cb(tUIPC_CH_ID ch_id, tUIPC_EVENT event);
47 static void btif_a2dp_ctrl_cb(tUIPC_CH_ID ch_id, tUIPC_EVENT event);
48
49 /* We can have max one command pending */
50 static tA2DP_CTRL_CMD a2dp_cmd_pending = A2DP_CTRL_CMD_NONE;
51 std::unique_ptr<tUIPC_STATE> a2dp_uipc;
52
btif_a2dp_control_init(void)53 void btif_a2dp_control_init(void) {
54 a2dp_uipc = UIPC_Init();
55 UIPC_Open(*a2dp_uipc, UIPC_CH_ID_AV_CTRL, btif_a2dp_ctrl_cb, A2DP_CTRL_PATH);
56 }
57
btif_a2dp_control_cleanup(void)58 void btif_a2dp_control_cleanup(void) {
59 /* This calls blocks until UIPC is fully closed */
60 UIPC_Close(*a2dp_uipc, UIPC_CH_ID_ALL);
61 }
62
btif_a2dp_recv_ctrl_data(void)63 static void btif_a2dp_recv_ctrl_data(void) {
64 tA2DP_CTRL_CMD cmd = A2DP_CTRL_CMD_NONE;
65 int n;
66
67 uint8_t read_cmd = 0; /* The read command size is one octet */
68 n = UIPC_Read(*a2dp_uipc, UIPC_CH_ID_AV_CTRL, NULL, &read_cmd, 1);
69 cmd = static_cast<tA2DP_CTRL_CMD>(read_cmd);
70
71 /* detach on ctrl channel means audioflinger process was terminated */
72 if (n == 0) {
73 APPL_TRACE_WARNING("%s: CTRL CH DETACHED", __func__);
74 UIPC_Close(*a2dp_uipc, UIPC_CH_ID_AV_CTRL);
75 return;
76 }
77
78 // Don't log A2DP_CTRL_GET_PRESENTATION_POSITION by default, because it
79 // could be very chatty when audio is streaming.
80 if (cmd == A2DP_CTRL_GET_PRESENTATION_POSITION) {
81 APPL_TRACE_DEBUG("%s: a2dp-ctrl-cmd : %s", __func__,
82 audio_a2dp_hw_dump_ctrl_event(cmd));
83 } else {
84 APPL_TRACE_WARNING("%s: a2dp-ctrl-cmd : %s", __func__,
85 audio_a2dp_hw_dump_ctrl_event(cmd));
86 }
87
88 a2dp_cmd_pending = cmd;
89 switch (cmd) {
90 case A2DP_CTRL_CMD_CHECK_READY:
91 if (btif_a2dp_source_media_task_is_shutting_down()) {
92 APPL_TRACE_WARNING("%s: A2DP command %s while media task shutting down",
93 __func__, audio_a2dp_hw_dump_ctrl_event(cmd));
94 btif_a2dp_command_ack(A2DP_CTRL_ACK_FAILURE);
95 return;
96 }
97
98 /* check whether AV is ready to setup A2DP datapath */
99 if (btif_av_stream_ready() || btif_av_stream_started_ready()) {
100 btif_a2dp_command_ack(A2DP_CTRL_ACK_SUCCESS);
101 } else {
102 APPL_TRACE_WARNING("%s: A2DP command %s while AV stream is not ready",
103 __func__, audio_a2dp_hw_dump_ctrl_event(cmd));
104 btif_a2dp_command_ack(A2DP_CTRL_ACK_FAILURE);
105 }
106 break;
107
108 case A2DP_CTRL_CMD_START:
109 /*
110 * Don't send START request to stack while we are in a call.
111 * Some headsets such as "Sony MW600", don't allow AVDTP START
112 * while in a call, and respond with BAD_STATE.
113 */
114 if (!bluetooth::headset::IsCallIdle()) {
115 APPL_TRACE_WARNING("%s: A2DP command %s while call state is busy",
116 __func__, audio_a2dp_hw_dump_ctrl_event(cmd));
117 btif_a2dp_command_ack(A2DP_CTRL_ACK_INCALL_FAILURE);
118 break;
119 }
120
121 if (btif_a2dp_source_is_streaming()) {
122 APPL_TRACE_WARNING("%s: A2DP command %s while source is streaming",
123 __func__, audio_a2dp_hw_dump_ctrl_event(cmd));
124 btif_a2dp_command_ack(A2DP_CTRL_ACK_FAILURE);
125 break;
126 }
127
128 if (btif_av_stream_ready()) {
129 /* Setup audio data channel listener */
130 UIPC_Open(*a2dp_uipc, UIPC_CH_ID_AV_AUDIO, btif_a2dp_data_cb,
131 A2DP_DATA_PATH);
132
133 /*
134 * Post start event and wait for audio path to open.
135 * If we are the source, the ACK will be sent after the start
136 * procedure is completed, othewise send it now.
137 */
138 btif_av_stream_start();
139 if (btif_av_get_peer_sep() == AVDT_TSEP_SRC)
140 btif_a2dp_command_ack(A2DP_CTRL_ACK_SUCCESS);
141 break;
142 }
143
144 if (btif_av_stream_started_ready()) {
145 /*
146 * Already started, setup audio data channel listener and ACK
147 * back immediately.
148 */
149 UIPC_Open(*a2dp_uipc, UIPC_CH_ID_AV_AUDIO, btif_a2dp_data_cb,
150 A2DP_DATA_PATH);
151 btif_a2dp_command_ack(A2DP_CTRL_ACK_SUCCESS);
152 break;
153 }
154 APPL_TRACE_WARNING("%s: A2DP command %s while AV stream is not ready",
155 __func__, audio_a2dp_hw_dump_ctrl_event(cmd));
156 btif_a2dp_command_ack(A2DP_CTRL_ACK_FAILURE);
157 break;
158
159 case A2DP_CTRL_CMD_STOP:
160 if (btif_av_get_peer_sep() == AVDT_TSEP_SNK &&
161 !btif_a2dp_source_is_streaming()) {
162 /* We are already stopped, just ack back */
163 btif_a2dp_command_ack(A2DP_CTRL_ACK_SUCCESS);
164 break;
165 }
166 btif_av_stream_stop(RawAddress::kEmpty);
167 btif_a2dp_command_ack(A2DP_CTRL_ACK_SUCCESS);
168 break;
169
170 case A2DP_CTRL_CMD_SUSPEND:
171 /* Local suspend */
172 if (btif_av_stream_started_ready()) {
173 btif_av_stream_suspend();
174 break;
175 }
176 /* If we are not in started state, just ack back ok and let
177 * audioflinger close the channel. This can happen if we are
178 * remotely suspended, clear REMOTE SUSPEND flag.
179 */
180 btif_av_clear_remote_suspend_flag();
181 btif_a2dp_command_ack(A2DP_CTRL_ACK_SUCCESS);
182 break;
183
184 case A2DP_CTRL_GET_INPUT_AUDIO_CONFIG: {
185 tA2DP_SAMPLE_RATE sample_rate = btif_a2dp_sink_get_sample_rate();
186 tA2DP_CHANNEL_COUNT channel_count = btif_a2dp_sink_get_channel_count();
187
188 btif_a2dp_command_ack(A2DP_CTRL_ACK_SUCCESS);
189 UIPC_Send(*a2dp_uipc, UIPC_CH_ID_AV_CTRL, 0,
190 reinterpret_cast<uint8_t*>(&sample_rate),
191 sizeof(tA2DP_SAMPLE_RATE));
192 UIPC_Send(*a2dp_uipc, UIPC_CH_ID_AV_CTRL, 0, &channel_count,
193 sizeof(tA2DP_CHANNEL_COUNT));
194 break;
195 }
196
197 case A2DP_CTRL_GET_OUTPUT_AUDIO_CONFIG: {
198 btav_a2dp_codec_config_t codec_config;
199 btav_a2dp_codec_config_t codec_capability;
200 codec_config.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
201 codec_config.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
202 codec_config.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
203 codec_capability.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
204 codec_capability.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
205 codec_capability.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
206
207 A2dpCodecConfig* current_codec = bta_av_get_a2dp_current_codec();
208 if (current_codec != nullptr) {
209 codec_config = current_codec->getCodecConfig();
210 codec_capability = current_codec->getCodecCapability();
211 }
212
213 btif_a2dp_command_ack(A2DP_CTRL_ACK_SUCCESS);
214 // Send the current codec config
215 UIPC_Send(*a2dp_uipc, UIPC_CH_ID_AV_CTRL, 0,
216 reinterpret_cast<const uint8_t*>(&codec_config.sample_rate),
217 sizeof(btav_a2dp_codec_sample_rate_t));
218 UIPC_Send(*a2dp_uipc, UIPC_CH_ID_AV_CTRL, 0,
219 reinterpret_cast<const uint8_t*>(&codec_config.bits_per_sample),
220 sizeof(btav_a2dp_codec_bits_per_sample_t));
221 UIPC_Send(*a2dp_uipc, UIPC_CH_ID_AV_CTRL, 0,
222 reinterpret_cast<const uint8_t*>(&codec_config.channel_mode),
223 sizeof(btav_a2dp_codec_channel_mode_t));
224 // Send the current codec capability
225 UIPC_Send(*a2dp_uipc, UIPC_CH_ID_AV_CTRL, 0,
226 reinterpret_cast<const uint8_t*>(&codec_capability.sample_rate),
227 sizeof(btav_a2dp_codec_sample_rate_t));
228 UIPC_Send(
229 *a2dp_uipc, UIPC_CH_ID_AV_CTRL, 0,
230 reinterpret_cast<const uint8_t*>(&codec_capability.bits_per_sample),
231 sizeof(btav_a2dp_codec_bits_per_sample_t));
232 UIPC_Send(
233 *a2dp_uipc, UIPC_CH_ID_AV_CTRL, 0,
234 reinterpret_cast<const uint8_t*>(&codec_capability.channel_mode),
235 sizeof(btav_a2dp_codec_channel_mode_t));
236 break;
237 }
238
239 case A2DP_CTRL_SET_OUTPUT_AUDIO_CONFIG: {
240 btav_a2dp_codec_config_t codec_config;
241 codec_config.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
242 codec_config.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
243 codec_config.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
244
245 btif_a2dp_command_ack(A2DP_CTRL_ACK_SUCCESS);
246 // Send the current codec config
247 if (UIPC_Read(*a2dp_uipc, UIPC_CH_ID_AV_CTRL, 0,
248 reinterpret_cast<uint8_t*>(&codec_config.sample_rate),
249 sizeof(btav_a2dp_codec_sample_rate_t)) !=
250 sizeof(btav_a2dp_codec_sample_rate_t)) {
251 APPL_TRACE_ERROR("%s: Error reading sample rate from audio HAL",
252 __func__);
253 break;
254 }
255 if (UIPC_Read(*a2dp_uipc, UIPC_CH_ID_AV_CTRL, 0,
256 reinterpret_cast<uint8_t*>(&codec_config.bits_per_sample),
257 sizeof(btav_a2dp_codec_bits_per_sample_t)) !=
258 sizeof(btav_a2dp_codec_bits_per_sample_t)) {
259 APPL_TRACE_ERROR("%s: Error reading bits per sample from audio HAL",
260 __func__);
261 break;
262 }
263 if (UIPC_Read(*a2dp_uipc, UIPC_CH_ID_AV_CTRL, 0,
264 reinterpret_cast<uint8_t*>(&codec_config.channel_mode),
265 sizeof(btav_a2dp_codec_channel_mode_t)) !=
266 sizeof(btav_a2dp_codec_channel_mode_t)) {
267 APPL_TRACE_ERROR("%s: Error reading channel mode from audio HAL",
268 __func__);
269 break;
270 }
271 APPL_TRACE_DEBUG(
272 "%s: A2DP_CTRL_SET_OUTPUT_AUDIO_CONFIG: "
273 "sample_rate=0x%x bits_per_sample=0x%x "
274 "channel_mode=0x%x",
275 __func__, codec_config.sample_rate, codec_config.bits_per_sample,
276 codec_config.channel_mode);
277 btif_a2dp_source_feeding_update_req(codec_config);
278 break;
279 }
280
281 case A2DP_CTRL_CMD_OFFLOAD_START:
282 btif_av_stream_start_offload();
283 break;
284
285 case A2DP_CTRL_GET_PRESENTATION_POSITION: {
286 btif_a2dp_command_ack(A2DP_CTRL_ACK_SUCCESS);
287
288 UIPC_Send(*a2dp_uipc, UIPC_CH_ID_AV_CTRL, 0,
289 (uint8_t*)&(delay_report_stats.total_bytes_read),
290 sizeof(uint64_t));
291 UIPC_Send(*a2dp_uipc, UIPC_CH_ID_AV_CTRL, 0,
292 (uint8_t*)&(delay_report_stats.audio_delay), sizeof(uint16_t));
293
294 uint32_t seconds = delay_report_stats.timestamp.tv_sec;
295 UIPC_Send(*a2dp_uipc, UIPC_CH_ID_AV_CTRL, 0, (uint8_t*)&seconds,
296 sizeof(seconds));
297
298 uint32_t nsec = delay_report_stats.timestamp.tv_nsec;
299 UIPC_Send(*a2dp_uipc, UIPC_CH_ID_AV_CTRL, 0, (uint8_t*)&nsec,
300 sizeof(nsec));
301 break;
302 }
303 default:
304 APPL_TRACE_ERROR("%s: UNSUPPORTED CMD (%d)", __func__, cmd);
305 btif_a2dp_command_ack(A2DP_CTRL_ACK_FAILURE);
306 break;
307 }
308
309 // Don't log A2DP_CTRL_GET_PRESENTATION_POSITION by default, because it
310 // could be very chatty when audio is streaming.
311 if (cmd == A2DP_CTRL_GET_PRESENTATION_POSITION) {
312 APPL_TRACE_DEBUG("%s: a2dp-ctrl-cmd : %s DONE", __func__,
313 audio_a2dp_hw_dump_ctrl_event(cmd));
314 } else {
315 APPL_TRACE_WARNING("%s: a2dp-ctrl-cmd : %s DONE", __func__,
316 audio_a2dp_hw_dump_ctrl_event(cmd));
317 }
318 }
319
btif_a2dp_ctrl_cb(UNUSED_ATTR tUIPC_CH_ID ch_id,tUIPC_EVENT event)320 static void btif_a2dp_ctrl_cb(UNUSED_ATTR tUIPC_CH_ID ch_id,
321 tUIPC_EVENT event) {
322 // Don't log UIPC_RX_DATA_READY_EVT by default, because it
323 // could be very chatty when audio is streaming.
324 if (event == UIPC_RX_DATA_READY_EVT) {
325 APPL_TRACE_DEBUG("%s: A2DP-CTRL-CHANNEL EVENT %s", __func__,
326 dump_uipc_event(event));
327 } else {
328 APPL_TRACE_WARNING("%s: A2DP-CTRL-CHANNEL EVENT %s", __func__,
329 dump_uipc_event(event));
330 }
331
332 switch (event) {
333 case UIPC_OPEN_EVT:
334 break;
335
336 case UIPC_CLOSE_EVT:
337 /* restart ctrl server unless we are shutting down */
338 if (btif_a2dp_source_media_task_is_running())
339 UIPC_Open(*a2dp_uipc, UIPC_CH_ID_AV_CTRL, btif_a2dp_ctrl_cb,
340 A2DP_CTRL_PATH);
341 break;
342
343 case UIPC_RX_DATA_READY_EVT:
344 btif_a2dp_recv_ctrl_data();
345 break;
346
347 default:
348 APPL_TRACE_ERROR("%s: ### A2DP-CTRL-CHANNEL EVENT %d NOT HANDLED ###",
349 __func__, event);
350 break;
351 }
352 }
353
btif_a2dp_data_cb(UNUSED_ATTR tUIPC_CH_ID ch_id,tUIPC_EVENT event)354 static void btif_a2dp_data_cb(UNUSED_ATTR tUIPC_CH_ID ch_id,
355 tUIPC_EVENT event) {
356 APPL_TRACE_WARNING("%s: BTIF MEDIA (A2DP-DATA) EVENT %s", __func__,
357 dump_uipc_event(event));
358
359 switch (event) {
360 case UIPC_OPEN_EVT:
361 /*
362 * Read directly from media task from here on (keep callback for
363 * connection events.
364 */
365 UIPC_Ioctl(*a2dp_uipc, UIPC_CH_ID_AV_AUDIO,
366 UIPC_REG_REMOVE_ACTIVE_READSET, NULL);
367 UIPC_Ioctl(*a2dp_uipc, UIPC_CH_ID_AV_AUDIO, UIPC_SET_READ_POLL_TMO,
368 reinterpret_cast<void*>(A2DP_DATA_READ_POLL_MS));
369
370 if (btif_av_get_peer_sep() == AVDT_TSEP_SNK) {
371 /* Start the media task to encode the audio */
372 btif_a2dp_source_start_audio_req();
373 }
374
375 /* ACK back when media task is fully started */
376 break;
377
378 case UIPC_CLOSE_EVT:
379 APPL_TRACE_EVENT("%s: ## AUDIO PATH DETACHED ##", __func__);
380 btif_a2dp_command_ack(A2DP_CTRL_ACK_SUCCESS);
381 /*
382 * Send stop request only if we are actively streaming and haven't
383 * received a stop request. Potentially, the audioflinger detached
384 * abnormally.
385 */
386 if (btif_a2dp_source_is_streaming()) {
387 /* Post stop event and wait for audio path to stop */
388 btif_av_stream_stop(RawAddress::kEmpty);
389 }
390 break;
391
392 default:
393 APPL_TRACE_ERROR("%s: ### A2DP-DATA EVENT %d NOT HANDLED ###", __func__,
394 event);
395 break;
396 }
397 }
398
btif_a2dp_command_ack(tA2DP_CTRL_ACK status)399 void btif_a2dp_command_ack(tA2DP_CTRL_ACK status) {
400 uint8_t ack = status;
401
402 // Don't log A2DP_CTRL_GET_PRESENTATION_POSITION by default, because it
403 // could be very chatty when audio is streaming.
404 if (a2dp_cmd_pending == A2DP_CTRL_GET_PRESENTATION_POSITION) {
405 APPL_TRACE_DEBUG("%s: ## a2dp ack : %s, status %d ##", __func__,
406 audio_a2dp_hw_dump_ctrl_event(a2dp_cmd_pending), status);
407 } else {
408 APPL_TRACE_WARNING("%s: ## a2dp ack : %s, status %d ##", __func__,
409 audio_a2dp_hw_dump_ctrl_event(a2dp_cmd_pending), status);
410 }
411
412 /* Sanity check */
413 if (a2dp_cmd_pending == A2DP_CTRL_CMD_NONE) {
414 APPL_TRACE_ERROR("%s: warning : no command pending, ignore ack", __func__);
415 return;
416 }
417
418 /* Clear pending */
419 a2dp_cmd_pending = A2DP_CTRL_CMD_NONE;
420
421 /* Acknowledge start request */
422 UIPC_Send(*a2dp_uipc, UIPC_CH_ID_AV_CTRL, 0, &ack, sizeof(ack));
423 }
424
btif_a2dp_control_log_bytes_read(uint32_t bytes_read)425 void btif_a2dp_control_log_bytes_read(uint32_t bytes_read) {
426 delay_report_stats.total_bytes_read += bytes_read;
427 clock_gettime(CLOCK_MONOTONIC, &delay_report_stats.timestamp);
428 }
429
btif_a2dp_control_set_audio_delay(uint16_t delay)430 void btif_a2dp_control_set_audio_delay(uint16_t delay) {
431 APPL_TRACE_DEBUG("%s: DELAY: %.1f ms", __func__, (float)delay / 10);
432 delay_report_stats.audio_delay = delay;
433 }
434
btif_a2dp_control_reset_audio_delay(void)435 void btif_a2dp_control_reset_audio_delay(void) {
436 APPL_TRACE_DEBUG("%s", __func__);
437 delay_report_stats.audio_delay = 0;
438 delay_report_stats.total_bytes_read = 0;
439 delay_report_stats.timestamp = {};
440 }
441