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

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

Issue 2784433002: Ensures that audio tasks cannot run after AudioManager is deleted. (Closed)
Patch Set: cleanup Created 3 years, 7 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
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_manager.h" 5 #include "media/audio/audio_manager.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <utility> 9 #include <utility>
10 10
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after
234 DISALLOW_COPY_AND_ASSIGN(AudioManagerHelper); 234 DISALLOW_COPY_AND_ASSIGN(AudioManagerHelper);
235 }; 235 };
236 236
237 AudioManagerHelper* GetHelper() { 237 AudioManagerHelper* GetHelper() {
238 static AudioManagerHelper* helper = new AudioManagerHelper(); 238 static AudioManagerHelper* helper = new AudioManagerHelper();
239 return helper; 239 return helper;
240 } 240 }
241 241
242 } // namespace 242 } // namespace
243 243
244 void AudioManagerDeleter::operator()(const AudioManager* instance) const {
245 CHECK(instance);
246 // We reset g_last_created here instead of in the destructor of AudioManager
247 // because the destructor runs on the audio thread. We want to always change
248 // g_last_created from the main thread.
249 if (g_last_created == instance) {
250 g_last_created = nullptr;
251 } else {
252 // We create multiple instances of AudioManager only when testing.
253 // We should not encounter this case in production.
254 LOG(WARNING) << "Multiple instances of AudioManager detected";
255 }
256
257 // The deleter runs on the main thread, and AudioManager must be destroyed on
258 // the audio thread. If the audio thread is the same as the main one, tasks
259 // after this point are not executed, hence this is the only chance to delete
260 // AudioManager. See http://crbug.com/623703 for more details.
261 if (instance->GetTaskRunner()->BelongsToCurrentThread()) {
262 delete instance;
263 return;
264 }
265
266 // AudioManager must be destroyed on the audio thread. See
267 // http://crbug.com/705455 for an existing AudioManager lifetime issue.
268 if (!instance->GetTaskRunner()->DeleteSoon(FROM_HERE, instance))
269 LOG(WARNING) << "Failed to delete AudioManager instance.";
270 }
271
272 // Forward declaration of the platform specific AudioManager factory function. 244 // Forward declaration of the platform specific AudioManager factory function.
273 ScopedAudioManagerPtr CreateAudioManager( 245 std::unique_ptr<AudioManager> CreateAudioManager(
274 scoped_refptr<base::SingleThreadTaskRunner> task_runner, 246 std::unique_ptr<AudioThread> audio_thread,
275 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner,
276 AudioLogFactory* audio_log_factory); 247 AudioLogFactory* audio_log_factory);
277 248
278 void AudioManager::SetMaxStreamCountForTesting(int max_input, int max_output) { 249 void AudioManager::SetMaxStreamCountForTesting(int max_input, int max_output) {
279 NOTREACHED(); 250 NOTREACHED();
280 } 251 }
281 252
282 AudioManager::AudioManager( 253 AudioManager::AudioManager(std::unique_ptr<AudioThread> audio_thread)
283 scoped_refptr<base::SingleThreadTaskRunner> task_runner, 254 : audio_thread_(std::move(audio_thread)) {
284 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner) 255 DCHECK(audio_thread_);
285 : task_runner_(std::move(task_runner)),
286 worker_task_runner_(std::move(worker_task_runner)) {
287 DCHECK(task_runner_);
288 DCHECK(worker_task_runner_);
289 256
290 if (g_last_created) { 257 if (g_last_created) {
291 // We create multiple instances of AudioManager only when testing. 258 // We create multiple instances of AudioManager only when testing.
292 // We should not encounter this case in production. 259 // We should not encounter this case in production.
293 LOG(WARNING) << "Multiple instances of AudioManager detected"; 260 LOG(WARNING) << "Multiple instances of AudioManager detected";
294 } 261 }
295 // We always override |g_last_created| irrespective of whether it is already 262 // We always override |g_last_created| irrespective of whether it is already
296 // set or not becuase it represents the last created instance. 263 // set or not becuase it represents the last created instance.
297 g_last_created = this; 264 g_last_created = this;
298 } 265 }
299 266
300 AudioManager::~AudioManager() { 267 AudioManager::~AudioManager() {
301 DCHECK(task_runner_->BelongsToCurrentThread()); 268 DCHECK(thread_checker_.CalledOnValidThread());
269
270 // Ensure that AudioManager has been shutdown.
271 DCHECK(!audio_thread_);
272
273 if (g_last_created == this) {
274 g_last_created = nullptr;
275 } else {
276 // We create multiple instances of AudioManager only when testing.
277 // We should not encounter this case in production.
278 LOG(WARNING) << "Multiple instances of AudioManager detected";
279 }
302 } 280 }
303 281
304 // static 282 // static
305 ScopedAudioManagerPtr AudioManager::Create( 283 std::unique_ptr<AudioManager> AudioManager::Create(
306 scoped_refptr<base::SingleThreadTaskRunner> task_runner, 284 std::unique_ptr<AudioThread> audio_thread,
307 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner,
308 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, 285 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner,
309 AudioLogFactory* audio_log_factory) { 286 AudioLogFactory* audio_log_factory) {
310 DCHECK(task_runner); 287 std::unique_ptr<AudioManager> manager =
311 DCHECK(worker_task_runner); 288 CreateAudioManager(std::move(audio_thread), audio_log_factory);
312 ScopedAudioManagerPtr manager = CreateAudioManager(
313 std::move(task_runner), std::move(worker_task_runner), audio_log_factory);
314 #if BUILDFLAG(ENABLE_WEBRTC) 289 #if BUILDFLAG(ENABLE_WEBRTC)
315 manager->InitializeOutputDebugRecording(std::move(file_task_runner)); 290 manager->InitializeOutputDebugRecording(std::move(file_task_runner));
316 #endif 291 #endif
317 return manager; 292 return manager;
318 } 293 }
319 294
320 // static 295 // static
321 ScopedAudioManagerPtr AudioManager::CreateForTesting( 296 std::unique_ptr<AudioManager> AudioManager::CreateForTesting(
322 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { 297 std::unique_ptr<AudioThread> audio_thread) {
323 #if defined(OS_WIN) 298 #if defined(OS_WIN)
324 GetHelper()->InitializeCOMForTesting(); 299 GetHelper()->InitializeCOMForTesting();
325 #endif 300 #endif
326 return Create(task_runner, task_runner, task_runner, 301 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner =
302 audio_thread->GetWorkerTaskRunner();
303 return Create(std::move(audio_thread), std::move(file_task_runner),
327 GetHelper()->fake_log_factory()); 304 GetHelper()->fake_log_factory());
328 } 305 }
329 306
330 // static 307 // static
331 void AudioManager::StartHangMonitorIfNeeded( 308 void AudioManager::StartHangMonitorIfNeeded(
332 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { 309 scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
333 if (GetHelper()->monitor_task_runner()) 310 if (GetHelper()->monitor_task_runner())
334 return; 311 return;
335 312
336 DCHECK(AudioManager::Get()); 313 DCHECK(AudioManager::Get());
(...skipping 13 matching lines...) Expand all
350 const std::string& AudioManager::GetGlobalAppName() { 327 const std::string& AudioManager::GetGlobalAppName() {
351 return GetHelper()->app_name(); 328 return GetHelper()->app_name();
352 } 329 }
353 #endif 330 #endif
354 331
355 // static 332 // static
356 AudioManager* AudioManager::Get() { 333 AudioManager* AudioManager::Get() {
357 return g_last_created; 334 return g_last_created;
358 } 335 }
359 336
337 void AudioManager::Shutdown() {
338 DCHECK(thread_checker_.CalledOnValidThread());
339 DCHECK(audio_thread_);
340
341 if (audio_thread_->GetTaskRunner()->BelongsToCurrentThread()) {
342 ShutdownOnAudioThread();
343 } else {
344 audio_thread_->GetTaskRunner()->PostTask(
345 FROM_HERE, base::Bind(&AudioManager::ShutdownOnAudioThread,
346 base::Unretained(this)));
347 }
348 audio_thread_->Stop();
o1ka 2017/04/28 13:24:20 Have you verified that hang monitor (l.308) will w
alokp 2017/04/28 17:31:12 Hang monitor runs on the file thread, which gets s
o1ka 2017/05/02 16:16:00 I would say hang monitor *currently in content* ru
alokp 2017/05/02 17:11:28 You are right - it runs on IO thread - I misread t
o1ka 2017/05/02 21:42:23 "take care of tasks posted to it" - I mean this ht
alokp 2017/05/09 19:06:15 I looked at this more closely. It would be easier
o1ka 2017/05/10 15:57:56 sgtm
349 audio_thread_.reset();
o1ka 2017/04/28 13:24:19 I believe we should only Stop() here and use anoth
alokp 2017/04/28 17:31:12 Even if we do not reset the |audio_thread| here, A
o1ka 2017/05/02 16:16:00 Could you clarify?
alokp 2017/05/02 17:11:28 base::Thread::task_runner() returns NULL after it
o1ka 2017/05/02 21:42:23 I'm not sure. To answer this question I need to di
alokp 2017/05/09 19:06:15 Done.
o1ka 2017/05/10 15:57:56 Thanks!
350 }
351
360 } // namespace media 352 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698