1 // Copyright 2014 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 "components/copresence/mediums/audio/audio_player.h"
8 #include "base/memory/weak_ptr.h"
9 #include "components/copresence/public/copresence_constants.h"
10 #include "components/copresence/test/audio_test_support.h"
11 #include "media/audio/audio_manager.h"
12 #include "media/audio/audio_manager_base.h"
13 #include "media/base/audio_bus.h"
14 #include "testing/gtest/include/gtest/gtest.h"
18 class TestAudioOutputStream : public media::AudioOutputStream {
20 typedef base::Callback<void(scoped_ptr<media::AudioBus>, int frames)>
21 GatherSamplesCallback;
22 TestAudioOutputStream(int default_frame_count,
24 GatherSamplesCallback gather_callback)
25 : default_frame_count_(default_frame_count),
26 max_frame_count_(max_frame_count),
27 gather_callback_(gather_callback),
29 caller_loop_ = base::MessageLoop::current();
32 virtual ~TestAudioOutputStream() {}
34 virtual bool Open() OVERRIDE { return true; }
35 virtual void Start(AudioSourceCallback* callback) OVERRIDE {
37 GatherPlayedSamples();
39 virtual void Stop() OVERRIDE {}
40 virtual void SetVolume(double volume) OVERRIDE {}
41 virtual void GetVolume(double* volume) OVERRIDE {}
42 virtual void Close() OVERRIDE {}
45 void GatherPlayedSamples() {
46 int frames = 0, total_frames = 0;
48 // Call back into the player to get samples that it wants us to play.
49 scoped_ptr<media::AudioBus> dest =
50 media::AudioBus::Create(1, default_frame_count_);
51 frames = callback_->OnMoreData(dest.get(), media::AudioBuffersState());
52 total_frames += frames;
53 // Send the samples given to us by the player to the gather callback.
54 caller_loop_->PostTask(
55 FROM_HERE, base::Bind(gather_callback_, base::Passed(&dest), frames));
56 } while (frames && total_frames < max_frame_count_);
59 int default_frame_count_;
61 GatherSamplesCallback gather_callback_;
62 AudioSourceCallback* callback_;
63 base::MessageLoop* caller_loop_;
65 DISALLOW_COPY_AND_ASSIGN(TestAudioOutputStream);
70 namespace copresence {
72 class AudioPlayerTest : public testing::Test,
73 public base::SupportsWeakPtr<AudioPlayerTest> {
75 AudioPlayerTest() : buffer_index_(0), player_(NULL) {
76 if (!media::AudioManager::Get())
77 media::AudioManager::CreateForTesting();
80 virtual ~AudioPlayerTest() { DeletePlayer(); }
84 player_ = new AudioPlayer();
85 player_->set_output_stream_for_testing(new TestAudioOutputStream(
88 base::Bind(&AudioPlayerTest::GatherSamples, AsWeakPtr())));
89 player_->Initialize();
99 void PlayAndVerifySamples(
100 const scoped_refptr<media::AudioBusRefCounted>& samples) {
101 buffer_ = media::AudioBus::Create(1, kMaxFrameCount);
102 player_->Play(samples);
103 player_->FlushAudioLoopForTesting();
106 for (int i = 0; i < samples->frames(); ++i)
107 differences += (buffer_->channel(0)[i] != samples->channel(0)[i]);
108 ASSERT_EQ(0, differences);
113 void GatherSamples(scoped_ptr<media::AudioBus> bus, int frames) {
116 bus->CopyPartialFramesTo(0, frames, buffer_index_, buffer_.get());
117 buffer_index_ += frames;
122 player_->FlushAudioLoopForTesting();
123 return player_->is_playing_;
126 static const int kDefaultFrameCount = 1024;
127 static const int kMaxFrameCount = 1024 * 10;
129 scoped_ptr<media::AudioBus> buffer_;
132 AudioPlayer* player_;
133 base::MessageLoop message_loop_;
136 TEST_F(AudioPlayerTest, BasicPlayAndStop) {
138 scoped_refptr<media::AudioBusRefCounted> samples =
139 media::AudioBusRefCounted::Create(1, 7331);
141 player_->Play(samples);
142 EXPECT_TRUE(IsPlaying());
144 EXPECT_FALSE(IsPlaying());
145 player_->Play(samples);
147 EXPECT_TRUE(IsPlaying());
149 EXPECT_FALSE(IsPlaying());
150 player_->Play(samples);
152 EXPECT_TRUE(IsPlaying());
154 EXPECT_FALSE(IsPlaying());
159 TEST_F(AudioPlayerTest, OutOfOrderPlayAndStopMultiple) {
161 scoped_refptr<media::AudioBusRefCounted> samples =
162 media::AudioBusRefCounted::Create(1, 1337);
167 EXPECT_FALSE(IsPlaying());
169 player_->Play(samples);
170 player_->Play(samples);
171 EXPECT_TRUE(IsPlaying());
175 EXPECT_FALSE(IsPlaying());
180 TEST_F(AudioPlayerTest, PlayingEndToEnd) {
181 const int kNumSamples = kDefaultFrameCount * 10;
184 PlayAndVerifySamples(CreateRandomAudioRefCounted(0x1337, 1, kNumSamples));
189 } // namespace copresence