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/audio_manager.h" | 5 #include "media/audio/audio_manager.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "build/build_config.h" |
12 #include "media/audio/fake_audio_log_factory.h" | 13 #include "media/audio/fake_audio_log_factory.h" |
13 | 14 |
14 namespace media { | 15 namespace media { |
15 namespace { | 16 namespace { |
16 AudioManager* g_last_created = NULL; | 17 AudioManager* g_last_created = NULL; |
17 static base::LazyInstance<FakeAudioLogFactory>::Leaky g_fake_log_factory = | 18 |
| 19 // Helper class for managing global AudioManager data and hang timers. If the |
| 20 // audio thread is unresponsive for more than a minute we want to crash the |
| 21 // process so we can catch offenders quickly in the field. |
| 22 class AudioManagerHelper { |
| 23 public: |
| 24 AudioManagerHelper() : max_hung_task_time_(base::TimeDelta::FromMinutes(1)) {} |
| 25 ~AudioManagerHelper() {} |
| 26 |
| 27 void StartHangTimer( |
| 28 const scoped_refptr<base::SingleThreadTaskRunner>& monitor_task_runner) { |
| 29 CHECK(!monitor_task_runner_); |
| 30 monitor_task_runner_ = monitor_task_runner; |
| 31 UpdateLastAudioThreadTimeTick(); |
| 32 CrashOnAudioThreadHang(); |
| 33 } |
| 34 |
| 35 // Runs on |monitor_task_runner| typically, but may be started on any thread. |
| 36 void CrashOnAudioThreadHang() { |
| 37 base::AutoLock lock(hang_lock_); |
| 38 CHECK(base::TimeTicks::Now() - last_audio_thread_timer_tick_ <= |
| 39 max_hung_task_time_); |
| 40 monitor_task_runner_->PostDelayedTask( |
| 41 FROM_HERE, base::Bind(&AudioManagerHelper::CrashOnAudioThreadHang, |
| 42 base::Unretained(this)), |
| 43 max_hung_task_time_); |
| 44 } |
| 45 |
| 46 // Runs on the audio thread typically, but may be started on any thread. |
| 47 void UpdateLastAudioThreadTimeTick() { |
| 48 base::AutoLock lock(hang_lock_); |
| 49 last_audio_thread_timer_tick_ = base::TimeTicks::Now(); |
| 50 g_last_created->GetTaskRunner()->PostDelayedTask( |
| 51 FROM_HERE, |
| 52 base::Bind(&AudioManagerHelper::UpdateLastAudioThreadTimeTick, |
| 53 base::Unretained(this)), |
| 54 max_hung_task_time_ / 2); |
| 55 } |
| 56 |
| 57 AudioLogFactory* fake_log_factory() { return &fake_log_factory_; } |
| 58 |
| 59 private: |
| 60 FakeAudioLogFactory fake_log_factory_; |
| 61 |
| 62 const base::TimeDelta max_hung_task_time_; |
| 63 scoped_refptr<base::SingleThreadTaskRunner> monitor_task_runner_; |
| 64 |
| 65 base::Lock hang_lock_; |
| 66 base::TimeTicks last_audio_thread_timer_tick_; |
| 67 |
| 68 DISALLOW_COPY_AND_ASSIGN(AudioManagerHelper); |
| 69 }; |
| 70 |
| 71 static base::LazyInstance<AudioManagerHelper>::Leaky g_helper = |
18 LAZY_INSTANCE_INITIALIZER; | 72 LAZY_INSTANCE_INITIALIZER; |
19 } | 73 } |
20 | 74 |
21 // Forward declaration of the platform specific AudioManager factory function. | 75 // Forward declaration of the platform specific AudioManager factory function. |
22 AudioManager* CreateAudioManager(AudioLogFactory* audio_log_factory); | 76 AudioManager* CreateAudioManager(AudioLogFactory* audio_log_factory); |
23 | 77 |
24 AudioManager::AudioManager() {} | 78 AudioManager::AudioManager() {} |
25 | 79 |
26 AudioManager::~AudioManager() { | 80 AudioManager::~AudioManager() { |
27 CHECK(!g_last_created || g_last_created == this); | 81 CHECK(!g_last_created || g_last_created == this); |
28 g_last_created = NULL; | 82 g_last_created = NULL; |
29 } | 83 } |
30 | 84 |
31 // static | 85 // static |
32 AudioManager* AudioManager::Create(AudioLogFactory* audio_log_factory) { | 86 AudioManager* AudioManager::Create(AudioLogFactory* audio_log_factory) { |
33 CHECK(!g_last_created); | 87 CHECK(!g_last_created); |
34 g_last_created = CreateAudioManager(audio_log_factory); | 88 g_last_created = CreateAudioManager(audio_log_factory); |
35 return g_last_created; | 89 return g_last_created; |
36 } | 90 } |
37 | 91 |
38 // static | 92 // static |
| 93 AudioManager* AudioManager::CreateWithHangTimer( |
| 94 AudioLogFactory* audio_log_factory, |
| 95 const scoped_refptr<base::SingleThreadTaskRunner>& monitor_task_runner) { |
| 96 AudioManager* manager = Create(audio_log_factory); |
| 97 // On OSX the audio thread is the UI thread, for which a hang monitor is not |
| 98 // necessary. |
| 99 #if !defined(OS_MACOSX) |
| 100 g_helper.Pointer()->StartHangTimer(monitor_task_runner); |
| 101 #endif |
| 102 return manager; |
| 103 } |
| 104 |
| 105 // static |
39 AudioManager* AudioManager::CreateForTesting() { | 106 AudioManager* AudioManager::CreateForTesting() { |
40 return Create(g_fake_log_factory.Pointer()); | 107 return Create(g_helper.Pointer()->fake_log_factory()); |
41 } | 108 } |
42 | 109 |
43 // static | 110 // static |
44 AudioManager* AudioManager::Get() { | 111 AudioManager* AudioManager::Get() { |
45 return g_last_created; | 112 return g_last_created; |
46 } | 113 } |
47 | 114 |
48 } // namespace media | 115 } // namespace media |
OLD | NEW |