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

Unified Diff: media/audio/mac/audio_manager_mac.cc

Issue 1780333007: AudioManagerBase: Create and run the audio thread lazily. (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « media/audio/mac/audio_manager_mac.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: media/audio/mac/audio_manager_mac.cc
diff --git a/media/audio/mac/audio_manager_mac.cc b/media/audio/mac/audio_manager_mac.cc
index beedf928687fed22af294f1dbd6d00575ba8bc19..e2268879c65457c19f0fc74377fcc4433d558923 100644
--- a/media/audio/mac/audio_manager_mac.cc
+++ b/media/audio/mac/audio_manager_mac.cc
@@ -370,11 +370,19 @@ AudioManagerMac::AudioManagerMac(AudioLogFactory* audio_log_factory)
current_output_device_(kAudioDeviceUnknown) {
SetMaxOutputStreamsAllowed(kMaxOutputStreams);
+ // CoreAudio calls must occur on the main thread of the process, which in our
+ // case is sadly the browser UI thread. Failure to execute calls on the right
+ // thread leads to crashes and odd behavior. See http://crbug.com/158170.
+ // TODO(dalecurtis): We should require the message loop to be passed in.
+ CHECK(base::MessageLoopForUI::IsCurrent());
+ task_runner_ = base::ThreadTaskRunnerHandle::Get();
+
// Task must be posted last to avoid races from handing out "this" to the
// audio thread. Always PostTask even if we're on the right thread since
// AudioManager creation is on the startup path and this may be slow.
- GetTaskRunner()->PostTask(FROM_HERE, base::Bind(
- &AudioManagerMac::InitializeOnAudioThread, base::Unretained(this)));
+ task_runner_->PostTask(FROM_HERE,
+ base::Bind(&AudioManagerMac::InitializeOnAudioThread,
+ base::Unretained(this)));
}
AudioManagerMac::~AudioManagerMac() {
@@ -390,6 +398,19 @@ AudioManagerMac::~AudioManagerMac() {
Shutdown();
}
+scoped_refptr<base::SingleThreadTaskRunner> AudioManagerMac::GetTaskRunner() {
+ return task_runner_;
+}
+
+scoped_refptr<base::SingleThreadTaskRunner>
+AudioManagerMac::GetWorkerTaskRunner() {
+ if (!worker_thread_) {
+ worker_thread_.reset(new base::Thread("AudioWorkerThread"));
+ CHECK(worker_thread_->Start());
+ }
+ return worker_thread_->task_runner();
+}
+
bool AudioManagerMac::HasAudioOutputDevices() {
return HasAudioHardware(kAudioHardwarePropertyDefaultOutputDevice);
}
@@ -524,7 +545,7 @@ AudioParameters AudioManagerMac::GetInputStreamParameters(
std::string AudioManagerMac::GetAssociatedOutputDeviceID(
const std::string& input_device_id) {
- DCHECK(GetTaskRunner()->BelongsToCurrentThread());
+ DCHECK(task_runner_->BelongsToCurrentThread());
DaleCurtis 2016/03/14 21:46:09 Why this change? Seems better to use the GetTaskRu
alokp 2016/03/14 22:07:17 reverted in the new patch.
AudioDeviceID device = GetAudioDeviceIdByUId(true, input_device_id);
if (device == kAudioObjectUnknown)
return std::string();
@@ -639,7 +660,7 @@ AudioOutputStream* AudioManagerMac::MakeLowLatencyOutputStream(
}
std::string AudioManagerMac::GetDefaultOutputDeviceID() {
- DCHECK(GetTaskRunner()->BelongsToCurrentThread());
+ DCHECK(task_runner_->BelongsToCurrentThread());
AudioDeviceID device_id = kAudioObjectUnknown;
if (!GetDefaultOutputDevice(&device_id))
return std::string();
@@ -738,12 +759,12 @@ AudioParameters AudioManagerMac::GetPreferredOutputStreamParameters(
}
void AudioManagerMac::InitializeOnAudioThread() {
- DCHECK(GetTaskRunner()->BelongsToCurrentThread());
+ DCHECK(task_runner_->BelongsToCurrentThread());
power_observer_.reset(new AudioPowerObserver());
}
void AudioManagerMac::ShutdownOnAudioThread() {
- DCHECK(GetTaskRunner()->BelongsToCurrentThread());
+ DCHECK(task_runner_->BelongsToCurrentThread());
output_device_listener_.reset();
power_observer_.reset();
@@ -762,7 +783,7 @@ void AudioManagerMac::ShutdownOnAudioThread() {
}
void AudioManagerMac::HandleDeviceChanges() {
- DCHECK(GetTaskRunner()->BelongsToCurrentThread());
+ DCHECK(task_runner_->BelongsToCurrentThread());
const int new_sample_rate = HardwareSampleRate();
AudioDeviceID new_output_device;
GetDefaultOutputDevice(&new_output_device);
@@ -804,17 +825,17 @@ int AudioManagerMac::ChooseBufferSize(bool is_input, int sample_rate) {
}
bool AudioManagerMac::ShouldDeferStreamStart() const {
- DCHECK(GetTaskRunner()->BelongsToCurrentThread());
+ DCHECK(task_runner_->BelongsToCurrentThread());
return power_observer_->ShouldDeferStreamStart();
}
bool AudioManagerMac::IsOnBatteryPower() const {
- DCHECK(GetTaskRunner()->BelongsToCurrentThread());
+ DCHECK(task_runner_->BelongsToCurrentThread());
return power_observer_->IsOnBatteryPower();
}
size_t AudioManagerMac::GetNumberOfResumeNotifications() const {
- DCHECK(GetTaskRunner()->BelongsToCurrentThread());
+ DCHECK(task_runner_->BelongsToCurrentThread());
return power_observer_->num_resume_notifications();
}
@@ -824,7 +845,7 @@ bool AudioManagerMac::MaybeChangeBufferSize(AudioDeviceID device_id,
size_t desired_buffer_size,
bool* size_was_changed,
size_t* io_buffer_frame_size) {
- DCHECK(GetTaskRunner()->BelongsToCurrentThread());
+ DCHECK(task_runner_->BelongsToCurrentThread());
const bool is_input = (element == 1);
DVLOG(1) << "MaybeChangeBufferSize(id=0x" << std::hex << device_id
<< ", is_input=" << is_input << ", desired_buffer_size=" << std::dec
« no previous file with comments | « media/audio/mac/audio_manager_mac.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698