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