• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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.
13 #include "webrtc/video/encoder_state_feedback.h"
14 
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 
18 #include "webrtc/base/scoped_ptr.h"
19 #include "webrtc/common.h"
20 #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h"
21 #include "webrtc/modules/pacing/paced_sender.h"
22 #include "webrtc/modules/pacing/packet_router.h"
23 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
24 #include "webrtc/modules/utility/include/mock/mock_process_thread.h"
25 #include "webrtc/video/payload_router.h"
26 #include "webrtc/video/vie_encoder.h"
27 
28 using ::testing::NiceMock;
29 
30 namespace webrtc {
31 
32 class MockVieEncoder : public ViEEncoder {
33  public:
MockVieEncoder(ProcessThread * process_thread,PacedSender * pacer)34   explicit MockVieEncoder(ProcessThread* process_thread, PacedSender* pacer)
35       : ViEEncoder(1, process_thread, nullptr, nullptr, pacer, nullptr) {}
~MockVieEncoder()36   ~MockVieEncoder() {}
37 
38   MOCK_METHOD1(OnReceivedIntraFrameRequest,
39                void(uint32_t));
40   MOCK_METHOD2(OnReceivedSLI,
41                void(uint32_t ssrc, uint8_t picture_id));
42   MOCK_METHOD2(OnReceivedRPSI,
43                void(uint32_t ssrc, uint64_t picture_id));
44   MOCK_METHOD2(OnLocalSsrcChanged,
45                void(uint32_t old_ssrc, uint32_t new_ssrc));
46 };
47 
48 class VieKeyRequestTest : public ::testing::Test {
49  protected:
VieKeyRequestTest()50   VieKeyRequestTest()
51       : pacer_(Clock::GetRealTimeClock(),
52                &router_,
53                BitrateController::kDefaultStartBitrateKbps,
54                PacedSender::kDefaultPaceMultiplier *
55                    BitrateController::kDefaultStartBitrateKbps,
56                0) {}
SetUp()57   virtual void SetUp() {
58     process_thread_.reset(new NiceMock<MockProcessThread>);
59     encoder_state_feedback_.reset(new EncoderStateFeedback());
60   }
61   rtc::scoped_ptr<MockProcessThread> process_thread_;
62   rtc::scoped_ptr<EncoderStateFeedback> encoder_state_feedback_;
63   PacketRouter router_;
64   PacedSender pacer_;
65 };
66 
TEST_F(VieKeyRequestTest,CreateAndTriggerRequests)67 TEST_F(VieKeyRequestTest, CreateAndTriggerRequests) {
68   const int ssrc = 1234;
69   MockVieEncoder encoder(process_thread_.get(), &pacer_);
70   encoder_state_feedback_->AddEncoder(std::vector<uint32_t>(1, ssrc), &encoder);
71 
72   EXPECT_CALL(encoder, OnReceivedIntraFrameRequest(ssrc))
73       .Times(1);
74   encoder_state_feedback_->GetRtcpIntraFrameObserver()->
75       OnReceivedIntraFrameRequest(ssrc);
76 
77   const uint8_t sli_picture_id = 3;
78   EXPECT_CALL(encoder, OnReceivedSLI(ssrc, sli_picture_id))
79       .Times(1);
80   encoder_state_feedback_->GetRtcpIntraFrameObserver()->OnReceivedSLI(
81       ssrc, sli_picture_id);
82 
83   const uint64_t rpsi_picture_id = 9;
84   EXPECT_CALL(encoder, OnReceivedRPSI(ssrc, rpsi_picture_id))
85       .Times(1);
86   encoder_state_feedback_->GetRtcpIntraFrameObserver()->OnReceivedRPSI(
87       ssrc, rpsi_picture_id);
88 
89   encoder_state_feedback_->RemoveEncoder(&encoder);
90 }
91 
92 // Register multiple encoders and make sure the request is relayed to correct
93 // ViEEncoder.
TEST_F(VieKeyRequestTest,MultipleEncoders)94 TEST_F(VieKeyRequestTest, MultipleEncoders) {
95   const int ssrc_1 = 1234;
96   const int ssrc_2 = 5678;
97   MockVieEncoder encoder_1(process_thread_.get(), &pacer_);
98   MockVieEncoder encoder_2(process_thread_.get(), &pacer_);
99   encoder_state_feedback_->AddEncoder(std::vector<uint32_t>(1, ssrc_1),
100                                       &encoder_1);
101   encoder_state_feedback_->AddEncoder(std::vector<uint32_t>(1, ssrc_2),
102                                       &encoder_2);
103 
104   EXPECT_CALL(encoder_1, OnReceivedIntraFrameRequest(ssrc_1))
105       .Times(1);
106   EXPECT_CALL(encoder_2, OnReceivedIntraFrameRequest(ssrc_2))
107       .Times(1);
108   encoder_state_feedback_->GetRtcpIntraFrameObserver()->
109       OnReceivedIntraFrameRequest(ssrc_1);
110   encoder_state_feedback_->GetRtcpIntraFrameObserver()->
111       OnReceivedIntraFrameRequest(ssrc_2);
112 
113   const uint8_t sli_pid_1 = 3;
114   const uint8_t sli_pid_2 = 4;
115   EXPECT_CALL(encoder_1, OnReceivedSLI(ssrc_1, sli_pid_1))
116       .Times(1);
117   EXPECT_CALL(encoder_2, OnReceivedSLI(ssrc_2, sli_pid_2))
118       .Times(1);
119   encoder_state_feedback_->GetRtcpIntraFrameObserver()->OnReceivedSLI(
120       ssrc_1, sli_pid_1);
121   encoder_state_feedback_->GetRtcpIntraFrameObserver()->OnReceivedSLI(
122       ssrc_2, sli_pid_2);
123 
124   const uint64_t rpsi_pid_1 = 9;
125   const uint64_t rpsi_pid_2 = 10;
126   EXPECT_CALL(encoder_1, OnReceivedRPSI(ssrc_1, rpsi_pid_1))
127       .Times(1);
128   EXPECT_CALL(encoder_2, OnReceivedRPSI(ssrc_2, rpsi_pid_2))
129       .Times(1);
130   encoder_state_feedback_->GetRtcpIntraFrameObserver()->OnReceivedRPSI(
131       ssrc_1, rpsi_pid_1);
132   encoder_state_feedback_->GetRtcpIntraFrameObserver()->OnReceivedRPSI(
133       ssrc_2, rpsi_pid_2);
134 
135   encoder_state_feedback_->RemoveEncoder(&encoder_1);
136   EXPECT_CALL(encoder_2, OnReceivedIntraFrameRequest(ssrc_2))
137       .Times(1);
138   encoder_state_feedback_->GetRtcpIntraFrameObserver()->
139       OnReceivedIntraFrameRequest(ssrc_2);
140   encoder_state_feedback_->RemoveEncoder(&encoder_2);
141 }
142 
143 }  // namespace webrtc
144