Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bd740f75 authored by Sal Savage's avatar Sal Savage
Browse files

Check for decoding start even if we hit the max buffer size

Problem: A2DP Sink could get into a state where we would lock up with
the inability to decode. This was happening because we had logic that
would check the size of the decoder buffer/queue and remove the front
element to trim it back down to size if we needed. The logic would then
return instead of continuing on to check if we can start decoding. If
we ever filled the queue before decoding started, we would get into a
feedback loop where we would hit max size, remove an element, return,
hit max size again, remove an element, return, etc. We would never check
to see if we can start decoding the data we're adding.

Solution: Don't return, and instead check to see if we have focus and
enough data to start decoding.

Flag: EXEMPT, trivial bug fix
Bug: 367403820
Test: m com.android.bt
Change-Id: I58351e939c70d73804b8c144b9204c54ca46bf94
parent 811f3370
Loading
Loading
Loading
Loading
+6 −4
Original line number Diff line number Diff line
@@ -732,6 +732,7 @@ uint8_t btif_a2dp_sink_enqueue_buf(BT_HDR* p_pkt) {
  }

  log::verbose("+");

  /* Allocate and queue this buffer */
  BT_HDR* p_msg = reinterpret_cast<BT_HDR*>(osi_malloc(sizeof(*p_msg) + p_pkt->len));
  memcpy(p_msg, p_pkt, sizeof(*p_msg));
@@ -739,17 +740,18 @@ uint8_t btif_a2dp_sink_enqueue_buf(BT_HDR* p_pkt) {
  memcpy(p_msg->data, p_pkt->data + p_pkt->offset, p_pkt->len);
  fixed_queue_enqueue(btif_a2dp_sink_cb.rx_audio_queue, p_msg);

  /* If the queue is full, pop the front off to make room for the new data */
  if (fixed_queue_length(btif_a2dp_sink_cb.rx_audio_queue) == MAX_INPUT_A2DP_FRAME_QUEUE_SZ) {
    log::verbose("Audio data buffer has reached max size. Dropping front packet");
    osi_free(fixed_queue_try_dequeue(btif_a2dp_sink_cb.rx_audio_queue));
    uint8_t ret = fixed_queue_length(btif_a2dp_sink_cb.rx_audio_queue);
    return ret;
  }

  // Avoid other checks if alarm has already been initialized.
  /* Check to see if we need to start decoding */
  if (btif_a2dp_sink_cb.decode_alarm == nullptr &&
      fixed_queue_length(btif_a2dp_sink_cb.rx_audio_queue) >= MAX_A2DP_DELAYED_START_FRAME_COUNT) {
    log::verbose("Initiate decoding. Current focus state:{}", btif_a2dp_sink_cb.rx_focus_state);
    log::verbose("Can initiate decoding, focus_state={}", btif_a2dp_sink_cb.rx_focus_state);
    if (btif_a2dp_sink_cb.rx_focus_state == BTIF_A2DP_SINK_FOCUS_GRANTED) {
      log::info("Request to begin decoding");
      btif_a2dp_sink_audio_handle_start_decoding();
    }
  }