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_sink"
21
22 #include <atomic>
23 #include <cstdio>
24 #include <cstring>
25 #include <mutex>
26 #include <string>
27
28 #include <base/bind.h>
29
30 #include "bt_common.h"
31 #include "btif_a2dp.h"
32 #include "btif_a2dp_sink.h"
33 #include "btif_av.h"
34 #include "btif_av_co.h"
35 #include "btif_avrcp_audio_track.h"
36 #include "btif_util.h"
37 #include "common/message_loop_thread.h"
38 #include "osi/include/fixed_queue.h"
39 #include "osi/include/log.h"
40 #include "osi/include/osi.h"
41
42 using bluetooth::common::MessageLoopThread;
43 using LockGuard = std::lock_guard<std::mutex>;
44
45 /**
46 * The receiving queue buffer size.
47 */
48 #define MAX_INPUT_A2DP_FRAME_QUEUE_SZ (MAX_PCM_FRAME_NUM_PER_TICK * 2)
49
50 #define BTIF_SINK_MEDIA_TIME_TICK_MS 20
51
52 /* In case of A2DP Sink, we will delay start by 5 AVDTP Packets */
53 #define MAX_A2DP_DELAYED_START_FRAME_COUNT 5
54
55 enum {
56 BTIF_A2DP_SINK_STATE_OFF,
57 BTIF_A2DP_SINK_STATE_STARTING_UP,
58 BTIF_A2DP_SINK_STATE_RUNNING,
59 BTIF_A2DP_SINK_STATE_SHUTTING_DOWN
60 };
61
62 /* BTIF Media Sink command event definition */
63 enum {
64 BTIF_MEDIA_SINK_DECODER_UPDATE = 1,
65 BTIF_MEDIA_SINK_CLEAR_TRACK,
66 BTIF_MEDIA_SINK_SET_FOCUS_STATE,
67 BTIF_MEDIA_SINK_AUDIO_RX_FLUSH
68 };
69
70 typedef struct {
71 BT_HDR hdr;
72 uint8_t codec_info[AVDT_CODEC_SIZE];
73 } tBTIF_MEDIA_SINK_DECODER_UPDATE;
74
75 typedef struct {
76 BT_HDR hdr;
77 btif_a2dp_sink_focus_state_t focus_state;
78 } tBTIF_MEDIA_SINK_FOCUS_UPDATE;
79
80 /* BTIF A2DP Sink control block */
81 class BtifA2dpSinkControlBlock {
82 public:
BtifA2dpSinkControlBlock(const std::string & thread_name)83 explicit BtifA2dpSinkControlBlock(const std::string& thread_name)
84 : worker_thread(thread_name),
85 rx_audio_queue(nullptr),
86 rx_flush(false),
87 decode_alarm(nullptr),
88 sample_rate(0),
89 channel_count(0),
90 rx_focus_state(BTIF_A2DP_SINK_FOCUS_NOT_GRANTED),
91 audio_track(nullptr),
92 decoder_interface(nullptr) {}
93
Reset()94 void Reset() {
95 if (audio_track != nullptr) {
96 BtifAvrcpAudioTrackStop(audio_track);
97 BtifAvrcpAudioTrackDelete(audio_track);
98 }
99 audio_track = nullptr;
100 fixed_queue_free(rx_audio_queue, nullptr);
101 rx_audio_queue = nullptr;
102 alarm_free(decode_alarm);
103 decode_alarm = nullptr;
104 rx_flush = false;
105 rx_focus_state = BTIF_A2DP_SINK_FOCUS_NOT_GRANTED;
106 sample_rate = 0;
107 channel_count = 0;
108 decoder_interface = nullptr;
109 }
110
111 MessageLoopThread worker_thread;
112 fixed_queue_t* rx_audio_queue;
113 bool rx_flush; /* discards any incoming data when true */
114 alarm_t* decode_alarm;
115 tA2DP_SAMPLE_RATE sample_rate;
116 tA2DP_BITS_PER_SAMPLE bits_per_sample;
117 tA2DP_CHANNEL_COUNT channel_count;
118 btif_a2dp_sink_focus_state_t rx_focus_state; /* audio focus state */
119 void* audio_track;
120 const tA2DP_DECODER_INTERFACE* decoder_interface;
121 };
122
123 // Mutex for below data structures.
124 static std::mutex g_mutex;
125
126 static BtifA2dpSinkControlBlock btif_a2dp_sink_cb("bt_a2dp_sink_worker_thread");
127
128 static std::atomic<int> btif_a2dp_sink_state{BTIF_A2DP_SINK_STATE_OFF};
129
130 static void btif_a2dp_sink_init_delayed();
131 static void btif_a2dp_sink_startup_delayed();
132 static void btif_a2dp_sink_start_session_delayed(
133 std::promise<void> peer_ready_promise);
134 static void btif_a2dp_sink_end_session_delayed();
135 static void btif_a2dp_sink_shutdown_delayed();
136 static void btif_a2dp_sink_cleanup_delayed();
137 static void btif_a2dp_sink_command_ready(BT_HDR* p_msg);
138 static void btif_a2dp_sink_audio_handle_stop_decoding();
139 static void btif_decode_alarm_cb(void* context);
140 static void btif_a2dp_sink_audio_handle_start_decoding();
141 static void btif_a2dp_sink_avk_handle_timer();
142 static void btif_a2dp_sink_audio_rx_flush_req();
143 /* Handle incoming media packets A2DP SINK streaming */
144 static void btif_a2dp_sink_handle_inc_media(BT_HDR* p_msg);
145 static void btif_a2dp_sink_decoder_update_event(
146 tBTIF_MEDIA_SINK_DECODER_UPDATE* p_buf);
147 static void btif_a2dp_sink_clear_track_event();
148 static void btif_a2dp_sink_set_focus_state_event(
149 btif_a2dp_sink_focus_state_t state);
150 static void btif_a2dp_sink_audio_rx_flush_event();
151 static void btif_a2dp_sink_clear_track_event_req();
152
dump_media_event(uint16_t event)153 UNUSED_ATTR static const char* dump_media_event(uint16_t event) {
154 switch (event) {
155 CASE_RETURN_STR(BTIF_MEDIA_SINK_DECODER_UPDATE)
156 CASE_RETURN_STR(BTIF_MEDIA_SINK_CLEAR_TRACK)
157 CASE_RETURN_STR(BTIF_MEDIA_SINK_SET_FOCUS_STATE)
158 CASE_RETURN_STR(BTIF_MEDIA_SINK_AUDIO_RX_FLUSH)
159 default:
160 break;
161 }
162 return "UNKNOWN A2DP SINK EVENT";
163 }
164
btif_a2dp_sink_init()165 bool btif_a2dp_sink_init() {
166 LOG_INFO(LOG_TAG, "%s", __func__);
167 LockGuard lock(g_mutex);
168
169 if (btif_a2dp_sink_state != BTIF_A2DP_SINK_STATE_OFF) {
170 LOG_ERROR(LOG_TAG, "%s: A2DP Sink media task already running", __func__);
171 return false;
172 }
173
174 btif_a2dp_sink_cb.Reset();
175 btif_a2dp_sink_state = BTIF_A2DP_SINK_STATE_STARTING_UP;
176
177 /* Start A2DP Sink media task */
178 btif_a2dp_sink_cb.worker_thread.StartUp();
179 if (!btif_a2dp_sink_cb.worker_thread.IsRunning()) {
180 LOG_ERROR(LOG_TAG, "%s: unable to start up media thread", __func__);
181 btif_a2dp_sink_state = BTIF_A2DP_SINK_STATE_OFF;
182 return false;
183 }
184
185 btif_a2dp_sink_cb.rx_audio_queue = fixed_queue_new(SIZE_MAX);
186
187 /* Schedule the rest of the operations */
188 if (!btif_a2dp_sink_cb.worker_thread.EnableRealTimeScheduling()) {
189 LOG(FATAL) << __func__
190 << ": Failed to increase A2DP decoder thread priority";
191 }
192 btif_a2dp_sink_cb.worker_thread.DoInThread(
193 FROM_HERE, base::BindOnce(btif_a2dp_sink_init_delayed));
194 return true;
195 }
196
btif_a2dp_sink_init_delayed()197 static void btif_a2dp_sink_init_delayed() {
198 LOG_INFO(LOG_TAG, "%s", __func__);
199 btif_a2dp_sink_state = BTIF_A2DP_SINK_STATE_RUNNING;
200 }
201
btif_a2dp_sink_startup()202 bool btif_a2dp_sink_startup() {
203 LOG_INFO(LOG_TAG, "%s", __func__);
204 btif_a2dp_sink_cb.worker_thread.DoInThread(
205 FROM_HERE, base::BindOnce(btif_a2dp_sink_startup_delayed));
206 return true;
207 }
208
btif_a2dp_sink_startup_delayed()209 static void btif_a2dp_sink_startup_delayed() {
210 LOG_INFO(LOG_TAG, "%s", __func__);
211 LockGuard lock(g_mutex);
212 // Nothing to do
213 }
214
btif_a2dp_sink_start_session(const RawAddress & peer_address,std::promise<void> peer_ready_promise)215 bool btif_a2dp_sink_start_session(const RawAddress& peer_address,
216 std::promise<void> peer_ready_promise) {
217 LOG(INFO) << __func__ << ": peer_address=" << peer_address;
218 if (btif_a2dp_sink_cb.worker_thread.DoInThread(
219 FROM_HERE, base::BindOnce(btif_a2dp_sink_start_session_delayed,
220 std::move(peer_ready_promise)))) {
221 return true;
222 } else {
223 // cannot set promise but triggers crash
224 LOG(FATAL) << __func__ << ": peer_address=" << peer_address
225 << " fails to context switch";
226 return false;
227 }
228 }
229
btif_a2dp_sink_start_session_delayed(std::promise<void> peer_ready_promise)230 static void btif_a2dp_sink_start_session_delayed(
231 std::promise<void> peer_ready_promise) {
232 LOG(INFO) << __func__;
233 LockGuard lock(g_mutex);
234 peer_ready_promise.set_value();
235 // Nothing to do
236 }
237
btif_a2dp_sink_restart_session(const RawAddress & old_peer_address,const RawAddress & new_peer_address,std::promise<void> peer_ready_promise)238 bool btif_a2dp_sink_restart_session(const RawAddress& old_peer_address,
239 const RawAddress& new_peer_address,
240 std::promise<void> peer_ready_promise) {
241 LOG(INFO) << __func__ << ": old_peer_address=" << old_peer_address
242 << " new_peer_address=" << new_peer_address;
243
244 CHECK(!new_peer_address.IsEmpty());
245
246 if (!old_peer_address.IsEmpty()) {
247 btif_a2dp_sink_end_session(old_peer_address);
248 }
249
250 if (!bta_av_co_set_active_peer(new_peer_address)) {
251 LOG(ERROR) << __func__
252 << ": Cannot stream audio: cannot set active peer to "
253 << new_peer_address;
254 peer_ready_promise.set_value();
255 return false;
256 }
257
258 if (old_peer_address.IsEmpty()) {
259 btif_a2dp_sink_startup();
260 }
261 btif_a2dp_sink_start_session(new_peer_address, std::move(peer_ready_promise));
262
263 return true;
264 }
265
btif_a2dp_sink_end_session(const RawAddress & peer_address)266 bool btif_a2dp_sink_end_session(const RawAddress& peer_address) {
267 LOG_INFO(LOG_TAG, "%s: peer_address=%s", __func__,
268 peer_address.ToString().c_str());
269 btif_a2dp_sink_cb.worker_thread.DoInThread(
270 FROM_HERE, base::BindOnce(btif_a2dp_sink_end_session_delayed));
271 return true;
272 }
273
btif_a2dp_sink_end_session_delayed()274 static void btif_a2dp_sink_end_session_delayed() {
275 LOG_INFO(LOG_TAG, "%s", __func__);
276 LockGuard lock(g_mutex);
277 // Nothing to do
278 }
279
btif_a2dp_sink_shutdown()280 void btif_a2dp_sink_shutdown() {
281 LOG_INFO(LOG_TAG, "%s", __func__);
282 btif_a2dp_sink_cb.worker_thread.DoInThread(
283 FROM_HERE, base::BindOnce(btif_a2dp_sink_shutdown_delayed));
284 }
285
btif_a2dp_sink_shutdown_delayed()286 static void btif_a2dp_sink_shutdown_delayed() {
287 LOG_INFO(LOG_TAG, "%s", __func__);
288 LockGuard lock(g_mutex);
289 // Nothing to do
290 }
291
btif_a2dp_sink_cleanup()292 void btif_a2dp_sink_cleanup() {
293 LOG_INFO(LOG_TAG, "%s", __func__);
294
295 alarm_t* decode_alarm;
296
297 // Make sure the sink is shutdown
298 btif_a2dp_sink_shutdown();
299
300 {
301 LockGuard lock(g_mutex);
302 if ((btif_a2dp_sink_state == BTIF_A2DP_SINK_STATE_OFF) ||
303 (btif_a2dp_sink_state == BTIF_A2DP_SINK_STATE_SHUTTING_DOWN)) {
304 return;
305 }
306 // Make sure no channels are restarted while shutting down
307 btif_a2dp_sink_state = BTIF_A2DP_SINK_STATE_SHUTTING_DOWN;
308
309 decode_alarm = btif_a2dp_sink_cb.decode_alarm;
310 btif_a2dp_sink_cb.decode_alarm = nullptr;
311 }
312
313 // Stop the timer
314 alarm_free(decode_alarm);
315
316 // Exit the thread
317 btif_a2dp_sink_cb.worker_thread.DoInThread(
318 FROM_HERE, base::BindOnce(btif_a2dp_sink_cleanup_delayed));
319 btif_a2dp_sink_cb.worker_thread.ShutDown();
320 }
321
btif_a2dp_sink_cleanup_delayed()322 static void btif_a2dp_sink_cleanup_delayed() {
323 LOG_INFO(LOG_TAG, "%s", __func__);
324 LockGuard lock(g_mutex);
325
326 fixed_queue_free(btif_a2dp_sink_cb.rx_audio_queue, nullptr);
327 btif_a2dp_sink_cb.rx_audio_queue = nullptr;
328 btif_a2dp_sink_state = BTIF_A2DP_SINK_STATE_OFF;
329 }
330
btif_a2dp_sink_get_sample_rate()331 tA2DP_SAMPLE_RATE btif_a2dp_sink_get_sample_rate() {
332 LockGuard lock(g_mutex);
333 return btif_a2dp_sink_cb.sample_rate;
334 }
335
btif_a2dp_sink_get_bits_per_sample()336 tA2DP_BITS_PER_SAMPLE btif_a2dp_sink_get_bits_per_sample() {
337 LockGuard lock(g_mutex);
338 return btif_a2dp_sink_cb.bits_per_sample;
339 }
340
btif_a2dp_sink_get_channel_count()341 tA2DP_CHANNEL_COUNT btif_a2dp_sink_get_channel_count() {
342 LockGuard lock(g_mutex);
343 return btif_a2dp_sink_cb.channel_count;
344 }
345
btif_a2dp_sink_command_ready(BT_HDR * p_msg)346 static void btif_a2dp_sink_command_ready(BT_HDR* p_msg) {
347 LOG_VERBOSE(LOG_TAG, "%s: event %d %s", __func__, p_msg->event,
348 dump_media_event(p_msg->event));
349
350 switch (p_msg->event) {
351 case BTIF_MEDIA_SINK_DECODER_UPDATE:
352 btif_a2dp_sink_decoder_update_event(
353 (tBTIF_MEDIA_SINK_DECODER_UPDATE*)p_msg);
354 break;
355 case BTIF_MEDIA_SINK_CLEAR_TRACK:
356 btif_a2dp_sink_clear_track_event();
357 break;
358 case BTIF_MEDIA_SINK_SET_FOCUS_STATE: {
359 btif_a2dp_sink_focus_state_t state =
360 ((tBTIF_MEDIA_SINK_FOCUS_UPDATE*)p_msg)->focus_state;
361 btif_a2dp_sink_set_focus_state_event(state);
362 break;
363 }
364 case BTIF_MEDIA_SINK_AUDIO_RX_FLUSH:
365 btif_a2dp_sink_audio_rx_flush_event();
366 break;
367 default:
368 LOG_ERROR(LOG_TAG, "%s: unknown event %d", __func__, p_msg->event);
369 break;
370 }
371
372 osi_free(p_msg);
373 LOG_VERBOSE(LOG_TAG, "%s: %s DONE", __func__, dump_media_event(p_msg->event));
374 }
375
btif_a2dp_sink_update_decoder(const uint8_t * p_codec_info)376 void btif_a2dp_sink_update_decoder(const uint8_t* p_codec_info) {
377 LOG_INFO(LOG_TAG, "%s", __func__);
378 tBTIF_MEDIA_SINK_DECODER_UPDATE* p_buf =
379 reinterpret_cast<tBTIF_MEDIA_SINK_DECODER_UPDATE*>(
380 osi_malloc(sizeof(tBTIF_MEDIA_SINK_DECODER_UPDATE)));
381
382 APPL_TRACE_EVENT("%s: p_codec_info[%x:%x:%x:%x:%x:%x]", __func__,
383 p_codec_info[1], p_codec_info[2], p_codec_info[3],
384 p_codec_info[4], p_codec_info[5], p_codec_info[6]);
385
386 memcpy(p_buf->codec_info, p_codec_info, AVDT_CODEC_SIZE);
387 p_buf->hdr.event = BTIF_MEDIA_SINK_DECODER_UPDATE;
388
389 btif_a2dp_sink_cb.worker_thread.DoInThread(
390 FROM_HERE, base::BindOnce(btif_a2dp_sink_command_ready, (BT_HDR*)p_buf));
391 }
392
btif_a2dp_sink_on_idle()393 void btif_a2dp_sink_on_idle() {
394 LOG_INFO(LOG_TAG, "%s", __func__);
395 if (btif_a2dp_sink_state == BTIF_A2DP_SINK_STATE_OFF) return;
396 btif_a2dp_sink_audio_handle_stop_decoding();
397 btif_a2dp_sink_clear_track_event_req();
398 }
399
btif_a2dp_sink_on_stopped(UNUSED_ATTR tBTA_AV_SUSPEND * p_av_suspend)400 void btif_a2dp_sink_on_stopped(UNUSED_ATTR tBTA_AV_SUSPEND* p_av_suspend) {
401 LOG_INFO(LOG_TAG, "%s", __func__);
402 if (btif_a2dp_sink_state == BTIF_A2DP_SINK_STATE_OFF) return;
403 btif_a2dp_sink_audio_handle_stop_decoding();
404 }
405
btif_a2dp_sink_on_suspended(UNUSED_ATTR tBTA_AV_SUSPEND * p_av_suspend)406 void btif_a2dp_sink_on_suspended(UNUSED_ATTR tBTA_AV_SUSPEND* p_av_suspend) {
407 LOG_INFO(LOG_TAG, "%s", __func__);
408 if (btif_a2dp_sink_state == BTIF_A2DP_SINK_STATE_OFF) return;
409 btif_a2dp_sink_audio_handle_stop_decoding();
410 }
411
btif_a2dp_sink_audio_handle_stop_decoding()412 static void btif_a2dp_sink_audio_handle_stop_decoding() {
413 LOG_INFO(LOG_TAG, "%s", __func__);
414 alarm_t* old_alarm;
415 {
416 LockGuard lock(g_mutex);
417 btif_a2dp_sink_cb.rx_flush = true;
418 btif_a2dp_sink_audio_rx_flush_req();
419 old_alarm = btif_a2dp_sink_cb.decode_alarm;
420 btif_a2dp_sink_cb.decode_alarm = nullptr;
421 }
422
423 // Drop the lock here, btif_decode_alarm_cb may in the process of being called
424 // while we alarm free leading to deadlock.
425 //
426 // alarm_free waits for btif_decode_alarm_cb which is waiting for g_mutex.
427 alarm_free(old_alarm);
428
429 {
430 LockGuard lock(g_mutex);
431 #ifndef OS_GENERIC
432 BtifAvrcpAudioTrackPause(btif_a2dp_sink_cb.audio_track);
433 #endif
434 }
435 }
436
btif_decode_alarm_cb(UNUSED_ATTR void * context)437 static void btif_decode_alarm_cb(UNUSED_ATTR void* context) {
438 LockGuard lock(g_mutex);
439 btif_a2dp_sink_cb.worker_thread.DoInThread(
440 FROM_HERE, base::BindOnce(btif_a2dp_sink_avk_handle_timer));
441 }
442
btif_a2dp_sink_clear_track_event()443 static void btif_a2dp_sink_clear_track_event() {
444 LOG_INFO(LOG_TAG, "%s", __func__);
445 LockGuard lock(g_mutex);
446
447 #ifndef OS_GENERIC
448 BtifAvrcpAudioTrackStop(btif_a2dp_sink_cb.audio_track);
449 BtifAvrcpAudioTrackDelete(btif_a2dp_sink_cb.audio_track);
450 #endif
451 btif_a2dp_sink_cb.audio_track = nullptr;
452 }
453
454 // Must be called while locked.
btif_a2dp_sink_audio_handle_start_decoding()455 static void btif_a2dp_sink_audio_handle_start_decoding() {
456 LOG_INFO(LOG_TAG, "%s", __func__);
457 if (btif_a2dp_sink_cb.decode_alarm != nullptr)
458 return; // Already started decoding
459
460 #ifndef OS_GENERIC
461 BtifAvrcpAudioTrackStart(btif_a2dp_sink_cb.audio_track);
462 #endif
463
464 btif_a2dp_sink_cb.decode_alarm = alarm_new_periodic("btif.a2dp_sink_decode");
465 if (btif_a2dp_sink_cb.decode_alarm == nullptr) {
466 LOG_ERROR(LOG_TAG, "%s: unable to allocate decode alarm", __func__);
467 return;
468 }
469 alarm_set(btif_a2dp_sink_cb.decode_alarm, BTIF_SINK_MEDIA_TIME_TICK_MS,
470 btif_decode_alarm_cb, nullptr);
471 }
472
btif_a2dp_sink_on_decode_complete(uint8_t * data,uint32_t len)473 static void btif_a2dp_sink_on_decode_complete(uint8_t* data, uint32_t len) {
474 #ifndef OS_GENERIC
475 BtifAvrcpAudioTrackWriteData(btif_a2dp_sink_cb.audio_track,
476 reinterpret_cast<void*>(data), len);
477 #endif
478 }
479
480 // Must be called while locked.
btif_a2dp_sink_handle_inc_media(BT_HDR * p_msg)481 static void btif_a2dp_sink_handle_inc_media(BT_HDR* p_msg) {
482 if ((btif_av_get_peer_sep() == AVDT_TSEP_SNK) ||
483 (btif_a2dp_sink_cb.rx_flush)) {
484 APPL_TRACE_DEBUG("%s: state changed happened in this tick", __func__);
485 return;
486 }
487
488 CHECK(btif_a2dp_sink_cb.decoder_interface != nullptr);
489 if (!btif_a2dp_sink_cb.decoder_interface->decode_packet(p_msg)) {
490 LOG_ERROR(LOG_TAG, "%s: decoding failed", __func__);
491 }
492 }
493
btif_a2dp_sink_avk_handle_timer()494 static void btif_a2dp_sink_avk_handle_timer() {
495 LockGuard lock(g_mutex);
496
497 BT_HDR* p_msg;
498 if (fixed_queue_is_empty(btif_a2dp_sink_cb.rx_audio_queue)) {
499 APPL_TRACE_DEBUG("%s: empty queue", __func__);
500 return;
501 }
502
503 /* Don't do anything in case of focus not granted */
504 if (btif_a2dp_sink_cb.rx_focus_state == BTIF_A2DP_SINK_FOCUS_NOT_GRANTED) {
505 APPL_TRACE_DEBUG("%s: skipping frames since focus is not present",
506 __func__);
507 return;
508 }
509 /* Play only in BTIF_A2DP_SINK_FOCUS_GRANTED case */
510 if (btif_a2dp_sink_cb.rx_flush) {
511 fixed_queue_flush(btif_a2dp_sink_cb.rx_audio_queue, osi_free);
512 return;
513 }
514
515 APPL_TRACE_DEBUG("%s: process frames begin", __func__);
516 while (true) {
517 p_msg = (BT_HDR*)fixed_queue_try_dequeue(btif_a2dp_sink_cb.rx_audio_queue);
518 if (p_msg == NULL) {
519 break;
520 }
521 APPL_TRACE_DEBUG("%s: number of packets in queue %zu", __func__,
522 fixed_queue_length(btif_a2dp_sink_cb.rx_audio_queue));
523
524 /* Queue packet has less frames */
525 btif_a2dp_sink_handle_inc_media(p_msg);
526 osi_free(p_msg);
527 }
528 APPL_TRACE_DEBUG("%s: process frames end", __func__);
529 }
530
531 /* when true media task discards any rx frames */
btif_a2dp_sink_set_rx_flush(bool enable)532 void btif_a2dp_sink_set_rx_flush(bool enable) {
533 LOG_INFO(LOG_TAG, "%s: enable=%s", __func__, (enable) ? "true" : "false");
534 LockGuard lock(g_mutex);
535
536 btif_a2dp_sink_cb.rx_flush = enable;
537 }
538
btif_a2dp_sink_audio_rx_flush_event()539 static void btif_a2dp_sink_audio_rx_flush_event() {
540 LOG_INFO(LOG_TAG, "%s", __func__);
541 LockGuard lock(g_mutex);
542 // Flush all received encoded audio buffers
543 fixed_queue_flush(btif_a2dp_sink_cb.rx_audio_queue, osi_free);
544 }
545
btif_a2dp_sink_decoder_update_event(tBTIF_MEDIA_SINK_DECODER_UPDATE * p_buf)546 static void btif_a2dp_sink_decoder_update_event(
547 tBTIF_MEDIA_SINK_DECODER_UPDATE* p_buf) {
548 LOG_INFO(LOG_TAG, "%s", __func__);
549 LockGuard lock(g_mutex);
550 APPL_TRACE_DEBUG("%s: p_codec_info[%x:%x:%x:%x:%x:%x]", __func__,
551 p_buf->codec_info[1], p_buf->codec_info[2],
552 p_buf->codec_info[3], p_buf->codec_info[4],
553 p_buf->codec_info[5], p_buf->codec_info[6]);
554
555 int sample_rate = A2DP_GetTrackSampleRate(p_buf->codec_info);
556 if (sample_rate == -1) {
557 LOG_ERROR(LOG_TAG, "%s: cannot get the track frequency", __func__);
558 return;
559 }
560 int bits_per_sample = A2DP_GetTrackBitsPerSample(p_buf->codec_info);
561 if (bits_per_sample == -1) {
562 LOG_ERROR(LOG_TAG, "%s: cannot get the bits per sample", __func__);
563 return;
564 }
565 int channel_count = A2DP_GetTrackChannelCount(p_buf->codec_info);
566 if (channel_count == -1) {
567 LOG_ERROR(LOG_TAG, "%s: cannot get the channel count", __func__);
568 return;
569 }
570 int channel_type = A2DP_GetSinkTrackChannelType(p_buf->codec_info);
571 if (channel_type == -1) {
572 LOG_ERROR(LOG_TAG, "%s: cannot get the Sink channel type", __func__);
573 return;
574 }
575 btif_a2dp_sink_cb.sample_rate = sample_rate;
576 btif_a2dp_sink_cb.bits_per_sample = bits_per_sample;
577 btif_a2dp_sink_cb.channel_count = channel_count;
578
579 btif_a2dp_sink_cb.rx_flush = false;
580 APPL_TRACE_DEBUG("%s: reset to Sink role", __func__);
581
582 btif_a2dp_sink_cb.decoder_interface = bta_av_co_get_decoder_interface();
583 if (btif_a2dp_sink_cb.decoder_interface == nullptr) {
584 LOG_ERROR(LOG_TAG, "%s: cannot stream audio: no source decoder interface",
585 __func__);
586 return;
587 }
588
589 if (!btif_a2dp_sink_cb.decoder_interface->decoder_init(
590 btif_a2dp_sink_on_decode_complete)) {
591 LOG_ERROR(LOG_TAG, "%s: failed to initialize decoder", __func__);
592 return;
593 }
594
595 APPL_TRACE_DEBUG("%s: create audio track", __func__);
596 btif_a2dp_sink_cb.audio_track =
597 #ifndef OS_GENERIC
598 BtifAvrcpAudioTrackCreate(sample_rate, bits_per_sample, channel_type);
599 #else
600 NULL;
601 #endif
602 if (btif_a2dp_sink_cb.audio_track == nullptr) {
603 LOG_ERROR(LOG_TAG, "%s: track creation failed", __func__);
604 return;
605 }
606 }
607
btif_a2dp_sink_enqueue_buf(BT_HDR * p_pkt)608 uint8_t btif_a2dp_sink_enqueue_buf(BT_HDR* p_pkt) {
609 LockGuard lock(g_mutex);
610 if (btif_a2dp_sink_cb.rx_flush) /* Flush enabled, do not enqueue */
611 return fixed_queue_length(btif_a2dp_sink_cb.rx_audio_queue);
612
613 if (fixed_queue_length(btif_a2dp_sink_cb.rx_audio_queue) ==
614 MAX_INPUT_A2DP_FRAME_QUEUE_SZ) {
615 uint8_t ret = fixed_queue_length(btif_a2dp_sink_cb.rx_audio_queue);
616 osi_free(fixed_queue_try_dequeue(btif_a2dp_sink_cb.rx_audio_queue));
617 return ret;
618 }
619
620 BTIF_TRACE_VERBOSE("%s +", __func__);
621 /* Allocate and queue this buffer */
622 BT_HDR* p_msg =
623 reinterpret_cast<BT_HDR*>(osi_malloc(sizeof(*p_msg) + p_pkt->len));
624 memcpy(p_msg, p_pkt, sizeof(*p_msg));
625 p_msg->offset = 0;
626 memcpy(p_msg->data, p_pkt->data + p_pkt->offset, p_pkt->len);
627 fixed_queue_enqueue(btif_a2dp_sink_cb.rx_audio_queue, p_msg);
628 if (fixed_queue_length(btif_a2dp_sink_cb.rx_audio_queue) ==
629 MAX_A2DP_DELAYED_START_FRAME_COUNT) {
630 BTIF_TRACE_DEBUG("%s: Initiate decoding", __func__);
631 btif_a2dp_sink_audio_handle_start_decoding();
632 }
633
634 return fixed_queue_length(btif_a2dp_sink_cb.rx_audio_queue);
635 }
636
btif_a2dp_sink_audio_rx_flush_req()637 void btif_a2dp_sink_audio_rx_flush_req() {
638 LOG_INFO(LOG_TAG, "%s", __func__);
639 if (fixed_queue_is_empty(btif_a2dp_sink_cb.rx_audio_queue)) {
640 /* Queue is already empty */
641 return;
642 }
643
644 BT_HDR* p_buf = reinterpret_cast<BT_HDR*>(osi_malloc(sizeof(BT_HDR)));
645 p_buf->event = BTIF_MEDIA_SINK_AUDIO_RX_FLUSH;
646 btif_a2dp_sink_cb.worker_thread.DoInThread(
647 FROM_HERE, base::BindOnce(btif_a2dp_sink_command_ready, p_buf));
648 }
649
btif_a2dp_sink_debug_dump(UNUSED_ATTR int fd)650 void btif_a2dp_sink_debug_dump(UNUSED_ATTR int fd) {
651 // Nothing to do
652 }
653
btif_a2dp_sink_set_focus_state_req(btif_a2dp_sink_focus_state_t state)654 void btif_a2dp_sink_set_focus_state_req(btif_a2dp_sink_focus_state_t state) {
655 LOG_INFO(LOG_TAG, "%s", __func__);
656 tBTIF_MEDIA_SINK_FOCUS_UPDATE* p_buf =
657 reinterpret_cast<tBTIF_MEDIA_SINK_FOCUS_UPDATE*>(
658 osi_malloc(sizeof(tBTIF_MEDIA_SINK_FOCUS_UPDATE)));
659 p_buf->focus_state = state;
660 p_buf->hdr.event = BTIF_MEDIA_SINK_SET_FOCUS_STATE;
661 btif_a2dp_sink_cb.worker_thread.DoInThread(
662 FROM_HERE, base::BindOnce(btif_a2dp_sink_command_ready, (BT_HDR*)p_buf));
663 }
664
btif_a2dp_sink_set_focus_state_event(btif_a2dp_sink_focus_state_t state)665 static void btif_a2dp_sink_set_focus_state_event(
666 btif_a2dp_sink_focus_state_t state) {
667 LOG_INFO(LOG_TAG, "%s: state=%d", __func__, state);
668 LockGuard lock(g_mutex);
669
670 if (!btif_av_is_connected()) return;
671 APPL_TRACE_DEBUG("%s: setting focus state to %d", __func__, state);
672 btif_a2dp_sink_cb.rx_focus_state = state;
673 if (btif_a2dp_sink_cb.rx_focus_state == BTIF_A2DP_SINK_FOCUS_NOT_GRANTED) {
674 fixed_queue_flush(btif_a2dp_sink_cb.rx_audio_queue, osi_free);
675 btif_a2dp_sink_cb.rx_flush = true;
676 } else if (btif_a2dp_sink_cb.rx_focus_state == BTIF_A2DP_SINK_FOCUS_GRANTED) {
677 btif_a2dp_sink_cb.rx_flush = false;
678 }
679 }
680
btif_a2dp_sink_set_audio_track_gain(float gain)681 void btif_a2dp_sink_set_audio_track_gain(float gain) {
682 LOG_INFO(LOG_TAG, "%s: set gain to %f", __func__, gain);
683 LockGuard lock(g_mutex);
684
685 #ifndef OS_GENERIC
686 BtifAvrcpSetAudioTrackGain(btif_a2dp_sink_cb.audio_track, gain);
687 #endif
688 }
689
btif_a2dp_sink_clear_track_event_req()690 static void btif_a2dp_sink_clear_track_event_req() {
691 LOG_INFO(LOG_TAG, "%s", __func__);
692 BT_HDR* p_buf = reinterpret_cast<BT_HDR*>(osi_malloc(sizeof(BT_HDR)));
693
694 p_buf->event = BTIF_MEDIA_SINK_CLEAR_TRACK;
695 btif_a2dp_sink_cb.worker_thread.DoInThread(
696 FROM_HERE, base::BindOnce(btif_a2dp_sink_command_ready, p_buf));
697 }
698