Markus Handell | 9c27ed2 | 2019-12-04 12:57:58 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "pc/video_rtp_receiver.h" |
| 12 | |
| 13 | #include <memory> |
| 14 | |
| 15 | #include "api/video/test/mock_recordable_encoded_frame.h" |
| 16 | #include "media/base/fake_media_engine.h" |
| 17 | #include "test/gmock.h" |
| 18 | |
| 19 | using ::testing::_; |
Tommi | 4ccdf93 | 2021-05-17 14:50:10 +0200 | [diff] [blame^] | 20 | using ::testing::AnyNumber; |
Markus Handell | 9c27ed2 | 2019-12-04 12:57:58 +0100 | [diff] [blame] | 21 | using ::testing::InSequence; |
| 22 | using ::testing::Mock; |
Tommi | 4ccdf93 | 2021-05-17 14:50:10 +0200 | [diff] [blame^] | 23 | using ::testing::NiceMock; |
Markus Handell | 9c27ed2 | 2019-12-04 12:57:58 +0100 | [diff] [blame] | 24 | using ::testing::SaveArg; |
| 25 | using ::testing::StrictMock; |
| 26 | |
| 27 | namespace webrtc { |
| 28 | namespace { |
| 29 | |
| 30 | class VideoRtpReceiverTest : public testing::Test { |
| 31 | protected: |
| 32 | class MockVideoMediaChannel : public cricket::FakeVideoMediaChannel { |
| 33 | public: |
Tommi | c9625f0 | 2021-05-06 22:03:19 +0200 | [diff] [blame] | 34 | MockVideoMediaChannel( |
| 35 | cricket::FakeVideoEngine* engine, |
| 36 | const cricket::VideoOptions& options, |
| 37 | TaskQueueBase* network_thread = rtc::Thread::Current()) |
| 38 | : FakeVideoMediaChannel(engine, options, network_thread) {} |
Danil Chapovalov | 3a35312 | 2020-05-15 11:16:53 +0200 | [diff] [blame] | 39 | MOCK_METHOD(void, |
| 40 | SetRecordableEncodedFrameCallback, |
| 41 | (uint32_t, std::function<void(const RecordableEncodedFrame&)>), |
| 42 | (override)); |
| 43 | MOCK_METHOD(void, |
| 44 | ClearRecordableEncodedFrameCallback, |
| 45 | (uint32_t), |
| 46 | (override)); |
| 47 | MOCK_METHOD(void, GenerateKeyFrame, (uint32_t), (override)); |
Markus Handell | 9c27ed2 | 2019-12-04 12:57:58 +0100 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | class MockVideoSink : public rtc::VideoSinkInterface<RecordableEncodedFrame> { |
| 51 | public: |
Danil Chapovalov | 3a35312 | 2020-05-15 11:16:53 +0200 | [diff] [blame] | 52 | MOCK_METHOD(void, OnFrame, (const RecordableEncodedFrame&), (override)); |
Markus Handell | 9c27ed2 | 2019-12-04 12:57:58 +0100 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | VideoRtpReceiverTest() |
| 56 | : worker_thread_(rtc::Thread::Create()), |
| 57 | channel_(nullptr, cricket::VideoOptions()), |
Tommi | 4ccdf93 | 2021-05-17 14:50:10 +0200 | [diff] [blame^] | 58 | receiver_(rtc::make_ref_counted<VideoRtpReceiver>( |
| 59 | worker_thread_.get(), |
| 60 | std::string("receiver"), |
| 61 | std::vector<std::string>({"stream"}))) { |
Markus Handell | 9c27ed2 | 2019-12-04 12:57:58 +0100 | [diff] [blame] | 62 | worker_thread_->Start(); |
| 63 | receiver_->SetMediaChannel(&channel_); |
| 64 | } |
| 65 | |
Tommi | 4ccdf93 | 2021-05-17 14:50:10 +0200 | [diff] [blame^] | 66 | ~VideoRtpReceiverTest() override { |
| 67 | // Clear expectations that tests may have set up before calling Stop(). |
| 68 | Mock::VerifyAndClearExpectations(&channel_); |
| 69 | receiver_->Stop(); |
| 70 | } |
| 71 | |
Markus Handell | 9c27ed2 | 2019-12-04 12:57:58 +0100 | [diff] [blame] | 72 | webrtc::VideoTrackSourceInterface* Source() { |
| 73 | return receiver_->streams()[0]->FindVideoTrack("receiver")->GetSource(); |
| 74 | } |
| 75 | |
| 76 | std::unique_ptr<rtc::Thread> worker_thread_; |
Tommi | 4ccdf93 | 2021-05-17 14:50:10 +0200 | [diff] [blame^] | 77 | NiceMock<MockVideoMediaChannel> channel_; |
Markus Handell | 9c27ed2 | 2019-12-04 12:57:58 +0100 | [diff] [blame] | 78 | rtc::scoped_refptr<VideoRtpReceiver> receiver_; |
| 79 | }; |
| 80 | |
| 81 | TEST_F(VideoRtpReceiverTest, SupportsEncodedOutput) { |
| 82 | EXPECT_TRUE(Source()->SupportsEncodedOutput()); |
| 83 | } |
| 84 | |
| 85 | TEST_F(VideoRtpReceiverTest, GeneratesKeyFrame) { |
| 86 | EXPECT_CALL(channel_, GenerateKeyFrame(0)); |
| 87 | Source()->GenerateKeyFrame(); |
| 88 | } |
| 89 | |
| 90 | TEST_F(VideoRtpReceiverTest, |
| 91 | GenerateKeyFrameOnChannelSwitchUnlessGenerateKeyframeCalled) { |
| 92 | // A channel switch without previous call to GenerateKeyFrame shouldn't |
| 93 | // cause a call to happen on the new channel. |
| 94 | MockVideoMediaChannel channel2(nullptr, cricket::VideoOptions()); |
| 95 | EXPECT_CALL(channel_, GenerateKeyFrame).Times(0); |
| 96 | EXPECT_CALL(channel2, GenerateKeyFrame).Times(0); |
| 97 | receiver_->SetMediaChannel(&channel2); |
| 98 | Mock::VerifyAndClearExpectations(&channel2); |
| 99 | |
| 100 | // Generate a key frame. When we switch channel next time, we will have to |
| 101 | // re-generate it as we don't know if it was eventually received |
| 102 | Source()->GenerateKeyFrame(); |
| 103 | MockVideoMediaChannel channel3(nullptr, cricket::VideoOptions()); |
| 104 | EXPECT_CALL(channel3, GenerateKeyFrame); |
| 105 | receiver_->SetMediaChannel(&channel3); |
| 106 | |
| 107 | // Switching to a new channel should now not cause calls to GenerateKeyFrame. |
| 108 | StrictMock<MockVideoMediaChannel> channel4(nullptr, cricket::VideoOptions()); |
| 109 | receiver_->SetMediaChannel(&channel4); |
Tommi | 4ccdf93 | 2021-05-17 14:50:10 +0200 | [diff] [blame^] | 110 | |
| 111 | // We must call Stop() here since the mock media channels live on the stack |
| 112 | // and `receiver_` still has a pointer to those objects. |
| 113 | receiver_->Stop(); |
Markus Handell | 9c27ed2 | 2019-12-04 12:57:58 +0100 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | TEST_F(VideoRtpReceiverTest, EnablesEncodedOutput) { |
| 117 | EXPECT_CALL(channel_, SetRecordableEncodedFrameCallback(/*ssrc=*/0, _)); |
| 118 | EXPECT_CALL(channel_, ClearRecordableEncodedFrameCallback).Times(0); |
| 119 | MockVideoSink sink; |
| 120 | Source()->AddEncodedSink(&sink); |
| 121 | } |
| 122 | |
| 123 | TEST_F(VideoRtpReceiverTest, DisablesEncodedOutput) { |
| 124 | EXPECT_CALL(channel_, ClearRecordableEncodedFrameCallback(/*ssrc=*/0)); |
| 125 | MockVideoSink sink; |
| 126 | Source()->AddEncodedSink(&sink); |
| 127 | Source()->RemoveEncodedSink(&sink); |
| 128 | } |
| 129 | |
| 130 | TEST_F(VideoRtpReceiverTest, DisablesEnablesEncodedOutputOnChannelSwitch) { |
| 131 | InSequence s; |
| 132 | EXPECT_CALL(channel_, SetRecordableEncodedFrameCallback); |
| 133 | EXPECT_CALL(channel_, ClearRecordableEncodedFrameCallback); |
| 134 | MockVideoSink sink; |
| 135 | Source()->AddEncodedSink(&sink); |
| 136 | MockVideoMediaChannel channel2(nullptr, cricket::VideoOptions()); |
| 137 | EXPECT_CALL(channel2, SetRecordableEncodedFrameCallback); |
| 138 | receiver_->SetMediaChannel(&channel2); |
| 139 | Mock::VerifyAndClearExpectations(&channel2); |
| 140 | |
| 141 | // When clearing encoded frame buffer function, we need channel switches |
| 142 | // to NOT set the callback again. |
| 143 | EXPECT_CALL(channel2, ClearRecordableEncodedFrameCallback); |
| 144 | Source()->RemoveEncodedSink(&sink); |
| 145 | StrictMock<MockVideoMediaChannel> channel3(nullptr, cricket::VideoOptions()); |
| 146 | receiver_->SetMediaChannel(&channel3); |
Tommi | 4ccdf93 | 2021-05-17 14:50:10 +0200 | [diff] [blame^] | 147 | |
| 148 | // We must call Stop() here since the mock media channels live on the stack |
| 149 | // and `receiver_` still has a pointer to those objects. |
| 150 | receiver_->Stop(); |
Markus Handell | 9c27ed2 | 2019-12-04 12:57:58 +0100 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | TEST_F(VideoRtpReceiverTest, BroadcastsEncodedFramesWhenEnabled) { |
| 154 | std::function<void(const RecordableEncodedFrame&)> broadcast; |
| 155 | EXPECT_CALL(channel_, SetRecordableEncodedFrameCallback(_, _)) |
| 156 | .WillRepeatedly(SaveArg<1>(&broadcast)); |
| 157 | MockVideoSink sink; |
| 158 | Source()->AddEncodedSink(&sink); |
| 159 | |
| 160 | // Make sure SetEncodedFrameBufferFunction completes. |
| 161 | Mock::VerifyAndClearExpectations(&channel_); |
| 162 | |
| 163 | // Pass two frames on different contexts. |
| 164 | EXPECT_CALL(sink, OnFrame).Times(2); |
| 165 | MockRecordableEncodedFrame frame; |
| 166 | broadcast(frame); |
| 167 | worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] { broadcast(frame); }); |
| 168 | } |
| 169 | |
| 170 | TEST_F(VideoRtpReceiverTest, EnablesEncodedOutputOnChannelRestart) { |
| 171 | InSequence s; |
| 172 | EXPECT_CALL(channel_, ClearRecordableEncodedFrameCallback(0)); |
| 173 | MockVideoSink sink; |
| 174 | Source()->AddEncodedSink(&sink); |
| 175 | EXPECT_CALL(channel_, SetRecordableEncodedFrameCallback(4711, _)); |
| 176 | receiver_->SetupMediaChannel(4711); |
| 177 | EXPECT_CALL(channel_, ClearRecordableEncodedFrameCallback(4711)); |
| 178 | EXPECT_CALL(channel_, SetRecordableEncodedFrameCallback(0, _)); |
| 179 | receiver_->SetupUnsignaledMediaChannel(); |
| 180 | } |
| 181 | |
| 182 | } // namespace |
| 183 | } // namespace webrtc |