mflodman@webrtc.org | 3be5863 | 2012-09-06 08:19:40 +0000 | [diff] [blame^] | 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 "video_engine/encoder_state_feedback.h" |
| 14 | |
| 15 | #include <gmock/gmock.h> |
| 16 | #include <gtest/gtest.h> |
| 17 | |
| 18 | #include "modules/rtp_rtcp/interface/rtp_rtcp_defines.h" |
| 19 | #include "modules/utility/interface/process_thread.h" |
| 20 | #include "system_wrappers/interface/scoped_ptr.h" |
| 21 | #include "video_engine/vie_encoder.h" |
| 22 | |
| 23 | namespace webrtc { |
| 24 | |
| 25 | // TODO(mflodman) Create a common mock in module utility. |
| 26 | class TestProcessThread : public ProcessThread { |
| 27 | public: |
| 28 | TestProcessThread() {} |
| 29 | ~TestProcessThread() {} |
| 30 | virtual WebRtc_Word32 Start() { return 0; } |
| 31 | virtual WebRtc_Word32 Stop() { return 0; } |
| 32 | virtual WebRtc_Word32 RegisterModule(const Module* module) { return 0; } |
| 33 | virtual WebRtc_Word32 DeRegisterModule(const Module* module) { return 0; } |
| 34 | }; |
| 35 | |
| 36 | class MockVieEncoder : public ViEEncoder { |
| 37 | public: |
| 38 | explicit MockVieEncoder(TestProcessThread* process_thread) |
| 39 | : ViEEncoder(1, 1, 1, *process_thread, NULL) {} |
| 40 | ~MockVieEncoder() {} |
| 41 | |
| 42 | MOCK_METHOD1(OnReceivedIntraFrameRequest, |
| 43 | void(uint32_t)); |
| 44 | MOCK_METHOD2(OnReceivedSLI, |
| 45 | void(uint32_t ssrc, uint8_t picture_id)); |
| 46 | MOCK_METHOD2(OnReceivedRPSI, |
| 47 | void(uint32_t ssrc, uint64_t picture_id)); |
| 48 | }; |
| 49 | |
| 50 | class VieKeyRequestTest : public ::testing::Test { |
| 51 | protected: |
| 52 | virtual void SetUp() { |
| 53 | process_thread_.reset(new TestProcessThread()); |
| 54 | encoder_state_feedback_.reset(new EncoderStateFeedback()); |
| 55 | } |
| 56 | scoped_ptr<TestProcessThread> process_thread_; |
| 57 | scoped_ptr<EncoderStateFeedback> encoder_state_feedback_; |
| 58 | }; |
| 59 | |
| 60 | TEST_F(VieKeyRequestTest, CreateAndTriggerRequests) { |
| 61 | const int ssrc = 1234; |
| 62 | MockVieEncoder encoder(process_thread_.get()); |
| 63 | EXPECT_EQ(true, encoder_state_feedback_->AddEncoder(ssrc, &encoder)); |
| 64 | |
| 65 | EXPECT_CALL(encoder, OnReceivedIntraFrameRequest(ssrc)) |
| 66 | .Times(1); |
| 67 | encoder_state_feedback_->GetRtcpIntraFrameObserver()-> |
| 68 | OnReceivedIntraFrameRequest(ssrc); |
| 69 | |
| 70 | const uint8_t sli_picture_id = 3; |
| 71 | EXPECT_CALL(encoder, OnReceivedSLI(ssrc, sli_picture_id)) |
| 72 | .Times(1); |
| 73 | encoder_state_feedback_->GetRtcpIntraFrameObserver()->OnReceivedSLI( |
| 74 | ssrc, sli_picture_id); |
| 75 | |
| 76 | const uint64_t rpsi_picture_id = 9; |
| 77 | EXPECT_CALL(encoder, OnReceivedRPSI(ssrc, rpsi_picture_id)) |
| 78 | .Times(1); |
| 79 | encoder_state_feedback_->GetRtcpIntraFrameObserver()->OnReceivedRPSI( |
| 80 | ssrc, rpsi_picture_id); |
| 81 | |
| 82 | encoder_state_feedback_->RemoveEncoder(ssrc); |
| 83 | } |
| 84 | |
| 85 | // Register multiple encoders and make sure the request is relayed to correct |
| 86 | // ViEEncoder. |
| 87 | TEST_F(VieKeyRequestTest, MultipleEncoders) { |
| 88 | const int ssrc_1 = 1234; |
| 89 | const int ssrc_2 = 5678; |
| 90 | MockVieEncoder encoder_1(process_thread_.get()); |
| 91 | MockVieEncoder encoder_2(process_thread_.get()); |
| 92 | EXPECT_EQ(true, encoder_state_feedback_->AddEncoder(ssrc_1, &encoder_1)); |
| 93 | EXPECT_EQ(true, encoder_state_feedback_->AddEncoder(ssrc_2, &encoder_2)); |
| 94 | |
| 95 | EXPECT_CALL(encoder_1, OnReceivedIntraFrameRequest(ssrc_1)) |
| 96 | .Times(1); |
| 97 | EXPECT_CALL(encoder_2, OnReceivedIntraFrameRequest(ssrc_2)) |
| 98 | .Times(1); |
| 99 | encoder_state_feedback_->GetRtcpIntraFrameObserver()-> |
| 100 | OnReceivedIntraFrameRequest(ssrc_1); |
| 101 | encoder_state_feedback_->GetRtcpIntraFrameObserver()-> |
| 102 | OnReceivedIntraFrameRequest(ssrc_2); |
| 103 | |
| 104 | const uint8_t sli_pid_1 = 3; |
| 105 | const uint8_t sli_pid_2 = 4; |
| 106 | EXPECT_CALL(encoder_1, OnReceivedSLI(ssrc_1, sli_pid_1)) |
| 107 | .Times(1); |
| 108 | EXPECT_CALL(encoder_2, OnReceivedSLI(ssrc_2, sli_pid_2)) |
| 109 | .Times(1); |
| 110 | encoder_state_feedback_->GetRtcpIntraFrameObserver()->OnReceivedSLI( |
| 111 | ssrc_1, sli_pid_1); |
| 112 | encoder_state_feedback_->GetRtcpIntraFrameObserver()->OnReceivedSLI( |
| 113 | ssrc_2, sli_pid_2); |
| 114 | |
| 115 | const uint64_t rpsi_pid_1 = 9; |
| 116 | const uint64_t rpsi_pid_2 = 10; |
| 117 | EXPECT_CALL(encoder_1, OnReceivedRPSI(ssrc_1, rpsi_pid_1)) |
| 118 | .Times(1); |
| 119 | EXPECT_CALL(encoder_2, OnReceivedRPSI(ssrc_2, rpsi_pid_2)) |
| 120 | .Times(1); |
| 121 | encoder_state_feedback_->GetRtcpIntraFrameObserver()->OnReceivedRPSI( |
| 122 | ssrc_1, rpsi_pid_1); |
| 123 | encoder_state_feedback_->GetRtcpIntraFrameObserver()->OnReceivedRPSI( |
| 124 | ssrc_2, rpsi_pid_2); |
| 125 | |
| 126 | encoder_state_feedback_->RemoveEncoder(ssrc_1); |
| 127 | EXPECT_CALL(encoder_2, OnReceivedIntraFrameRequest(ssrc_2)) |
| 128 | .Times(1); |
| 129 | encoder_state_feedback_->GetRtcpIntraFrameObserver()-> |
| 130 | OnReceivedIntraFrameRequest(ssrc_2); |
| 131 | encoder_state_feedback_->RemoveEncoder(ssrc_2); |
| 132 | } |
| 133 | |
| 134 | TEST_F(VieKeyRequestTest, AddTwiceError) { |
| 135 | const int ssrc = 1234; |
| 136 | MockVieEncoder encoder(process_thread_.get()); |
| 137 | EXPECT_EQ(true, encoder_state_feedback_->AddEncoder(ssrc, &encoder)); |
| 138 | EXPECT_EQ(false, encoder_state_feedback_->AddEncoder(ssrc, &encoder)); |
| 139 | encoder_state_feedback_->RemoveEncoder(ssrc); |
| 140 | } |
| 141 | |
| 142 | } // namespace webrtc |