Revert 290001 because it's causing test failures:
[chromium-blink-merge.git] / media / audio / fake_audio_input_stream.cc
blob74ac579c1a23e1c03bbaa33469a5694c91a18153
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/audio/fake_audio_input_stream.h"
7 #include "base/bind.h"
8 #include "base/lazy_instance.h"
9 #include "media/audio/audio_manager_base.h"
10 #include "media/base/audio_bus.h"
12 using base::TimeTicks;
13 using base::TimeDelta;
15 namespace media {
17 namespace {
19 // These values are based on experiments for local-to-local
20 // PeerConnection to demonstrate audio/video synchronization.
21 const int kBeepDurationMilliseconds = 20;
22 const int kBeepFrequency = 400;
24 // Intervals between two automatic beeps.
25 const int kAutomaticBeepIntervalInMs = 500;
27 // Automatic beep will be triggered every |kAutomaticBeepIntervalInMs| unless
28 // users explicitly call BeepOnce(), which will disable the automatic beep.
29 class BeepContext {
30 public:
31 BeepContext() : beep_once_(false), automatic_beep_(true) {}
33 void SetBeepOnce(bool enable) {
34 base::AutoLock auto_lock(lock_);
35 beep_once_ = enable;
37 // Disable the automatic beep if users explicit set |beep_once_| to true.
38 if (enable)
39 automatic_beep_ = false;
41 bool beep_once() const {
42 base::AutoLock auto_lock(lock_);
43 return beep_once_;
45 bool automatic_beep() const {
46 base::AutoLock auto_lock(lock_);
47 return automatic_beep_;
50 private:
51 mutable base::Lock lock_;
52 bool beep_once_;
53 bool automatic_beep_;
56 static base::LazyInstance<BeepContext> g_beep_context =
57 LAZY_INSTANCE_INITIALIZER;
59 } // namespace
61 AudioInputStream* FakeAudioInputStream::MakeFakeStream(
62 AudioManagerBase* manager,
63 const AudioParameters& params) {
64 return new FakeAudioInputStream(manager, params);
67 FakeAudioInputStream::FakeAudioInputStream(AudioManagerBase* manager,
68 const AudioParameters& params)
69 : audio_manager_(manager),
70 callback_(NULL),
71 buffer_size_((params.channels() * params.bits_per_sample() *
72 params.frames_per_buffer()) /
73 8),
74 params_(params),
75 task_runner_(manager->GetTaskRunner()),
76 callback_interval_(base::TimeDelta::FromMilliseconds(
77 (params.frames_per_buffer() * 1000) / params.sample_rate())),
78 beep_duration_in_buffers_(kBeepDurationMilliseconds *
79 params.sample_rate() /
80 params.frames_per_buffer() /
81 1000),
82 beep_generated_in_buffers_(0),
83 beep_period_in_frames_(params.sample_rate() / kBeepFrequency),
84 frames_elapsed_(0),
85 audio_bus_(AudioBus::Create(params)),
86 weak_factory_(this) {
87 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread());
90 FakeAudioInputStream::~FakeAudioInputStream() {}
92 bool FakeAudioInputStream::Open() {
93 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread());
94 buffer_.reset(new uint8[buffer_size_]);
95 memset(buffer_.get(), 0, buffer_size_);
96 audio_bus_->Zero();
97 return true;
100 void FakeAudioInputStream::Start(AudioInputCallback* callback) {
101 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread());
102 DCHECK(!callback_);
103 callback_ = callback;
104 last_callback_time_ = TimeTicks::Now();
105 task_runner_->PostDelayedTask(
106 FROM_HERE,
107 base::Bind(&FakeAudioInputStream::DoCallback, weak_factory_.GetWeakPtr()),
108 callback_interval_);
111 void FakeAudioInputStream::DoCallback() {
112 DCHECK(callback_);
114 const TimeTicks now = TimeTicks::Now();
115 base::TimeDelta next_callback_time =
116 last_callback_time_ + callback_interval_ * 2 - now;
118 // If we are falling behind, try to catch up as much as we can in the next
119 // callback.
120 if (next_callback_time < base::TimeDelta())
121 next_callback_time = base::TimeDelta();
123 // Accumulate the time from the last beep.
124 interval_from_last_beep_ += now - last_callback_time_;
126 last_callback_time_ = now;
128 memset(buffer_.get(), 0, buffer_size_);
130 bool should_beep = false;
132 BeepContext* beep_context = g_beep_context.Pointer();
133 if (beep_context->automatic_beep()) {
134 base::TimeDelta delta = interval_from_last_beep_ -
135 TimeDelta::FromMilliseconds(kAutomaticBeepIntervalInMs);
136 if (delta > base::TimeDelta()) {
137 should_beep = true;
138 interval_from_last_beep_ = delta;
140 } else {
141 should_beep = beep_context->beep_once();
142 beep_context->SetBeepOnce(false);
146 // If this object was instructed to generate a beep or has started to
147 // generate a beep sound.
148 if (should_beep || beep_generated_in_buffers_) {
149 // Compute the number of frames to output high value. Then compute the
150 // number of bytes based on channels and bits per channel.
151 int high_frames = beep_period_in_frames_ / 2;
152 int high_bytes = high_frames * params_.bits_per_sample() *
153 params_.channels() / 8;
155 // Separate high and low with the same number of bytes to generate a
156 // square wave.
157 int position = 0;
158 while (position + high_bytes <= buffer_size_) {
159 // Write high values first.
160 memset(buffer_.get() + position, 128, high_bytes);
161 // Then leave low values in the buffer with |high_bytes|.
162 position += high_bytes * 2;
165 ++beep_generated_in_buffers_;
166 if (beep_generated_in_buffers_ >= beep_duration_in_buffers_)
167 beep_generated_in_buffers_ = 0;
170 audio_bus_->FromInterleaved(
171 buffer_.get(), audio_bus_->frames(), params_.bits_per_sample() / 8);
172 callback_->OnData(this, audio_bus_.get(), buffer_size_, 1.0);
173 frames_elapsed_ += params_.frames_per_buffer();
175 task_runner_->PostDelayedTask(
176 FROM_HERE,
177 base::Bind(&FakeAudioInputStream::DoCallback, weak_factory_.GetWeakPtr()),
178 next_callback_time);
181 void FakeAudioInputStream::Stop() {
182 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread());
183 weak_factory_.InvalidateWeakPtrs();
184 callback_ = NULL;
187 void FakeAudioInputStream::Close() {
188 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread());
189 audio_manager_->ReleaseInputStream(this);
192 double FakeAudioInputStream::GetMaxVolume() {
193 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread());
194 return 1.0;
197 void FakeAudioInputStream::SetVolume(double volume) {
198 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread());
201 double FakeAudioInputStream::GetVolume() {
202 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread());
203 return 1.0;
206 void FakeAudioInputStream::SetAutomaticGainControl(bool enabled) {}
208 bool FakeAudioInputStream::GetAutomaticGainControl() {
209 return true;
212 // static
213 void FakeAudioInputStream::BeepOnce() {
214 BeepContext* beep_context = g_beep_context.Pointer();
215 beep_context->SetBeepOnce(true);
218 } // namespace media