audio_pump_unittest.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. #include "remoting/protocol/audio_pump.h"
  5. #include <stddef.h>
  6. #include <memory>
  7. #include <utility>
  8. #include <vector>
  9. #include "base/memory/ptr_util.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/run_loop.h"
  12. #include "base/test/task_environment.h"
  13. #include "remoting/codec/audio_encoder.h"
  14. #include "remoting/proto/audio.pb.h"
  15. #include "remoting/protocol/audio_source.h"
  16. #include "remoting/protocol/audio_stub.h"
  17. #include "remoting/protocol/fake_audio_source.h"
  18. #include "testing/gtest/include/gtest/gtest.h"
  19. namespace remoting {
  20. namespace protocol {
  21. namespace {
  22. // Creates a dummy packet with 1k data
  23. std::unique_ptr<AudioPacket> MakeAudioPacket(int channel_count = 2) {
  24. std::unique_ptr<AudioPacket> packet(new AudioPacket);
  25. packet->add_data()->resize(1024);
  26. packet->set_encoding(AudioPacket::ENCODING_RAW);
  27. packet->set_sampling_rate(AudioPacket::SAMPLING_RATE_44100);
  28. packet->set_bytes_per_sample(AudioPacket::BYTES_PER_SAMPLE_2);
  29. packet->set_channels(static_cast<AudioPacket::Channels>(channel_count));
  30. return packet;
  31. }
  32. } // namespace
  33. class FakeAudioEncoder : public AudioEncoder {
  34. public:
  35. FakeAudioEncoder() = default;
  36. FakeAudioEncoder(const FakeAudioEncoder&) = delete;
  37. FakeAudioEncoder& operator=(const FakeAudioEncoder&) = delete;
  38. ~FakeAudioEncoder() override = default;
  39. std::unique_ptr<AudioPacket> Encode(
  40. std::unique_ptr<AudioPacket> packet) override {
  41. EXPECT_TRUE(!!packet);
  42. EXPECT_EQ(packet->encoding(), AudioPacket::ENCODING_RAW);
  43. EXPECT_EQ(packet->sampling_rate(), AudioPacket::SAMPLING_RATE_44100);
  44. EXPECT_EQ(packet->bytes_per_sample(), AudioPacket::BYTES_PER_SAMPLE_2);
  45. EXPECT_LE(packet->channels(), AudioPacket::CHANNELS_STEREO);
  46. return packet;
  47. }
  48. int GetBitrate() override { return 160000; }
  49. };
  50. class AudioPumpTest : public testing::Test, public protocol::AudioStub {
  51. public:
  52. AudioPumpTest() = default;
  53. AudioPumpTest(const AudioPumpTest&) = delete;
  54. AudioPumpTest& operator=(const AudioPumpTest&) = delete;
  55. void SetUp() override;
  56. void TearDown() override;
  57. // protocol::AudioStub interface.
  58. void ProcessAudioPacket(std::unique_ptr<AudioPacket> audio_packet,
  59. base::OnceClosure done) override;
  60. protected:
  61. base::test::SingleThreadTaskEnvironment task_environment_;
  62. // |source_| and |encoder_| are owned by the |pump_|.
  63. raw_ptr<FakeAudioSource> source_;
  64. raw_ptr<FakeAudioEncoder> encoder_;
  65. std::unique_ptr<AudioPump> pump_;
  66. std::vector<std::unique_ptr<AudioPacket>> sent_packets_;
  67. std::vector<base::OnceClosure> done_closures_;
  68. };
  69. void AudioPumpTest::SetUp() {
  70. source_ = new FakeAudioSource();
  71. encoder_ = new FakeAudioEncoder();
  72. pump_ = std::make_unique<AudioPump>(
  73. task_environment_.GetMainThreadTaskRunner(),
  74. base::WrapUnique(source_.get()), base::WrapUnique(encoder_.get()), this);
  75. }
  76. void AudioPumpTest::TearDown() {
  77. pump_.reset();
  78. // Let the message loop run to finish destroying the capturer.
  79. base::RunLoop().RunUntilIdle();
  80. }
  81. void AudioPumpTest::ProcessAudioPacket(
  82. std::unique_ptr<AudioPacket> audio_packet,
  83. base::OnceClosure done) {
  84. sent_packets_.push_back(std::move(audio_packet));
  85. done_closures_.push_back(std::move(done));
  86. }
  87. // Verify that the pump pauses pumping when the network is congested.
  88. TEST_F(AudioPumpTest, BufferSizeLimit) {
  89. // Run message loop to let the pump start the capturer.
  90. base::RunLoop().RunUntilIdle();
  91. ASSERT_FALSE(source_->callback().is_null());
  92. // Try sending 100 packets, 1k each. The pump should stop pumping and start
  93. // dropping the data at some point.
  94. for (size_t i = 0; i < 100; ++i) {
  95. source_->callback().Run(MakeAudioPacket());
  96. base::RunLoop().RunUntilIdle();
  97. }
  98. size_t num_sent_packets = sent_packets_.size();
  99. EXPECT_LT(num_sent_packets, 100U);
  100. EXPECT_GT(num_sent_packets, 0U);
  101. // Call done closure for the first packet. This should allow one more packet
  102. // to be sent below.
  103. std::move(done_closures_.front()).Run();
  104. base::RunLoop().RunUntilIdle();
  105. // Verify that the pump continues to send captured audio.
  106. source_->callback().Run(MakeAudioPacket());
  107. base::RunLoop().RunUntilIdle();
  108. EXPECT_EQ(num_sent_packets + 1, sent_packets_.size());
  109. }
  110. TEST_F(AudioPumpTest, DownmixAudioPacket) {
  111. // Run message loop to let the pump start the capturer.
  112. base::RunLoop().RunUntilIdle();
  113. ASSERT_TRUE(source_->callback());
  114. // Generate several audio packets with different channel counts.
  115. static const int kChannels[] = {
  116. AudioPacket::CHANNELS_7_1,
  117. AudioPacket::CHANNELS_6_1,
  118. AudioPacket::CHANNELS_5_1,
  119. AudioPacket::CHANNELS_STEREO,
  120. AudioPacket::CHANNELS_MONO,
  121. AudioPacket::CHANNELS_7_1,
  122. AudioPacket::CHANNELS_7_1,
  123. AudioPacket::CHANNELS_7_1,
  124. AudioPacket::CHANNELS_7_1,
  125. AudioPacket::CHANNELS_6_1,
  126. AudioPacket::CHANNELS_6_1,
  127. AudioPacket::CHANNELS_6_1,
  128. AudioPacket::CHANNELS_6_1,
  129. AudioPacket::CHANNELS_5_1,
  130. AudioPacket::CHANNELS_5_1,
  131. AudioPacket::CHANNELS_5_1,
  132. AudioPacket::CHANNELS_5_1,
  133. AudioPacket::CHANNELS_STEREO,
  134. AudioPacket::CHANNELS_STEREO,
  135. AudioPacket::CHANNELS_STEREO,
  136. AudioPacket::CHANNELS_STEREO,
  137. AudioPacket::CHANNELS_MONO,
  138. AudioPacket::CHANNELS_MONO,
  139. AudioPacket::CHANNELS_MONO,
  140. AudioPacket::CHANNELS_MONO,
  141. };
  142. for (size_t i = 0; i < std::size(kChannels); i++) {
  143. source_->callback().Run(MakeAudioPacket(kChannels[i]));
  144. // Run message loop to let the pump processes the audio packet and send it
  145. // to the encoder.
  146. base::RunLoop().RunUntilIdle();
  147. // Call done closure to allow one more packet to be sent.
  148. ASSERT_EQ(done_closures_.size(), 1U);
  149. std::move(done_closures_.front()).Run();
  150. done_closures_.pop_back();
  151. base::RunLoop().RunUntilIdle();
  152. }
  153. ASSERT_EQ(sent_packets_.size(), std::size(kChannels));
  154. }
  155. } // namespace protocol
  156. } // namespace remoting