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

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: destroy AudioManagerPulse 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"
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after
251 }; 251 };
252 252
253 base::LazyInstance<AudioManagerHelper>::Leaky g_helper = 253 base::LazyInstance<AudioManagerHelper>::Leaky g_helper =
254 LAZY_INSTANCE_INITIALIZER; 254 LAZY_INSTANCE_INITIALIZER;
255 255
256 } // namespace 256 } // namespace
257 257
258 // Forward declaration of the platform specific AudioManager factory function. 258 // Forward declaration of the platform specific AudioManager factory function.
259 AudioManager* CreateAudioManager(AudioLogFactory* audio_log_factory); 259 AudioManager* CreateAudioManager(AudioLogFactory* audio_log_factory);
260 260
261 AudioManager::AudioManager() {} 261 AudioManager::AudioManager(
262 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
263 const scoped_refptr<base::SingleThreadTaskRunner>& worker_task_runner)
264 : task_runner_(task_runner), worker_task_runner_(worker_task_runner) {
265 g_last_created = this;
266 }
262 267
263 AudioManager::~AudioManager() { 268 AudioManager::~AudioManager() {
264 CHECK(!g_last_created || g_last_created == this); 269 DCHECK(task_runner_->BelongsToCurrentThread());
265 g_last_created = nullptr;
266 } 270 }
267 271
268 // static 272 // static
269 void AudioManager::SetFactory(AudioManagerFactory* factory) { 273 void AudioManager::SetFactory(AudioManagerFactory* factory) {
270 CHECK(factory); 274 CHECK(factory);
271 CHECK(!g_last_created); 275 CHECK(!g_last_created);
272 CHECK(!g_audio_manager_factory); 276 CHECK(!g_audio_manager_factory);
273 g_audio_manager_factory = factory; 277 g_audio_manager_factory = factory;
274 } 278 }
275 279
276 // static 280 // static
277 void AudioManager::ResetFactoryForTesting() { 281 void AudioManager::ResetFactoryForTesting() {
278 if (g_audio_manager_factory) { 282 if (g_audio_manager_factory) {
279 delete g_audio_manager_factory; 283 delete g_audio_manager_factory;
280 g_audio_manager_factory = nullptr; 284 g_audio_manager_factory = nullptr;
281 } 285 }
282 } 286 }
283 287
284 // static 288 // static
285 AudioManager* AudioManager::Create(AudioLogFactory* audio_log_factory) { 289 AudioManager* AudioManager::Create(AudioLogFactory* audio_log_factory) {
286 CHECK(!g_last_created);
287 if (g_audio_manager_factory) 290 if (g_audio_manager_factory)
288 g_last_created = g_audio_manager_factory->CreateInstance(audio_log_factory); 291 return g_audio_manager_factory->CreateInstance(audio_log_factory);
289 else 292 else
290 g_last_created = CreateAudioManager(audio_log_factory); 293 return CreateAudioManager(audio_log_factory);
291
292 return g_last_created;
293 } 294 }
294 295
295 // static 296 // static
296 AudioManager* AudioManager::CreateWithHangTimer( 297 AudioManager* AudioManager::CreateWithHangTimer(
297 AudioLogFactory* audio_log_factory, 298 AudioLogFactory* audio_log_factory,
298 const scoped_refptr<base::SingleThreadTaskRunner>& monitor_task_runner) { 299 const scoped_refptr<base::SingleThreadTaskRunner>& monitor_task_runner) {
299 AudioManager* manager = Create(audio_log_factory); 300 AudioManager* manager = Create(audio_log_factory);
300 301
301 // On OSX the audio thread is the UI thread, for which a hang monitor is not 302 // On OSX the audio thread is the UI thread, for which a hang monitor is not
302 // necessary or recommended. 303 // necessary or recommended.
303 #if !defined(OS_MACOSX) 304 #if !defined(OS_MACOSX)
304 g_helper.Pointer()->StartHangTimer(monitor_task_runner); 305 g_helper.Pointer()->StartHangTimer(monitor_task_runner);
305 #endif 306 #endif
306 307
307 return manager; 308 return manager;
308 } 309 }
309 310
310 // static 311 // static
311 AudioManager* AudioManager::CreateForTesting() { 312 AudioManager* AudioManager::CreateForTesting() {
312 #if defined(OS_WIN) 313 #if defined(OS_WIN)
313 g_helper.Pointer()->InitializeCOMForTesting(); 314 g_helper.Pointer()->InitializeCOMForTesting();
314 #endif 315 #endif
315 return Create(g_helper.Pointer()->fake_log_factory()); 316 return Create(g_helper.Pointer()->fake_log_factory());
316 } 317 }
317 318
318 // static 319 // static
320 void AudioManager::Destroy(AudioManager* instance) {
321 CHECK(instance);
322 CHECK_EQ(g_last_created, instance);
323
324 // AudioManager must be destroyed on the audio thread.
325 scoped_refptr<base::SingleThreadTaskRunner> task_runner =
326 instance->GetTaskRunner();
327 if (task_runner->BelongsToCurrentThread()) {
328 delete instance;
329 } else {
330 task_runner->DeleteSoon(FROM_HERE, instance);
331 }
332
333 // We reset g_last_created here instead of in the destructor of AudioManager
334 // to allow construction of another AudioManager while the current instance
335 // is being destroyed. This overlap does not happen in production - only in
336 // unittests.
337 g_last_created = nullptr;
338 }
339
340 // static
319 void AudioManager::EnableCrashKeyLoggingForAudioThreadHangs() { 341 void AudioManager::EnableCrashKeyLoggingForAudioThreadHangs() {
320 CHECK(!g_last_created); 342 CHECK(!g_last_created);
321 g_helper.Pointer()->enable_crash_key_logging(); 343 g_helper.Pointer()->enable_crash_key_logging();
322 } 344 }
323 345
324 #if defined(OS_LINUX) 346 #if defined(OS_LINUX)
325 // static 347 // static
326 void AudioManager::SetGlobalAppName(const std::string& app_name) { 348 void AudioManager::SetGlobalAppName(const std::string& app_name) {
327 g_helper.Pointer()->set_app_name(app_name); 349 g_helper.Pointer()->set_app_name(app_name);
328 } 350 }
(...skipping 23 matching lines...) Expand all
352 std::string AudioManager::GetCommunicationsDeviceName() { 374 std::string AudioManager::GetCommunicationsDeviceName() {
353 #if defined(OS_WIN) 375 #if defined(OS_WIN)
354 return GetLocalizedStringUTF8(COMMUNICATIONS_AUDIO_DEVICE_NAME); 376 return GetLocalizedStringUTF8(COMMUNICATIONS_AUDIO_DEVICE_NAME);
355 #else 377 #else
356 NOTREACHED(); 378 NOTREACHED();
357 return ""; 379 return "";
358 #endif 380 #endif
359 } 381 }
360 382
361 } // namespace media 383 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698