blob: 3a8099d30f96d2f10cdd70ae296a08e858292db7 [file] [log] [blame]
Markus Handell9c27ed22019-12-04 12:57:58 +01001/*
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
19using ::testing::_;
Tommi4ccdf932021-05-17 14:50:10 +020020using ::testing::AnyNumber;
Markus Handell9c27ed22019-12-04 12:57:58 +010021using ::testing::InSequence;
22using ::testing::Mock;
Tommi4ccdf932021-05-17 14:50:10 +020023using ::testing::NiceMock;
Markus Handell9c27ed22019-12-04 12:57:58 +010024using ::testing::SaveArg;
25using ::testing::StrictMock;
26
27namespace webrtc {
28namespace {
29
30class VideoRtpReceiverTest : public testing::Test {
31 protected:
32 class MockVideoMediaChannel : public cricket::FakeVideoMediaChannel {
33 public:
Tommic9625f02021-05-06 22:03:19 +020034 MockVideoMediaChannel(
35 cricket::FakeVideoEngine* engine,
36 const cricket::VideoOptions& options,
37 TaskQueueBase* network_thread = rtc::Thread::Current())
38 : FakeVideoMediaChannel(engine, options, network_thread) {}
Danil Chapovalov3a353122020-05-15 11:16:53 +020039 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 Handell9c27ed22019-12-04 12:57:58 +010048 };
49
50 class MockVideoSink : public rtc::VideoSinkInterface<RecordableEncodedFrame> {
51 public:
Danil Chapovalov3a353122020-05-15 11:16:53 +020052 MOCK_METHOD(void, OnFrame, (const RecordableEncodedFrame&), (override));
Markus Handell9c27ed22019-12-04 12:57:58 +010053 };
54
55 VideoRtpReceiverTest()
56 : worker_thread_(rtc::Thread::Create()),
57 channel_(nullptr, cricket::VideoOptions()),
Tommi4ccdf932021-05-17 14:50:10 +020058 receiver_(rtc::make_ref_counted<VideoRtpReceiver>(
59 worker_thread_.get(),
60 std::string("receiver"),
61 std::vector<std::string>({"stream"}))) {
Markus Handell9c27ed22019-12-04 12:57:58 +010062 worker_thread_->Start();
63 receiver_->SetMediaChannel(&channel_);
64 }
65
Tommi4ccdf932021-05-17 14:50:10 +020066 ~VideoRtpReceiverTest() override {
67 // Clear expectations that tests may have set up before calling Stop().
68 Mock::VerifyAndClearExpectations(&channel_);
69 receiver_->Stop();
70 }
71
Markus Handell9c27ed22019-12-04 12:57:58 +010072 webrtc::VideoTrackSourceInterface* Source() {
73 return receiver_->streams()[0]->FindVideoTrack("receiver")->GetSource();
74 }
75
76 std::unique_ptr<rtc::Thread> worker_thread_;
Tommi4ccdf932021-05-17 14:50:10 +020077 NiceMock<MockVideoMediaChannel> channel_;
Markus Handell9c27ed22019-12-04 12:57:58 +010078 rtc::scoped_refptr<VideoRtpReceiver> receiver_;
79};
80
81TEST_F(VideoRtpReceiverTest, SupportsEncodedOutput) {
82 EXPECT_TRUE(Source()->SupportsEncodedOutput());
83}
84
85TEST_F(VideoRtpReceiverTest, GeneratesKeyFrame) {
86 EXPECT_CALL(channel_, GenerateKeyFrame(0));
87 Source()->GenerateKeyFrame();
88}
89
90TEST_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);
Tommi4ccdf932021-05-17 14:50:10 +0200110
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 Handell9c27ed22019-12-04 12:57:58 +0100114}
115
116TEST_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
123TEST_F(VideoRtpReceiverTest, DisablesEncodedOutput) {
124 EXPECT_CALL(channel_, ClearRecordableEncodedFrameCallback(/*ssrc=*/0));
125 MockVideoSink sink;
126 Source()->AddEncodedSink(&sink);
127 Source()->RemoveEncodedSink(&sink);
128}
129
130TEST_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);
Tommi4ccdf932021-05-17 14:50:10 +0200147
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 Handell9c27ed22019-12-04 12:57:58 +0100151}
152
153TEST_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
170TEST_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