summaryrefslogtreecommitdiff
path: root/chromium/media/audio/sounds
diff options
context:
space:
mode:
Diffstat (limited to 'chromium/media/audio/sounds')
-rw-r--r--chromium/media/audio/sounds/sounds_manager.cc47
-rw-r--r--chromium/media/audio/sounds/sounds_manager.h4
-rw-r--r--chromium/media/audio/sounds/sounds_manager_unittest.cc45
3 files changed, 84 insertions, 12 deletions
diff --git a/chromium/media/audio/sounds/sounds_manager.cc b/chromium/media/audio/sounds/sounds_manager.cc
index 72a7a3b15c1..17f0dd87436 100644
--- a/chromium/media/audio/sounds/sounds_manager.cc
+++ b/chromium/media/audio/sounds/sounds_manager.cc
@@ -29,9 +29,12 @@ class SoundsManagerImpl : public SoundsManager {
// SoundsManager implementation:
bool Initialize(SoundKey key, const base::StringPiece& data) override;
bool Play(SoundKey key) override;
+ bool Stop(SoundKey key) override;
base::TimeDelta GetDuration(SoundKey key) override;
private:
+ linked_ptr<AudioStreamHandler> GetHandler(SoundKey key);
+
base::hash_map<SoundKey, linked_ptr<AudioStreamHandler> > handlers_;
scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
@@ -46,36 +49,56 @@ SoundsManagerImpl::~SoundsManagerImpl() { DCHECK(CalledOnValidThread()); }
bool SoundsManagerImpl::Initialize(SoundKey key,
const base::StringPiece& data) {
- if (handlers_.find(key) != handlers_.end() && handlers_[key]->IsInitialized())
+ linked_ptr<AudioStreamHandler> current_handler = GetHandler(key);
+ if (current_handler.get() && current_handler->IsInitialized())
return true;
- linked_ptr<AudioStreamHandler> handler(new AudioStreamHandler(data));
- if (!handler->IsInitialized()) {
+ linked_ptr<AudioStreamHandler> new_handler(new AudioStreamHandler(data));
+ if (!new_handler->IsInitialized()) {
LOG(WARNING) << "Can't initialize AudioStreamHandler for key=" << key;
return false;
}
- handlers_[key] = handler;
+ handlers_[key] = new_handler;
return true;
}
bool SoundsManagerImpl::Play(SoundKey key) {
DCHECK(CalledOnValidThread());
- if (handlers_.find(key) == handlers_.end() ||
- !handlers_[key]->IsInitialized()) {
+ linked_ptr<AudioStreamHandler> handler = GetHandler(key);
+ if (!handler.get())
return false;
- }
- return handlers_[key]->Play();
+ if (!handler->IsInitialized())
+ return false;
+ return handler->Play();
+}
+
+bool SoundsManagerImpl::Stop(SoundKey key) {
+ DCHECK(CalledOnValidThread());
+ linked_ptr<AudioStreamHandler> handler = GetHandler(key);
+ if (!handler.get())
+ return false;
+ if (!handler->IsInitialized())
+ return false;
+ handler->Stop();
+ return true;
}
base::TimeDelta SoundsManagerImpl::GetDuration(SoundKey key) {
DCHECK(CalledOnValidThread());
- if (handlers_.find(key) == handlers_.end() ||
- !handlers_[key]->IsInitialized()) {
+ linked_ptr<AudioStreamHandler> handler = GetHandler(key);
+ if (!handler.get())
+ return base::TimeDelta();
+ if (!handler->IsInitialized())
return base::TimeDelta();
- }
- const WavAudioHandler& wav_audio = handlers_[key]->wav_audio_handler();
+ const WavAudioHandler& wav_audio = handler->wav_audio_handler();
return wav_audio.GetDuration();
}
+linked_ptr<AudioStreamHandler> SoundsManagerImpl::GetHandler(SoundKey key) {
+ auto key_handler_pair_iter = handlers_.find(key);
+ return key_handler_pair_iter == handlers_.end() ?
+ linked_ptr<AudioStreamHandler>() : key_handler_pair_iter->second;
+}
+
} // namespace
SoundsManager::SoundsManager() {}
diff --git a/chromium/media/audio/sounds/sounds_manager.h b/chromium/media/audio/sounds/sounds_manager.h
index 71184da3522..7db164fa3be 100644
--- a/chromium/media/audio/sounds/sounds_manager.h
+++ b/chromium/media/audio/sounds/sounds_manager.h
@@ -42,6 +42,10 @@ class MEDIA_EXPORT SoundsManager : public base::NonThreadSafe {
// was not properly initialized.
virtual bool Play(SoundKey key) = 0;
+ // Stops playing sound identified by |key|, returns false if SoundsManager
+ // was not properly initialized.
+ virtual bool Stop(SoundKey key) = 0;
+
// Returns duration of the sound identified by |key|. If SoundsManager
// was not properly initialized or |key| was not registered, this
// method returns an empty value.
diff --git a/chromium/media/audio/sounds/sounds_manager_unittest.cc b/chromium/media/audio/sounds/sounds_manager_unittest.cc
index 9741e6ef029..018fb5c1f37 100644
--- a/chromium/media/audio/sounds/sounds_manager_unittest.cc
+++ b/chromium/media/audio/sounds/sounds_manager_unittest.cc
@@ -11,6 +11,7 @@
#include "base/run_loop.h"
#include "base/strings/string_piece.h"
#include "media/audio/audio_manager.h"
+#include "media/audio/simple_sources.h"
#include "media/audio/sounds/audio_stream_handler.h"
#include "media/audio/sounds/sounds_manager.h"
#include "media/audio/sounds/test_data.h"
@@ -37,6 +38,11 @@ class SoundsManagerTest : public testing::Test {
AudioStreamHandler::SetObserverForTesting(observer);
}
+ void SetAudioSourceForTesting(
+ AudioOutputStream::AudioSourceCallback* source) {
+ AudioStreamHandler::SetAudioSourceForTesting(source);
+ }
+
private:
scoped_ptr<AudioManager> audio_manager_;
@@ -66,4 +72,43 @@ TEST_F(SoundsManagerTest, Play) {
SetObserverForTesting(NULL);
}
+TEST_F(SoundsManagerTest, Stop) {
+ ASSERT_TRUE(SoundsManager::Get());
+
+ base::RunLoop run_loop;
+ TestObserver observer(run_loop.QuitClosure());
+
+ SetObserverForTesting(&observer);
+
+ ASSERT_TRUE(SoundsManager::Get()->Initialize(
+ kTestAudioKey,
+ base::StringPiece(kTestAudioData, arraysize(kTestAudioData))));
+
+ // This overrides the wav data set by kTestAudioData and results in
+ // a never-ending sine wave being played.
+ const int kChannels = 1;
+ const double kFreq = 200;
+ const double kSampleFreq = 44100;
+ SineWaveAudioSource sine_source(kChannels, kFreq, kSampleFreq);
+ SetAudioSourceForTesting(&sine_source);
+
+ ASSERT_EQ(0, observer.num_play_requests());
+ ASSERT_EQ(0, observer.num_stop_requests());
+
+ ASSERT_TRUE(SoundsManager::Get()->Play(kTestAudioKey));
+ ASSERT_TRUE(SoundsManager::Get()->Stop(kTestAudioKey));
+ run_loop.Run();
+
+ ASSERT_EQ(1, observer.num_play_requests());
+ ASSERT_EQ(1, observer.num_stop_requests());
+
+ SetObserverForTesting(NULL);
+}
+
+TEST_F(SoundsManagerTest, Uninitialized) {
+ ASSERT_TRUE(SoundsManager::Get());
+ ASSERT_FALSE(SoundsManager::Get()->Play(kTestAudioKey));
+ ASSERT_FALSE(SoundsManager::Get()->Stop(kTestAudioKey));
+}
+
} // namespace media