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