rtp_stream_unittest.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Copyright 2018 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 "components/mirroring/service/rtp_stream.h"
  5. #include "base/callback_helpers.h"
  6. #include "base/memory/raw_ptr.h"
  7. #include "base/run_loop.h"
  8. #include "base/test/mock_callback.h"
  9. #include "base/test/simple_test_tick_clock.h"
  10. #include "base/test/task_environment.h"
  11. #include "base/time/time.h"
  12. #include "media/base/video_frame.h"
  13. #include "media/cast/cast_config.h"
  14. #include "media/cast/cast_environment.h"
  15. #include "media/cast/sender/audio_sender.h"
  16. #include "media/cast/sender/video_sender.h"
  17. #include "media/cast/test/mock_cast_transport.h"
  18. #include "media/cast/test/utility/audio_utility.h"
  19. #include "media/cast/test/utility/default_config.h"
  20. #include "media/cast/test/utility/video_utility.h"
  21. #include "testing/gmock/include/gmock/gmock.h"
  22. #include "testing/gtest/include/gtest/gtest.h"
  23. using ::testing::InvokeWithoutArgs;
  24. using ::testing::_;
  25. using media::cast::TestAudioBusFactory;
  26. namespace mirroring {
  27. namespace {
  28. class StreamClient final : public RtpStreamClient {
  29. public:
  30. explicit StreamClient(base::SimpleTestTickClock* clock) : clock_(clock) {}
  31. StreamClient(const StreamClient&) = delete;
  32. StreamClient& operator=(const StreamClient&) = delete;
  33. ~StreamClient() override = default;
  34. void SetVideoRtpStream(VideoRtpStream* stream) { video_stream_ = stream; }
  35. // RtpStreamClient implementation.
  36. void OnError(const std::string& message) override {}
  37. void RequestRefreshFrame() override {
  38. if (video_stream_) {
  39. video_stream_->InsertVideoFrame(CreateVideoFrame());
  40. }
  41. }
  42. void CreateVideoEncodeAccelerator(
  43. media::cast::ReceiveVideoEncodeAcceleratorCallback callback) override {}
  44. scoped_refptr<media::VideoFrame> CreateVideoFrame() {
  45. constexpr gfx::Size kFrameSize(640, 480);
  46. base::TimeDelta frame_timestamp;
  47. if (first_frame_time_.is_null()) {
  48. first_frame_time_ = clock_->NowTicks();
  49. frame_timestamp = base::TimeDelta();
  50. } else {
  51. clock_->Advance(base::Milliseconds(10));
  52. frame_timestamp = clock_->NowTicks() - first_frame_time_;
  53. }
  54. auto frame = media::VideoFrame::CreateFrame(
  55. media::PIXEL_FORMAT_I420, kFrameSize, gfx::Rect(kFrameSize), kFrameSize,
  56. frame_timestamp);
  57. media::cast::PopulateVideoFrame(frame.get(), 1);
  58. frame->metadata().reference_time = clock_->NowTicks();
  59. return frame;
  60. }
  61. base::WeakPtr<RtpStreamClient> GetWeakPtr() {
  62. return weak_factory_.GetWeakPtr();
  63. }
  64. private:
  65. raw_ptr<VideoRtpStream> video_stream_;
  66. base::TimeTicks first_frame_time_;
  67. const raw_ptr<base::SimpleTestTickClock> clock_;
  68. base::WeakPtrFactory<StreamClient> weak_factory_{this};
  69. };
  70. } // namespace
  71. class RtpStreamTest : public ::testing::Test {
  72. public:
  73. RtpStreamTest()
  74. : cast_environment_(new media::cast::CastEnvironment(
  75. &testing_clock_,
  76. task_environment_.GetMainThreadTaskRunner(),
  77. task_environment_.GetMainThreadTaskRunner(),
  78. task_environment_.GetMainThreadTaskRunner())),
  79. client_(&testing_clock_) {
  80. testing_clock_.Advance(base::TimeTicks::Now() - base::TimeTicks());
  81. }
  82. RtpStreamTest(const RtpStreamTest&) = delete;
  83. RtpStreamTest& operator=(const RtpStreamTest&) = delete;
  84. ~RtpStreamTest() override { task_environment_.RunUntilIdle(); }
  85. protected:
  86. base::test::TaskEnvironment task_environment_;
  87. base::SimpleTestTickClock testing_clock_;
  88. const scoped_refptr<media::cast::CastEnvironment> cast_environment_;
  89. StreamClient client_;
  90. // We currently don't care about sender reports, so we use a nice mock here.
  91. testing::NiceMock<media::cast::MockCastTransport> transport_;
  92. };
  93. // Test the video streaming pipeline.
  94. TEST_F(RtpStreamTest, VideoStreaming) {
  95. auto video_sender = std::make_unique<media::cast::VideoSender>(
  96. cast_environment_, media::cast::GetDefaultVideoSenderConfig(),
  97. base::DoNothing(), base::DoNothing(), &transport_, base::DoNothing(),
  98. base::DoNothing());
  99. VideoRtpStream video_stream(std::move(video_sender), client_.GetWeakPtr());
  100. {
  101. base::RunLoop run_loop;
  102. // Expect the video frame is sent to video sender for encoding, and the
  103. // encoded frame is sent to the transport.
  104. EXPECT_CALL(transport_, InsertFrame(_, _))
  105. .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit));
  106. video_stream.InsertVideoFrame(client_.CreateVideoFrame());
  107. run_loop.Run();
  108. }
  109. task_environment_.RunUntilIdle();
  110. }
  111. TEST_F(RtpStreamTest, VideoStreamEmitsFramesWhenNoUpdates) {
  112. auto video_sender = std::make_unique<media::cast::VideoSender>(
  113. cast_environment_, media::cast::GetDefaultVideoSenderConfig(),
  114. base::DoNothing(), base::DoNothing(), &transport_, base::DoNothing(),
  115. base::DoNothing());
  116. VideoRtpStream video_stream(std::move(video_sender), client_.GetWeakPtr());
  117. client_.SetVideoRtpStream(&video_stream);
  118. {
  119. base::RunLoop run_loop;
  120. int loop_count = 0;
  121. // Expect the video frame is sent to video sender for encoding, and the
  122. // encoded frame is sent to the transport.
  123. EXPECT_CALL(transport_, InsertFrame(_, _))
  124. .WillRepeatedly(InvokeWithoutArgs([&run_loop, &loop_count] {
  125. if (loop_count++ == 5) {
  126. run_loop.Quit();
  127. }
  128. }));
  129. // We start with one valid frame, then the rest should be update requests.
  130. video_stream.InsertVideoFrame(client_.CreateVideoFrame());
  131. run_loop.Run();
  132. }
  133. task_environment_.RunUntilIdle();
  134. }
  135. // Test the audio streaming pipeline.
  136. TEST_F(RtpStreamTest, AudioStreaming) {
  137. // Create audio data.
  138. const base::TimeDelta kDuration = base::Milliseconds(10);
  139. media::cast::FrameSenderConfig audio_config =
  140. media::cast::GetDefaultAudioSenderConfig();
  141. std::unique_ptr<media::AudioBus> audio_bus =
  142. TestAudioBusFactory(audio_config.channels, audio_config.rtp_timebase,
  143. TestAudioBusFactory::kMiddleANoteFreq, 0.5f)
  144. .NextAudioBus(kDuration);
  145. auto audio_sender = std::make_unique<media::cast::AudioSender>(
  146. cast_environment_, audio_config, base::DoNothing(), &transport_);
  147. AudioRtpStream audio_stream(std::move(audio_sender), client_.GetWeakPtr());
  148. {
  149. base::RunLoop run_loop;
  150. // Expect the audio data is sent to audio sender for encoding, and the
  151. // encoded frame is sent to the transport.
  152. EXPECT_CALL(transport_, InsertFrame(_, _))
  153. .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit));
  154. audio_stream.InsertAudio(std::move(audio_bus), testing_clock_.NowTicks());
  155. run_loop.Run();
  156. }
  157. task_environment_.RunUntilIdle();
  158. }
  159. } // namespace mirroring