Let MetricsService know about some Android Activities
[chromium-blink-merge.git] / media / audio / fake_audio_input_stream.cc
blob5ec09a45c870b16995d0aaa1545beb9fe4e13d2e
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 struct BeepContext {
24 BeepContext() : beep_once(false) {}
25 base::Lock beep_lock;
26 bool beep_once;
29 static base::LazyInstance<BeepContext> g_beep_context =
30 LAZY_INSTANCE_INITIALIZER;
32 } // namespace
34 AudioInputStream* FakeAudioInputStream::MakeFakeStream(
35 AudioManagerBase* manager,
36 const AudioParameters& params) {
37 return new FakeAudioInputStream(manager, params);
40 FakeAudioInputStream::FakeAudioInputStream(AudioManagerBase* manager,
41 const AudioParameters& params)
42 : audio_manager_(manager),
43 callback_(NULL),
44 buffer_size_((params.channels() * params.bits_per_sample() *
45 params.frames_per_buffer()) / 8),
46 params_(params),
47 thread_("FakeAudioRecordingThread"),
48 callback_interval_(base::TimeDelta::FromMilliseconds(
49 (params.frames_per_buffer() * 1000) / params.sample_rate())),
50 beep_duration_in_buffers_(
51 kBeepDurationMilliseconds * params.sample_rate() /
52 params.frames_per_buffer() / 1000),
53 beep_generated_in_buffers_(0),
54 beep_period_in_frames_(params.sample_rate() / kBeepFrequency),
55 frames_elapsed_(0) {
58 FakeAudioInputStream::~FakeAudioInputStream() {}
60 bool FakeAudioInputStream::Open() {
61 buffer_.reset(new uint8[buffer_size_]);
62 memset(buffer_.get(), 0, buffer_size_);
63 return true;
66 void FakeAudioInputStream::Start(AudioInputCallback* callback) {
67 DCHECK(!thread_.IsRunning());
68 DCHECK(!callback_);
69 callback_ = callback;
70 last_callback_time_ = TimeTicks::Now();
71 thread_.Start();
72 thread_.message_loop()->PostDelayedTask(
73 FROM_HERE,
74 base::Bind(&FakeAudioInputStream::DoCallback, base::Unretained(this)),
75 callback_interval_);
78 void FakeAudioInputStream::DoCallback() {
79 DCHECK(callback_);
81 memset(buffer_.get(), 0, buffer_size_);
83 bool should_beep = false;
85 BeepContext* beep_context = g_beep_context.Pointer();
86 base::AutoLock auto_lock(beep_context->beep_lock);
87 should_beep = beep_context->beep_once;
88 beep_context->beep_once = false;
91 // If this object was instructed to generate a beep or has started to
92 // generate a beep sound.
93 if (should_beep || beep_generated_in_buffers_) {
94 // Compute the number of frames to output high value. Then compute the
95 // number of bytes based on channels and bits per channel.
96 int high_frames = beep_period_in_frames_ / 2;
97 int high_bytes = high_frames * params_.bits_per_sample() *
98 params_.channels() / 8;
100 // Separate high and low with the same number of bytes to generate a
101 // square wave.
102 int position = 0;
103 while (position + high_bytes <= buffer_size_) {
104 // Write high values first.
105 memset(buffer_.get() + position, 128, high_bytes);
107 // Then leave low values in the buffer with |high_bytes|.
108 position += high_bytes * 2;
111 ++beep_generated_in_buffers_;
112 if (beep_generated_in_buffers_ >= beep_duration_in_buffers_)
113 beep_generated_in_buffers_ = 0;
116 callback_->OnData(this, buffer_.get(), buffer_size_, buffer_size_, 1.0);
117 frames_elapsed_ += params_.frames_per_buffer();
119 const TimeTicks now = TimeTicks::Now();
120 base::TimeDelta next_callback_time =
121 last_callback_time_ + callback_interval_ * 2 - now;
123 // If we are falling behind, try to catch up as much as we can in the next
124 // callback.
125 if (next_callback_time < base::TimeDelta())
126 next_callback_time = base::TimeDelta();
128 last_callback_time_ = now;
129 thread_.message_loop()->PostDelayedTask(
130 FROM_HERE,
131 base::Bind(&FakeAudioInputStream::DoCallback, base::Unretained(this)),
132 next_callback_time);
135 void FakeAudioInputStream::Stop() {
136 thread_.Stop();
137 callback_ = NULL;
140 void FakeAudioInputStream::Close() {
141 audio_manager_->ReleaseInputStream(this);
144 double FakeAudioInputStream::GetMaxVolume() {
145 return 1.0;
148 void FakeAudioInputStream::SetVolume(double volume) {
151 double FakeAudioInputStream::GetVolume() {
152 return 1.0;
155 void FakeAudioInputStream::SetAutomaticGainControl(bool enabled) {}
157 bool FakeAudioInputStream::GetAutomaticGainControl() {
158 return true;
161 // static
162 void FakeAudioInputStream::BeepOnce() {
163 BeepContext* beep_context = g_beep_context.Pointer();
164 base::AutoLock auto_lock(beep_context->beep_lock);
165 beep_context->beep_once = true;
168 } // namespace media