Fix issue with browser action toolbar putting all extension icons in overflow once...
[chromium-blink-merge.git] / media / audio / fake_audio_input_stream.cc
blobe05b257997e300db425736f5b380deeb2b250a4b
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"
11 using base::TimeTicks;
12 using base::TimeDelta;
14 namespace media {
16 namespace {
18 // These values are based on experiments for local-to-local
19 // PeerConnection to demonstrate audio/video synchronization.
20 const int kBeepDurationMilliseconds = 20;
21 const int kBeepFrequency = 400;
23 // Intervals between two automatic beeps.
24 const int kAutomaticBeepIntervalInMs = 500;
26 // Automatic beep will be triggered every |kAutomaticBeepIntervalInMs| unless
27 // users explicitly call BeepOnce(), which will disable the automatic beep.
28 struct BeepContext {
29 BeepContext() : beep_once(false), automatic(true) {}
30 base::Lock beep_lock;
31 bool beep_once;
32 bool automatic;
35 static base::LazyInstance<BeepContext> g_beep_context =
36 LAZY_INSTANCE_INITIALIZER;
38 } // namespace
40 AudioInputStream* FakeAudioInputStream::MakeFakeStream(
41 AudioManagerBase* manager,
42 const AudioParameters& params) {
43 return new FakeAudioInputStream(manager, params);
46 FakeAudioInputStream::FakeAudioInputStream(AudioManagerBase* manager,
47 const AudioParameters& params)
48 : audio_manager_(manager),
49 callback_(NULL),
50 buffer_size_((params.channels() * params.bits_per_sample() *
51 params.frames_per_buffer()) / 8),
52 params_(params),
53 thread_("FakeAudioRecordingThread"),
54 callback_interval_(base::TimeDelta::FromMilliseconds(
55 (params.frames_per_buffer() * 1000) / params.sample_rate())),
56 beep_duration_in_buffers_(
57 kBeepDurationMilliseconds * params.sample_rate() /
58 params.frames_per_buffer() / 1000),
59 beep_generated_in_buffers_(0),
60 beep_period_in_frames_(params.sample_rate() / kBeepFrequency),
61 frames_elapsed_(0) {
64 FakeAudioInputStream::~FakeAudioInputStream() {}
66 bool FakeAudioInputStream::Open() {
67 buffer_.reset(new uint8[buffer_size_]);
68 memset(buffer_.get(), 0, buffer_size_);
69 return true;
72 void FakeAudioInputStream::Start(AudioInputCallback* callback) {
73 DCHECK(!thread_.IsRunning());
74 DCHECK(!callback_);
75 callback_ = callback;
76 last_callback_time_ = TimeTicks::Now();
77 thread_.Start();
78 thread_.message_loop()->PostDelayedTask(
79 FROM_HERE,
80 base::Bind(&FakeAudioInputStream::DoCallback, base::Unretained(this)),
81 callback_interval_);
84 void FakeAudioInputStream::DoCallback() {
85 DCHECK(callback_);
87 const TimeTicks now = TimeTicks::Now();
88 base::TimeDelta next_callback_time =
89 last_callback_time_ + callback_interval_ * 2 - now;
91 // If we are falling behind, try to catch up as much as we can in the next
92 // callback.
93 if (next_callback_time < base::TimeDelta())
94 next_callback_time = base::TimeDelta();
96 // Accumulate the time from the last beep.
97 interval_from_last_beep_ += now - last_callback_time_;
99 last_callback_time_ = now;
101 memset(buffer_.get(), 0, buffer_size_);
103 bool should_beep = false;
105 BeepContext* beep_context = g_beep_context.Pointer();
106 base::AutoLock auto_lock(beep_context->beep_lock);
107 if (beep_context->automatic) {
108 base::TimeDelta delta = interval_from_last_beep_ -
109 TimeDelta::FromMilliseconds(kAutomaticBeepIntervalInMs);
110 if (delta > base::TimeDelta()) {
111 should_beep = true;
112 interval_from_last_beep_ = delta;
114 } else {
115 should_beep = beep_context->beep_once;
116 beep_context->beep_once = false;
120 // If this object was instructed to generate a beep or has started to
121 // generate a beep sound.
122 if (should_beep || beep_generated_in_buffers_) {
123 // Compute the number of frames to output high value. Then compute the
124 // number of bytes based on channels and bits per channel.
125 int high_frames = beep_period_in_frames_ / 2;
126 int high_bytes = high_frames * params_.bits_per_sample() *
127 params_.channels() / 8;
129 // Separate high and low with the same number of bytes to generate a
130 // square wave.
131 int position = 0;
132 while (position + high_bytes <= buffer_size_) {
133 // Write high values first.
134 memset(buffer_.get() + position, 128, high_bytes);
135 // Then leave low values in the buffer with |high_bytes|.
136 position += high_bytes * 2;
139 ++beep_generated_in_buffers_;
140 if (beep_generated_in_buffers_ >= beep_duration_in_buffers_)
141 beep_generated_in_buffers_ = 0;
144 callback_->OnData(this, buffer_.get(), buffer_size_, buffer_size_, 1.0);
145 frames_elapsed_ += params_.frames_per_buffer();
147 thread_.message_loop()->PostDelayedTask(
148 FROM_HERE,
149 base::Bind(&FakeAudioInputStream::DoCallback, base::Unretained(this)),
150 next_callback_time);
153 void FakeAudioInputStream::Stop() {
154 thread_.Stop();
155 callback_ = NULL;
158 void FakeAudioInputStream::Close() {
159 audio_manager_->ReleaseInputStream(this);
162 double FakeAudioInputStream::GetMaxVolume() {
163 return 1.0;
166 void FakeAudioInputStream::SetVolume(double volume) {
169 double FakeAudioInputStream::GetVolume() {
170 return 1.0;
173 void FakeAudioInputStream::SetAutomaticGainControl(bool enabled) {}
175 bool FakeAudioInputStream::GetAutomaticGainControl() {
176 return true;
179 // static
180 void FakeAudioInputStream::BeepOnce() {
181 BeepContext* beep_context = g_beep_context.Pointer();
182 base::AutoLock auto_lock(beep_context->beep_lock);
183 beep_context->beep_once = true;
184 beep_context->automatic = false;
187 } // namespace media