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

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

Issue 66183002: Replace MessageLoopProxy with SingleThreadTaskRunner for the rest of media/. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix win and audio tests Created 6 years, 11 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
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/audio_input_device.h" 5 #include "media/audio/audio_input_device.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/threading/thread_restrictions.h" 9 #include "base/threading/thread_restrictions.h"
11 #include "base/time/time.h" 10 #include "base/time/time.h"
12 #include "media/audio/audio_manager_base.h" 11 #include "media/audio/audio_manager_base.h"
13 #include "media/base/audio_bus.h" 12 #include "media/base/audio_bus.h"
14 13
15 namespace media { 14 namespace media {
16 15
17 // The number of shared memory buffer segments indicated to browser process 16 // The number of shared memory buffer segments indicated to browser process
18 // in order to avoid data overwriting. This number can be any positive number, 17 // in order to avoid data overwriting. This number can be any positive number,
19 // dependent how fast the renderer process can pick up captured data from 18 // dependent how fast the renderer process can pick up captured data from
(...skipping 20 matching lines...) Expand all
40 39
41 private: 40 private:
42 int current_segment_id_; 41 int current_segment_id_;
43 CaptureCallback* capture_callback_; 42 CaptureCallback* capture_callback_;
44 scoped_ptr<AudioBus> audio_bus_; 43 scoped_ptr<AudioBus> audio_bus_;
45 DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback); 44 DISALLOW_COPY_AND_ASSIGN(AudioThreadCallback);
46 }; 45 };
47 46
48 AudioInputDevice::AudioInputDevice( 47 AudioInputDevice::AudioInputDevice(
49 scoped_ptr<AudioInputIPC> ipc, 48 scoped_ptr<AudioInputIPC> ipc,
50 const scoped_refptr<base::MessageLoopProxy>& io_loop) 49 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner)
51 : ScopedLoopObserver(io_loop), 50 : ScopedTaskRunnerObserver(io_task_runner),
52 callback_(NULL), 51 callback_(NULL),
53 ipc_(ipc.Pass()), 52 ipc_(ipc.Pass()),
54 state_(IDLE), 53 state_(IDLE),
55 session_id_(0), 54 session_id_(0),
56 agc_is_enabled_(false), 55 agc_is_enabled_(false),
57 stopping_hack_(false) { 56 stopping_hack_(false) {
58 CHECK(ipc_); 57 CHECK(ipc_);
59 58
60 // The correctness of the code depends on the relative values assigned in the 59 // The correctness of the code depends on the relative values assigned in the
61 // State enum. 60 // State enum.
62 COMPILE_ASSERT(IPC_CLOSED < IDLE, invalid_enum_value_assignment_0); 61 COMPILE_ASSERT(IPC_CLOSED < IDLE, invalid_enum_value_assignment_0);
63 COMPILE_ASSERT(IDLE < CREATING_STREAM, invalid_enum_value_assignment_1); 62 COMPILE_ASSERT(IDLE < CREATING_STREAM, invalid_enum_value_assignment_1);
64 COMPILE_ASSERT(CREATING_STREAM < RECORDING, invalid_enum_value_assignment_2); 63 COMPILE_ASSERT(CREATING_STREAM < RECORDING, invalid_enum_value_assignment_2);
65 } 64 }
66 65
67 void AudioInputDevice::Initialize(const AudioParameters& params, 66 void AudioInputDevice::Initialize(const AudioParameters& params,
68 CaptureCallback* callback, 67 CaptureCallback* callback,
69 int session_id) { 68 int session_id) {
70 DCHECK(params.IsValid()); 69 DCHECK(params.IsValid());
71 DCHECK(!callback_); 70 DCHECK(!callback_);
72 DCHECK_EQ(0, session_id_); 71 DCHECK_EQ(0, session_id_);
73 audio_parameters_ = params; 72 audio_parameters_ = params;
74 callback_ = callback; 73 callback_ = callback;
75 session_id_ = session_id; 74 session_id_ = session_id;
76 } 75 }
77 76
78 void AudioInputDevice::Start() { 77 void AudioInputDevice::Start() {
79 DCHECK(callback_) << "Initialize hasn't been called"; 78 DCHECK(callback_) << "Initialize hasn't been called";
80 DVLOG(1) << "Start()"; 79 DVLOG(1) << "Start()";
81 message_loop()->PostTask(FROM_HERE, 80 task_runner()->PostTask(FROM_HERE,
82 base::Bind(&AudioInputDevice::StartUpOnIOThread, this)); 81 base::Bind(&AudioInputDevice::StartUpOnIOThread, this));
83 } 82 }
84 83
85 void AudioInputDevice::Stop() { 84 void AudioInputDevice::Stop() {
86 DVLOG(1) << "Stop()"; 85 DVLOG(1) << "Stop()";
87 86
88 { 87 {
89 base::AutoLock auto_lock(audio_thread_lock_); 88 base::AutoLock auto_lock(audio_thread_lock_);
90 audio_thread_.Stop(base::MessageLoop::current()); 89 audio_thread_.Stop(base::MessageLoop::current());
91 stopping_hack_ = true; 90 stopping_hack_ = true;
92 } 91 }
93 92
94 message_loop()->PostTask(FROM_HERE, 93 task_runner()->PostTask(FROM_HERE,
95 base::Bind(&AudioInputDevice::ShutDownOnIOThread, this)); 94 base::Bind(&AudioInputDevice::ShutDownOnIOThread, this));
96 } 95 }
97 96
98 void AudioInputDevice::SetVolume(double volume) { 97 void AudioInputDevice::SetVolume(double volume) {
99 if (volume < 0 || volume > 1.0) { 98 if (volume < 0 || volume > 1.0) {
100 DLOG(ERROR) << "Invalid volume value specified"; 99 DLOG(ERROR) << "Invalid volume value specified";
101 return; 100 return;
102 } 101 }
103 102
104 message_loop()->PostTask(FROM_HERE, 103 task_runner()->PostTask(FROM_HERE,
105 base::Bind(&AudioInputDevice::SetVolumeOnIOThread, this, volume)); 104 base::Bind(&AudioInputDevice::SetVolumeOnIOThread, this, volume));
106 } 105 }
107 106
108 void AudioInputDevice::SetAutomaticGainControl(bool enabled) { 107 void AudioInputDevice::SetAutomaticGainControl(bool enabled) {
109 DVLOG(1) << "SetAutomaticGainControl(enabled=" << enabled << ")"; 108 DVLOG(1) << "SetAutomaticGainControl(enabled=" << enabled << ")";
110 message_loop()->PostTask(FROM_HERE, 109 task_runner()->PostTask(FROM_HERE,
111 base::Bind(&AudioInputDevice::SetAutomaticGainControlOnIOThread, 110 base::Bind(&AudioInputDevice::SetAutomaticGainControlOnIOThread,
112 this, enabled)); 111 this, enabled));
113 } 112 }
114 113
115 void AudioInputDevice::OnStreamCreated( 114 void AudioInputDevice::OnStreamCreated(
116 base::SharedMemoryHandle handle, 115 base::SharedMemoryHandle handle,
117 base::SyncSocket::Handle socket_handle, 116 base::SyncSocket::Handle socket_handle,
118 int length, 117 int length,
119 int total_segments) { 118 int total_segments) {
120 DCHECK(message_loop()->BelongsToCurrentThread()); 119 DCHECK(task_runner()->BelongsToCurrentThread());
121 #if defined(OS_WIN) 120 #if defined(OS_WIN)
122 DCHECK(handle); 121 DCHECK(handle);
123 DCHECK(socket_handle); 122 DCHECK(socket_handle);
124 #else 123 #else
125 DCHECK_GE(handle.fd, 0); 124 DCHECK_GE(handle.fd, 0);
126 DCHECK_GE(socket_handle, 0); 125 DCHECK_GE(socket_handle, 0);
127 #endif 126 #endif
128 DCHECK_GT(length, 0); 127 DCHECK_GT(length, 0);
129 128
130 if (state_ != CREATING_STREAM) 129 if (state_ != CREATING_STREAM)
(...skipping 15 matching lines...) Expand all
146 state_ = RECORDING; 145 state_ = RECORDING;
147 ipc_->RecordStream(); 146 ipc_->RecordStream();
148 } 147 }
149 148
150 void AudioInputDevice::OnVolume(double volume) { 149 void AudioInputDevice::OnVolume(double volume) {
151 NOTIMPLEMENTED(); 150 NOTIMPLEMENTED();
152 } 151 }
153 152
154 void AudioInputDevice::OnStateChanged( 153 void AudioInputDevice::OnStateChanged(
155 AudioInputIPCDelegate::State state) { 154 AudioInputIPCDelegate::State state) {
156 DCHECK(message_loop()->BelongsToCurrentThread()); 155 DCHECK(task_runner()->BelongsToCurrentThread());
157 156
158 // Do nothing if the stream has been closed. 157 // Do nothing if the stream has been closed.
159 if (state_ < CREATING_STREAM) 158 if (state_ < CREATING_STREAM)
160 return; 159 return;
161 160
162 // TODO(miu): Clean-up inconsistent and incomplete handling here. 161 // TODO(miu): Clean-up inconsistent and incomplete handling here.
163 // http://crbug.com/180640 162 // http://crbug.com/180640
164 switch (state) { 163 switch (state) {
165 case AudioInputIPCDelegate::kStopped: 164 case AudioInputIPCDelegate::kStopped:
166 ShutDownOnIOThread(); 165 ShutDownOnIOThread();
(...skipping 12 matching lines...) Expand all
179 if (!audio_thread_.IsStopped()) 178 if (!audio_thread_.IsStopped())
180 callback_->OnCaptureError(); 179 callback_->OnCaptureError();
181 break; 180 break;
182 default: 181 default:
183 NOTREACHED(); 182 NOTREACHED();
184 break; 183 break;
185 } 184 }
186 } 185 }
187 186
188 void AudioInputDevice::OnIPCClosed() { 187 void AudioInputDevice::OnIPCClosed() {
189 DCHECK(message_loop()->BelongsToCurrentThread()); 188 DCHECK(task_runner()->BelongsToCurrentThread());
190 state_ = IPC_CLOSED; 189 state_ = IPC_CLOSED;
191 ipc_.reset(); 190 ipc_.reset();
192 } 191 }
193 192
194 AudioInputDevice::~AudioInputDevice() { 193 AudioInputDevice::~AudioInputDevice() {
195 // TODO(henrika): The current design requires that the user calls 194 // TODO(henrika): The current design requires that the user calls
196 // Stop before deleting this class. 195 // Stop before deleting this class.
197 DCHECK(audio_thread_.IsStopped()); 196 DCHECK(audio_thread_.IsStopped());
198 } 197 }
199 198
200 void AudioInputDevice::StartUpOnIOThread() { 199 void AudioInputDevice::StartUpOnIOThread() {
201 DCHECK(message_loop()->BelongsToCurrentThread()); 200 DCHECK(task_runner()->BelongsToCurrentThread());
202 201
203 // Make sure we don't call Start() more than once. 202 // Make sure we don't call Start() more than once.
204 if (state_ != IDLE) 203 if (state_ != IDLE)
205 return; 204 return;
206 205
207 if (session_id_ <= 0) { 206 if (session_id_ <= 0) {
208 DLOG(WARNING) << "Invalid session id for the input stream " << session_id_; 207 DLOG(WARNING) << "Invalid session id for the input stream " << session_id_;
209 return; 208 return;
210 } 209 }
211 210
212 state_ = CREATING_STREAM; 211 state_ = CREATING_STREAM;
213 ipc_->CreateStream(this, session_id_, audio_parameters_, 212 ipc_->CreateStream(this, session_id_, audio_parameters_,
214 agc_is_enabled_, kRequestedSharedMemoryCount); 213 agc_is_enabled_, kRequestedSharedMemoryCount);
215 } 214 }
216 215
217 void AudioInputDevice::ShutDownOnIOThread() { 216 void AudioInputDevice::ShutDownOnIOThread() {
218 DCHECK(message_loop()->BelongsToCurrentThread()); 217 DCHECK(task_runner()->BelongsToCurrentThread());
219 218
220 // Close the stream, if we haven't already. 219 // Close the stream, if we haven't already.
221 if (state_ >= CREATING_STREAM) { 220 if (state_ >= CREATING_STREAM) {
222 ipc_->CloseStream(); 221 ipc_->CloseStream();
223 state_ = IDLE; 222 state_ = IDLE;
224 agc_is_enabled_ = false; 223 agc_is_enabled_ = false;
225 } 224 }
226 225
227 // We can run into an issue where ShutDownOnIOThread is called right after 226 // We can run into an issue where ShutDownOnIOThread is called right after
228 // OnStreamCreated is called in cases where Start/Stop are called before we 227 // OnStreamCreated is called in cases where Start/Stop are called before we
229 // get the OnStreamCreated callback. To handle that corner case, we call 228 // get the OnStreamCreated callback. To handle that corner case, we call
230 // Stop(). In most cases, the thread will already be stopped. 229 // Stop(). In most cases, the thread will already be stopped.
231 // 230 //
232 // Another situation is when the IO thread goes away before Stop() is called 231 // Another situation is when the IO thread goes away before Stop() is called
233 // in which case, we cannot use the message loop to close the thread handle 232 // in which case, we cannot use the message loop to close the thread handle
234 // and can't not rely on the main thread existing either. 233 // and can't not rely on the main thread existing either.
235 base::AutoLock auto_lock_(audio_thread_lock_); 234 base::AutoLock auto_lock_(audio_thread_lock_);
236 base::ThreadRestrictions::ScopedAllowIO allow_io; 235 base::ThreadRestrictions::ScopedAllowIO allow_io;
237 audio_thread_.Stop(NULL); 236 audio_thread_.Stop(NULL);
238 audio_callback_.reset(); 237 audio_callback_.reset();
239 stopping_hack_ = false; 238 stopping_hack_ = false;
240 } 239 }
241 240
242 void AudioInputDevice::SetVolumeOnIOThread(double volume) { 241 void AudioInputDevice::SetVolumeOnIOThread(double volume) {
243 DCHECK(message_loop()->BelongsToCurrentThread()); 242 DCHECK(task_runner()->BelongsToCurrentThread());
244 if (state_ >= CREATING_STREAM) 243 if (state_ >= CREATING_STREAM)
245 ipc_->SetVolume(volume); 244 ipc_->SetVolume(volume);
246 } 245 }
247 246
248 void AudioInputDevice::SetAutomaticGainControlOnIOThread(bool enabled) { 247 void AudioInputDevice::SetAutomaticGainControlOnIOThread(bool enabled) {
249 DCHECK(message_loop()->BelongsToCurrentThread()); 248 DCHECK(task_runner()->BelongsToCurrentThread());
250 249
251 if (state_ >= CREATING_STREAM) { 250 if (state_ >= CREATING_STREAM) {
252 DLOG(WARNING) << "The AGC state can not be modified after starting."; 251 DLOG(WARNING) << "The AGC state can not be modified after starting.";
253 return; 252 return;
254 } 253 }
255 254
256 // We simply store the new AGC setting here. This value will be used when 255 // We simply store the new AGC setting here. This value will be used when
257 // a new stream is initialized and by GetAutomaticGainControl(). 256 // a new stream is initialized and by GetAutomaticGainControl().
258 agc_is_enabled_ = enabled; 257 agc_is_enabled_ = enabled;
259 } 258 }
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
309 // with nominal range -1.0 -> +1.0. 308 // with nominal range -1.0 -> +1.0.
310 audio_bus_->FromInterleaved(memory, audio_bus_->frames(), bytes_per_sample); 309 audio_bus_->FromInterleaved(memory, audio_bus_->frames(), bytes_per_sample);
311 310
312 // Deliver captured data to the client in floating point format 311 // Deliver captured data to the client in floating point format
313 // and update the audio-delay measurement. 312 // and update the audio-delay measurement.
314 capture_callback_->Capture( 313 capture_callback_->Capture(
315 audio_bus_.get(), audio_delay_milliseconds, volume, key_pressed); 314 audio_bus_.get(), audio_delay_milliseconds, volume, key_pressed);
316 } 315 }
317 316
318 } // namespace media 317 } // namespace media
OLDNEW
« no previous file with comments | « media/audio/audio_input_device.h ('k') | media/audio/audio_low_latency_input_output_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698