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;
24 BeepContext() : beep_once(false) {}
29 static base::LazyInstance
<BeepContext
> g_beep_context
=
30 LAZY_INSTANCE_INITIALIZER
;
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
),
44 buffer_size_((params
.channels() * params
.bits_per_sample() *
45 params
.frames_per_buffer()) / 8),
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
),
58 FakeAudioInputStream::~FakeAudioInputStream() {}
60 bool FakeAudioInputStream::Open() {
61 buffer_
.reset(new uint8
[buffer_size_
]);
62 memset(buffer_
.get(), 0, buffer_size_
);
66 void FakeAudioInputStream::Start(AudioInputCallback
* callback
) {
67 DCHECK(!thread_
.IsRunning());
70 last_callback_time_
= TimeTicks::Now();
72 thread_
.message_loop()->PostDelayedTask(
74 base::Bind(&FakeAudioInputStream::DoCallback
, base::Unretained(this)),
78 void FakeAudioInputStream::DoCallback() {
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
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
125 if (next_callback_time
< base::TimeDelta())
126 next_callback_time
= base::TimeDelta();
128 last_callback_time_
= now
;
129 thread_
.message_loop()->PostDelayedTask(
131 base::Bind(&FakeAudioInputStream::DoCallback
, base::Unretained(this)),
135 void FakeAudioInputStream::Stop() {
140 void FakeAudioInputStream::Close() {
141 audio_manager_
->ReleaseInputStream(this);
144 double FakeAudioInputStream::GetMaxVolume() {
148 void FakeAudioInputStream::SetVolume(double volume
) {
151 double FakeAudioInputStream::GetVolume() {
155 void FakeAudioInputStream::SetAutomaticGainControl(bool enabled
) {}
157 bool FakeAudioInputStream::GetAutomaticGainControl() {
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;