Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(385)

Side by Side Diff: media/audio/fake_audio_input_stream.cc

Issue 399623008: Use audio thread for the fake input audio stream. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « media/audio/fake_audio_input_stream.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "media/base/audio_bus.h" 10 #include "media/base/audio_bus.h"
11 11
12 using base::TimeTicks; 12 using base::TimeTicks;
13 using base::TimeDelta; 13 using base::TimeDelta;
14 14
15 namespace media { 15 namespace media {
16 16
17 namespace { 17 namespace {
18 18
19 // These values are based on experiments for local-to-local 19 // These values are based on experiments for local-to-local
20 // PeerConnection to demonstrate audio/video synchronization. 20 // PeerConnection to demonstrate audio/video synchronization.
21 const int kBeepDurationMilliseconds = 20; 21 const int kBeepDurationMilliseconds = 20;
22 const int kBeepFrequency = 400; 22 const int kBeepFrequency = 400;
23 23
24 // Intervals between two automatic beeps. 24 // Intervals between two automatic beeps.
25 const int kAutomaticBeepIntervalInMs = 500; 25 const int kAutomaticBeepIntervalInMs = 500;
26 26
27 // Automatic beep will be triggered every |kAutomaticBeepIntervalInMs| unless 27 // Automatic beep will be triggered every |kAutomaticBeepIntervalInMs| unless
28 // users explicitly call BeepOnce(), which will disable the automatic beep. 28 // users explicitly call BeepOnce(), which will disable the automatic beep.
29 struct BeepContext { 29 class BeepContext {
henrika (OOO until Aug 14) 2014/07/17 11:21:59 How are the changes related to this CL?
no longer working on chromium 2014/07/17 11:31:15 Comment was addressed offline, this is a better wa
30 BeepContext() : beep_once(false), automatic(true) {} 30 public:
31 base::Lock beep_lock; 31 BeepContext() : beep_once_(false), automatic_beep_(true) {}
32 bool beep_once; 32
33 bool automatic; 33 void SetBeepOnce(bool enable) {
34 base::AutoLock auto_lock(lock_);
35 beep_once_ = enable;
36
37 // Disable the automatic beep if users explicit set |beep_once_| to true.
38 if (enable)
39 automatic_beep_ = false;
40 }
41 bool beep_once() const {
42 base::AutoLock auto_lock(lock_);
43 return beep_once_;
44 }
45 bool automatic_beep() const {
46 base::AutoLock auto_lock(lock_);
47 return automatic_beep_;
48 }
49
50 private:
51 mutable base::Lock lock_;
52 bool beep_once_;
53 bool automatic_beep_;
34 }; 54 };
35 55
36 static base::LazyInstance<BeepContext> g_beep_context = 56 static base::LazyInstance<BeepContext> g_beep_context =
37 LAZY_INSTANCE_INITIALIZER; 57 LAZY_INSTANCE_INITIALIZER;
38 58
39 } // namespace 59 } // namespace
40 60
41 AudioInputStream* FakeAudioInputStream::MakeFakeStream( 61 AudioInputStream* FakeAudioInputStream::MakeFakeStream(
42 AudioManagerBase* manager, 62 AudioManagerBase* manager,
43 const AudioParameters& params) { 63 const AudioParameters& params) {
44 return new FakeAudioInputStream(manager, params); 64 return new FakeAudioInputStream(manager, params);
45 } 65 }
46 66
47 FakeAudioInputStream::FakeAudioInputStream(AudioManagerBase* manager, 67 FakeAudioInputStream::FakeAudioInputStream(AudioManagerBase* manager,
48 const AudioParameters& params) 68 const AudioParameters& params)
49 : audio_manager_(manager), 69 : audio_manager_(manager),
50 callback_(NULL), 70 callback_(NULL),
51 buffer_size_((params.channels() * params.bits_per_sample() * 71 buffer_size_((params.channels() * params.bits_per_sample() *
52 params.frames_per_buffer()) / 72 params.frames_per_buffer()) /
53 8), 73 8),
54 params_(params), 74 params_(params),
55 thread_("FakeAudioRecordingThread"), 75 task_runner_(manager->GetWorkerTaskRunner()),
56 callback_interval_(base::TimeDelta::FromMilliseconds( 76 callback_interval_(base::TimeDelta::FromMilliseconds(
57 (params.frames_per_buffer() * 1000) / params.sample_rate())), 77 (params.frames_per_buffer() * 1000) / params.sample_rate())),
58 beep_duration_in_buffers_(kBeepDurationMilliseconds * 78 beep_duration_in_buffers_(kBeepDurationMilliseconds *
59 params.sample_rate() / 79 params.sample_rate() /
60 params.frames_per_buffer() / 80 params.frames_per_buffer() /
61 1000), 81 1000),
62 beep_generated_in_buffers_(0), 82 beep_generated_in_buffers_(0),
63 beep_period_in_frames_(params.sample_rate() / kBeepFrequency), 83 beep_period_in_frames_(params.sample_rate() / kBeepFrequency),
64 frames_elapsed_(0), 84 frames_elapsed_(0),
65 audio_bus_(AudioBus::Create(params)) { 85 audio_bus_(AudioBus::Create(params)),
86 weak_factory_(this) {
87 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread());
66 } 88 }
67 89
68 FakeAudioInputStream::~FakeAudioInputStream() {} 90 FakeAudioInputStream::~FakeAudioInputStream() {}
69 91
70 bool FakeAudioInputStream::Open() { 92 bool FakeAudioInputStream::Open() {
93 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread());
71 buffer_.reset(new uint8[buffer_size_]); 94 buffer_.reset(new uint8[buffer_size_]);
72 memset(buffer_.get(), 0, buffer_size_); 95 memset(buffer_.get(), 0, buffer_size_);
73 audio_bus_->Zero(); 96 audio_bus_->Zero();
74 return true; 97 return true;
75 } 98 }
76 99
77 void FakeAudioInputStream::Start(AudioInputCallback* callback) { 100 void FakeAudioInputStream::Start(AudioInputCallback* callback) {
78 DCHECK(!thread_.IsRunning()); 101 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread());
79 DCHECK(!callback_); 102 DCHECK(!callback_);
80 callback_ = callback; 103 callback_ = callback;
81 last_callback_time_ = TimeTicks::Now(); 104 last_callback_time_ = TimeTicks::Now();
82 thread_.Start(); 105 task_runner_->PostDelayedTask(
83 thread_.message_loop()->PostDelayedTask(
84 FROM_HERE, 106 FROM_HERE,
85 base::Bind(&FakeAudioInputStream::DoCallback, base::Unretained(this)), 107 base::Bind(&FakeAudioInputStream::DoCallback, weak_factory_.GetWeakPtr()),
henrika (OOO until Aug 14) 2014/07/17 11:21:21 Why do you need a weak pointer here?
no longer working on chromium 2014/07/17 11:22:48 If Stop() is called before the task is executed, t
86 callback_interval_); 108 callback_interval_);
87 } 109 }
88 110
89 void FakeAudioInputStream::DoCallback() { 111 void FakeAudioInputStream::DoCallback() {
90 DCHECK(callback_); 112 DCHECK(callback_);
91 113
92 const TimeTicks now = TimeTicks::Now(); 114 const TimeTicks now = TimeTicks::Now();
93 base::TimeDelta next_callback_time = 115 base::TimeDelta next_callback_time =
94 last_callback_time_ + callback_interval_ * 2 - now; 116 last_callback_time_ + callback_interval_ * 2 - now;
95 117
96 // If we are falling behind, try to catch up as much as we can in the next 118 // If we are falling behind, try to catch up as much as we can in the next
97 // callback. 119 // callback.
98 if (next_callback_time < base::TimeDelta()) 120 if (next_callback_time < base::TimeDelta())
99 next_callback_time = base::TimeDelta(); 121 next_callback_time = base::TimeDelta();
100 122
101 // Accumulate the time from the last beep. 123 // Accumulate the time from the last beep.
102 interval_from_last_beep_ += now - last_callback_time_; 124 interval_from_last_beep_ += now - last_callback_time_;
103 125
104 last_callback_time_ = now; 126 last_callback_time_ = now;
105 127
106 memset(buffer_.get(), 0, buffer_size_); 128 memset(buffer_.get(), 0, buffer_size_);
107 129
108 bool should_beep = false; 130 bool should_beep = false;
109 { 131 {
110 BeepContext* beep_context = g_beep_context.Pointer(); 132 BeepContext* beep_context = g_beep_context.Pointer();
111 base::AutoLock auto_lock(beep_context->beep_lock); 133 if (beep_context->automatic_beep()) {
112 if (beep_context->automatic) {
113 base::TimeDelta delta = interval_from_last_beep_ - 134 base::TimeDelta delta = interval_from_last_beep_ -
114 TimeDelta::FromMilliseconds(kAutomaticBeepIntervalInMs); 135 TimeDelta::FromMilliseconds(kAutomaticBeepIntervalInMs);
115 if (delta > base::TimeDelta()) { 136 if (delta > base::TimeDelta()) {
116 should_beep = true; 137 should_beep = true;
117 interval_from_last_beep_ = delta; 138 interval_from_last_beep_ = delta;
118 } 139 }
119 } else { 140 } else {
120 should_beep = beep_context->beep_once; 141 should_beep = beep_context->beep_once();
121 beep_context->beep_once = false; 142 beep_context->SetBeepOnce(false);
122 } 143 }
123 } 144 }
124 145
125 // If this object was instructed to generate a beep or has started to 146 // If this object was instructed to generate a beep or has started to
126 // generate a beep sound. 147 // generate a beep sound.
127 if (should_beep || beep_generated_in_buffers_) { 148 if (should_beep || beep_generated_in_buffers_) {
128 // Compute the number of frames to output high value. Then compute the 149 // Compute the number of frames to output high value. Then compute the
129 // number of bytes based on channels and bits per channel. 150 // number of bytes based on channels and bits per channel.
130 int high_frames = beep_period_in_frames_ / 2; 151 int high_frames = beep_period_in_frames_ / 2;
131 int high_bytes = high_frames * params_.bits_per_sample() * 152 int high_bytes = high_frames * params_.bits_per_sample() *
(...skipping 12 matching lines...) Expand all
144 ++beep_generated_in_buffers_; 165 ++beep_generated_in_buffers_;
145 if (beep_generated_in_buffers_ >= beep_duration_in_buffers_) 166 if (beep_generated_in_buffers_ >= beep_duration_in_buffers_)
146 beep_generated_in_buffers_ = 0; 167 beep_generated_in_buffers_ = 0;
147 } 168 }
148 169
149 audio_bus_->FromInterleaved( 170 audio_bus_->FromInterleaved(
150 buffer_.get(), audio_bus_->frames(), params_.bits_per_sample() / 8); 171 buffer_.get(), audio_bus_->frames(), params_.bits_per_sample() / 8);
151 callback_->OnData(this, audio_bus_.get(), buffer_size_, 1.0); 172 callback_->OnData(this, audio_bus_.get(), buffer_size_, 1.0);
152 frames_elapsed_ += params_.frames_per_buffer(); 173 frames_elapsed_ += params_.frames_per_buffer();
153 174
154 thread_.message_loop()->PostDelayedTask( 175 task_runner_->PostDelayedTask(
155 FROM_HERE, 176 FROM_HERE,
156 base::Bind(&FakeAudioInputStream::DoCallback, base::Unretained(this)), 177 base::Bind(&FakeAudioInputStream::DoCallback, weak_factory_.GetWeakPtr()),
157 next_callback_time); 178 next_callback_time);
158 } 179 }
159 180
160 void FakeAudioInputStream::Stop() { 181 void FakeAudioInputStream::Stop() {
161 thread_.Stop(); 182 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread());
183 weak_factory_.InvalidateWeakPtrs();
162 callback_ = NULL; 184 callback_ = NULL;
163 } 185 }
164 186
165 void FakeAudioInputStream::Close() { 187 void FakeAudioInputStream::Close() {
188 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread());
166 audio_manager_->ReleaseInputStream(this); 189 audio_manager_->ReleaseInputStream(this);
167 } 190 }
168 191
169 double FakeAudioInputStream::GetMaxVolume() { 192 double FakeAudioInputStream::GetMaxVolume() {
193 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread());
170 return 1.0; 194 return 1.0;
171 } 195 }
172 196
173 void FakeAudioInputStream::SetVolume(double volume) { 197 void FakeAudioInputStream::SetVolume(double volume) {
198 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread());
174 } 199 }
175 200
176 double FakeAudioInputStream::GetVolume() { 201 double FakeAudioInputStream::GetVolume() {
202 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread());
177 return 1.0; 203 return 1.0;
178 } 204 }
179 205
180 void FakeAudioInputStream::SetAutomaticGainControl(bool enabled) {} 206 void FakeAudioInputStream::SetAutomaticGainControl(bool enabled) {}
181 207
182 bool FakeAudioInputStream::GetAutomaticGainControl() { 208 bool FakeAudioInputStream::GetAutomaticGainControl() {
183 return true; 209 return true;
184 } 210 }
185 211
186 // static 212 // static
187 void FakeAudioInputStream::BeepOnce() { 213 void FakeAudioInputStream::BeepOnce() {
188 BeepContext* beep_context = g_beep_context.Pointer(); 214 BeepContext* beep_context = g_beep_context.Pointer();
189 base::AutoLock auto_lock(beep_context->beep_lock); 215 beep_context->SetBeepOnce(true);
190 beep_context->beep_once = true;
191 beep_context->automatic = false;
192 } 216 }
193 217
194 } // namespace media 218 } // namespace media
OLDNEW
« no previous file with comments | « media/audio/fake_audio_input_stream.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698