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

Commit 9c3ca59c authored by Jakub Tyszkowski's avatar Jakub Tyszkowski
Browse files

LeAudio: Fix buffer size type conversion for the SW encoder

The LC3 software codec uses the value of -1 to indicate the invalid number
of samples, while the codec interface uses 0 (and unsigned data type).
With this fix we handle -1 gracefully.

Bug: 377177980
Test: m -j
Flag: Exempt; trivial fix - no functional changes
Change-Id: Ibc970a240fbedba0e9c0cdb887a0a7495e25a946
parent a411fa9a
Loading
Loading
Loading
Loading
+16 −2
Original line number Diff line number Diff line
@@ -80,8 +80,14 @@ struct CodecInterface::Impl {
              (pcm_config_->bits_per_sample == 24) ? LC3_PCM_FORMAT_S24 : LC3_PCM_FORMAT_S16;

      // Prepare the decoded output buffer
      output_channel_samples_ =
      auto num_samples =
              lc3_frame_samples(bt_codec_config_.data_interval_us, pcm_config_->sample_rate);
      if (num_samples == -1) {
        log::error("Could not determine the sample count for data_interval: {}, sample_rate: {}",
                   bt_codec_config_.data_interval_us, pcm_config_->sample_rate);
        return CodecInterface::Status::STATUS_ERR_CODEC_NOT_READY;
      }
      output_channel_samples_ = num_samples;
      adjustOutputBufferSizeIfNeeded(&output_channel_data_);

      // Prepare the decoder
@@ -185,7 +191,14 @@ struct CodecInterface::Impl {
    }

    if (codec_id_.coding_format == types::kLeAudioCodingFormatLC3) {
      return lc3_frame_samples(bt_codec_config_.data_interval_us, pcm_config_->sample_rate);
      auto num_samples =
              lc3_frame_samples(bt_codec_config_.data_interval_us, pcm_config_->sample_rate);
      if (num_samples == -1) {
        log::error("Could not determine the sample count for data_interval: {}, sample_rate: {}",
                   bt_codec_config_.data_interval_us, pcm_config_->sample_rate);
        return 0;
      }
      return num_samples;
    }

    log::error("Invalid codec ID: [{}:{}:{}]", codec_id_.coding_format, codec_id_.vendor_company_id,
@@ -206,6 +219,7 @@ struct CodecInterface::Impl {
private:
  inline void adjustOutputBufferSizeIfNeeded(std::vector<int16_t>* out_buffer) {
    if (out_buffer->size() < output_channel_samples_) {
      log::debug("Changing buffer size to {}", output_channel_samples_);
      out_buffer->resize(output_channel_samples_);
    }
  }