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