OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/audio/fake_audio_input_stream.h" | 5 #include "media/audio/fake_audio_input_stream.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
9 #include "media/audio/audio_manager_base.h" | 9 #include "media/audio/audio_manager_base.h" |
10 | 10 |
11 using base::TimeTicks; | 11 using base::TimeTicks; |
12 using base::TimeDelta; | 12 using base::TimeDelta; |
13 | 13 |
14 namespace media { | 14 namespace media { |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 // These values are based on experiments for local-to-local | 18 // These values are based on experiments for local-to-local |
19 // PeerConnection to demonstrate audio/video synchronization. | 19 // PeerConnection to demonstrate audio/video synchronization. |
20 const int kBeepDurationMilliseconds = 20; | 20 const int kBeepDurationMilliseconds = 20; |
21 const int kBeepFrequency = 400; | 21 const int kBeepFrequency = 400; |
22 | 22 |
23 // Intervals between two automatic beeps. | |
24 const int kAutomaticBeepIntervalInMs = 500; | |
25 | |
26 // Automatic beep will be triggered every |kAutomaticBeepIntervalInMs| unless | |
27 // users explicitly call BeepOnce(), which will disable the automatic beep. | |
28 struct BeepContext { | 23 struct BeepContext { |
29 BeepContext() : beep_once(false), automatic(true) {} | 24 BeepContext() : beep_once(false) {} |
30 base::Lock beep_lock; | 25 base::Lock beep_lock; |
31 bool beep_once; | 26 bool beep_once; |
32 bool automatic; | |
33 }; | 27 }; |
34 | 28 |
35 static base::LazyInstance<BeepContext> g_beep_context = | 29 static base::LazyInstance<BeepContext> g_beep_context = |
36 LAZY_INSTANCE_INITIALIZER; | 30 LAZY_INSTANCE_INITIALIZER; |
37 | 31 |
38 } // namespace | 32 } // namespace |
39 | 33 |
40 AudioInputStream* FakeAudioInputStream::MakeFakeStream( | 34 AudioInputStream* FakeAudioInputStream::MakeFakeStream( |
41 AudioManagerBase* manager, | 35 AudioManagerBase* manager, |
42 const AudioParameters& params) { | 36 const AudioParameters& params) { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 thread_.Start(); | 71 thread_.Start(); |
78 thread_.message_loop()->PostDelayedTask( | 72 thread_.message_loop()->PostDelayedTask( |
79 FROM_HERE, | 73 FROM_HERE, |
80 base::Bind(&FakeAudioInputStream::DoCallback, base::Unretained(this)), | 74 base::Bind(&FakeAudioInputStream::DoCallback, base::Unretained(this)), |
81 callback_interval_); | 75 callback_interval_); |
82 } | 76 } |
83 | 77 |
84 void FakeAudioInputStream::DoCallback() { | 78 void FakeAudioInputStream::DoCallback() { |
85 DCHECK(callback_); | 79 DCHECK(callback_); |
86 | 80 |
87 const TimeTicks now = TimeTicks::Now(); | |
88 base::TimeDelta next_callback_time = | |
89 last_callback_time_ + callback_interval_ * 2 - now; | |
90 | |
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(); | |
95 | |
96 // Accumulate the time from the last beep. | |
97 interval_from_last_beep_ += now - last_callback_time_; | |
98 | |
99 last_callback_time_ = now; | |
100 | |
101 memset(buffer_.get(), 0, buffer_size_); | 81 memset(buffer_.get(), 0, buffer_size_); |
102 | 82 |
103 bool should_beep = false; | 83 bool should_beep = false; |
104 { | 84 { |
105 BeepContext* beep_context = g_beep_context.Pointer(); | 85 BeepContext* beep_context = g_beep_context.Pointer(); |
106 base::AutoLock auto_lock(beep_context->beep_lock); | 86 base::AutoLock auto_lock(beep_context->beep_lock); |
107 if (beep_context->automatic) { | 87 should_beep = beep_context->beep_once; |
108 base::TimeDelta delta = interval_from_last_beep_ - | 88 beep_context->beep_once = false; |
109 TimeDelta::FromMilliseconds(kAutomaticBeepIntervalInMs); | |
110 if (delta > base::TimeDelta()) { | |
111 should_beep = true; | |
112 interval_from_last_beep_ = delta; | |
113 } | |
114 } else { | |
115 should_beep = beep_context->beep_once; | |
116 beep_context->beep_once = false; | |
117 } | |
118 } | 89 } |
119 | 90 |
120 // If this object was instructed to generate a beep or has started to | 91 // If this object was instructed to generate a beep or has started to |
121 // generate a beep sound. | 92 // generate a beep sound. |
122 if (should_beep || beep_generated_in_buffers_) { | 93 if (should_beep || beep_generated_in_buffers_) { |
123 // Compute the number of frames to output high value. Then compute the | 94 // Compute the number of frames to output high value. Then compute the |
124 // number of bytes based on channels and bits per channel. | 95 // number of bytes based on channels and bits per channel. |
125 int high_frames = beep_period_in_frames_ / 2; | 96 int high_frames = beep_period_in_frames_ / 2; |
126 int high_bytes = high_frames * params_.bits_per_sample() * | 97 int high_bytes = high_frames * params_.bits_per_sample() * |
127 params_.channels() / 8; | 98 params_.channels() / 8; |
128 | 99 |
129 // Separate high and low with the same number of bytes to generate a | 100 // Separate high and low with the same number of bytes to generate a |
130 // square wave. | 101 // square wave. |
131 int position = 0; | 102 int position = 0; |
132 while (position + high_bytes <= buffer_size_) { | 103 while (position + high_bytes <= buffer_size_) { |
133 // Write high values first. | 104 // Write high values first. |
134 memset(buffer_.get() + position, 128, high_bytes); | 105 memset(buffer_.get() + position, 128, high_bytes); |
| 106 |
135 // Then leave low values in the buffer with |high_bytes|. | 107 // Then leave low values in the buffer with |high_bytes|. |
136 position += high_bytes * 2; | 108 position += high_bytes * 2; |
137 } | 109 } |
138 | 110 |
139 ++beep_generated_in_buffers_; | 111 ++beep_generated_in_buffers_; |
140 if (beep_generated_in_buffers_ >= beep_duration_in_buffers_) | 112 if (beep_generated_in_buffers_ >= beep_duration_in_buffers_) |
141 beep_generated_in_buffers_ = 0; | 113 beep_generated_in_buffers_ = 0; |
142 } | 114 } |
143 | 115 |
144 callback_->OnData(this, buffer_.get(), buffer_size_, buffer_size_, 1.0); | 116 callback_->OnData(this, buffer_.get(), buffer_size_, buffer_size_, 1.0); |
145 frames_elapsed_ += params_.frames_per_buffer(); | 117 frames_elapsed_ += params_.frames_per_buffer(); |
146 | 118 |
| 119 const TimeTicks now = TimeTicks::Now(); |
| 120 base::TimeDelta next_callback_time = |
| 121 last_callback_time_ + callback_interval_ * 2 - now; |
| 122 |
| 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(); |
| 127 |
| 128 last_callback_time_ = now; |
147 thread_.message_loop()->PostDelayedTask( | 129 thread_.message_loop()->PostDelayedTask( |
148 FROM_HERE, | 130 FROM_HERE, |
149 base::Bind(&FakeAudioInputStream::DoCallback, base::Unretained(this)), | 131 base::Bind(&FakeAudioInputStream::DoCallback, base::Unretained(this)), |
150 next_callback_time); | 132 next_callback_time); |
151 } | 133 } |
152 | 134 |
153 void FakeAudioInputStream::Stop() { | 135 void FakeAudioInputStream::Stop() { |
154 thread_.Stop(); | 136 thread_.Stop(); |
155 callback_ = NULL; | 137 callback_ = NULL; |
156 } | 138 } |
(...skipping 17 matching lines...) Expand all Loading... |
174 | 156 |
175 bool FakeAudioInputStream::GetAutomaticGainControl() { | 157 bool FakeAudioInputStream::GetAutomaticGainControl() { |
176 return true; | 158 return true; |
177 } | 159 } |
178 | 160 |
179 // static | 161 // static |
180 void FakeAudioInputStream::BeepOnce() { | 162 void FakeAudioInputStream::BeepOnce() { |
181 BeepContext* beep_context = g_beep_context.Pointer(); | 163 BeepContext* beep_context = g_beep_context.Pointer(); |
182 base::AutoLock auto_lock(beep_context->beep_lock); | 164 base::AutoLock auto_lock(beep_context->beep_lock); |
183 beep_context->beep_once = true; | 165 beep_context->beep_once = true; |
184 beep_context->automatic = false; | |
185 } | 166 } |
186 | 167 |
187 } // namespace media | 168 } // namespace media |
OLD | NEW |