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
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133

media / base / fake_audio_renderer_sink.cc [blame]

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

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "media/base/fake_audio_renderer_sink.h"

#include "base/functional/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/task/sequenced_task_runner.h"
#include "media/base/audio_glitch_info.h"

namespace media {

FakeAudioRendererSink::FakeAudioRendererSink()
    : FakeAudioRendererSink(
          AudioParameters(AudioParameters::AUDIO_FAKE,
                          ChannelLayoutConfig::Stereo(),
                          AudioParameters::kTelephoneSampleRate,
                          1)) {}

FakeAudioRendererSink::FakeAudioRendererSink(
    const AudioParameters& hardware_params)
    : state_(kUninitialized),
      callback_(nullptr),
      output_device_info_(std::string(),
                          OUTPUT_DEVICE_STATUS_OK,
                          hardware_params),
      is_optimized_for_hw_params_(true) {}

FakeAudioRendererSink::~FakeAudioRendererSink() {
  DCHECK(!callback_);
}

void FakeAudioRendererSink::Initialize(const AudioParameters& params,
                                       RenderCallback* callback) {
  DCHECK(state_ == kUninitialized || state_ == kStopped);
  DCHECK(!callback_);
  DCHECK(callback);

  callback_ = callback;
  ChangeState(kInitialized);
}

void FakeAudioRendererSink::Start() {
  DCHECK_EQ(state_, kInitialized);
  ChangeState(kStarted);
}

void FakeAudioRendererSink::Stop() {
  callback_ = nullptr;
  ChangeState(kStopped);
}

void FakeAudioRendererSink::Flush() {
  DCHECK_NE(state_, kPlaying);
}

void FakeAudioRendererSink::Pause() {
  DCHECK(state_ == kStarted || state_ == kPlaying) << "state_ " << state_;
  ChangeState(kPaused);
}

void FakeAudioRendererSink::Play() {
  DCHECK(state_ == kStarted || state_ == kPaused) << "state_ " << state_;
  ChangeState(kPlaying);
}

bool FakeAudioRendererSink::SetVolume(double volume) {
  return true;
}

OutputDeviceInfo FakeAudioRendererSink::GetOutputDeviceInfo() {
  return output_device_info_;
}

void FakeAudioRendererSink::GetOutputDeviceInfoAsync(
    OutputDeviceInfoCB info_cb) {
  base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE, base::BindOnce(std::move(info_cb), output_device_info_));
}

bool FakeAudioRendererSink::IsOptimizedForHardwareParameters() {
  return is_optimized_for_hw_params_;
}

bool FakeAudioRendererSink::CurrentThreadIsRenderingThread() {
  NOTIMPLEMENTED();
  return false;
}

bool FakeAudioRendererSink::Render(AudioBus* dest,
                                   base::TimeDelta delay,
                                   int* frames_written) {
  if (state_ != kPlaying)
    return false;

  *frames_written = callback_->Render(delay, base::TimeTicks::Now(), {}, dest);
  return true;
}

void FakeAudioRendererSink::OnRenderError() {
  DCHECK_NE(state_, kUninitialized);
  DCHECK_NE(state_, kStopped);

  callback_->OnRenderError();
}

void FakeAudioRendererSink::SetIsOptimizedForHardwareParameters(bool value) {
  is_optimized_for_hw_params_ = value;
}

void FakeAudioRendererSink::ChangeState(State new_state) {
  static const char* kStateNames[] = {
    "kUninitialized",
    "kInitialized",
    "kStarted",
    "kPaused",
    "kPlaying",
    "kStopped"
  };

  DVLOG(1) << __func__ << " : " << kStateNames[state_] << " -> "
           << kStateNames[new_state];
  state_ = new_state;
}

}  // namespace media