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