blob: e65bc8c7edaee70f99b404782e170864642b0739 [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::_;
20using ::testing::InSequence;
21using ::testing::Mock;
22using ::testing::SaveArg;
23using ::testing::StrictMock;
24
25namespace webrtc {
26namespace {
27
28class VideoRtpReceiverTest : public testing::Test {
29 protected:
30 class MockVideoMediaChannel : public cricket::FakeVideoMediaChannel {
31 public:
Tommic9625f02021-05-06 22:03:19 +020032 MockVideoMediaChannel(
33 cricket::FakeVideoEngine* engine,
34 const cricket::VideoOptions& options,
35 TaskQueueBase* network_thread = rtc::Thread::Current())
36 : FakeVideoMediaChannel(engine, options, network_thread) {}
Danil Chapovalov3a353122020-05-15 11:16:53 +020037 MOCK_METHOD(void,
38 SetRecordableEncodedFrameCallback,
39 (uint32_t, std::function<void(const RecordableEncodedFrame&)>),
40 (override));
41 MOCK_METHOD(void,
42 ClearRecordableEncodedFrameCallback,
43 (uint32_t),
44 (override));
45 MOCK_METHOD(void, GenerateKeyFrame, (uint32_t), (override));
Markus Handell9c27ed22019-12-04 12:57:58 +010046 };
47
48 class MockVideoSink : public rtc::VideoSinkInterface<RecordableEncodedFrame> {
49 public:
Danil Chapovalov3a353122020-05-15 11:16:53 +020050 MOCK_METHOD(void, OnFrame, (const RecordableEncodedFrame&), (override));
Markus Handell9c27ed22019-12-04 12:57:58 +010051 };
52
53 VideoRtpReceiverTest()
54 : worker_thread_(rtc::Thread::Create()),
55 channel_(nullptr, cricket::VideoOptions()),
56 receiver_(new VideoRtpReceiver(worker_thread_.get(),
57 "receiver",
58 {"stream"})) {
59 worker_thread_->Start();
60 receiver_->SetMediaChannel(&channel_);
61 }
62
63 webrtc::VideoTrackSourceInterface* Source() {
64 return receiver_->streams()[0]->FindVideoTrack("receiver")->GetSource();
65 }
66
67 std::unique_ptr<rtc::Thread> worker_thread_;
68 MockVideoMediaChannel channel_;
69 rtc::scoped_refptr<VideoRtpReceiver> receiver_;
70};
71
72TEST_F(VideoRtpReceiverTest, SupportsEncodedOutput) {
73 EXPECT_TRUE(Source()->SupportsEncodedOutput());
74}
75
76TEST_F(VideoRtpReceiverTest, GeneratesKeyFrame) {
77 EXPECT_CALL(channel_, GenerateKeyFrame(0));
78 Source()->GenerateKeyFrame();
79}
80
81TEST_F(VideoRtpReceiverTest,
82 GenerateKeyFrameOnChannelSwitchUnlessGenerateKeyframeCalled) {
83 // A channel switch without previous call to GenerateKeyFrame shouldn't
84 // cause a call to happen on the new channel.
85 MockVideoMediaChannel channel2(nullptr, cricket::VideoOptions());
86 EXPECT_CALL(channel_, GenerateKeyFrame).Times(0);
87 EXPECT_CALL(channel2, GenerateKeyFrame).Times(0);
88 receiver_->SetMediaChannel(&channel2);
89 Mock::VerifyAndClearExpectations(&channel2);
90
91 // Generate a key frame. When we switch channel next time, we will have to
92 // re-generate it as we don't know if it was eventually received
93 Source()->GenerateKeyFrame();
94 MockVideoMediaChannel channel3(nullptr, cricket::VideoOptions());
95 EXPECT_CALL(channel3, GenerateKeyFrame);
96 receiver_->SetMediaChannel(&channel3);
97
98 // Switching to a new channel should now not cause calls to GenerateKeyFrame.
99 StrictMock<MockVideoMediaChannel> channel4(nullptr, cricket::VideoOptions());
100 receiver_->SetMediaChannel(&channel4);
101}
102
103TEST_F(VideoRtpReceiverTest, EnablesEncodedOutput) {
104 EXPECT_CALL(channel_, SetRecordableEncodedFrameCallback(/*ssrc=*/0, _));
105 EXPECT_CALL(channel_, ClearRecordableEncodedFrameCallback).Times(0);
106 MockVideoSink sink;
107 Source()->AddEncodedSink(&sink);
108}
109
110TEST_F(VideoRtpReceiverTest, DisablesEncodedOutput) {
111 EXPECT_CALL(channel_, ClearRecordableEncodedFrameCallback(/*ssrc=*/0));
112 MockVideoSink sink;
113 Source()->AddEncodedSink(&sink);
114 Source()->RemoveEncodedSink(&sink);
115}
116
117TEST_F(VideoRtpReceiverTest, DisablesEnablesEncodedOutputOnChannelSwitch) {
118 InSequence s;
119 EXPECT_CALL(channel_, SetRecordableEncodedFrameCallback);
120 EXPECT_CALL(channel_, ClearRecordableEncodedFrameCallback);
121 MockVideoSink sink;
122 Source()->AddEncodedSink(&sink);
123 MockVideoMediaChannel channel2(nullptr, cricket::VideoOptions());
124 EXPECT_CALL(channel2, SetRecordableEncodedFrameCallback);
125 receiver_->SetMediaChannel(&channel2);
126 Mock::VerifyAndClearExpectations(&channel2);
127
128 // When clearing encoded frame buffer function, we need channel switches
129 // to NOT set the callback again.
130 EXPECT_CALL(channel2, ClearRecordableEncodedFrameCallback);
131 Source()->RemoveEncodedSink(&sink);
132 StrictMock<MockVideoMediaChannel> channel3(nullptr, cricket::VideoOptions());
133 receiver_->SetMediaChannel(&channel3);
134}
135
136TEST_F(VideoRtpReceiverTest, BroadcastsEncodedFramesWhenEnabled) {
137 std::function<void(const RecordableEncodedFrame&)> broadcast;
138 EXPECT_CALL(channel_, SetRecordableEncodedFrameCallback(_, _))
139 .WillRepeatedly(SaveArg<1>(&broadcast));
140 MockVideoSink sink;
141 Source()->AddEncodedSink(&sink);
142
143 // Make sure SetEncodedFrameBufferFunction completes.
144 Mock::VerifyAndClearExpectations(&channel_);
145
146 // Pass two frames on different contexts.
147 EXPECT_CALL(sink, OnFrame).Times(2);
148 MockRecordableEncodedFrame frame;
149 broadcast(frame);
150 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] { broadcast(frame); });
151}
152
153TEST_F(VideoRtpReceiverTest, EnablesEncodedOutputOnChannelRestart) {
154 InSequence s;
155 EXPECT_CALL(channel_, ClearRecordableEncodedFrameCallback(0));
156 MockVideoSink sink;
157 Source()->AddEncodedSink(&sink);
158 EXPECT_CALL(channel_, SetRecordableEncodedFrameCallback(4711, _));
159 receiver_->SetupMediaChannel(4711);
160 EXPECT_CALL(channel_, ClearRecordableEncodedFrameCallback(4711));
161 EXPECT_CALL(channel_, SetRecordableEncodedFrameCallback(0, _));
162 receiver_->SetupUnsignaledMediaChannel();
163}
164
165} // namespace
166} // namespace webrtc