blob: 5666b77d5fc01011f344757b23cdf46c8547eaf3 [file] [log] [blame]
Markus Handell15f2ff42019-11-22 09:34:371/*
2 * Copyright (c) 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_track_source.h"
12
13#include "rtc_base/ref_counted_object.h"
14#include "test/gmock.h"
15#include "test/gtest.h"
16
17namespace webrtc {
18namespace {
19
Markus Handelld5e2f212019-11-26 08:30:0820class MockCallback : public VideoRtpTrackSource::Callback {
21 public:
Danil Chapovalov3a353122020-05-15 09:16:5322 MOCK_METHOD(void, OnGenerateKeyFrame, (), (override));
23 MOCK_METHOD(void, OnEncodedSinkEnabled, (bool), (override));
Markus Handelld5e2f212019-11-26 08:30:0824};
25
26class MockSink : public rtc::VideoSinkInterface<RecordableEncodedFrame> {
27 public:
Danil Chapovalov3a353122020-05-15 09:16:5328 MOCK_METHOD(void, OnFrame, (const RecordableEncodedFrame&), (override));
Markus Handelld5e2f212019-11-26 08:30:0829};
30
31rtc::scoped_refptr<VideoRtpTrackSource> MakeSource(
32 VideoRtpTrackSource::Callback* callback) {
Tommi87f70902021-04-27 12:43:0833 return rtc::make_ref_counted<VideoRtpTrackSource>(callback);
Markus Handelld5e2f212019-11-26 08:30:0834}
35
36TEST(VideoRtpTrackSourceTest, CreatesWithRemoteAtttributeSet) {
37 EXPECT_TRUE(MakeSource(nullptr)->remote());
38}
39
40TEST(VideoRtpTrackSourceTest, EnablesEncodingOutputOnAddingSink) {
41 MockCallback mock_callback;
42 EXPECT_CALL(mock_callback, OnGenerateKeyFrame).Times(0);
43 auto source = MakeSource(&mock_callback);
44 MockSink sink;
45 EXPECT_CALL(mock_callback, OnEncodedSinkEnabled(true));
46 source->AddEncodedSink(&sink);
47}
48
49TEST(VideoRtpTrackSourceTest, EnablesEncodingOutputOnceOnAddingTwoSinks) {
50 MockCallback mock_callback;
51 EXPECT_CALL(mock_callback, OnGenerateKeyFrame).Times(0);
52 auto source = MakeSource(&mock_callback);
53 MockSink sink;
54 EXPECT_CALL(mock_callback, OnEncodedSinkEnabled(true)).Times(1);
55 source->AddEncodedSink(&sink);
56 MockSink sink2;
57 source->AddEncodedSink(&sink2);
58}
59
60TEST(VideoRtpTrackSourceTest, DisablesEncodingOutputOnOneSinkRemoved) {
61 MockCallback mock_callback;
62 EXPECT_CALL(mock_callback, OnGenerateKeyFrame).Times(0);
63 EXPECT_CALL(mock_callback, OnEncodedSinkEnabled(true));
64 EXPECT_CALL(mock_callback, OnEncodedSinkEnabled(false)).Times(0);
65 auto source = MakeSource(&mock_callback);
66 MockSink sink;
67 source->AddEncodedSink(&sink);
68 testing::Mock::VerifyAndClearExpectations(&mock_callback);
69 EXPECT_CALL(mock_callback, OnEncodedSinkEnabled(false));
70 source->RemoveEncodedSink(&sink);
71}
72
73TEST(VideoRtpTrackSourceTest, DisablesEncodingOutputOnLastSinkRemoved) {
74 MockCallback mock_callback;
75 EXPECT_CALL(mock_callback, OnGenerateKeyFrame).Times(0);
76 EXPECT_CALL(mock_callback, OnEncodedSinkEnabled(true));
77 auto source = MakeSource(&mock_callback);
78 MockSink sink;
79 source->AddEncodedSink(&sink);
80 MockSink sink2;
81 source->AddEncodedSink(&sink2);
82 source->RemoveEncodedSink(&sink);
83 testing::Mock::VerifyAndClearExpectations(&mock_callback);
84 EXPECT_CALL(mock_callback, OnEncodedSinkEnabled(false));
85 source->RemoveEncodedSink(&sink2);
86}
87
88TEST(VideoRtpTrackSourceTest, GeneratesKeyFrameWhenRequested) {
89 MockCallback mock_callback;
90 auto source = MakeSource(&mock_callback);
91 EXPECT_CALL(mock_callback, OnGenerateKeyFrame);
92 source->GenerateKeyFrame();
93}
94
95TEST(VideoRtpTrackSourceTest, NoCallbacksAfterClearedCallback) {
96 testing::StrictMock<MockCallback> mock_callback;
97 auto source = MakeSource(&mock_callback);
98 source->ClearCallback();
99 MockSink sink;
100 source->AddEncodedSink(&sink);
101 source->GenerateKeyFrame();
102 source->RemoveEncodedSink(&sink);
103}
104
105class TestFrame : public RecordableEncodedFrame {
106 public:
107 rtc::scoped_refptr<const webrtc::EncodedImageBufferInterface> encoded_buffer()
108 const override {
109 return nullptr;
110 }
111 absl::optional<webrtc::ColorSpace> color_space() const override {
112 return absl::nullopt;
113 }
114 VideoCodecType codec() const override { return kVideoCodecGeneric; }
115 bool is_key_frame() const override { return false; }
116 EncodedResolution resolution() const override {
117 return EncodedResolution{0, 0};
118 }
Danil Chapovalov0c626af2020-02-10 10:16:00119 Timestamp render_time() const override { return Timestamp::Millis(0); }
Markus Handelld5e2f212019-11-26 08:30:08120};
121
122TEST(VideoRtpTrackSourceTest, BroadcastsFrames) {
123 auto source = MakeSource(nullptr);
124 MockSink sink;
125 source->AddEncodedSink(&sink);
126 MockSink sink2;
127 source->AddEncodedSink(&sink2);
128 TestFrame frame;
129 EXPECT_CALL(sink, OnFrame);
130 EXPECT_CALL(sink2, OnFrame);
131 source->BroadcastRecordableEncodedFrame(frame);
Markus Handell15f2ff42019-11-22 09:34:37132}
133
134} // namespace
135} // namespace webrtc