| 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/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/debug/alias.h" | 10 #include "base/debug/alias.h" |
| 11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/power_monitor/power_monitor.h" | 14 #include "base/power_monitor/power_monitor.h" |
| 15 #include "build/build_config.h" | 15 #include "build/build_config.h" |
| 16 #include "media/audio/audio_manager_factory.h" | 16 #include "media/audio/audio_manager_factory.h" |
| 17 #include "media/audio/fake_audio_log_factory.h" | 17 #include "media/audio/fake_audio_log_factory.h" |
| 18 #include "media/base/media_switches.h" | 18 #include "media/base/media_switches.h" |
| 19 | 19 |
| 20 #if defined(OS_WIN) | 20 #if defined(OS_WIN) |
| 21 #include "base/win/scoped_com_initializer.h" | 21 #include "base/win/scoped_com_initializer.h" |
| 22 #endif | 22 #endif |
| 23 | 23 |
| 24 namespace media { | 24 namespace media { |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 // The singleton instance of AudioManager. This is set when Create() is called. | 27 // The singleton instance of AudioManager. This is set when Create() is called. |
| 28 AudioManager* g_last_created = NULL; | 28 AudioManager* g_last_created = nullptr; |
| 29 | 29 |
| 30 // The singleton instance of AudioManagerFactory. This is only set if | 30 // The singleton instance of AudioManagerFactory. This is only set if |
| 31 // SetFactory() is called. If it is set when Create() is called, its | 31 // SetFactory() is called. If it is set when Create() is called, its |
| 32 // CreateInstance() function is used to set |g_last_created|. Otherwise, the | 32 // CreateInstance() function is used to set |g_last_created|. Otherwise, the |
| 33 // linked implementation of media::CreateAudioManager is used to set | 33 // linked implementation of media::CreateAudioManager is used to set |
| 34 // |g_last_created|. | 34 // |g_last_created|. |
| 35 AudioManagerFactory* g_audio_manager_factory = NULL; | 35 AudioManagerFactory* g_audio_manager_factory = nullptr; |
| 36 | 36 |
| 37 // Maximum number of failed pings to the audio thread allowed. A crash will be | 37 // Maximum number of failed pings to the audio thread allowed. A crash will be |
| 38 // issued once this count is reached. We require at least two pings before | 38 // issued once this count is reached. We require at least two pings before |
| 39 // crashing to ensure unobservable power events aren't mistakenly caught (e.g., | 39 // crashing to ensure unobservable power events aren't mistakenly caught (e.g., |
| 40 // the system suspends before a OnSuspend() event can be fired.). | 40 // the system suspends before a OnSuspend() event can be fired.). |
| 41 const int kMaxHangFailureCount = 2; | 41 const int kMaxHangFailureCount = 2; |
| 42 | 42 |
| 43 // Helper class for managing global AudioManager data and hang timers. If the | 43 // Helper class for managing global AudioManager data and hang timers. If the |
| 44 // audio thread is unresponsive for more than two minutes we want to crash the | 44 // audio thread is unresponsive for more than two minutes we want to crash the |
| 45 // process so we can catch offenders quickly in the field. | 45 // process so we can catch offenders quickly in the field. |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 AudioLogFactory* fake_log_factory() { return &fake_log_factory_; } | 149 AudioLogFactory* fake_log_factory() { return &fake_log_factory_; } |
| 150 | 150 |
| 151 #if defined(OS_WIN) | 151 #if defined(OS_WIN) |
| 152 // This should be called before creating an AudioManager in tests to ensure | 152 // This should be called before creating an AudioManager in tests to ensure |
| 153 // that the creating thread is COM initialized. | 153 // that the creating thread is COM initialized. |
| 154 void InitializeCOMForTesting() { | 154 void InitializeCOMForTesting() { |
| 155 com_initializer_for_testing_.reset(new base::win::ScopedCOMInitializer()); | 155 com_initializer_for_testing_.reset(new base::win::ScopedCOMInitializer()); |
| 156 } | 156 } |
| 157 #endif | 157 #endif |
| 158 | 158 |
| 159 #if defined(OS_LINUX) |
| 160 void set_app_name(const std::string& app_name) { |
| 161 app_name_ = app_name; |
| 162 } |
| 163 |
| 164 const std::string& app_name() const { |
| 165 return app_name_; |
| 166 } |
| 167 #endif |
| 168 |
| 159 private: | 169 private: |
| 160 FakeAudioLogFactory fake_log_factory_; | 170 FakeAudioLogFactory fake_log_factory_; |
| 161 | 171 |
| 162 const base::TimeDelta max_hung_task_time_; | 172 const base::TimeDelta max_hung_task_time_; |
| 163 scoped_refptr<base::SingleThreadTaskRunner> monitor_task_runner_; | 173 scoped_refptr<base::SingleThreadTaskRunner> monitor_task_runner_; |
| 164 | 174 |
| 165 base::Lock hang_lock_; | 175 base::Lock hang_lock_; |
| 166 bool hang_detection_enabled_; | 176 bool hang_detection_enabled_; |
| 167 base::TimeTicks last_audio_thread_timer_tick_; | 177 base::TimeTicks last_audio_thread_timer_tick_; |
| 168 int hang_failures_; | 178 int hang_failures_; |
| 169 bool io_task_running_; | 179 bool io_task_running_; |
| 170 bool audio_task_running_; | 180 bool audio_task_running_; |
| 171 | 181 |
| 172 #if defined(OS_WIN) | 182 #if defined(OS_WIN) |
| 173 scoped_ptr<base::win::ScopedCOMInitializer> com_initializer_for_testing_; | 183 scoped_ptr<base::win::ScopedCOMInitializer> com_initializer_for_testing_; |
| 174 #endif | 184 #endif |
| 175 | 185 |
| 186 #if defined(OS_LINUX) |
| 187 std::string app_name_; |
| 188 #endif |
| 189 |
| 176 DISALLOW_COPY_AND_ASSIGN(AudioManagerHelper); | 190 DISALLOW_COPY_AND_ASSIGN(AudioManagerHelper); |
| 177 }; | 191 }; |
| 178 | 192 |
| 179 static bool g_hang_monitor_enabled = false; | 193 bool g_hang_monitor_enabled = false; |
| 180 | 194 |
| 181 static base::LazyInstance<AudioManagerHelper>::Leaky g_helper = | 195 base::LazyInstance<AudioManagerHelper>::Leaky g_helper = |
| 182 LAZY_INSTANCE_INITIALIZER; | 196 LAZY_INSTANCE_INITIALIZER; |
| 197 |
| 183 } // namespace | 198 } // namespace |
| 184 | 199 |
| 185 // Forward declaration of the platform specific AudioManager factory function. | 200 // Forward declaration of the platform specific AudioManager factory function. |
| 186 AudioManager* CreateAudioManager(AudioLogFactory* audio_log_factory); | 201 AudioManager* CreateAudioManager(AudioLogFactory* audio_log_factory); |
| 187 | 202 |
| 188 AudioManager::AudioManager() {} | 203 AudioManager::AudioManager() {} |
| 189 | 204 |
| 190 AudioManager::~AudioManager() { | 205 AudioManager::~AudioManager() { |
| 191 CHECK(!g_last_created || g_last_created == this); | 206 CHECK(!g_last_created || g_last_created == this); |
| 192 g_last_created = NULL; | 207 g_last_created = nullptr; |
| 193 } | 208 } |
| 194 | 209 |
| 195 // static | 210 // static |
| 196 void AudioManager::SetFactory(AudioManagerFactory* factory) { | 211 void AudioManager::SetFactory(AudioManagerFactory* factory) { |
| 197 CHECK(factory); | 212 CHECK(factory); |
| 198 CHECK(!g_last_created); | 213 CHECK(!g_last_created); |
| 199 CHECK(!g_audio_manager_factory); | 214 CHECK(!g_audio_manager_factory); |
| 200 g_audio_manager_factory = factory; | 215 g_audio_manager_factory = factory; |
| 201 } | 216 } |
| 202 | 217 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 void AudioManager::EnableHangMonitor() { | 259 void AudioManager::EnableHangMonitor() { |
| 245 CHECK(!g_last_created); | 260 CHECK(!g_last_created); |
| 246 // On OSX the audio thread is the UI thread, for which a hang monitor is not | 261 // On OSX the audio thread is the UI thread, for which a hang monitor is not |
| 247 // necessary or recommended. If it's manually requested, we should allow it | 262 // necessary or recommended. If it's manually requested, we should allow it |
| 248 // to start though. | 263 // to start though. |
| 249 #if !defined(OS_MACOSX) | 264 #if !defined(OS_MACOSX) |
| 250 g_hang_monitor_enabled = true; | 265 g_hang_monitor_enabled = true; |
| 251 #endif | 266 #endif |
| 252 } | 267 } |
| 253 | 268 |
| 269 #if defined(OS_LINUX) |
| 270 // static |
| 271 void AudioManager::SetGlobalAppName(const std::string& app_name) { |
| 272 g_helper.Pointer()->set_app_name(app_name); |
| 273 } |
| 274 |
| 275 // static |
| 276 const std::string& AudioManager::GetGlobalAppName() { |
| 277 return g_helper.Pointer()->app_name(); |
| 278 } |
| 279 #endif |
| 280 |
| 254 // static | 281 // static |
| 255 AudioManager* AudioManager::Get() { | 282 AudioManager* AudioManager::Get() { |
| 256 return g_last_created; | 283 return g_last_created; |
| 257 } | 284 } |
| 258 | 285 |
| 259 } // namespace media | 286 } // namespace media |
| OLD | NEW |