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

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

Issue 1806313003: Pass task runners to AudioManager constructor. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: removed AudioManagerBase::Shutdown 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_base.h" 5 #include "media/audio/audio_manager_base.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/macros.h" 10 #include "base/macros.h"
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 } 74 }
75 75
76 private: 76 private:
77 const DispatcherParams* dispatcher_; 77 const DispatcherParams* dispatcher_;
78 }; 78 };
79 79
80 static bool IsDefaultDeviceId(const std::string& device_id) { 80 static bool IsDefaultDeviceId(const std::string& device_id) {
81 return device_id.empty() || device_id == AudioManagerBase::kDefaultDeviceId; 81 return device_id.empty() || device_id == AudioManagerBase::kDefaultDeviceId;
82 } 82 }
83 83
84 AudioManagerBase::AudioManagerBase(AudioLogFactory* audio_log_factory) 84 AudioManagerBase::AudioManagerBase(
85 : max_num_output_streams_(kDefaultMaxOutputStreams), 85 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
86 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner,
87 AudioLogFactory* audio_log_factory)
88 : AudioManager(std::move(task_runner), std::move(worker_task_runner)),
89 max_num_output_streams_(kDefaultMaxOutputStreams),
86 max_num_input_streams_(kDefaultMaxInputStreams), 90 max_num_input_streams_(kDefaultMaxInputStreams),
87 num_output_streams_(0), 91 num_output_streams_(0),
88 num_input_streams_(0), 92 num_input_streams_(0),
89 // TODO(dalecurtis): Switch this to an base::ObserverListThreadSafe, so we 93 // TODO(dalecurtis): Switch this to an base::ObserverListThreadSafe, so we
90 // don't 94 // don't
91 // block the UI thread when swapping devices. 95 // block the UI thread when swapping devices.
92 output_listeners_( 96 output_listeners_(
93 base::ObserverList<AudioDeviceListener>::NOTIFY_EXISTING_ONLY), 97 base::ObserverList<AudioDeviceListener>::NOTIFY_EXISTING_ONLY),
94 audio_log_factory_(audio_log_factory) { 98 audio_log_factory_(audio_log_factory) {}
95 }
96 99
97 AudioManagerBase::~AudioManagerBase() { 100 AudioManagerBase::~AudioManagerBase() {
98 // The platform specific AudioManager implementation must have already 101 DCHECK(GetTaskRunner()->BelongsToCurrentThread());
99 // stopped the audio thread. Otherwise, we may destroy audio streams before 102 // Close all output streams.
100 // stopping the thread, resulting an unexpected behavior. 103 while (!output_dispatchers_.empty()) {
101 // This way we make sure activities of the audio streams are all stopped 104 output_dispatchers_.back()->dispatcher->Shutdown();
102 // before we destroy them. 105 output_dispatchers_.pop_back();
103 CHECK(!audio_thread_); 106 }
104 // All the output streams should have been deleted. 107 // All the output streams should have been deleted.
105 DCHECK_EQ(0, num_output_streams_); 108 DCHECK_EQ(0, num_output_streams_);
106 // All the input streams should have been deleted. 109 // All the input streams should have been deleted.
107 DCHECK_EQ(0, num_input_streams_); 110 DCHECK_EQ(0, num_input_streams_);
108 } 111 }
109 112
110 base::string16 AudioManagerBase::GetAudioInputDeviceModel() { 113 base::string16 AudioManagerBase::GetAudioInputDeviceModel() {
111 return base::string16(); 114 return base::string16();
112 } 115 }
113 116
114 scoped_refptr<base::SingleThreadTaskRunner> AudioManagerBase::GetTaskRunner() {
115 if (!audio_thread_) {
116 audio_thread_.reset(new base::Thread("AudioThread"));
117 #if defined(OS_WIN)
118 audio_thread_->init_com_with_mta(true);
119 #endif
120 CHECK(audio_thread_->Start());
121 }
122 return audio_thread_->task_runner();
123 }
124
125 scoped_refptr<base::SingleThreadTaskRunner>
126 AudioManagerBase::GetWorkerTaskRunner() {
127 return GetTaskRunner();
128 }
129
130 AudioOutputStream* AudioManagerBase::MakeAudioOutputStream( 117 AudioOutputStream* AudioManagerBase::MakeAudioOutputStream(
131 const AudioParameters& params, 118 const AudioParameters& params,
132 const std::string& device_id) { 119 const std::string& device_id) {
133 // TODO(miu): Fix ~50 call points across several unit test modules to call 120 // TODO(miu): Fix ~50 call points across several unit test modules to call
134 // this method on the audio thread, then uncomment the following: 121 // this method on the audio thread, then uncomment the following:
135 // DCHECK(GetTaskRunner()->BelongsToCurrentThread()); 122 // DCHECK(GetTaskRunner()->BelongsToCurrentThread());
136 123
137 if (!params.IsValid()) { 124 if (!params.IsValid()) {
138 DLOG(ERROR) << "Audio parameters are invalid"; 125 DLOG(ERROR) << "Audio parameters are invalid";
139 return NULL; 126 return NULL;
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 delete stream; 300 delete stream;
314 } 301 }
315 302
316 void AudioManagerBase::ReleaseInputStream(AudioInputStream* stream) { 303 void AudioManagerBase::ReleaseInputStream(AudioInputStream* stream) {
317 DCHECK(stream); 304 DCHECK(stream);
318 // TODO(xians) : Have a clearer destruction path for the AudioInputStream. 305 // TODO(xians) : Have a clearer destruction path for the AudioInputStream.
319 --num_input_streams_; 306 --num_input_streams_;
320 delete stream; 307 delete stream;
321 } 308 }
322 309
323 void AudioManagerBase::Shutdown() {
324 // Only true when we're sharing the UI message loop with the browser. The UI
325 // loop is no longer running at this time and browser destruction is imminent.
326 auto task_runner = GetTaskRunner();
327 if (task_runner->BelongsToCurrentThread()) {
328 ShutdownOnAudioThread();
329 } else {
330 task_runner->PostTask(FROM_HERE,
331 base::Bind(&AudioManagerBase::ShutdownOnAudioThread,
332 base::Unretained(this)));
333 }
334
335 // Stop() will wait for any posted messages to be processed first.
336 if (audio_thread_) {
337 audio_thread_->Stop();
338 audio_thread_.reset();
339 }
340 }
341
342 void AudioManagerBase::ShutdownOnAudioThread() {
343 DCHECK(GetTaskRunner()->BelongsToCurrentThread());
344 while (!output_dispatchers_.empty()) {
345 output_dispatchers_.back()->dispatcher->Shutdown();
346 output_dispatchers_.pop_back();
347 }
348 }
349
350 void AudioManagerBase::AddOutputDeviceChangeListener( 310 void AudioManagerBase::AddOutputDeviceChangeListener(
351 AudioDeviceListener* listener) { 311 AudioDeviceListener* listener) {
352 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); 312 DCHECK(GetTaskRunner()->BelongsToCurrentThread());
353 output_listeners_.AddObserver(listener); 313 output_listeners_.AddObserver(listener);
354 } 314 }
355 315
356 void AudioManagerBase::RemoveOutputDeviceChangeListener( 316 void AudioManagerBase::RemoveOutputDeviceChangeListener(
357 AudioDeviceListener* listener) { 317 AudioDeviceListener* listener) {
358 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); 318 DCHECK(GetTaskRunner()->BelongsToCurrentThread());
359 output_listeners_.RemoveObserver(listener); 319 output_listeners_.RemoveObserver(listener);
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
401 361
402 return 0; 362 return 0;
403 } 363 }
404 364
405 scoped_ptr<AudioLog> AudioManagerBase::CreateAudioLog( 365 scoped_ptr<AudioLog> AudioManagerBase::CreateAudioLog(
406 AudioLogFactory::AudioComponent component) { 366 AudioLogFactory::AudioComponent component) {
407 return audio_log_factory_->CreateAudioLog(component); 367 return audio_log_factory_->CreateAudioLog(component);
408 } 368 }
409 369
410 } // namespace media 370 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698