blob: dd527bf59bb3595bb2bd66f8cc32ddeabd2d2dc8 [file] [log] [blame]
Markus Handell15f2ff42019-11-22 10:34:37 +01001/*
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 09:30:08 +010020class MockCallback : public VideoRtpTrackSource::Callback {
21 public:
22 MOCK_METHOD0(OnGenerateKeyFrame, void());
23 MOCK_METHOD1(OnEncodedSinkEnabled, void(bool));
24};
25
26class MockSink : public rtc::VideoSinkInterface<RecordableEncodedFrame> {
27 public:
28 MOCK_METHOD1(OnFrame, void(const RecordableEncodedFrame&));
29};
30
31rtc::scoped_refptr<VideoRtpTrackSource> MakeSource(
32 VideoRtpTrackSource::Callback* callback) {
Markus Handell15f2ff42019-11-22 10:34:37 +010033 rtc::scoped_refptr<VideoRtpTrackSource> source(
Markus Handelld5e2f212019-11-26 09:30:08 +010034 new rtc::RefCountedObject<VideoRtpTrackSource>(callback));
35 return source;
36}
37
38TEST(VideoRtpTrackSourceTest, CreatesWithRemoteAtttributeSet) {
39 EXPECT_TRUE(MakeSource(nullptr)->remote());
40}
41
42TEST(VideoRtpTrackSourceTest, EnablesEncodingOutputOnAddingSink) {
43 MockCallback mock_callback;
44 EXPECT_CALL(mock_callback, OnGenerateKeyFrame).Times(0);
45 auto source = MakeSource(&mock_callback);
46 MockSink sink;
47 EXPECT_CALL(mock_callback, OnEncodedSinkEnabled(true));
48 source->AddEncodedSink(&sink);
49}
50
51TEST(VideoRtpTrackSourceTest, EnablesEncodingOutputOnceOnAddingTwoSinks) {
52 MockCallback mock_callback;
53 EXPECT_CALL(mock_callback, OnGenerateKeyFrame).Times(0);
54 auto source = MakeSource(&mock_callback);
55 MockSink sink;
56 EXPECT_CALL(mock_callback, OnEncodedSinkEnabled(true)).Times(1);
57 source->AddEncodedSink(&sink);
58 MockSink sink2;
59 source->AddEncodedSink(&sink2);
60}
61
62TEST(VideoRtpTrackSourceTest, DisablesEncodingOutputOnOneSinkRemoved) {
63 MockCallback mock_callback;
64 EXPECT_CALL(mock_callback, OnGenerateKeyFrame).Times(0);
65 EXPECT_CALL(mock_callback, OnEncodedSinkEnabled(true));
66 EXPECT_CALL(mock_callback, OnEncodedSinkEnabled(false)).Times(0);
67 auto source = MakeSource(&mock_callback);
68 MockSink sink;
69 source->AddEncodedSink(&sink);
70 testing::Mock::VerifyAndClearExpectations(&mock_callback);
71 EXPECT_CALL(mock_callback, OnEncodedSinkEnabled(false));
72 source->RemoveEncodedSink(&sink);
73}
74
75TEST(VideoRtpTrackSourceTest, DisablesEncodingOutputOnLastSinkRemoved) {
76 MockCallback mock_callback;
77 EXPECT_CALL(mock_callback, OnGenerateKeyFrame).Times(0);
78 EXPECT_CALL(mock_callback, OnEncodedSinkEnabled(true));
79 auto source = MakeSource(&mock_callback);
80 MockSink sink;
81 source->AddEncodedSink(&sink);
82 MockSink sink2;
83 source->AddEncodedSink(&sink2);
84 source->RemoveEncodedSink(&sink);
85 testing::Mock::VerifyAndClearExpectations(&mock_callback);
86 EXPECT_CALL(mock_callback, OnEncodedSinkEnabled(false));
87 source->RemoveEncodedSink(&sink2);
88}
89
90TEST(VideoRtpTrackSourceTest, GeneratesKeyFrameWhenRequested) {
91 MockCallback mock_callback;
92 auto source = MakeSource(&mock_callback);
93 EXPECT_CALL(mock_callback, OnGenerateKeyFrame);
94 source->GenerateKeyFrame();
95}
96
97TEST(VideoRtpTrackSourceTest, NoCallbacksAfterClearedCallback) {
98 testing::StrictMock<MockCallback> mock_callback;
99 auto source = MakeSource(&mock_callback);
100 source->ClearCallback();
101 MockSink sink;
102 source->AddEncodedSink(&sink);
103 source->GenerateKeyFrame();
104 source->RemoveEncodedSink(&sink);
105}
106
107class TestFrame : public RecordableEncodedFrame {
108 public:
109 rtc::scoped_refptr<const webrtc::EncodedImageBufferInterface> encoded_buffer()
110 const override {
111 return nullptr;
112 }
113 absl::optional<webrtc::ColorSpace> color_space() const override {
114 return absl::nullopt;
115 }
116 VideoCodecType codec() const override { return kVideoCodecGeneric; }
117 bool is_key_frame() const override { return false; }
118 EncodedResolution resolution() const override {
119 return EncodedResolution{0, 0};
120 }
121 Timestamp render_time() const override { return Timestamp::ms(0); }
122};
123
124TEST(VideoRtpTrackSourceTest, BroadcastsFrames) {
125 auto source = MakeSource(nullptr);
126 MockSink sink;
127 source->AddEncodedSink(&sink);
128 MockSink sink2;
129 source->AddEncodedSink(&sink2);
130 TestFrame frame;
131 EXPECT_CALL(sink, OnFrame);
132 EXPECT_CALL(sink2, OnFrame);
133 source->BroadcastRecordableEncodedFrame(frame);
Markus Handell15f2ff42019-11-22 10:34:37 +0100134}
135
136} // namespace
137} // namespace webrtc