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 "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" |
| 15 |
| 16 namespace remoting { |
| 17 |
| 18 namespace { |
| 19 |
| 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); |
| 24 return packet.Pass(); |
| 25 } |
| 26 |
| 27 } // namespace |
| 28 |
| 29 class FakeAudioCapturer : public AudioCapturer { |
| 30 public: |
| 31 FakeAudioCapturer() {} |
| 32 ~FakeAudioCapturer() override {} |
| 33 |
| 34 bool Start(const PacketCapturedCallback& callback) override { |
| 35 callback_ = callback; |
| 36 return true; |
| 37 } |
| 38 |
| 39 const PacketCapturedCallback& callback() { return callback_; } |
| 40 |
| 41 private: |
| 42 PacketCapturedCallback callback_; |
| 43 |
| 44 DISALLOW_COPY_AND_ASSIGN(FakeAudioCapturer); |
| 45 }; |
| 46 |
| 47 class FakeAudioEncoder : public AudioEncoder { |
| 48 public: |
| 49 FakeAudioEncoder() {} |
| 50 ~FakeAudioEncoder() override {} |
| 51 |
| 52 scoped_ptr<AudioPacket> Encode(scoped_ptr<AudioPacket> packet) { |
| 53 return packet.Pass(); |
| 54 } |
| 55 |
| 56 private: |
| 57 DISALLOW_COPY_AND_ASSIGN(FakeAudioEncoder); |
| 58 }; |
| 59 |
| 60 class AudioPumpTest : public testing::Test, public protocol::AudioStub { |
| 61 public: |
| 62 AudioPumpTest() {} |
| 63 |
| 64 void SetUp() override; |
| 65 |
| 66 // protocol::AudioStub interface. |
| 67 void ProcessAudioPacket(scoped_ptr<AudioPacket> audio_packet, |
| 68 const base::Closure& done) override; |
| 69 |
| 70 protected: |
| 71 base::MessageLoop message_loop_; |
| 72 |
| 73 // |capturer_| and |encoder_| are owned by the |pump_|. |
| 74 FakeAudioCapturer* capturer_; |
| 75 FakeAudioEncoder* encoder_; |
| 76 |
| 77 scoped_ptr<AudioPump> pump_; |
| 78 |
| 79 ScopedVector<AudioPacket> sent_packets_; |
| 80 std::vector<base::Closure> done_closures_; |
| 81 |
| 82 private: |
| 83 DISALLOW_COPY_AND_ASSIGN(AudioPumpTest); |
| 84 }; |
| 85 |
| 86 void AudioPumpTest::SetUp() { |
| 87 capturer_ = new FakeAudioCapturer(); |
| 88 encoder_ = new FakeAudioEncoder(); |
| 89 pump_.reset(new AudioPump(message_loop_.task_runner(), |
| 90 make_scoped_ptr(capturer_), |
| 91 make_scoped_ptr(encoder_), this)); |
| 92 } |
| 93 |
| 94 void AudioPumpTest::ProcessAudioPacket( |
| 95 scoped_ptr<AudioPacket> audio_packet, |
| 96 const base::Closure& done) { |
| 97 sent_packets_.push_back(audio_packet.Pass()); |
| 98 done_closures_.push_back(done); |
| 99 } |
| 100 |
| 101 // Verify that the pump pauses pumping when the network is congested. |
| 102 TEST_F(AudioPumpTest, BufferSizeLimit) { |
| 103 // Run message loop to let the pump start the capturer. |
| 104 base::RunLoop().RunUntilIdle(); |
| 105 ASSERT_FALSE(capturer_->callback().is_null()); |
| 106 |
| 107 // Try sending 100 packets, 1k each. The pump should stop pumping and start |
| 108 // dropping the data at some point. |
| 109 for (size_t i = 0; i < 100; ++i) { |
| 110 capturer_->callback().Run(MakeAudioPacket()); |
| 111 base::RunLoop().RunUntilIdle(); |
| 112 } |
| 113 |
| 114 size_t num_sent_packets = sent_packets_.size(); |
| 115 EXPECT_LT(num_sent_packets, 100U); |
| 116 EXPECT_GT(num_sent_packets, 0U); |
| 117 |
| 118 // Call done closure for the first packet. This should allow one more packet |
| 119 // to be sent below. |
| 120 done_closures_.front().Run(); |
| 121 base::RunLoop().RunUntilIdle(); |
| 122 |
| 123 // Verify that the pump continues to send captured audio. |
| 124 capturer_->callback().Run(MakeAudioPacket()); |
| 125 base::RunLoop().RunUntilIdle(); |
| 126 EXPECT_EQ(num_sent_packets + 1, sent_packets_.size()); |
| 127 } |
| 128 |
| 129 } // namespace remoting |
OLD | NEW |