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

Commit 87130fa1 authored by Cheney Ni's avatar Cheney Ni
Browse files

Release encoder state when cleaning up the hearing aids instance

When the Bluetooth state changes from ON to BLE_ON, the hearing aid
instance was cleaned up, but not encoder state. Since the Bluetooth
process is kept at BLE_ON but not exited, the new instance of the
hearing aids would see the encoder as initialized without starting a new
Bluetooth audio session, and caused the audio HAL to be unable to talk
to the stack. We now reset the encoder state when cleaning up the
instance, so it will start a session next time during the first
connection of a new hearing aid instance.

Bug: 127610666
Test: ON / OFF BT with BLE_ON and switch active device manually
Change-Id: I426fed4ea22c0b858bee273727fca6e2e7481e84
parent 95c949f4
Loading
Loading
Loading
Loading
+24 −14
Original line number Diff line number Diff line
@@ -171,6 +171,24 @@ static void write_rpt_ctl_cfg_cb(uint16_t conn_id, tGATT_STATUS status,
g722_encode_state_t* encoder_state_left = nullptr;
g722_encode_state_t* encoder_state_right = nullptr;

inline void encoder_state_init() {
  if (encoder_state_left != nullptr) {
    LOG(WARNING) << __func__ << ": encoder already initialized";
    return;
  }
  encoder_state_left = g722_encode_init(nullptr, 64000, G722_PACKED);
  encoder_state_right = g722_encode_init(nullptr, 64000, G722_PACKED);
}

inline void encoder_state_release() {
  if (encoder_state_left != nullptr) {
    g722_encode_release(encoder_state_left);
    encoder_state_left = nullptr;
    g722_encode_release(encoder_state_right);
    encoder_state_right = nullptr;
  }
}

class HearingAidImpl : public HearingAid {
 private:
  // Keep track of whether the Audio Service has resumed audio playback
@@ -846,8 +864,7 @@ class HearingAidImpl : public HearingAid {
    VLOG(0) << __func__ << ": device=" << hearingDevice.address;

    if (encoder_state_left == nullptr) {
      encoder_state_left = g722_encode_init(nullptr, 64000, G722_PACKED);
      encoder_state_right = g722_encode_init(nullptr, 64000, G722_PACKED);
      encoder_state_init();
      seq_counter = 0;

      // use the best codec avaliable for this pair of devices.
@@ -909,12 +926,8 @@ class HearingAidImpl : public HearingAid {
    audio_running = true;

    // TODO: shall we also reset the encoder ?
    if (encoder_state_left != nullptr) {
      g722_encode_release(encoder_state_left);
      g722_encode_release(encoder_state_right);
    }
    encoder_state_left = g722_encode_init(nullptr, 64000, G722_PACKED);
    encoder_state_right = g722_encode_init(nullptr, 64000, G722_PACKED);
    encoder_state_release();
    encoder_state_init();
    seq_counter = 0;

    for (auto& device : hearingDevices.devices) {
@@ -1002,12 +1015,7 @@ class HearingAidImpl : public HearingAid {

    if (left == nullptr && right == nullptr) {
      HearingAidAudioSource::Stop();
      if (encoder_state_left != nullptr) {
        g722_encode_release(encoder_state_left);
        encoder_state_left = nullptr;
        g722_encode_release(encoder_state_right);
        encoder_state_right = nullptr;
      }
      encoder_state_release();
      current_volume = VOLUME_UNKNOWN;
      return;
    }
@@ -1360,6 +1368,8 @@ class HearingAidImpl : public HearingAid {
    }

    hearingDevices.devices.clear();

    encoder_state_release();
  }

 private: