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"
11 using base::TimeTicks
;
12 using base::TimeDelta
;
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.
29 BeepContext() : beep_once(false), automatic(true) {}
35 static base::LazyInstance
<BeepContext
> g_beep_context
=
36 LAZY_INSTANCE_INITIALIZER
;
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
),
50 buffer_size_((params
.channels() * params
.bits_per_sample() *
51 params
.frames_per_buffer()) / 8),
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
),
64 FakeAudioInputStream::~FakeAudioInputStream() {}
66 bool FakeAudioInputStream::Open() {
67 buffer_
.reset(new uint8
[buffer_size_
]);
68 memset(buffer_
.get(), 0, buffer_size_
);
72 void FakeAudioInputStream::Start(AudioInputCallback
* callback
) {
73 DCHECK(!thread_
.IsRunning());
76 last_callback_time_
= TimeTicks::Now();
78 thread_
.message_loop()->PostDelayedTask(
80 base::Bind(&FakeAudioInputStream::DoCallback
, base::Unretained(this)),
84 void FakeAudioInputStream::DoCallback() {
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
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()) {
112 interval_from_last_beep_
= delta
;
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
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(
149 base::Bind(&FakeAudioInputStream::DoCallback
, base::Unretained(this)),
153 void FakeAudioInputStream::Stop() {
158 void FakeAudioInputStream::Close() {
159 audio_manager_
->ReleaseInputStream(this);
162 double FakeAudioInputStream::GetMaxVolume() {
166 void FakeAudioInputStream::SetVolume(double volume
) {
169 double FakeAudioInputStream::GetVolume() {
173 void FakeAudioInputStream::SetAutomaticGainControl(bool enabled
) {}
175 bool FakeAudioInputStream::GetAutomaticGainControl() {
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;