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"
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
;
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.
31 BeepContext() : beep_once_(false), automatic_beep_(true) {}
33 void SetBeepOnce(bool enable
) {
34 base::AutoLock
auto_lock(lock_
);
37 // Disable the automatic beep if users explicit set |beep_once_| to true.
39 automatic_beep_
= false;
41 bool beep_once() const {
42 base::AutoLock
auto_lock(lock_
);
45 bool automatic_beep() const {
46 base::AutoLock
auto_lock(lock_
);
47 return automatic_beep_
;
51 mutable base::Lock lock_
;
56 static base::LazyInstance
<BeepContext
> g_beep_context
=
57 LAZY_INSTANCE_INITIALIZER
;
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
),
71 buffer_size_((params
.channels() * params
.bits_per_sample() *
72 params
.frames_per_buffer()) /
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() /
82 beep_generated_in_buffers_(0),
83 beep_period_in_frames_(params
.sample_rate() / kBeepFrequency
),
85 audio_bus_(AudioBus::Create(params
)),
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_
);
100 void FakeAudioInputStream::Start(AudioInputCallback
* callback
) {
101 DCHECK(audio_manager_
->GetTaskRunner()->BelongsToCurrentThread());
103 callback_
= callback
;
104 last_callback_time_
= TimeTicks::Now();
105 task_runner_
->PostDelayedTask(
107 base::Bind(&FakeAudioInputStream::DoCallback
, weak_factory_
.GetWeakPtr()),
111 void FakeAudioInputStream::DoCallback() {
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
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()) {
138 interval_from_last_beep_
= delta
;
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
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(
177 base::Bind(&FakeAudioInputStream::DoCallback
, weak_factory_
.GetWeakPtr()),
181 void FakeAudioInputStream::Stop() {
182 DCHECK(audio_manager_
->GetTaskRunner()->BelongsToCurrentThread());
183 weak_factory_
.InvalidateWeakPtrs();
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());
197 void FakeAudioInputStream::SetVolume(double volume
) {
198 DCHECK(audio_manager_
->GetTaskRunner()->BelongsToCurrentThread());
201 double FakeAudioInputStream::GetVolume() {
202 DCHECK(audio_manager_
->GetTaskRunner()->BelongsToCurrentThread());
206 void FakeAudioInputStream::SetAutomaticGainControl(bool enabled
) {}
208 bool FakeAudioInputStream::GetAutomaticGainControl() {
213 void FakeAudioInputStream::BeepOnce() {
214 BeepContext
* beep_context
= g_beep_context
.Pointer();
215 beep_context
->SetBeepOnce(true);