blob: 7f3316591239e430e21dab8c2ded199b330b9e90 [file] [log] [blame]
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001/*
2 * Copyright (c) 2012 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
12// This file includes unit tests for EncoderStateFeedback.
andresp@webrtc.orgac6d9192013-05-13 10:50:50 +000013#include "webrtc/video_engine/encoder_state_feedback.h"
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000014
pbos@webrtc.org281cff82013-05-17 13:44:48 +000015#include "testing/gmock/include/gmock/gmock.h"
16#include "testing/gtest/include/gtest/gtest.h"
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000017
andresp@webrtc.orgac6d9192013-05-13 10:50:50 +000018#include "webrtc/common.h"
19#include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h"
20#include "webrtc/modules/utility/interface/process_thread.h"
21#include "webrtc/system_wrappers/interface/scoped_ptr.h"
22#include "webrtc/video_engine/vie_encoder.h"
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000023
24namespace webrtc {
25
26// TODO(mflodman) Create a common mock in module utility.
27class TestProcessThread : public ProcessThread {
28 public:
29 TestProcessThread() {}
30 ~TestProcessThread() {}
pbos@webrtc.org67879bc2013-04-09 13:41:51 +000031 virtual int32_t Start() { return 0; }
32 virtual int32_t Stop() { return 0; }
33 virtual int32_t RegisterModule(const Module* module) { return 0; }
34 virtual int32_t DeRegisterModule(const Module* module) { return 0; }
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000035};
36
37class MockVieEncoder : public ViEEncoder {
38 public:
39 explicit MockVieEncoder(TestProcessThread* process_thread)
andresp@webrtc.orgac6d9192013-05-13 10:50:50 +000040 : ViEEncoder(1, 1, 1, config_, *process_thread, NULL) {}
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000041 ~MockVieEncoder() {}
42
43 MOCK_METHOD1(OnReceivedIntraFrameRequest,
44 void(uint32_t));
45 MOCK_METHOD2(OnReceivedSLI,
46 void(uint32_t ssrc, uint8_t picture_id));
47 MOCK_METHOD2(OnReceivedRPSI,
48 void(uint32_t ssrc, uint64_t picture_id));
49 MOCK_METHOD2(OnLocalSsrcChanged,
50 void(uint32_t old_ssrc, uint32_t new_ssrc));
andresp@webrtc.orgac6d9192013-05-13 10:50:50 +000051
52 const Config config_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000053};
54
55class VieKeyRequestTest : public ::testing::Test {
56 protected:
57 virtual void SetUp() {
58 process_thread_.reset(new TestProcessThread());
59 encoder_state_feedback_.reset(new EncoderStateFeedback());
60 }
61 scoped_ptr<TestProcessThread> process_thread_;
62 scoped_ptr<EncoderStateFeedback> encoder_state_feedback_;
63};
64
65TEST_F(VieKeyRequestTest, CreateAndTriggerRequests) {
66 const int ssrc = 1234;
67 MockVieEncoder encoder(process_thread_.get());
68 EXPECT_TRUE(encoder_state_feedback_->AddEncoder(ssrc, &encoder));
69
70 EXPECT_CALL(encoder, OnReceivedIntraFrameRequest(ssrc))
71 .Times(1);
72 encoder_state_feedback_->GetRtcpIntraFrameObserver()->
73 OnReceivedIntraFrameRequest(ssrc);
74
75 const uint8_t sli_picture_id = 3;
76 EXPECT_CALL(encoder, OnReceivedSLI(ssrc, sli_picture_id))
77 .Times(1);
78 encoder_state_feedback_->GetRtcpIntraFrameObserver()->OnReceivedSLI(
79 ssrc, sli_picture_id);
80
81 const uint64_t rpsi_picture_id = 9;
82 EXPECT_CALL(encoder, OnReceivedRPSI(ssrc, rpsi_picture_id))
83 .Times(1);
84 encoder_state_feedback_->GetRtcpIntraFrameObserver()->OnReceivedRPSI(
85 ssrc, rpsi_picture_id);
86
mflodman@webrtc.orgb6d9cfc2012-10-25 11:30:29 +000087 encoder_state_feedback_->RemoveEncoder(&encoder);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000088}
89
90// Register multiple encoders and make sure the request is relayed to correct
91// ViEEncoder.
92TEST_F(VieKeyRequestTest, MultipleEncoders) {
93 const int ssrc_1 = 1234;
94 const int ssrc_2 = 5678;
95 MockVieEncoder encoder_1(process_thread_.get());
96 MockVieEncoder encoder_2(process_thread_.get());
97 EXPECT_TRUE(encoder_state_feedback_->AddEncoder(ssrc_1, &encoder_1));
98 EXPECT_TRUE(encoder_state_feedback_->AddEncoder(ssrc_2, &encoder_2));
99
100 EXPECT_CALL(encoder_1, OnReceivedIntraFrameRequest(ssrc_1))
101 .Times(1);
102 EXPECT_CALL(encoder_2, OnReceivedIntraFrameRequest(ssrc_2))
103 .Times(1);
104 encoder_state_feedback_->GetRtcpIntraFrameObserver()->
105 OnReceivedIntraFrameRequest(ssrc_1);
106 encoder_state_feedback_->GetRtcpIntraFrameObserver()->
107 OnReceivedIntraFrameRequest(ssrc_2);
108
109 const uint8_t sli_pid_1 = 3;
110 const uint8_t sli_pid_2 = 4;
111 EXPECT_CALL(encoder_1, OnReceivedSLI(ssrc_1, sli_pid_1))
112 .Times(1);
113 EXPECT_CALL(encoder_2, OnReceivedSLI(ssrc_2, sli_pid_2))
114 .Times(1);
115 encoder_state_feedback_->GetRtcpIntraFrameObserver()->OnReceivedSLI(
116 ssrc_1, sli_pid_1);
117 encoder_state_feedback_->GetRtcpIntraFrameObserver()->OnReceivedSLI(
118 ssrc_2, sli_pid_2);
119
120 const uint64_t rpsi_pid_1 = 9;
121 const uint64_t rpsi_pid_2 = 10;
122 EXPECT_CALL(encoder_1, OnReceivedRPSI(ssrc_1, rpsi_pid_1))
123 .Times(1);
124 EXPECT_CALL(encoder_2, OnReceivedRPSI(ssrc_2, rpsi_pid_2))
125 .Times(1);
126 encoder_state_feedback_->GetRtcpIntraFrameObserver()->OnReceivedRPSI(
127 ssrc_1, rpsi_pid_1);
128 encoder_state_feedback_->GetRtcpIntraFrameObserver()->OnReceivedRPSI(
129 ssrc_2, rpsi_pid_2);
130
mflodman@webrtc.orgb6d9cfc2012-10-25 11:30:29 +0000131 encoder_state_feedback_->RemoveEncoder(&encoder_1);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000132 EXPECT_CALL(encoder_2, OnReceivedIntraFrameRequest(ssrc_2))
133 .Times(1);
134 encoder_state_feedback_->GetRtcpIntraFrameObserver()->
135 OnReceivedIntraFrameRequest(ssrc_2);
mflodman@webrtc.orgb6d9cfc2012-10-25 11:30:29 +0000136 encoder_state_feedback_->RemoveEncoder(&encoder_2);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000137}
138
139TEST_F(VieKeyRequestTest, AddTwiceError) {
140 const int ssrc = 1234;
141 MockVieEncoder encoder(process_thread_.get());
142 EXPECT_TRUE(encoder_state_feedback_->AddEncoder(ssrc, &encoder));
143 EXPECT_FALSE(encoder_state_feedback_->AddEncoder(ssrc, &encoder));
mflodman@webrtc.orgb6d9cfc2012-10-25 11:30:29 +0000144 encoder_state_feedback_->RemoveEncoder(&encoder);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000145}
146
147} // namespace webrtc