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

Commit 15bfb90d authored by Yuyang Huang's avatar Yuyang Huang
Browse files

Add ASHA packet drop frequency limit

Some HA companies need packet sequence number to align with real world time.
Limit drop frequency to decrease clock drift heuristic's impact.

Test: manual
Bug: 233715486
Change-Id: Ie698626e27aa28d02a775287e68173e8e79c249e
parent 0d3404e3
Loading
Loading
Loading
Loading
+21 −2
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@
#include <base/logging.h>
#include <base/strings/string_number_conversions.h>  // HexEncode

#include <chrono>
#include <cstdint>
#include <mutex>
#include <vector>
@@ -33,6 +34,7 @@
#include "bta/include/bta_gatt_queue.h"
#include "bta/include/bta_hearing_aid_api.h"
#include "btm_iso_api.h"
#include "common/init_flags.h"
#include "device/include/controller.h"
#include "embdrv/g722/g722_enc_dec.h"
#include "osi/include/compat.h"
@@ -259,6 +261,11 @@ class HearingAidImpl : public HearingAid {
      "persist.bluetooth.hearing_aid_max_ce_len";
  // Record whether the connection parameter needs to update to a better one
  bool needs_parameter_update = false;
  std::chrono::time_point<std::chrono::steady_clock> last_drop_time_point =
      std::chrono::steady_clock::now();
  // at most 1 packet DROP per DROP_FREQUENCY_THRESHOLD seconds
  const int DROP_FREQUENCY_THRESHOLD =
      bluetooth::common::init_flags::get_asha_packet_drop_frequency_threshold();

 public:
  ~HearingAidImpl() override = default;
@@ -395,6 +402,16 @@ class HearingAidImpl : public HearingAid {
    return connection_interval;
  }

  bool IsBelowDropFrequency(
      std::chrono::time_point<std::chrono::steady_clock> tp) {
    auto duration = tp - last_drop_time_point;
    bool droppable =
        std::chrono::duration_cast<std::chrono::seconds>(duration).count() >=
        DROP_FREQUENCY_THRESHOLD;
    LOG_INFO("IsBelowDropFrequency %s", droppable ? "true" : "false");
    return droppable;
  }

  void Connect(const RawAddress& address) {
    LOG_DEBUG("%s", ADDRESS_TO_LOGGABLE_CSTR(address));
    hearingDevices.Add(HearingDevice(address, true));
@@ -1383,6 +1400,7 @@ class HearingAidImpl : public HearingAid {
    // reallocations
    // TODO: this should basically fit the encoded data, tune the size later
    std::vector<uint8_t> encoded_data_left;
    auto time_point = std::chrono::steady_clock::now();
    if (left) {
      // TODO: instead of a magic number, we need to figure out the correct
      // buffer size
@@ -1397,7 +1415,7 @@ class HearingAidImpl : public HearingAid {
      if (packets_in_chans) {
        // Compare the two sides LE CoC credit value to confirm need to drop or
        // skip audio packet.
        if (NeedToDropPacket(left, right)) {
        if (NeedToDropPacket(left, right) && IsBelowDropFrequency(time_point)) {
          LOG_INFO("%s triggers dropping, %u packets in channel",
                   ADDRESS_TO_LOGGABLE_CSTR(left->address),
                   packets_in_chans);
@@ -1431,7 +1449,7 @@ class HearingAidImpl : public HearingAid {
      if (packets_in_chans) {
        // Compare the two sides LE CoC credit value to confirm need to drop or
        // skip audio packet.
        if (NeedToDropPacket(right, left)) {
        if (NeedToDropPacket(right, left) && IsBelowDropFrequency(time_point)) {
          LOG_INFO("%s triggers dropping, %u packets in channel",
                   ADDRESS_TO_LOGGABLE_CSTR(right->address),
                   packets_in_chans);
@@ -1457,6 +1475,7 @@ class HearingAidImpl : public HearingAid {
        CalcCompressedAudioPacketSize(codec_in_use, default_data_interval_ms);

    if (need_drop) {
      last_drop_time_point = time_point;
      if (left) {
        left->audio_stats.packet_drop_count++;
      }
+1 −0
Original line number Diff line number Diff line
@@ -358,6 +358,7 @@ pub const LOG_TAG_VERBOSE: i32 = 6;
init_flags!(
    name: InitFlags
    flags: {
        asha_packet_drop_frequency_threshold: i32 = 60,
        always_send_services_if_gatt_disc_done = true,
        always_use_private_gatt_for_debugging,
        asynchronously_start_l2cap_coc = true,
+1 −0
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ mod ffi {
        fn get_default_log_level() -> i32;
        fn get_hci_adapter() -> i32;
        fn get_log_level_for_tag(tag: &str) -> i32;
        fn get_asha_packet_drop_frequency_threshold() -> i32;
        fn hfp_dynamic_version_is_enabled() -> bool;
        fn irk_rotation_is_enabled() -> bool;
        fn leaudio_targeted_announcement_reconnection_mode_is_enabled() -> bool;