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.
5 #include "media/audio/sounds/sounds_manager.h"
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"
19 SoundsManager
* g_instance
= NULL
;
20 bool g_initialized_for_testing
= false;
22 // SoundsManagerImpl ---------------------------------------------------
24 class SoundsManagerImpl
: public SoundsManager
{
27 ~SoundsManagerImpl() override
;
29 // SoundsManager implementation:
30 bool Initialize(SoundKey key
, const base::StringPiece
& data
) override
;
31 bool Play(SoundKey key
) override
;
32 base::TimeDelta
GetDuration(SoundKey key
) override
;
35 base::hash_map
<SoundKey
, linked_ptr
<AudioStreamHandler
> > handlers_
;
36 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
38 DISALLOW_COPY_AND_ASSIGN(SoundsManagerImpl
);
41 SoundsManagerImpl::SoundsManagerImpl()
42 : task_runner_(AudioManager::Get()->GetTaskRunner()) {
45 SoundsManagerImpl::~SoundsManagerImpl() { DCHECK(CalledOnValidThread()); }
47 bool SoundsManagerImpl::Initialize(SoundKey key
,
48 const base::StringPiece
& data
) {
49 if (handlers_
.find(key
) != handlers_
.end() && handlers_
[key
]->IsInitialized())
51 linked_ptr
<AudioStreamHandler
> handler(new AudioStreamHandler(data
));
52 if (!handler
->IsInitialized()) {
53 LOG(WARNING
) << "Can't initialize AudioStreamHandler for key=" << key
;
56 handlers_
[key
] = handler
;
60 bool SoundsManagerImpl::Play(SoundKey key
) {
61 DCHECK(CalledOnValidThread());
62 if (handlers_
.find(key
) == handlers_
.end() ||
63 !handlers_
[key
]->IsInitialized()) {
66 return handlers_
[key
]->Play();
69 base::TimeDelta
SoundsManagerImpl::GetDuration(SoundKey key
) {
70 DCHECK(CalledOnValidThread());
71 if (handlers_
.find(key
) == handlers_
.end() ||
72 !handlers_
[key
]->IsInitialized()) {
73 return base::TimeDelta();
75 const WavAudioHandler
& wav_audio
= handlers_
[key
]->wav_audio_handler();
76 return wav_audio
.GetDuration();
81 SoundsManager::SoundsManager() {}
83 SoundsManager::~SoundsManager() { DCHECK(CalledOnValidThread()); }
86 void SoundsManager::Create() {
87 CHECK(!g_instance
|| g_initialized_for_testing
)
88 << "SoundsManager::Create() is called twice";
89 if (g_initialized_for_testing
)
91 g_instance
= new SoundsManagerImpl();
95 void SoundsManager::Shutdown() {
96 CHECK(g_instance
) << "SoundsManager::Shutdown() is called "
97 << "without previous call to Create()";
103 SoundsManager
* SoundsManager::Get() {
104 CHECK(g_instance
) << "SoundsManager::Get() is called before Create()";
109 void SoundsManager::InitializeForTesting(SoundsManager
* manager
) {
110 CHECK(!g_instance
) << "SoundsManager is already initialized.";
112 g_instance
= manager
;
113 g_initialized_for_testing
= true;