1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64

media / base / limiting_audio_queue.cc [blame]

// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/base/limiting_audio_queue.h"

#include "base/logging.h"
#include "media/base/audio_timestamp_helper.h"

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/373960632): Replace unsafe usage once AudioBus is spanified.
#pragma allow_unsafe_buffers
#endif

namespace media {

LimitingAudioQueue::LimitingAudioQueue(ChannelLayout channel_layout,
                                       int sample_rate,
                                       int channels,
                                       int max_buffer_size)
    : channel_layout_(channel_layout),
      max_buffer_size_(max_buffer_size),
      channels_(channels),
      sample_rate_(sample_rate),
      limiter_(std::make_unique<AudioLimiter>(sample_rate_, channels_)),
      pool_(base::MakeRefCounted<AudioBufferMemoryPool>()) {}

LimitingAudioQueue::~LimitingAudioQueue() = default;

void LimitingAudioQueue::Push(const AudioBus& input,
                              int num_frames,
                              base::TimeDelta timestamp,
                              OutputCB output_cb) {
  CHECK_GT(num_frames, 0);
  CHECK_LE(num_frames, max_buffer_size_);
  CHECK_LE(input.channels(), channels_);

  // Always create buffers using `default_buffer_size_`, so they can be reused
  // by the buffer pool.
  auto buffer = AudioBuffer::CreateBuffer(
      kSampleFormatPlanarF32, channel_layout_, channels_, sample_rate_,
      max_buffer_size_, pool_);

  buffer->set_timestamp(timestamp);
  buffer->TrimEnd(max_buffer_size_ - num_frames);

  AudioLimiter::OutputChannels output_spans;
  for (uint8_t* data : buffer->channel_data()) {
    output_spans.emplace_back(data, num_frames * sizeof(float));
  }

  limiter_->LimitPeaksPartial(
      input, num_frames, output_spans,
      base::BindOnce(std::move(output_cb), std::move(buffer)));
}

void LimitingAudioQueue::Flush() {
  limiter_->Flush();
}
void LimitingAudioQueue::Clear() {
  limiter_ = std::make_unique<AudioLimiter>(sample_rate_, channels_);
}

}  // namespace media