OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "remoting/host/audio_pump.h" | |
6 | |
7 #include <stddef.h> | |
8 | |
9 #include <utility> | |
10 | |
11 #include "base/macros.h" | |
12 #include "base/memory/ptr_util.h" | |
13 #include "base/memory/scoped_vector.h" | |
14 #include "base/message_loop/message_loop.h" | |
15 #include "base/run_loop.h" | |
16 #include "remoting/codec/audio_encoder.h" | |
17 #include "remoting/host/audio_capturer.h" | |
18 #include "remoting/proto/audio.pb.h" | |
19 #include "remoting/protocol/audio_stub.h" | |
20 #include "testing/gtest/include/gtest/gtest.h" | |
21 | |
22 namespace remoting { | |
23 | |
24 namespace { | |
25 | |
26 // Creates a dummy packet with 1k data | |
27 std::unique_ptr<AudioPacket> MakeAudioPacket() { | |
28 std::unique_ptr<AudioPacket> packet(new AudioPacket); | |
29 packet->add_data()->resize(1000); | |
30 return packet; | |
31 } | |
32 | |
33 } // namespace | |
34 | |
35 class FakeAudioCapturer : public AudioCapturer { | |
36 public: | |
37 FakeAudioCapturer() {} | |
38 ~FakeAudioCapturer() override {} | |
39 | |
40 bool Start(const PacketCapturedCallback& callback) override { | |
41 callback_ = callback; | |
42 return true; | |
43 } | |
44 | |
45 const PacketCapturedCallback& callback() { return callback_; } | |
46 | |
47 private: | |
48 PacketCapturedCallback callback_; | |
49 | |
50 DISALLOW_COPY_AND_ASSIGN(FakeAudioCapturer); | |
51 }; | |
52 | |
53 class FakeAudioEncoder : public AudioEncoder { | |
54 public: | |
55 FakeAudioEncoder() {} | |
56 ~FakeAudioEncoder() override {} | |
57 | |
58 std::unique_ptr<AudioPacket> Encode( | |
59 std::unique_ptr<AudioPacket> packet) override { | |
60 return packet; | |
61 } | |
62 int GetBitrate() override { | |
63 return 160000; | |
64 } | |
65 | |
66 private: | |
67 DISALLOW_COPY_AND_ASSIGN(FakeAudioEncoder); | |
68 }; | |
69 | |
70 class AudioPumpTest : public testing::Test, public protocol::AudioStub { | |
71 public: | |
72 AudioPumpTest() {} | |
73 | |
74 void SetUp() override; | |
75 void TearDown() override; | |
76 | |
77 // protocol::AudioStub interface. | |
78 void ProcessAudioPacket(std::unique_ptr<AudioPacket> audio_packet, | |
79 const base::Closure& done) override; | |
80 | |
81 protected: | |
82 base::MessageLoop message_loop_; | |
83 | |
84 // |capturer_| and |encoder_| are owned by the |pump_|. | |
85 FakeAudioCapturer* capturer_; | |
86 FakeAudioEncoder* encoder_; | |
87 | |
88 std::unique_ptr<AudioPump> pump_; | |
89 | |
90 ScopedVector<AudioPacket> sent_packets_; | |
91 std::vector<base::Closure> done_closures_; | |
92 | |
93 private: | |
94 DISALLOW_COPY_AND_ASSIGN(AudioPumpTest); | |
95 }; | |
96 | |
97 void AudioPumpTest::SetUp() { | |
98 capturer_ = new FakeAudioCapturer(); | |
99 encoder_ = new FakeAudioEncoder(); | |
100 pump_.reset(new AudioPump(message_loop_.task_runner(), | |
101 base::WrapUnique(capturer_), | |
102 base::WrapUnique(encoder_), this)); | |
103 } | |
104 | |
105 void AudioPumpTest::TearDown() { | |
106 pump_.reset(); | |
107 | |
108 // Let the message loop run to finish destroying the capturer. | |
109 base::RunLoop().RunUntilIdle(); | |
110 } | |
111 | |
112 void AudioPumpTest::ProcessAudioPacket( | |
113 std::unique_ptr<AudioPacket> audio_packet, | |
114 const base::Closure& done) { | |
115 sent_packets_.push_back(std::move(audio_packet)); | |
116 done_closures_.push_back(done); | |
117 } | |
118 | |
119 // Verify that the pump pauses pumping when the network is congested. | |
120 TEST_F(AudioPumpTest, BufferSizeLimit) { | |
121 // Run message loop to let the pump start the capturer. | |
122 base::RunLoop().RunUntilIdle(); | |
123 ASSERT_FALSE(capturer_->callback().is_null()); | |
124 | |
125 // Try sending 100 packets, 1k each. The pump should stop pumping and start | |
126 // dropping the data at some point. | |
127 for (size_t i = 0; i < 100; ++i) { | |
128 capturer_->callback().Run(MakeAudioPacket()); | |
129 base::RunLoop().RunUntilIdle(); | |
130 } | |
131 | |
132 size_t num_sent_packets = sent_packets_.size(); | |
133 EXPECT_LT(num_sent_packets, 100U); | |
134 EXPECT_GT(num_sent_packets, 0U); | |
135 | |
136 // Call done closure for the first packet. This should allow one more packet | |
137 // to be sent below. | |
138 done_closures_.front().Run(); | |
139 base::RunLoop().RunUntilIdle(); | |
140 | |
141 // Verify that the pump continues to send captured audio. | |
142 capturer_->callback().Run(MakeAudioPacket()); | |
143 base::RunLoop().RunUntilIdle(); | |
144 EXPECT_EQ(num_sent_packets + 1, sent_packets_.size()); | |
145 } | |
146 | |
147 } // namespace remoting | |
OLD | NEW |