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

media / base / audio_bus_pool.cc [blame]

// Copyright 2022 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/audio_bus_pool.h"

#include <memory>
#include "base/check_op.h"
#include "base/functional/bind.h"
#include "base/synchronization/lock.h"
#include "media/base/audio_bus.h"
#include "media/base/audio_parameters.h"

namespace media {

AudioBusPoolImpl::AudioBusPoolImpl(const AudioParameters& params,
                                   size_t preallocated,
                                   size_t max_capacity)
    : AudioBusPoolImpl(params,
                       preallocated,
                       max_capacity,
                       base::BindRepeating(
                           static_cast<std::unique_ptr<AudioBus> (*)(
                               const AudioParameters&)>(&AudioBus::Create))) {}

AudioBusPoolImpl::~AudioBusPoolImpl() = default;

std::unique_ptr<AudioBus> AudioBusPoolImpl::GetAudioBus() {
  base::AutoLock auto_lock(lock_);
  if (!audio_buses_.empty()) {
    std::unique_ptr<AudioBus> bus = std::move(audio_buses_.top());
    audio_buses_.pop();
    return bus;
  }
  return create_audio_bus_.Run(params_);
}

void AudioBusPoolImpl::InsertAudioBus(std::unique_ptr<AudioBus> audio_bus) {
  CHECK_EQ(audio_bus->channels(), params_.channels());
  CHECK_EQ(audio_bus->frames(), params_.frames_per_buffer());

  base::AutoLock auto_lock(lock_);
  if (audio_buses_.size() < max_capacity_) {
    audio_buses_.push(std::move(audio_bus));
  }
}

AudioBusPoolImpl::AudioBusPoolImpl(const AudioParameters& params,
                                   size_t preallocated,
                                   size_t max_capacity,
                                   CreateAudioBusCallback create_audio_bus)
    : params_(params),
      max_capacity_(max_capacity),
      create_audio_bus_(std::move(create_audio_bus)) {
  DCHECK_GE(max_capacity, preallocated);
  while (preallocated-- > 0) {
    audio_buses_.push(create_audio_bus_.Run(params_));
  }
}

}  // namespace media