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 bool Stop(SoundKey key
) override
;
33 base::TimeDelta
GetDuration(SoundKey key
) override
;
36 linked_ptr
<AudioStreamHandler
> GetHandler(SoundKey key
);
38 base::hash_map
<SoundKey
, linked_ptr
<AudioStreamHandler
> > handlers_
;
39 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
41 DISALLOW_COPY_AND_ASSIGN(SoundsManagerImpl
);
44 SoundsManagerImpl::SoundsManagerImpl()
45 : task_runner_(AudioManager::Get()->GetTaskRunner()) {
48 SoundsManagerImpl::~SoundsManagerImpl() { DCHECK(CalledOnValidThread()); }
50 bool SoundsManagerImpl::Initialize(SoundKey key
,
51 const base::StringPiece
& data
) {
52 linked_ptr
<AudioStreamHandler
> current_handler
= GetHandler(key
);
53 if (current_handler
.get() && current_handler
->IsInitialized())
55 linked_ptr
<AudioStreamHandler
> new_handler(new AudioStreamHandler(data
));
56 if (!new_handler
->IsInitialized()) {
57 LOG(WARNING
) << "Can't initialize AudioStreamHandler for key=" << key
;
60 handlers_
[key
] = new_handler
;
64 bool SoundsManagerImpl::Play(SoundKey key
) {
65 DCHECK(CalledOnValidThread());
66 linked_ptr
<AudioStreamHandler
> handler
= GetHandler(key
);
69 if (!handler
->IsInitialized())
71 return handler
->Play();
74 bool SoundsManagerImpl::Stop(SoundKey key
) {
75 DCHECK(CalledOnValidThread());
76 linked_ptr
<AudioStreamHandler
> handler
= GetHandler(key
);
79 if (!handler
->IsInitialized())
85 base::TimeDelta
SoundsManagerImpl::GetDuration(SoundKey key
) {
86 DCHECK(CalledOnValidThread());
87 linked_ptr
<AudioStreamHandler
> handler
= GetHandler(key
);
89 return base::TimeDelta();
90 if (!handler
->IsInitialized())
91 return base::TimeDelta();
92 const WavAudioHandler
& wav_audio
= handler
->wav_audio_handler();
93 return wav_audio
.GetDuration();
96 linked_ptr
<AudioStreamHandler
> SoundsManagerImpl::GetHandler(SoundKey key
) {
97 auto key_handler_pair_iter
= handlers_
.find(key
);
98 return key_handler_pair_iter
== handlers_
.end() ?
99 linked_ptr
<AudioStreamHandler
>() : key_handler_pair_iter
->second
;
104 SoundsManager::SoundsManager() {}
106 SoundsManager::~SoundsManager() { DCHECK(CalledOnValidThread()); }
109 void SoundsManager::Create() {
110 CHECK(!g_instance
|| g_initialized_for_testing
)
111 << "SoundsManager::Create() is called twice";
112 if (g_initialized_for_testing
)
114 g_instance
= new SoundsManagerImpl();
118 void SoundsManager::Shutdown() {
119 CHECK(g_instance
) << "SoundsManager::Shutdown() is called "
120 << "without previous call to Create()";
126 SoundsManager
* SoundsManager::Get() {
127 CHECK(g_instance
) << "SoundsManager::Get() is called before Create()";
132 void SoundsManager::InitializeForTesting(SoundsManager
* manager
) {
133 CHECK(!g_instance
) << "SoundsManager is already initialized.";
135 g_instance
= manager
;
136 g_initialized_for_testing
= true;