1 // Copyright 2015 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/audio_modem/audio_player.h"
8 #include "base/memory/weak_ptr.h"
9 #include "components/audio_modem/audio_player_impl.h"
10 #include "components/audio_modem/public/audio_modem_types.h"
11 #include "components/audio_modem/test/random_samples.h"
12 #include "media/audio/audio_manager.h"
13 #include "media/audio/audio_manager_base.h"
14 #include "media/base/audio_bus.h"
15 #include "testing/gtest/include/gtest/gtest.h"
19 class TestAudioOutputStream
: public media::AudioOutputStream
{
21 using GatherSamplesCallback
=
22 base::Callback
<void(scoped_ptr
<media::AudioBus
>, int frames
)>;
23 TestAudioOutputStream(int default_frame_count
,
25 GatherSamplesCallback gather_callback
)
26 : default_frame_count_(default_frame_count
),
27 max_frame_count_(max_frame_count
),
28 gather_callback_(gather_callback
),
30 caller_loop_
= base::MessageLoop::current();
33 ~TestAudioOutputStream() override
{}
35 bool Open() override
{ return true; }
36 void Start(AudioSourceCallback
* callback
) override
{
38 GatherPlayedSamples();
40 void Stop() override
{}
41 void SetVolume(double volume
) override
{}
42 void GetVolume(double* volume
) override
{}
43 void Close() override
{}
46 void GatherPlayedSamples() {
47 int frames
= 0, total_frames
= 0;
49 // Call back into the player to get samples that it wants us to play.
50 scoped_ptr
<media::AudioBus
> dest
=
51 media::AudioBus::Create(1, default_frame_count_
);
52 frames
= callback_
->OnMoreData(dest
.get(), 0);
53 total_frames
+= frames
;
54 // Send the samples given to us by the player to the gather callback.
55 caller_loop_
->PostTask(
56 FROM_HERE
, base::Bind(gather_callback_
, base::Passed(&dest
), frames
));
57 } while (frames
&& total_frames
< max_frame_count_
);
60 int default_frame_count_
;
62 GatherSamplesCallback gather_callback_
;
63 AudioSourceCallback
* callback_
;
64 base::MessageLoop
* caller_loop_
;
66 DISALLOW_COPY_AND_ASSIGN(TestAudioOutputStream
);
71 namespace audio_modem
{
73 class AudioPlayerTest
: public testing::Test
,
74 public base::SupportsWeakPtr
<AudioPlayerTest
> {
76 AudioPlayerTest() : buffer_index_(0), player_(nullptr) {
77 if (!media::AudioManager::Get())
78 media::AudioManager::CreateForTesting();
81 ~AudioPlayerTest() override
{ DeletePlayer(); }
85 player_
= new AudioPlayerImpl();
86 player_
->set_output_stream_for_testing(new TestAudioOutputStream(
89 base::Bind(&AudioPlayerTest::GatherSamples
, AsWeakPtr())));
90 player_
->Initialize();
100 void PlayAndVerifySamples(
101 const scoped_refptr
<media::AudioBusRefCounted
>& samples
) {
102 DCHECK_LT(samples
->frames(), kMaxFrameCount
);
104 buffer_
= media::AudioBus::Create(1, kMaxFrameCount
);
106 player_
->Play(samples
);
107 player_
->FlushAudioLoopForTesting();
111 for (int i
= 0; i
< kMaxFrameCount
; ++i
) {
112 differences
+= (buffer_
->channel(0)[i
] !=
113 samples
->channel(0)[i
% samples
->frames()]);
115 ASSERT_EQ(0, differences
);
120 void GatherSamples(scoped_ptr
<media::AudioBus
> bus
, int frames
) {
123 bus
->CopyPartialFramesTo(0, frames
, buffer_index_
, buffer_
.get());
124 buffer_index_
+= frames
;
129 player_
->FlushAudioLoopForTesting();
130 return player_
->is_playing_
;
133 static const int kDefaultFrameCount
= 1024;
134 static const int kMaxFrameCount
= 1024 * 100;
136 scoped_ptr
<media::AudioBus
> buffer_
;
139 // Deleted by calling Finalize() on the object.
140 AudioPlayerImpl
* player_
;
141 base::MessageLoop message_loop_
;
144 TEST_F(AudioPlayerTest
, BasicPlayAndStop
) {
146 scoped_refptr
<media::AudioBusRefCounted
> samples
=
147 media::AudioBusRefCounted::Create(1, 7331);
149 player_
->Play(samples
);
150 EXPECT_TRUE(IsPlaying());
152 EXPECT_FALSE(IsPlaying());
153 player_
->Play(samples
);
155 EXPECT_TRUE(IsPlaying());
157 EXPECT_FALSE(IsPlaying());
158 player_
->Play(samples
);
160 EXPECT_TRUE(IsPlaying());
162 EXPECT_FALSE(IsPlaying());
167 TEST_F(AudioPlayerTest
, OutOfOrderPlayAndStopMultiple
) {
169 scoped_refptr
<media::AudioBusRefCounted
> samples
=
170 media::AudioBusRefCounted::Create(1, 1337);
175 EXPECT_FALSE(IsPlaying());
177 player_
->Play(samples
);
178 player_
->Play(samples
);
179 EXPECT_TRUE(IsPlaying());
183 EXPECT_FALSE(IsPlaying());
188 TEST_F(AudioPlayerTest
, PlayingEndToEnd
) {
189 const int kNumSamples
= kDefaultFrameCount
* 7 + 321;
192 PlayAndVerifySamples(CreateRandomAudioRefCounted(0x1337, 1, kNumSamples
));
197 TEST_F(AudioPlayerTest
, PlayingEndToEndRepeated
) {
198 const int kNumSamples
= kDefaultFrameCount
* 7 + 537;
201 PlayAndVerifySamples(CreateRandomAudioRefCounted(0x1337, 1, kNumSamples
));
203 PlayAndVerifySamples(
204 CreateRandomAudioRefCounted(0x7331, 1, kNumSamples
- 3123));
206 PlayAndVerifySamples(CreateRandomAudioRefCounted(0xf00d, 1, kNumSamples
* 2));
211 } // namespace audio_modem