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/location.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/single_thread_task_runner.h"
11 #include "components/audio_modem/audio_player_impl.h"
12 #include "components/audio_modem/public/audio_modem_types.h"
13 #include "components/audio_modem/test/random_samples.h"
14 #include "media/audio/audio_manager.h"
15 #include "media/audio/audio_manager_base.h"
16 #include "media/base/audio_bus.h"
17 #include "testing/gtest/include/gtest/gtest.h"
21 class TestAudioOutputStream
: public media::AudioOutputStream
{
23 using GatherSamplesCallback
=
24 base::Callback
<void(scoped_ptr
<media::AudioBus
>, int frames
)>;
25 TestAudioOutputStream(int default_frame_count
,
27 GatherSamplesCallback gather_callback
)
28 : default_frame_count_(default_frame_count
),
29 max_frame_count_(max_frame_count
),
30 gather_callback_(gather_callback
),
32 caller_loop_
= base::MessageLoop::current();
35 ~TestAudioOutputStream() override
{}
37 bool Open() override
{ return true; }
38 void Start(AudioSourceCallback
* callback
) override
{
40 GatherPlayedSamples();
42 void Stop() override
{}
43 void SetVolume(double volume
) override
{}
44 void GetVolume(double* volume
) override
{}
45 void Close() override
{}
48 void GatherPlayedSamples() {
49 int frames
= 0, total_frames
= 0;
51 // Call back into the player to get samples that it wants us to play.
52 scoped_ptr
<media::AudioBus
> dest
=
53 media::AudioBus::Create(1, default_frame_count_
);
54 frames
= callback_
->OnMoreData(dest
.get(), 0);
55 total_frames
+= frames
;
56 // Send the samples given to us by the player to the gather callback.
57 caller_loop_
->task_runner()->PostTask(
58 FROM_HERE
, base::Bind(gather_callback_
, base::Passed(&dest
), frames
));
59 } while (frames
&& total_frames
< max_frame_count_
);
62 int default_frame_count_
;
64 GatherSamplesCallback gather_callback_
;
65 AudioSourceCallback
* callback_
;
66 base::MessageLoop
* caller_loop_
;
68 DISALLOW_COPY_AND_ASSIGN(TestAudioOutputStream
);
73 namespace audio_modem
{
75 class AudioPlayerTest
: public testing::Test
,
76 public base::SupportsWeakPtr
<AudioPlayerTest
> {
78 AudioPlayerTest() : buffer_index_(0), player_(nullptr) {
79 if (!media::AudioManager::Get())
80 media::AudioManager::CreateForTesting();
83 ~AudioPlayerTest() override
{ DeletePlayer(); }
87 player_
= new AudioPlayerImpl();
88 player_
->set_output_stream_for_testing(new TestAudioOutputStream(
91 base::Bind(&AudioPlayerTest::GatherSamples
, AsWeakPtr())));
92 player_
->Initialize();
102 void PlayAndVerifySamples(
103 const scoped_refptr
<media::AudioBusRefCounted
>& samples
) {
104 DCHECK_LT(samples
->frames(), kMaxFrameCount
);
106 buffer_
= media::AudioBus::Create(1, kMaxFrameCount
);
108 player_
->Play(samples
);
109 player_
->FlushAudioLoopForTesting();
113 for (int i
= 0; i
< kMaxFrameCount
; ++i
) {
114 differences
+= (buffer_
->channel(0)[i
] !=
115 samples
->channel(0)[i
% samples
->frames()]);
117 ASSERT_EQ(0, differences
);
122 void GatherSamples(scoped_ptr
<media::AudioBus
> bus
, int frames
) {
125 bus
->CopyPartialFramesTo(0, frames
, buffer_index_
, buffer_
.get());
126 buffer_index_
+= frames
;
131 player_
->FlushAudioLoopForTesting();
132 return player_
->is_playing_
;
135 static const int kDefaultFrameCount
= 1024;
136 static const int kMaxFrameCount
= 1024 * 100;
138 scoped_ptr
<media::AudioBus
> buffer_
;
141 // Deleted by calling Finalize() on the object.
142 AudioPlayerImpl
* player_
;
143 base::MessageLoop message_loop_
;
146 TEST_F(AudioPlayerTest
, BasicPlayAndStop
) {
148 scoped_refptr
<media::AudioBusRefCounted
> samples
=
149 media::AudioBusRefCounted::Create(1, 7331);
151 player_
->Play(samples
);
152 EXPECT_TRUE(IsPlaying());
154 EXPECT_FALSE(IsPlaying());
155 player_
->Play(samples
);
157 EXPECT_TRUE(IsPlaying());
159 EXPECT_FALSE(IsPlaying());
160 player_
->Play(samples
);
162 EXPECT_TRUE(IsPlaying());
164 EXPECT_FALSE(IsPlaying());
169 TEST_F(AudioPlayerTest
, OutOfOrderPlayAndStopMultiple
) {
171 scoped_refptr
<media::AudioBusRefCounted
> samples
=
172 media::AudioBusRefCounted::Create(1, 1337);
177 EXPECT_FALSE(IsPlaying());
179 player_
->Play(samples
);
180 player_
->Play(samples
);
181 EXPECT_TRUE(IsPlaying());
185 EXPECT_FALSE(IsPlaying());
190 TEST_F(AudioPlayerTest
, PlayingEndToEnd
) {
191 const int kNumSamples
= kDefaultFrameCount
* 7 + 321;
194 PlayAndVerifySamples(CreateRandomAudioRefCounted(0x1337, 1, kNumSamples
));
199 TEST_F(AudioPlayerTest
, PlayingEndToEndRepeated
) {
200 const int kNumSamples
= kDefaultFrameCount
* 7 + 537;
203 PlayAndVerifySamples(CreateRandomAudioRefCounted(0x1337, 1, kNumSamples
));
205 PlayAndVerifySamples(
206 CreateRandomAudioRefCounted(0x7331, 1, kNumSamples
- 3123));
208 PlayAndVerifySamples(CreateRandomAudioRefCounted(0xf00d, 1, kNumSamples
* 2));
213 } // namespace audio_modem