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 "base/power_monitor/power_monitor.h" |
12 #include "build/build_config.h" | 13 #include "build/build_config.h" |
13 #include "media/audio/fake_audio_log_factory.h" | 14 #include "media/audio/fake_audio_log_factory.h" |
14 | 15 |
15 namespace media { | 16 namespace media { |
16 namespace { | 17 namespace { |
17 AudioManager* g_last_created = NULL; | 18 AudioManager* g_last_created = NULL; |
18 | 19 |
19 // Helper class for managing global AudioManager data and hang timers. If the | 20 // 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 // 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 // process so we can catch offenders quickly in the field. |
22 class AudioManagerHelper { | 23 class AudioManagerHelper : public base::PowerObserver { |
23 public: | 24 public: |
24 AudioManagerHelper() : max_hung_task_time_(base::TimeDelta::FromMinutes(1)) {} | 25 AudioManagerHelper() |
25 ~AudioManagerHelper() {} | 26 : max_hung_task_time_(base::TimeDelta::FromMinutes(1)), |
| 27 hang_detection_enabled_(true) {} |
| 28 ~AudioManagerHelper() override {} |
26 | 29 |
27 void StartHangTimer( | 30 void StartHangTimer( |
28 const scoped_refptr<base::SingleThreadTaskRunner>& monitor_task_runner) { | 31 const scoped_refptr<base::SingleThreadTaskRunner>& monitor_task_runner) { |
29 CHECK(!monitor_task_runner_); | 32 CHECK(!monitor_task_runner_); |
30 monitor_task_runner_ = monitor_task_runner; | 33 monitor_task_runner_ = monitor_task_runner; |
| 34 base::PowerMonitor::Get()->AddObserver(this); |
31 UpdateLastAudioThreadTimeTick(); | 35 UpdateLastAudioThreadTimeTick(); |
32 CrashOnAudioThreadHang(); | 36 CrashOnAudioThreadHang(); |
33 } | 37 } |
34 | 38 |
| 39 // Disable hang detection when the system goes into the suspend state. |
| 40 void OnSuspend() override { |
| 41 base::AutoLock lock(hang_lock_); |
| 42 hang_detection_enabled_ = false; |
| 43 } |
| 44 |
| 45 // Reenable hang detection once the system comes out of the suspend state. |
| 46 void OnResume() override { |
| 47 base::AutoLock lock(hang_lock_); |
| 48 hang_detection_enabled_ = true; |
| 49 last_audio_thread_timer_tick_ = base::TimeTicks::Now(); |
| 50 } |
| 51 |
35 // Runs on |monitor_task_runner| typically, but may be started on any thread. | 52 // Runs on |monitor_task_runner| typically, but may be started on any thread. |
36 void CrashOnAudioThreadHang() { | 53 void CrashOnAudioThreadHang() { |
37 base::AutoLock lock(hang_lock_); | 54 base::AutoLock lock(hang_lock_); |
38 CHECK(base::TimeTicks::Now() - last_audio_thread_timer_tick_ <= | 55 |
39 max_hung_task_time_); | 56 // Don't attempt to verify the tick time if the system is in the process of |
| 57 // suspending or resuming. |
| 58 if (hang_detection_enabled_) { |
| 59 CHECK(base::TimeTicks::Now() - last_audio_thread_timer_tick_ <= |
| 60 max_hung_task_time_); |
| 61 } |
| 62 |
40 monitor_task_runner_->PostDelayedTask( | 63 monitor_task_runner_->PostDelayedTask( |
41 FROM_HERE, base::Bind(&AudioManagerHelper::CrashOnAudioThreadHang, | 64 FROM_HERE, base::Bind(&AudioManagerHelper::CrashOnAudioThreadHang, |
42 base::Unretained(this)), | 65 base::Unretained(this)), |
43 max_hung_task_time_); | 66 max_hung_task_time_); |
44 } | 67 } |
45 | 68 |
46 // Runs on the audio thread typically, but may be started on any thread. | 69 // Runs on the audio thread typically, but may be started on any thread. |
47 void UpdateLastAudioThreadTimeTick() { | 70 void UpdateLastAudioThreadTimeTick() { |
48 base::AutoLock lock(hang_lock_); | 71 base::AutoLock lock(hang_lock_); |
49 last_audio_thread_timer_tick_ = base::TimeTicks::Now(); | 72 last_audio_thread_timer_tick_ = base::TimeTicks::Now(); |
50 g_last_created->GetTaskRunner()->PostDelayedTask( | 73 g_last_created->GetTaskRunner()->PostDelayedTask( |
51 FROM_HERE, | 74 FROM_HERE, |
52 base::Bind(&AudioManagerHelper::UpdateLastAudioThreadTimeTick, | 75 base::Bind(&AudioManagerHelper::UpdateLastAudioThreadTimeTick, |
53 base::Unretained(this)), | 76 base::Unretained(this)), |
54 max_hung_task_time_ / 2); | 77 max_hung_task_time_ / 2); |
55 } | 78 } |
56 | 79 |
57 AudioLogFactory* fake_log_factory() { return &fake_log_factory_; } | 80 AudioLogFactory* fake_log_factory() { return &fake_log_factory_; } |
58 | 81 |
59 private: | 82 private: |
60 FakeAudioLogFactory fake_log_factory_; | 83 FakeAudioLogFactory fake_log_factory_; |
61 | 84 |
62 const base::TimeDelta max_hung_task_time_; | 85 const base::TimeDelta max_hung_task_time_; |
63 scoped_refptr<base::SingleThreadTaskRunner> monitor_task_runner_; | 86 scoped_refptr<base::SingleThreadTaskRunner> monitor_task_runner_; |
64 | 87 |
65 base::Lock hang_lock_; | 88 base::Lock hang_lock_; |
| 89 bool hang_detection_enabled_; |
66 base::TimeTicks last_audio_thread_timer_tick_; | 90 base::TimeTicks last_audio_thread_timer_tick_; |
67 | 91 |
68 DISALLOW_COPY_AND_ASSIGN(AudioManagerHelper); | 92 DISALLOW_COPY_AND_ASSIGN(AudioManagerHelper); |
69 }; | 93 }; |
70 | 94 |
71 static base::LazyInstance<AudioManagerHelper>::Leaky g_helper = | 95 static base::LazyInstance<AudioManagerHelper>::Leaky g_helper = |
72 LAZY_INSTANCE_INITIALIZER; | 96 LAZY_INSTANCE_INITIALIZER; |
73 } | 97 } |
74 | 98 |
75 // Forward declaration of the platform specific AudioManager factory function. | 99 // Forward declaration of the platform specific AudioManager factory function. |
(...skipping 30 matching lines...) Expand all Loading... |
106 AudioManager* AudioManager::CreateForTesting() { | 130 AudioManager* AudioManager::CreateForTesting() { |
107 return Create(g_helper.Pointer()->fake_log_factory()); | 131 return Create(g_helper.Pointer()->fake_log_factory()); |
108 } | 132 } |
109 | 133 |
110 // static | 134 // static |
111 AudioManager* AudioManager::Get() { | 135 AudioManager* AudioManager::Get() { |
112 return g_last_created; | 136 return g_last_created; |
113 } | 137 } |
114 | 138 |
115 } // namespace media | 139 } // namespace media |
OLD | NEW |