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

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

Issue 1908423006: Allow content embedders to create AudioManager. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 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"
24 #include "media/audio/fake_audio_log_factory.h" 23 #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 {
35 34
36 // The singleton instance of AudioManager. This is set when Create() is called. 35 // The singleton instance of AudioManager. This is set when Create() is called.
37 AudioManager* g_last_created = nullptr; 36 AudioManager* g_last_created = nullptr;
38 37
39 // The singleton instance of AudioManagerFactory. This is only set if
40 // SetFactory() is called. If it is set when Create() is called, its
41 // CreateInstance() function is used to set |g_last_created|. Otherwise, the
42 // linked implementation of media::CreateAudioManager is used to set
43 // |g_last_created|.
44 AudioManagerFactory* g_audio_manager_factory = nullptr;
45
46 // Maximum number of failed pings to the audio thread allowed. A UMA will be 38 // Maximum number of failed pings to the audio thread allowed. A UMA will be
47 // recorded once this count is reached; if enabled, a non-crash dump will be 39 // recorded once this count is reached; if enabled, a non-crash dump will be
48 // captured as well. We require at least three failed pings before recording to 40 // captured as well. We require at least three failed pings before recording to
49 // ensure unobservable power events aren't mistakenly caught (e.g., the system 41 // ensure unobservable power events aren't mistakenly caught (e.g., the system
50 // suspends before a OnSuspend() event can be fired). 42 // suspends before a OnSuspend() event can be fired).
51 const int kMaxFailedPingsCount = 3; 43 const int kMaxFailedPingsCount = 3;
52 44
53 // Helper class for managing global AudioManager data and hang monitor. If the 45 // Helper class for managing global AudioManager data and hang monitor. If the
54 // audio thread is hung for > |kMaxFailedPingsCount| * |max_hung_task_time_|, we 46 // audio thread is hung for > |kMaxFailedPingsCount| * |max_hung_task_time_|, we
55 // want to record a UMA and optionally a non-crash dump to find offenders in the 47 // want to record a UMA and optionally a non-crash dump to find offenders in the
(...skipping 10 matching lines...) Expand all
66 THREAD_MAX = THREAD_RECOVERED 58 THREAD_MAX = THREAD_RECOVERED
67 }; 59 };
68 60
69 AudioManagerHelper() {} 61 AudioManagerHelper() {}
70 ~AudioManagerHelper() override {} 62 ~AudioManagerHelper() override {}
71 63
72 void StartHangTimer( 64 void StartHangTimer(
73 scoped_refptr<base::SingleThreadTaskRunner> monitor_task_runner) { 65 scoped_refptr<base::SingleThreadTaskRunner> monitor_task_runner) {
74 CHECK(!monitor_task_runner_); 66 CHECK(!monitor_task_runner_);
75 CHECK(!audio_task_runner_); 67 CHECK(!audio_task_runner_);
76 CHECK(g_last_created);
77 monitor_task_runner_ = std::move(monitor_task_runner); 68 monitor_task_runner_ = std::move(monitor_task_runner);
78 audio_task_runner_ = g_last_created->GetTaskRunner(); 69 audio_task_runner_ = AudioManager::Get()->GetTaskRunner();
79 base::PowerMonitor::Get()->AddObserver(this); 70 base::PowerMonitor::Get()->AddObserver(this);
80 71
81 io_task_running_ = audio_task_running_ = true; 72 io_task_running_ = audio_task_running_ = true;
82 audio_task_runner_->PostTask( 73 audio_task_runner_->PostTask(
83 FROM_HERE, 74 FROM_HERE,
84 base::Bind(&AudioManagerHelper::UpdateLastAudioThreadTimeTick, 75 base::Bind(&AudioManagerHelper::UpdateLastAudioThreadTimeTick,
85 base::Unretained(this))); 76 base::Unretained(this)));
86 monitor_task_runner_->PostTask( 77 monitor_task_runner_->PostTask(
87 FROM_HERE, base::Bind(&AudioManagerHelper::RecordAudioThreadStatus, 78 FROM_HERE, base::Bind(&AudioManagerHelper::RecordAudioThreadStatus,
88 base::Unretained(this))); 79 base::Unretained(this)));
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after
309 // We always override |g_last_created| irrespective of whether it is already 300 // We always override |g_last_created| irrespective of whether it is already
310 // set or not becuase it represents the last created instance. 301 // set or not becuase it represents the last created instance.
311 g_last_created = this; 302 g_last_created = this;
312 } 303 }
313 304
314 AudioManager::~AudioManager() { 305 AudioManager::~AudioManager() {
315 DCHECK(task_runner_->BelongsToCurrentThread()); 306 DCHECK(task_runner_->BelongsToCurrentThread());
316 } 307 }
317 308
318 // static 309 // static
319 void AudioManager::SetFactory(AudioManagerFactory* factory) {
320 CHECK(factory);
321 CHECK(!g_last_created);
322 CHECK(!g_audio_manager_factory);
323 g_audio_manager_factory = factory;
324 }
325
326 // static
327 void AudioManager::ResetFactoryForTesting() {
328 if (g_audio_manager_factory) {
329 delete g_audio_manager_factory;
330 g_audio_manager_factory = nullptr;
331 }
332 }
333
334 // static
335 ScopedAudioManagerPtr AudioManager::Create( 310 ScopedAudioManagerPtr AudioManager::Create(
336 scoped_refptr<base::SingleThreadTaskRunner> task_runner, 311 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
337 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner, 312 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner,
338 scoped_refptr<base::SingleThreadTaskRunner> monitor_task_runner,
339 AudioLogFactory* audio_log_factory) { 313 AudioLogFactory* audio_log_factory) {
340 DCHECK(task_runner); 314 DCHECK(task_runner);
341 DCHECK(worker_task_runner); 315 DCHECK(worker_task_runner);
342 ScopedAudioManagerPtr manager; 316 return CreateAudioManager(std::move(task_runner),
343 if (g_audio_manager_factory) { 317 std::move(worker_task_runner), audio_log_factory);
344 manager = g_audio_manager_factory->CreateInstance(
345 std::move(task_runner), std::move(worker_task_runner),
346 audio_log_factory);
347 } else {
348 manager =
349 CreateAudioManager(std::move(task_runner),
350 std::move(worker_task_runner), audio_log_factory);
351 }
352
353 if (monitor_task_runner)
354 g_helper.Pointer()->StartHangTimer(std::move(monitor_task_runner));
355
356 return manager;
357 } 318 }
358 319
359 // static 320 // static
360 ScopedAudioManagerPtr AudioManager::CreateForTesting( 321 ScopedAudioManagerPtr AudioManager::CreateForTesting(
361 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { 322 scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
362 #if defined(OS_WIN) 323 #if defined(OS_WIN)
363 g_helper.Pointer()->InitializeCOMForTesting(); 324 g_helper.Pointer()->InitializeCOMForTesting();
364 #endif 325 #endif
365 return Create(task_runner, task_runner, nullptr, 326 return Create(task_runner, task_runner,
366 g_helper.Pointer()->fake_log_factory()); 327 g_helper.Pointer()->fake_log_factory());
367 } 328 }
368 329
369 // static 330 // static
331 void AudioManager::StartHangMonitor(
332 scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
333 DCHECK(AudioManager::Get());
334 DCHECK(task_runner);
335 DCHECK_NE(task_runner, AudioManager::Get()->GetTaskRunner());
336
337 g_helper.Pointer()->StartHangTimer(std::move(task_runner));
338 }
339
340 // static
370 void AudioManager::EnableCrashKeyLoggingForAudioThreadHangs() { 341 void AudioManager::EnableCrashKeyLoggingForAudioThreadHangs() {
371 CHECK(!g_last_created); 342 CHECK(!g_last_created);
372 g_helper.Pointer()->enable_crash_key_logging(); 343 g_helper.Pointer()->enable_crash_key_logging();
373 } 344 }
374 345
375 #if defined(OS_LINUX) 346 #if defined(OS_LINUX)
376 // static 347 // static
377 void AudioManager::SetGlobalAppName(const std::string& app_name) { 348 void AudioManager::SetGlobalAppName(const std::string& app_name) {
378 g_helper.Pointer()->set_app_name(app_name); 349 g_helper.Pointer()->set_app_name(app_name);
379 } 350 }
(...skipping 23 matching lines...) Expand all
403 std::string AudioManager::GetCommunicationsDeviceName() { 374 std::string AudioManager::GetCommunicationsDeviceName() {
404 #if defined(OS_WIN) 375 #if defined(OS_WIN)
405 return GetLocalizedStringUTF8(COMMUNICATIONS_AUDIO_DEVICE_NAME); 376 return GetLocalizedStringUTF8(COMMUNICATIONS_AUDIO_DEVICE_NAME);
406 #else 377 #else
407 NOTREACHED(); 378 NOTREACHED();
408 return ""; 379 return "";
409 #endif 380 #endif
410 } 381 }
411 382
412 } // namespace media 383 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698