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 "media/audio/fake_audio_log_factory.h" | 12 #include "media/audio/fake_audio_log_factory.h" |
13 | 13 |
14 namespace media { | 14 namespace media { |
15 namespace { | 15 namespace { |
16 AudioManager* g_last_created = NULL; | 16 AudioManager* g_last_created = NULL; |
17 static base::LazyInstance<FakeAudioLogFactory>::Leaky g_fake_log_factory = | 17 |
18 // Helper class for managing global AudioManager data and hang timers. If the | |
19 // audio thread is unresponsive for more than a minute we want to crash the | |
20 // process so we can catch offenders quickly in the field. | |
21 class AudioManagerHelper { | |
22 public: | |
23 AudioManagerHelper() : max_hung_task_time_(base::TimeDelta::FromMinutes(1)) {} | |
24 ~AudioManagerHelper() {} | |
25 | |
26 void StartHangTimer( | |
27 const scoped_refptr<base::SingleThreadTaskRunner>& monitor_task_runner) { | |
28 CHECK(!monitor_task_runner_); | |
nasko
2015/03/25 17:28:00
nit: Can we also check that this is called on the
DaleCurtis
2015/03/25 20:28:56
Sent you a chat about this, because I'm not clear
nasko
2015/03/25 20:52:23
Clarified over chat. Since this method isn't expec
| |
29 monitor_task_runner_ = monitor_task_runner; | |
30 UpdateLastAudioThreadTimeTick(); | |
31 CrashOnAudioThreadHang(); | |
32 } | |
33 | |
34 void CrashOnAudioThreadHang() { | |
35 base::AutoLock lock(hang_lock_); | |
36 CHECK(base::TimeTicks::Now() - last_audio_thread_timer_tick_ <= | |
37 max_hung_task_time_); | |
38 monitor_task_runner_->PostDelayedTask( | |
39 FROM_HERE, base::Bind(&AudioManagerHelper::CrashOnAudioThreadHang, | |
40 base::Unretained(this)), | |
nasko
2015/03/25 17:28:00
Are we guaranteed that this object will never be n
DaleCurtis
2015/03/25 20:28:56
Yes, it's a LazyInstance::Leaky, which means it'll
nasko
2015/03/25 20:52:23
Acknowledged.
| |
41 max_hung_task_time_); | |
42 } | |
43 | |
44 void UpdateLastAudioThreadTimeTick() { | |
45 base::AutoLock lock(hang_lock_); | |
46 last_audio_thread_timer_tick_ = base::TimeTicks::Now(); | |
47 g_last_created->GetTaskRunner()->PostDelayedTask( | |
48 FROM_HERE, | |
49 base::Bind(&AudioManagerHelper::UpdateLastAudioThreadTimeTick, | |
50 base::Unretained(this)), | |
51 max_hung_task_time_ / 2); | |
52 } | |
53 | |
54 AudioLogFactory* fake_log_factory() { return &fake_log_factory_; } | |
55 | |
56 private: | |
57 FakeAudioLogFactory fake_log_factory_; | |
58 | |
59 const base::TimeDelta max_hung_task_time_; | |
60 scoped_refptr<base::SingleThreadTaskRunner> monitor_task_runner_; | |
61 | |
62 base::Lock hang_lock_; | |
63 base::TimeTicks last_audio_thread_timer_tick_; | |
64 | |
65 DISALLOW_COPY_AND_ASSIGN(AudioManagerHelper); | |
66 }; | |
67 | |
68 static base::LazyInstance<AudioManagerHelper>::Leaky g_helper = | |
18 LAZY_INSTANCE_INITIALIZER; | 69 LAZY_INSTANCE_INITIALIZER; |
19 } | 70 } |
20 | 71 |
21 // Forward declaration of the platform specific AudioManager factory function. | 72 // Forward declaration of the platform specific AudioManager factory function. |
22 AudioManager* CreateAudioManager(AudioLogFactory* audio_log_factory); | 73 AudioManager* CreateAudioManager(AudioLogFactory* audio_log_factory); |
23 | 74 |
24 AudioManager::AudioManager() {} | 75 AudioManager::AudioManager() {} |
25 | 76 |
26 AudioManager::~AudioManager() { | 77 AudioManager::~AudioManager() { |
27 CHECK(!g_last_created || g_last_created == this); | 78 CHECK(!g_last_created || g_last_created == this); |
28 g_last_created = NULL; | 79 g_last_created = NULL; |
29 } | 80 } |
30 | 81 |
31 // static | 82 // static |
32 AudioManager* AudioManager::Create(AudioLogFactory* audio_log_factory) { | 83 AudioManager* AudioManager::Create(AudioLogFactory* audio_log_factory) { |
33 CHECK(!g_last_created); | 84 CHECK(!g_last_created); |
34 g_last_created = CreateAudioManager(audio_log_factory); | 85 g_last_created = CreateAudioManager(audio_log_factory); |
35 return g_last_created; | 86 return g_last_created; |
36 } | 87 } |
37 | 88 |
38 // static | 89 // static |
90 AudioManager* AudioManager::CreateWithHangTimer( | |
91 AudioLogFactory* audio_log_factory, | |
92 const scoped_refptr<base::SingleThreadTaskRunner>& monitor_task_runner) { | |
93 AudioManager* manager = Create(audio_log_factory); | |
94 g_helper.Pointer()->StartHangTimer(monitor_task_runner); | |
95 return manager; | |
96 } | |
97 | |
98 // static | |
39 AudioManager* AudioManager::CreateForTesting() { | 99 AudioManager* AudioManager::CreateForTesting() { |
40 return Create(g_fake_log_factory.Pointer()); | 100 return Create(g_helper.Pointer()->fake_log_factory()); |
41 } | 101 } |
42 | 102 |
43 // static | 103 // static |
44 AudioManager* AudioManager::Get() { | 104 AudioManager* AudioManager::Get() { |
45 return g_last_created; | 105 return g_last_created; |
46 } | 106 } |
47 | 107 |
48 } // namespace media | 108 } // namespace media |
OLD | NEW |