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

Commit 948eaa89 authored by Łukasz Rymanowski's avatar Łukasz Rymanowski Committed by Automerger Merge Worker
Browse files

leaudio: Remove old LC3 encoder am: c7269884 am: 3fd7100d am: 77f65896

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Bluetooth/+/1927374

Change-Id: Ida3600997e9f3b4dd5382b52b990dfe00f912afb
parents e8c926c2 77f65896
Loading
Loading
Loading
Loading
+66 −123
Original line number Diff line number Diff line
@@ -120,8 +120,6 @@ CigCallbacks* stateMachineHciCallbacks;
LeAudioGroupStateMachine::Callbacks* stateMachineCallbacks;
DeviceGroupsCallbacks* device_group_callbacks;

bool use_new_encoder = true;

/*
 * Coordinatet Set Identification Profile (CSIP) based on CSIP 1.0
 * and Coordinatet Set Identification Service (CSIS) 1.0
@@ -173,7 +171,6 @@ class LeAudioClientImpl : public LeAudioClient {
        current_sink_codec_config({0, 0, 0, 0}),
        lc3_encoder_left_mem(nullptr),
        lc3_encoder_right_mem(nullptr),
        lc3_encoder(nullptr),
        lc3_decoder(nullptr),
        audio_source_instance_(nullptr),
        audio_sink_instance_(nullptr) {
@@ -1778,19 +1775,38 @@ class LeAudioClientImpl : public LeAudioClient {
    return true;
  }

  void get_mono_stream(const std::vector<uint8_t>& data,
                       std::vector<int16_t>& chan_mono) {
    uint16_t num_of_frames_per_ch;

    int dt_us = current_source_codec_config.data_interval_us;
    int sr_hz = current_source_codec_config.sample_rate;
    num_of_frames_per_ch = lc3_frame_samples(dt_us, sr_hz);

    chan_mono.reserve(num_of_frames_per_ch);
    for (int i = 0; i < num_of_frames_per_ch; i++) {
      const uint8_t* sample = data.data() + i * 4;

      int16_t left = (int16_t)((*(sample + 1) << 8) + *sample) >> 1;

      sample += 2;
      int16_t right = (int16_t)((*(sample + 1) << 8) + *sample) >> 1;

      uint16_t mono_data = (int16_t)(((uint32_t)left + (uint32_t)right) >> 1);

      chan_mono.push_back(mono_data);
    }
  }

  void get_left_and_right_stream(const std::vector<uint8_t>& data,
                                 std::vector<int16_t>& chan_left,
                                 std::vector<int16_t>& chan_right,
                                 bool prepare_mono = false) {
    uint16_t num_of_frames_per_ch;

    if (use_new_encoder) {
    int dt_us = current_source_codec_config.data_interval_us;
    int sr_hz = current_source_codec_config.sample_rate;
    num_of_frames_per_ch = lc3_frame_samples(dt_us, sr_hz);
    } else {
      num_of_frames_per_ch = lc3_encoder->lc3Config.NF;
    }

    chan_left.reserve(num_of_frames_per_ch);
    chan_right.reserve(num_of_frames_per_ch);
@@ -1821,13 +1837,9 @@ class LeAudioClientImpl : public LeAudioClient {
    uint16_t right_cis_handle = 0;
    uint16_t number_of_required_samples_per_channel;

    if (use_new_encoder) {
    int dt_us = current_source_codec_config.data_interval_us;
    int sr_hz = current_source_codec_config.sample_rate;
    number_of_required_samples_per_channel = lc3_frame_samples(dt_us, sr_hz);
    } else {
      number_of_required_samples_per_channel = lc3_encoder->lc3Config.NF;
    }

    for (auto [cis_handle, audio_location] : stream_conf->sink_streams) {
      if (audio_location & le_audio::codec_spec_conf::kLeAudioLocationAnyLeft)
@@ -1848,8 +1860,7 @@ class LeAudioClientImpl : public LeAudioClient {
    std::vector<uint8_t> chan_right_enc(byte_count, 0);

    bool mono = (left_cis_handle == 0) || (right_cis_handle == 0);

    if (!mono && use_new_encoder) {
    if (!mono) {
      lc3_encode(lc3_encoder_left, (const int16_t*)data.data(), 2,
                 chan_left_enc.data(), chan_left_enc.size());
      lc3_encode(lc3_encoder_right, ((const int16_t*)data.data()) + 1, 2,
@@ -1859,35 +1870,14 @@ class LeAudioClientImpl : public LeAudioClient {
      std::vector<int16_t> chan_right;
      get_left_and_right_stream(data, chan_left, chan_right, mono);

      uint8_t err = 0;
      if (left_cis_handle) {
        if (use_new_encoder) {
        lc3_encode(lc3_encoder_left, (const int16_t*)chan_left.data(), 1,
                   chan_left_enc.data(), chan_left_enc.size());
        } else {
          err |=
              lc3_encoder->run((const int16_t*)chan_left.data(),
                               chan_left_enc.size(), chan_left_enc.data(), 0);
        }
      }

      if (right_cis_handle) {
        if (use_new_encoder) {
        lc3_encode(lc3_encoder_right, (const int16_t*)chan_right.data(), 1,
                   chan_right_enc.data(), chan_right_enc.size());

        } else {
          err |=
              lc3_encoder->run((const int16_t*)chan_right.data(),
                               chan_right_enc.size(), chan_right_enc.data(), 1);
        }
      }

      if (err != Lc3Encoder::ERROR_FREE) {
        LOG(ERROR) << " error while encoding; error code: "
                   << "\t encoded samples: " << chan_left_enc.size()
                   << "\t err: " << static_cast<uint8_t>(err);
        return;
      }
    }

@@ -1904,18 +1894,14 @@ class LeAudioClientImpl : public LeAudioClient {
  void PrepareAndSendToSingleDevice(
      const std::vector<uint8_t>& data,
      struct le_audio::stream_configuration* stream_conf) {
    int num_channels = current_source_codec_config.num_channels;
    int num_channels = stream_conf->sink_num_of_channels;
    uint16_t byte_count = stream_conf->sink_octets_per_codec_frame;
    auto cis_handle = stream_conf->sink_streams.front().first;
    uint16_t number_of_required_samples_per_channel;

    if (use_new_encoder) {
    int dt_us = current_source_codec_config.data_interval_us;
    int sr_hz = current_source_codec_config.sample_rate;
    number_of_required_samples_per_channel = lc3_frame_samples(dt_us, sr_hz);
    } else {
      number_of_required_samples_per_channel = lc3_encoder->lc3Config.NF;
    }

    if ((int)data.size() < (2 /* bytes per sample */ * num_channels *
                            number_of_required_samples_per_channel)) {
@@ -1923,41 +1909,25 @@ class LeAudioClientImpl : public LeAudioClient {
      return;
    }
    std::vector<uint8_t> chan_encoded(num_channels * byte_count, 0);
    uint8_t err = 0;

    if (num_channels == 1) {
      if (use_new_encoder) {
        lc3_encode(lc3_encoder_left, (const int16_t*)data.data(), 1,
      /* Since we always get two channels from framework, lets make it mono here
       */
      std::vector<int16_t> chan_mono;
      get_mono_stream(data, chan_mono);

      lc3_encode(lc3_encoder_left, (const int16_t*)chan_mono.data(), 1,
                 chan_encoded.data(), byte_count);
      } else {
        err = lc3_encoder->run((const int16_t*)data.data(), byte_count,
                               chan_encoded.data(), 0);
      }

    } else {
      std::vector<int16_t> chan_left;
      std::vector<int16_t> chan_right;
      get_left_and_right_stream(data, chan_left, chan_right, false);

      if (use_new_encoder) {
      lc3_encode(lc3_encoder_left, (const int16_t*)chan_left.data(), 1,
                 chan_encoded.data(), byte_count);
      } else {
        err |= lc3_encoder->run((const int16_t*)chan_left.data(), byte_count,
                                chan_encoded.data(), 0);
      }

      if (use_new_encoder) {
      lc3_encode(lc3_encoder_right, (const int16_t*)chan_right.data(), 1,
                 chan_encoded.data() + byte_count, byte_count);
      } else {
        err |= lc3_encoder->run((const int16_t*)chan_right.data(), byte_count,
                                chan_encoded.data() + byte_count, 1);
      }
    }

    if (err != Lc3Encoder::ERROR_FREE) {
      LOG(ERROR) << " error while encoding; error code: "
                 << "\t err: " << static_cast<uint8_t>(err);
      return;
    }

    /* Send data to the controller */
@@ -2129,7 +2099,6 @@ class LeAudioClientImpl : public LeAudioClient {
      return false;
    }

    if (use_new_encoder) {
    if (lc3_encoder_left_mem) {
      LOG(WARNING)
          << " The encoder instance should have been already released.";
@@ -2146,23 +2115,7 @@ class LeAudioClientImpl : public LeAudioClient {
    lc3_encoder_right_mem = malloc(enc_size);

    lc3_encoder_left = lc3_setup_encoder(dt_us, sr_hz, lc3_encoder_left_mem);
      lc3_encoder_right =
          lc3_setup_encoder(dt_us, sr_hz, lc3_encoder_right_mem);

    } else {
      if (lc3_encoder) {
        LOG(WARNING)
            << " The encoder instance should have been already released.";
        delete lc3_encoder;
        lc3_encoder = nullptr;
      }

      /* One or multiple audio channels encoder */
      lc3_encoder = new Lc3Encoder(Lc3Config(
          current_source_codec_config.sample_rate,
          Lc3ConfigFrameDuration(current_source_codec_config.data_interval_us),
          current_source_codec_config.num_channels));
    }
    lc3_encoder_right = lc3_setup_encoder(dt_us, sr_hz, lc3_encoder_right_mem);

    uint16_t remote_delay_ms =
        group->GetRemoteDelay(le_audio::types::kLeAudioDirectionSink);
@@ -2251,11 +2204,6 @@ class LeAudioClientImpl : public LeAudioClient {
      lc3_encoder_right_mem = nullptr;
    }

    if (lc3_encoder) {
      delete lc3_encoder;
      lc3_encoder = nullptr;
    }

    if (lc3_decoder) {
      LOG(INFO) << __func__ << " stopping sink";

@@ -3043,7 +2991,6 @@ class LeAudioClientImpl : public LeAudioClient {
  lc3_encoder_t lc3_encoder_left;
  lc3_encoder_t lc3_encoder_right;

  Lc3Encoder* lc3_encoder;
  Lc3Decoder* lc3_decoder;
  std::vector<uint8_t> encoded_data;
  const void* audio_source_instance_;
@@ -3273,10 +3220,6 @@ void LeAudioClient::Initialize(
      << ", LE Audio Client requires Bluetooth Audio HAL V2.1 at least. Either "
         "disable LE Audio Profile, or update your HAL";

  use_new_encoder =
      osi_property_get_bool("persist.bluetooth.use_new_lc3", true);
  LOG(INFO) << "use_new_encoder = " << +use_new_encoder;

  IsoManager::GetInstance()->Start();

  audioSinkReceiver = &audioSinkReceiverImpl;