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 "remoting/host/audio_pump.h"
7 #include "base/memory/scoped_vector.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "remoting/codec/audio_encoder.h"
11 #include "remoting/host/audio_capturer.h"
12 #include "remoting/proto/audio.pb.h"
13 #include "remoting/protocol/audio_stub.h"
14 #include "testing/gtest/include/gtest/gtest.h"
20 // Creates a dummy packet with 1k data
21 scoped_ptr
<AudioPacket
> MakeAudioPacket() {
22 scoped_ptr
<AudioPacket
> packet(new AudioPacket
);
23 packet
->add_data()->resize(1000);
29 class FakeAudioCapturer
: public AudioCapturer
{
31 FakeAudioCapturer() {}
32 ~FakeAudioCapturer() override
{}
34 bool Start(const PacketCapturedCallback
& callback
) override
{
39 const PacketCapturedCallback
& callback() { return callback_
; }
42 PacketCapturedCallback callback_
;
44 DISALLOW_COPY_AND_ASSIGN(FakeAudioCapturer
);
47 class FakeAudioEncoder
: public AudioEncoder
{
50 ~FakeAudioEncoder() override
{}
52 scoped_ptr
<AudioPacket
> Encode(scoped_ptr
<AudioPacket
> packet
) override
{
55 int GetBitrate() override
{
60 DISALLOW_COPY_AND_ASSIGN(FakeAudioEncoder
);
63 class AudioPumpTest
: public testing::Test
, public protocol::AudioStub
{
67 void SetUp() override
;
68 void TearDown() override
;
70 // protocol::AudioStub interface.
71 void ProcessAudioPacket(scoped_ptr
<AudioPacket
> audio_packet
,
72 const base::Closure
& done
) override
;
75 base::MessageLoop message_loop_
;
77 // |capturer_| and |encoder_| are owned by the |pump_|.
78 FakeAudioCapturer
* capturer_
;
79 FakeAudioEncoder
* encoder_
;
81 scoped_ptr
<AudioPump
> pump_
;
83 ScopedVector
<AudioPacket
> sent_packets_
;
84 std::vector
<base::Closure
> done_closures_
;
87 DISALLOW_COPY_AND_ASSIGN(AudioPumpTest
);
90 void AudioPumpTest::SetUp() {
91 capturer_
= new FakeAudioCapturer();
92 encoder_
= new FakeAudioEncoder();
93 pump_
.reset(new AudioPump(message_loop_
.task_runner(),
94 make_scoped_ptr(capturer_
),
95 make_scoped_ptr(encoder_
), this));
98 void AudioPumpTest::TearDown() {
101 // Let the message loop run to finish destroying the capturer.
102 base::RunLoop().RunUntilIdle();
105 void AudioPumpTest::ProcessAudioPacket(
106 scoped_ptr
<AudioPacket
> audio_packet
,
107 const base::Closure
& done
) {
108 sent_packets_
.push_back(audio_packet
.Pass());
109 done_closures_
.push_back(done
);
112 // Verify that the pump pauses pumping when the network is congested.
113 TEST_F(AudioPumpTest
, BufferSizeLimit
) {
114 // Run message loop to let the pump start the capturer.
115 base::RunLoop().RunUntilIdle();
116 ASSERT_FALSE(capturer_
->callback().is_null());
118 // Try sending 100 packets, 1k each. The pump should stop pumping and start
119 // dropping the data at some point.
120 for (size_t i
= 0; i
< 100; ++i
) {
121 capturer_
->callback().Run(MakeAudioPacket());
122 base::RunLoop().RunUntilIdle();
125 size_t num_sent_packets
= sent_packets_
.size();
126 EXPECT_LT(num_sent_packets
, 100U);
127 EXPECT_GT(num_sent_packets
, 0U);
129 // Call done closure for the first packet. This should allow one more packet
131 done_closures_
.front().Run();
132 base::RunLoop().RunUntilIdle();
134 // Verify that the pump continues to send captured audio.
135 capturer_
->callback().Run(MakeAudioPacket());
136 base::RunLoop().RunUntilIdle();
137 EXPECT_EQ(num_sent_packets
+ 1, sent_packets_
.size());
140 } // namespace remoting