blob: 3ec9a2829565b7481d605c881c016a05f3eb0a30 [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
Harald Alvestrandc24a2182022-02-23 13:44:59 +000013#include <functional>
Markus Handell9c27ed22019-12-04 12:57:58 +010014#include <memory>
15
Harald Alvestrandc24a2182022-02-23 13:44:59 +000016#include "api/task_queue/task_queue_base.h"
17#include "api/video/recordable_encoded_frame.h"
Markus Handell9c27ed22019-12-04 12:57:58 +010018#include "api/video/test/mock_recordable_encoded_frame.h"
19#include "media/base/fake_media_engine.h"
Danil Chapovalov2aaef452022-08-12 15:55:11 +020020#include "rtc_base/task_queue_for_test.h"
Markus Handell9c27ed22019-12-04 12:57:58 +010021#include "test/gmock.h"
Harald Alvestrandc24a2182022-02-23 13:44:59 +000022#include "test/gtest.h"
Markus Handell9c27ed22019-12-04 12:57:58 +010023
24using ::testing::_;
Tommi4ccdf932021-05-17 14:50:10 +020025using ::testing::AnyNumber;
Markus Handell9c27ed22019-12-04 12:57:58 +010026using ::testing::InSequence;
27using ::testing::Mock;
Tommi4ccdf932021-05-17 14:50:10 +020028using ::testing::NiceMock;
Markus Handell9c27ed22019-12-04 12:57:58 +010029using ::testing::SaveArg;
30using ::testing::StrictMock;
31
32namespace webrtc {
33namespace {
34
35class VideoRtpReceiverTest : public testing::Test {
36 protected:
37 class MockVideoMediaChannel : public cricket::FakeVideoMediaChannel {
38 public:
Tommic9625f02021-05-06 22:03:19 +020039 MockVideoMediaChannel(
40 cricket::FakeVideoEngine* engine,
41 const cricket::VideoOptions& options,
42 TaskQueueBase* network_thread = rtc::Thread::Current())
43 : FakeVideoMediaChannel(engine, options, network_thread) {}
Danil Chapovalov3a353122020-05-15 11:16:53 +020044 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 Hancked237c2b2022-10-25 09:54:28 +020052 MOCK_METHOD(void, RequestRecvKeyFrame, (uint32_t), (override));
Philipp Hanckea1b4eb22022-11-04 14:45:23 +010053 MOCK_METHOD(void,
54 GenerateSendKeyFrame,
55 (uint32_t, const std::vector<std::string>&),
56 (override));
Markus Handell9c27ed22019-12-04 12:57:58 +010057 };
58
59 class MockVideoSink : public rtc::VideoSinkInterface<RecordableEncodedFrame> {
60 public:
Danil Chapovalov3a353122020-05-15 11:16:53 +020061 MOCK_METHOD(void, OnFrame, (const RecordableEncodedFrame&), (override));
Markus Handell9c27ed22019-12-04 12:57:58 +010062 };
63
64 VideoRtpReceiverTest()
65 : worker_thread_(rtc::Thread::Create()),
66 channel_(nullptr, cricket::VideoOptions()),
Tommi4ccdf932021-05-17 14:50:10 +020067 receiver_(rtc::make_ref_counted<VideoRtpReceiver>(
68 worker_thread_.get(),
69 std::string("receiver"),
70 std::vector<std::string>({"stream"}))) {
Markus Handell9c27ed22019-12-04 12:57:58 +010071 worker_thread_->Start();
Tommi6589def2022-02-17 23:36:47 +010072 SetMediaChannel(&channel_);
Markus Handell9c27ed22019-12-04 12:57:58 +010073 }
74
Tommi4ccdf932021-05-17 14:50:10 +020075 ~VideoRtpReceiverTest() override {
Tommi6589def2022-02-17 23:36:47 +010076 // Clear expectations that tests may have set up before calling
77 // SetMediaChannel(nullptr).
Tommi4ccdf932021-05-17 14:50:10 +020078 Mock::VerifyAndClearExpectations(&channel_);
79 receiver_->Stop();
Tommi6589def2022-02-17 23:36:47 +010080 SetMediaChannel(nullptr);
81 }
82
83 void SetMediaChannel(cricket::MediaChannel* media_channel) {
Danil Chapovalov2aaef452022-08-12 15:55:11 +020084 SendTask(worker_thread_.get(),
85 [&]() { receiver_->SetMediaChannel(media_channel); });
Tommi4ccdf932021-05-17 14:50:10 +020086 }
87
Markus Handell9c27ed22019-12-04 12:57:58 +010088 webrtc::VideoTrackSourceInterface* Source() {
89 return receiver_->streams()[0]->FindVideoTrack("receiver")->GetSource();
90 }
91
Niels Möller83830f32022-05-20 09:12:57 +020092 rtc::AutoThread main_thread_;
Markus Handell9c27ed22019-12-04 12:57:58 +010093 std::unique_ptr<rtc::Thread> worker_thread_;
Tommi4ccdf932021-05-17 14:50:10 +020094 NiceMock<MockVideoMediaChannel> channel_;
Markus Handell9c27ed22019-12-04 12:57:58 +010095 rtc::scoped_refptr<VideoRtpReceiver> receiver_;
96};
97
98TEST_F(VideoRtpReceiverTest, SupportsEncodedOutput) {
99 EXPECT_TRUE(Source()->SupportsEncodedOutput());
100}
101
102TEST_F(VideoRtpReceiverTest, GeneratesKeyFrame) {
Philipp Hancked237c2b2022-10-25 09:54:28 +0200103 EXPECT_CALL(channel_, RequestRecvKeyFrame(0));
Markus Handell9c27ed22019-12-04 12:57:58 +0100104 Source()->GenerateKeyFrame();
105}
106
107TEST_F(VideoRtpReceiverTest,
108 GenerateKeyFrameOnChannelSwitchUnlessGenerateKeyframeCalled) {
109 // A channel switch without previous call to GenerateKeyFrame shouldn't
110 // cause a call to happen on the new channel.
111 MockVideoMediaChannel channel2(nullptr, cricket::VideoOptions());
Philipp Hancked237c2b2022-10-25 09:54:28 +0200112 EXPECT_CALL(channel_, RequestRecvKeyFrame).Times(0);
113 EXPECT_CALL(channel2, RequestRecvKeyFrame).Times(0);
Tommi6589def2022-02-17 23:36:47 +0100114 SetMediaChannel(&channel2);
Markus Handell9c27ed22019-12-04 12:57:58 +0100115 Mock::VerifyAndClearExpectations(&channel2);
116
117 // Generate a key frame. When we switch channel next time, we will have to
118 // re-generate it as we don't know if it was eventually received
Philipp Hancked237c2b2022-10-25 09:54:28 +0200119 EXPECT_CALL(channel2, RequestRecvKeyFrame).Times(1);
Markus Handell9c27ed22019-12-04 12:57:58 +0100120 Source()->GenerateKeyFrame();
121 MockVideoMediaChannel channel3(nullptr, cricket::VideoOptions());
Philipp Hancked237c2b2022-10-25 09:54:28 +0200122 EXPECT_CALL(channel3, RequestRecvKeyFrame);
Tommi6589def2022-02-17 23:36:47 +0100123 SetMediaChannel(&channel3);
Markus Handell9c27ed22019-12-04 12:57:58 +0100124
125 // Switching to a new channel should now not cause calls to GenerateKeyFrame.
126 StrictMock<MockVideoMediaChannel> channel4(nullptr, cricket::VideoOptions());
Tommi6589def2022-02-17 23:36:47 +0100127 SetMediaChannel(&channel4);
Tommi4ccdf932021-05-17 14:50:10 +0200128
Tommi6589def2022-02-17 23:36:47 +0100129 // We must call SetMediaChannel(nullptr) here since the mock media channels
130 // live on the stack and `receiver_` still has a pointer to those objects.
131 SetMediaChannel(nullptr);
Markus Handell9c27ed22019-12-04 12:57:58 +0100132}
133
134TEST_F(VideoRtpReceiverTest, EnablesEncodedOutput) {
135 EXPECT_CALL(channel_, SetRecordableEncodedFrameCallback(/*ssrc=*/0, _));
136 EXPECT_CALL(channel_, ClearRecordableEncodedFrameCallback).Times(0);
137 MockVideoSink sink;
138 Source()->AddEncodedSink(&sink);
139}
140
141TEST_F(VideoRtpReceiverTest, DisablesEncodedOutput) {
142 EXPECT_CALL(channel_, ClearRecordableEncodedFrameCallback(/*ssrc=*/0));
143 MockVideoSink sink;
144 Source()->AddEncodedSink(&sink);
145 Source()->RemoveEncodedSink(&sink);
146}
147
148TEST_F(VideoRtpReceiverTest, DisablesEnablesEncodedOutputOnChannelSwitch) {
149 InSequence s;
150 EXPECT_CALL(channel_, SetRecordableEncodedFrameCallback);
151 EXPECT_CALL(channel_, ClearRecordableEncodedFrameCallback);
152 MockVideoSink sink;
153 Source()->AddEncodedSink(&sink);
154 MockVideoMediaChannel channel2(nullptr, cricket::VideoOptions());
155 EXPECT_CALL(channel2, SetRecordableEncodedFrameCallback);
Tommi6589def2022-02-17 23:36:47 +0100156 SetMediaChannel(&channel2);
Markus Handell9c27ed22019-12-04 12:57:58 +0100157 Mock::VerifyAndClearExpectations(&channel2);
158
159 // When clearing encoded frame buffer function, we need channel switches
160 // to NOT set the callback again.
161 EXPECT_CALL(channel2, ClearRecordableEncodedFrameCallback);
162 Source()->RemoveEncodedSink(&sink);
163 StrictMock<MockVideoMediaChannel> channel3(nullptr, cricket::VideoOptions());
Tommi6589def2022-02-17 23:36:47 +0100164 SetMediaChannel(&channel3);
Tommi4ccdf932021-05-17 14:50:10 +0200165
Tommi6589def2022-02-17 23:36:47 +0100166 // We must call SetMediaChannel(nullptr) here since the mock media channels
167 // live on the stack and `receiver_` still has a pointer to those objects.
168 SetMediaChannel(nullptr);
Markus Handell9c27ed22019-12-04 12:57:58 +0100169}
170
171TEST_F(VideoRtpReceiverTest, BroadcastsEncodedFramesWhenEnabled) {
172 std::function<void(const RecordableEncodedFrame&)> broadcast;
173 EXPECT_CALL(channel_, SetRecordableEncodedFrameCallback(_, _))
174 .WillRepeatedly(SaveArg<1>(&broadcast));
175 MockVideoSink sink;
176 Source()->AddEncodedSink(&sink);
177
178 // Make sure SetEncodedFrameBufferFunction completes.
179 Mock::VerifyAndClearExpectations(&channel_);
180
181 // Pass two frames on different contexts.
182 EXPECT_CALL(sink, OnFrame).Times(2);
183 MockRecordableEncodedFrame frame;
184 broadcast(frame);
Danil Chapovalov2aaef452022-08-12 15:55:11 +0200185 SendTask(worker_thread_.get(), [&] { broadcast(frame); });
Markus Handell9c27ed22019-12-04 12:57:58 +0100186}
187
188TEST_F(VideoRtpReceiverTest, EnablesEncodedOutputOnChannelRestart) {
189 InSequence s;
Markus Handell9c27ed22019-12-04 12:57:58 +0100190 MockVideoSink sink;
191 Source()->AddEncodedSink(&sink);
192 EXPECT_CALL(channel_, SetRecordableEncodedFrameCallback(4711, _));
193 receiver_->SetupMediaChannel(4711);
194 EXPECT_CALL(channel_, ClearRecordableEncodedFrameCallback(4711));
195 EXPECT_CALL(channel_, SetRecordableEncodedFrameCallback(0, _));
196 receiver_->SetupUnsignaledMediaChannel();
197}
198
199} // namespace
200} // namespace webrtc