sounds_manager.cc revision f8ee788a64d60abd8f2d742a5fdedde054ecd910
1// Copyright 2013 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include "media/audio/sounds/sounds_manager.h" 6 7#include "base/compiler_specific.h" 8#include "base/logging.h" 9#include "base/memory/linked_ptr.h" 10#include "base/memory/ref_counted.h" 11#include "base/single_thread_task_runner.h" 12#include "media/audio/audio_manager.h" 13#include "media/audio/sounds/audio_stream_handler.h" 14 15namespace media { 16 17namespace { 18 19SoundsManager* g_instance = NULL; 20bool g_initialized_for_testing = false; 21 22// SoundsManagerImpl --------------------------------------------------- 23 24class SoundsManagerImpl : public SoundsManager { 25 public: 26 SoundsManagerImpl(); 27 virtual ~SoundsManagerImpl(); 28 29 // SoundsManager implementation: 30 virtual bool Initialize(SoundKey key, 31 const base::StringPiece& data) OVERRIDE; 32 virtual bool Play(SoundKey key) OVERRIDE; 33 virtual base::TimeDelta GetDuration(SoundKey key) OVERRIDE; 34 35 private: 36 base::hash_map<SoundKey, linked_ptr<AudioStreamHandler> > handlers_; 37 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; 38 39 DISALLOW_COPY_AND_ASSIGN(SoundsManagerImpl); 40}; 41 42SoundsManagerImpl::SoundsManagerImpl() 43 : task_runner_(AudioManager::Get()->GetTaskRunner()) { 44} 45 46SoundsManagerImpl::~SoundsManagerImpl() { DCHECK(CalledOnValidThread()); } 47 48bool SoundsManagerImpl::Initialize(SoundKey key, 49 const base::StringPiece& data) { 50 if (handlers_.find(key) != handlers_.end() && handlers_[key]->IsInitialized()) 51 return true; 52 linked_ptr<AudioStreamHandler> handler(new AudioStreamHandler(data)); 53 if (!handler->IsInitialized()) { 54 LOG(WARNING) << "Can't initialize AudioStreamHandler for key=" << key; 55 return false; 56 } 57 handlers_[key] = handler; 58 return true; 59} 60 61bool SoundsManagerImpl::Play(SoundKey key) { 62 DCHECK(CalledOnValidThread()); 63 if (handlers_.find(key) == handlers_.end() || 64 !handlers_[key]->IsInitialized()) { 65 return false; 66 } 67 return handlers_[key]->Play(); 68} 69 70base::TimeDelta SoundsManagerImpl::GetDuration(SoundKey key) { 71 DCHECK(CalledOnValidThread()); 72 if (handlers_.find(key) == handlers_.end() || 73 !handlers_[key]->IsInitialized()) { 74 return base::TimeDelta(); 75 } 76 const WavAudioHandler& wav_audio = handlers_[key]->wav_audio_handler(); 77 return wav_audio.params().GetBufferDuration(); 78} 79 80} // namespace 81 82SoundsManager::SoundsManager() {} 83 84SoundsManager::~SoundsManager() { DCHECK(CalledOnValidThread()); } 85 86// static 87void SoundsManager::Create() { 88 CHECK(!g_instance || g_initialized_for_testing) 89 << "SoundsManager::Create() is called twice"; 90 if (g_initialized_for_testing) 91 return; 92 g_instance = new SoundsManagerImpl(); 93} 94 95// static 96void SoundsManager::Shutdown() { 97 CHECK(g_instance) << "SoundsManager::Shutdown() is called " 98 << "without previous call to Create()"; 99 delete g_instance; 100 g_instance = NULL; 101} 102 103// static 104SoundsManager* SoundsManager::Get() { 105 CHECK(g_instance) << "SoundsManager::Get() is called before Create()"; 106 return g_instance; 107} 108 109// static 110void SoundsManager::InitializeForTesting(SoundsManager* manager) { 111 CHECK(!g_instance) << "SoundsManager is already initialized."; 112 CHECK(manager); 113 g_instance = manager; 114 g_initialized_for_testing = true; 115} 116 117} // namespace media 118