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

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

Issue 1806313003: Pass task runners to AudioManager constructor. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: implemented ScopedAudioManagerPtr Created 4 years, 9 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 "base/bind.h" 9 #include "base/bind.h"
10 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
11 #include "base/command_line.h" 11 #include "base/command_line.h"
12 #include "base/debug/alias.h" 12 #include "base/debug/alias.h"
13 #include "base/debug/crash_logging.h" 13 #include "base/debug/crash_logging.h"
14 #include "base/debug/dump_without_crashing.h" 14 #include "base/debug/dump_without_crashing.h"
15 #include "base/lazy_instance.h" 15 #include "base/lazy_instance.h"
16 #include "base/logging.h" 16 #include "base/logging.h"
17 #include "base/macros.h" 17 #include "base/macros.h"
18 #include "base/message_loop/message_loop.h" 18 #include "base/message_loop/message_loop.h"
19 #include "base/metrics/histogram_macros.h" 19 #include "base/metrics/histogram_macros.h"
20 #include "base/power_monitor/power_monitor.h" 20 #include "base/power_monitor/power_monitor.h"
21 #include "base/strings/stringprintf.h" 21 #include "base/strings/stringprintf.h"
22 #include "build/build_config.h" 22 #include "build/build_config.h"
23 #include "media/audio/audio_manager_factory.h" 23 #include "media/audio/audio_manager_factory.h"
24 #include "media/audio/fake_audio_log_factory.h"
25 #include "media/base/media_resources.h" 24 #include "media/base/media_resources.h"
26 #include "media/base/media_switches.h" 25 #include "media/base/media_switches.h"
27 26
28 #if defined(OS_WIN) 27 #if defined(OS_WIN)
29 #include "base/win/scoped_com_initializer.h" 28 #include "base/win/scoped_com_initializer.h"
30 #include "media/audio/win/core_audio_util_win.h" 29 #include "media/audio/win/core_audio_util_win.h"
31 #endif 30 #endif
32 31
33 namespace media { 32 namespace media {
34 namespace { 33 namespace {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
69 AudioManagerHelper() {} 68 AudioManagerHelper() {}
70 ~AudioManagerHelper() override {} 69 ~AudioManagerHelper() override {}
71 70
72 void HistogramThreadStatus(ThreadStatus status) { 71 void HistogramThreadStatus(ThreadStatus status) {
73 audio_thread_status_ = status; 72 audio_thread_status_ = status;
74 UMA_HISTOGRAM_ENUMERATION("Media.AudioThreadStatus", audio_thread_status_, 73 UMA_HISTOGRAM_ENUMERATION("Media.AudioThreadStatus", audio_thread_status_,
75 THREAD_MAX + 1); 74 THREAD_MAX + 1);
76 } 75 }
77 76
78 void StartHangTimer( 77 void StartHangTimer(
79 const scoped_refptr<base::SingleThreadTaskRunner>& monitor_task_runner) { 78 scoped_refptr<base::SingleThreadTaskRunner> monitor_task_runner) {
tommi (sloooow) - chröme 2016/03/19 02:32:41 does the caller use std::move?
alokp 2016/03/22 21:47:06 Done.
80 CHECK(!monitor_task_runner_); 79 CHECK(!monitor_task_runner_);
81 monitor_task_runner_ = monitor_task_runner; 80 monitor_task_runner_ = monitor_task_runner;
tommi (sloooow) - chröme 2016/03/19 02:32:41 if the above is intended, then you should use std:
alokp 2016/03/22 21:47:06 Done.
82 base::PowerMonitor::Get()->AddObserver(this); 81 base::PowerMonitor::Get()->AddObserver(this);
83 failed_pings_ = 0; 82 failed_pings_ = 0;
84 io_task_running_ = audio_task_running_ = true; 83 io_task_running_ = audio_task_running_ = true;
85 UpdateLastAudioThreadTimeTick(); 84 UpdateLastAudioThreadTimeTick();
86 RecordAudioThreadStatus(); 85 RecordAudioThreadStatus();
87 } 86 }
88 87
89 // Disable hang detection when the system goes into the suspend state. 88 // Disable hang detection when the system goes into the suspend state.
90 void OnSuspend() override { 89 void OnSuspend() override {
91 base::AutoLock lock(hang_lock_); 90 base::AutoLock lock(hang_lock_);
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 } 175 }
177 176
178 // Don't hold the lock while posting the next task. 177 // Don't hold the lock while posting the next task.
179 g_last_created->GetTaskRunner()->PostDelayedTask( 178 g_last_created->GetTaskRunner()->PostDelayedTask(
180 FROM_HERE, 179 FROM_HERE,
181 base::Bind(&AudioManagerHelper::UpdateLastAudioThreadTimeTick, 180 base::Bind(&AudioManagerHelper::UpdateLastAudioThreadTimeTick,
182 base::Unretained(this)), 181 base::Unretained(this)),
183 max_hung_task_time_ / 5); 182 max_hung_task_time_ / 5);
184 } 183 }
185 184
186 AudioLogFactory* fake_log_factory() { return &fake_log_factory_; }
187
188 #if defined(OS_WIN) 185 #if defined(OS_WIN)
189 // This should be called before creating an AudioManager in tests to ensure 186 // This should be called before creating an AudioManager in tests to ensure
190 // that the creating thread is COM initialized. 187 // that the creating thread is COM initialized.
191 void InitializeCOMForTesting() { 188 void InitializeCOMForTesting() {
192 com_initializer_for_testing_.reset(new base::win::ScopedCOMInitializer()); 189 com_initializer_for_testing_.reset(new base::win::ScopedCOMInitializer());
193 } 190 }
194 #endif 191 #endif
195 192
196 #if defined(OS_LINUX) 193 #if defined(OS_LINUX)
197 void set_app_name(const std::string& app_name) { 194 void set_app_name(const std::string& app_name) {
(...skipping 19 matching lines...) Expand all
217 base::debug::ScopedCrashKey crash_key( 214 base::debug::ScopedCrashKey crash_key(
218 "hung-audio-thread-details", 215 "hung-audio-thread-details",
219 base::StringPrintf("%s:%s", driver_name.c_str(), 216 base::StringPrintf("%s:%s", driver_name.c_str(),
220 driver_version.c_str())); 217 driver_version.c_str()));
221 218
222 // Please forward crash reports to http://crbug.com/422522 219 // Please forward crash reports to http://crbug.com/422522
223 base::debug::DumpWithoutCrashing(); 220 base::debug::DumpWithoutCrashing();
224 #endif 221 #endif
225 } 222 }
226 223
227 FakeAudioLogFactory fake_log_factory_;
228
229 const base::TimeDelta max_hung_task_time_ = base::TimeDelta::FromMinutes(1); 224 const base::TimeDelta max_hung_task_time_ = base::TimeDelta::FromMinutes(1);
230 scoped_refptr<base::SingleThreadTaskRunner> monitor_task_runner_; 225 scoped_refptr<base::SingleThreadTaskRunner> monitor_task_runner_;
231 226
232 base::Lock hang_lock_; 227 base::Lock hang_lock_;
233 bool hang_detection_enabled_ = true; 228 bool hang_detection_enabled_ = true;
234 base::TimeTicks last_audio_thread_timer_tick_; 229 base::TimeTicks last_audio_thread_timer_tick_;
235 uint32_t failed_pings_ = 0; 230 uint32_t failed_pings_ = 0;
236 bool io_task_running_ = false; 231 bool io_task_running_ = false;
237 bool audio_task_running_ = false; 232 bool audio_task_running_ = false;
238 ThreadStatus audio_thread_status_ = THREAD_NONE; 233 ThreadStatus audio_thread_status_ = THREAD_NONE;
239 bool enable_crash_key_logging_ = false; 234 bool enable_crash_key_logging_ = false;
240 uint32_t successful_pings_ = 0; 235 uint32_t successful_pings_ = 0;
241 236
242 #if defined(OS_WIN) 237 #if defined(OS_WIN)
243 scoped_ptr<base::win::ScopedCOMInitializer> com_initializer_for_testing_; 238 scoped_ptr<base::win::ScopedCOMInitializer> com_initializer_for_testing_;
244 #endif 239 #endif
245 240
246 #if defined(OS_LINUX) 241 #if defined(OS_LINUX)
247 std::string app_name_; 242 std::string app_name_;
248 #endif 243 #endif
249 244
250 DISALLOW_COPY_AND_ASSIGN(AudioManagerHelper); 245 DISALLOW_COPY_AND_ASSIGN(AudioManagerHelper);
251 }; 246 };
252 247
253 base::LazyInstance<AudioManagerHelper>::Leaky g_helper = 248 base::LazyInstance<AudioManagerHelper>::Leaky g_helper =
254 LAZY_INSTANCE_INITIALIZER; 249 LAZY_INSTANCE_INITIALIZER;
255 250
256 } // namespace 251 } // namespace
257 252
253 void AudioManagerDeleter::operator()(const AudioManager* instance) const {
254 CHECK(instance);
255 CHECK_EQ(g_last_created, instance);
256
257 // We reset g_last_created here instead of in the destructor of AudioManager
258 // to allow construction of another AudioManager while the current instance
259 // is being destroyed. This overlap does not happen in production - only in
260 // unittests.
261 g_last_created = nullptr;
262
263 // AudioManager must be destroyed on the audio thread.
264 scoped_refptr<base::SingleThreadTaskRunner> task_runner =
tommi (sloooow) - chröme 2016/03/19 02:32:41 won't we always be on the main thread when we get
alokp 2016/03/22 21:47:06 Are you suggesting always posting the task to dele
265 instance->GetTaskRunner();
266 if (task_runner->BelongsToCurrentThread()) {
267 delete instance;
268 } else {
269 task_runner->DeleteSoon(FROM_HERE, instance);
270 }
271 }
272
258 // Forward declaration of the platform specific AudioManager factory function. 273 // Forward declaration of the platform specific AudioManager factory function.
259 AudioManager* CreateAudioManager(AudioLogFactory* audio_log_factory); 274 ScopedAudioManagerPtr CreateAudioManager(
275 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
276 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner,
277 AudioLogFactory* audio_log_factory);
260 278
261 AudioManager::AudioManager() {} 279 AudioManager::AudioManager(
280 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
281 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner)
282 : task_runner_(task_runner), worker_task_runner_(worker_task_runner) {
283 DCHECK(task_runner_);
284 DCHECK(worker_task_runner_);
285 g_last_created = this;
286 }
262 287
263 AudioManager::~AudioManager() { 288 AudioManager::~AudioManager() {
264 CHECK(!g_last_created || g_last_created == this); 289 DCHECK(task_runner_->BelongsToCurrentThread());
265 g_last_created = nullptr;
266 } 290 }
267 291
268 // static 292 // static
269 void AudioManager::SetFactory(AudioManagerFactory* factory) { 293 void AudioManager::SetFactory(AudioManagerFactory* factory) {
270 CHECK(factory); 294 CHECK(factory);
271 CHECK(!g_last_created); 295 CHECK(!g_last_created);
272 CHECK(!g_audio_manager_factory); 296 CHECK(!g_audio_manager_factory);
273 g_audio_manager_factory = factory; 297 g_audio_manager_factory = factory;
274 } 298 }
275 299
276 // static 300 // static
277 void AudioManager::ResetFactoryForTesting() { 301 void AudioManager::ResetFactoryForTesting() {
278 if (g_audio_manager_factory) { 302 if (g_audio_manager_factory) {
279 delete g_audio_manager_factory; 303 delete g_audio_manager_factory;
280 g_audio_manager_factory = nullptr; 304 g_audio_manager_factory = nullptr;
281 } 305 }
282 } 306 }
283 307
284 // static 308 // static
285 AudioManager* AudioManager::Create(AudioLogFactory* audio_log_factory) { 309 ScopedAudioManagerPtr AudioManager::Create(
286 CHECK(!g_last_created); 310 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
287 if (g_audio_manager_factory) 311 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner,
288 g_last_created = g_audio_manager_factory->CreateInstance(audio_log_factory); 312 scoped_refptr<base::SingleThreadTaskRunner> monitor_task_runner,
289 else 313 AudioLogFactory* audio_log_factory) {
290 g_last_created = CreateAudioManager(audio_log_factory); 314 ScopedAudioManagerPtr manager;
315 if (g_audio_manager_factory) {
316 manager = g_audio_manager_factory->CreateInstance(
317 task_runner, worker_task_runner, audio_log_factory);
318 } else {
319 manager =
320 CreateAudioManager(task_runner, worker_task_runner, audio_log_factory);
321 }
291 322
292 return g_last_created; 323 if (monitor_task_runner)
293 } 324 g_helper.Pointer()->StartHangTimer(monitor_task_runner);
tommi (sloooow) - chröme 2016/03/19 02:32:41 std::move() (or see other comment)
alokp 2016/03/22 21:47:06 Done.
294
295 // static
296 AudioManager* AudioManager::CreateWithHangTimer(
297 AudioLogFactory* audio_log_factory,
298 const scoped_refptr<base::SingleThreadTaskRunner>& monitor_task_runner) {
299 AudioManager* manager = Create(audio_log_factory);
300
301 // On OSX the audio thread is the UI thread, for which a hang monitor is not
302 // necessary or recommended.
303 #if !defined(OS_MACOSX)
304 g_helper.Pointer()->StartHangTimer(monitor_task_runner);
305 #endif
306 325
307 return manager; 326 return manager;
308 } 327 }
309 328
310 // static 329 // static
311 AudioManager* AudioManager::CreateForTesting() {
312 #if defined(OS_WIN)
313 g_helper.Pointer()->InitializeCOMForTesting();
314 #endif
315 return Create(g_helper.Pointer()->fake_log_factory());
316 }
317
318 // static
319 void AudioManager::EnableCrashKeyLoggingForAudioThreadHangs() { 330 void AudioManager::EnableCrashKeyLoggingForAudioThreadHangs() {
320 CHECK(!g_last_created); 331 CHECK(!g_last_created);
321 g_helper.Pointer()->enable_crash_key_logging(); 332 g_helper.Pointer()->enable_crash_key_logging();
322 } 333 }
323 334
324 #if defined(OS_LINUX) 335 #if defined(OS_LINUX)
325 // static 336 // static
326 void AudioManager::SetGlobalAppName(const std::string& app_name) { 337 void AudioManager::SetGlobalAppName(const std::string& app_name) {
327 g_helper.Pointer()->set_app_name(app_name); 338 g_helper.Pointer()->set_app_name(app_name);
328 } 339 }
(...skipping 23 matching lines...) Expand all
352 std::string AudioManager::GetCommunicationsDeviceName() { 363 std::string AudioManager::GetCommunicationsDeviceName() {
353 #if defined(OS_WIN) 364 #if defined(OS_WIN)
354 return GetLocalizedStringUTF8(COMMUNICATIONS_AUDIO_DEVICE_NAME); 365 return GetLocalizedStringUTF8(COMMUNICATIONS_AUDIO_DEVICE_NAME);
355 #else 366 #else
356 NOTREACHED(); 367 NOTREACHED();
357 return ""; 368 return "";
358 #endif 369 #endif
359 } 370 }
360 371
361 } // namespace media 372 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698