Revert "Omit calls to set composing region when pasting image."
[chromium-blink-merge.git] / media / audio / sounds / sounds_manager.cc
blob17f0dd874368f89c23c2aab57749e9c771b8282f
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"
15 namespace media {
17 namespace {
19 SoundsManager* g_instance = NULL;
20 bool g_initialized_for_testing = false;
22 // SoundsManagerImpl ---------------------------------------------------
24 class SoundsManagerImpl : public SoundsManager {
25 public:
26 SoundsManagerImpl();
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;
35 private:
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())
54 return true;
55 linked_ptr<AudioStreamHandler> new_handler(new AudioStreamHandler(data));
56 if (!new_handler->IsInitialized()) {
57 LOG(WARNING) << "Can't initialize AudioStreamHandler for key=" << key;
58 return false;
60 handlers_[key] = new_handler;
61 return true;
64 bool SoundsManagerImpl::Play(SoundKey key) {
65 DCHECK(CalledOnValidThread());
66 linked_ptr<AudioStreamHandler> handler = GetHandler(key);
67 if (!handler.get())
68 return false;
69 if (!handler->IsInitialized())
70 return false;
71 return handler->Play();
74 bool SoundsManagerImpl::Stop(SoundKey key) {
75 DCHECK(CalledOnValidThread());
76 linked_ptr<AudioStreamHandler> handler = GetHandler(key);
77 if (!handler.get())
78 return false;
79 if (!handler->IsInitialized())
80 return false;
81 handler->Stop();
82 return true;
85 base::TimeDelta SoundsManagerImpl::GetDuration(SoundKey key) {
86 DCHECK(CalledOnValidThread());
87 linked_ptr<AudioStreamHandler> handler = GetHandler(key);
88 if (!handler.get())
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;
102 } // namespace
104 SoundsManager::SoundsManager() {}
106 SoundsManager::~SoundsManager() { DCHECK(CalledOnValidThread()); }
108 // static
109 void SoundsManager::Create() {
110 CHECK(!g_instance || g_initialized_for_testing)
111 << "SoundsManager::Create() is called twice";
112 if (g_initialized_for_testing)
113 return;
114 g_instance = new SoundsManagerImpl();
117 // static
118 void SoundsManager::Shutdown() {
119 CHECK(g_instance) << "SoundsManager::Shutdown() is called "
120 << "without previous call to Create()";
121 delete g_instance;
122 g_instance = NULL;
125 // static
126 SoundsManager* SoundsManager::Get() {
127 CHECK(g_instance) << "SoundsManager::Get() is called before Create()";
128 return g_instance;
131 // static
132 void SoundsManager::InitializeForTesting(SoundsManager* manager) {
133 CHECK(!g_instance) << "SoundsManager is already initialized.";
134 CHECK(manager);
135 g_instance = manager;
136 g_initialized_for_testing = true;
139 } // namespace media