This source file includes following definitions.
- Initialize
- Play
- GetDuration
- Create
- Shutdown
- Get
- InitializeForTesting
#include "media/audio/sounds/sounds_manager.h"
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/memory/linked_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/single_thread_task_runner.h"
#include "media/audio/audio_manager.h"
#include "media/audio/sounds/audio_stream_handler.h"
#include "media/base/media_switches.h"
namespace media {
namespace {
SoundsManager* g_instance = NULL;
bool g_initialized_for_testing = false;
class SoundsManagerImpl : public SoundsManager {
public:
SoundsManagerImpl();
virtual ~SoundsManagerImpl();
virtual bool Initialize(SoundKey key,
const base::StringPiece& data) OVERRIDE;
virtual bool Play(SoundKey key) OVERRIDE;
virtual base::TimeDelta GetDuration(SoundKey key) OVERRIDE;
private:
base::hash_map<SoundKey, linked_ptr<AudioStreamHandler> > handlers_;
scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
DISALLOW_COPY_AND_ASSIGN(SoundsManagerImpl);
};
SoundsManagerImpl::SoundsManagerImpl()
: task_runner_(AudioManager::Get()->GetTaskRunner()) {
}
SoundsManagerImpl::~SoundsManagerImpl() { DCHECK(CalledOnValidThread()); }
bool SoundsManagerImpl::Initialize(SoundKey key,
const base::StringPiece& data) {
if (handlers_.find(key) != handlers_.end() && handlers_[key]->IsInitialized())
return true;
linked_ptr<AudioStreamHandler> handler(new AudioStreamHandler(data));
if (!handler->IsInitialized()) {
LOG(WARNING) << "Can't initialize AudioStreamHandler for key=" << key;
return false;
}
handlers_[key] = handler;
return true;
}
bool SoundsManagerImpl::Play(SoundKey key) {
DCHECK(CalledOnValidThread());
if (handlers_.find(key) == handlers_.end() ||
!handlers_[key]->IsInitialized()) {
return false;
}
return handlers_[key]->Play();
}
base::TimeDelta SoundsManagerImpl::GetDuration(SoundKey key) {
DCHECK(CalledOnValidThread());
if (handlers_.find(key) == handlers_.end() ||
!handlers_[key]->IsInitialized()) {
return base::TimeDelta();
}
const WavAudioHandler& wav_audio = handlers_[key]->wav_audio_handler();
return wav_audio.params().GetBufferDuration();
}
}
SoundsManager::SoundsManager() {}
SoundsManager::~SoundsManager() { DCHECK(CalledOnValidThread()); }
void SoundsManager::Create() {
CHECK(!g_instance || g_initialized_for_testing)
<< "SoundsManager::Create() is called twice";
if (g_initialized_for_testing)
return;
g_instance = new SoundsManagerImpl();
}
void SoundsManager::Shutdown() {
CHECK(g_instance) << "SoundsManager::Shutdown() is called "
<< "without previous call to Create()";
delete g_instance;
g_instance = NULL;
}
SoundsManager* SoundsManager::Get() {
CHECK(g_instance) << "SoundsManager::Get() is called before Create()";
return g_instance;
}
void SoundsManager::InitializeForTesting(SoundsManager* manager) {
CHECK(!g_instance) << "SoundsManager is already initialized.";
CHECK(manager);
g_instance = manager;
g_initialized_for_testing = true;
}
}