blob: 0b26bb854598bc8e2eed63b43da9bdb9b46153cb [file] [log] [blame]
Ilya Nikolaevskiyc22a3a62017-10-25 10:04:54 +02001/*
2 * Copyright (c) 2017 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#include "media/engine/vp8_encoder_simulcast_proxy.h"
13
14#include <string>
15
16#include "media/engine/webrtcvideoencoderfactory.h"
17#include "modules/video_coding/codecs/vp8/temporal_layers.h"
18#include "modules/video_coding/include/video_codec_interface.h"
19#include "test/gmock.h"
20#include "test/gtest.h"
21#include "test/video_codec_settings.h"
22
23namespace webrtc {
24namespace testing {
25
26using ::testing::Return;
27using ::testing::_;
28using ::testing::NiceMock;
29
30class MockEncoder : public VideoEncoder {
31 public:
32 // TODO(nisse): Valid overrides commented out, because the gmock
33 // methods don't use any override declarations, and we want to avoid
34 // warnings from -Winconsistent-missing-override. See
35 // http://crbug.com/428099.
36 MockEncoder() {}
37 virtual ~MockEncoder() {}
38
39 MOCK_METHOD3(InitEncode,
40 int32_t(const VideoCodec* codec_settings,
41 int32_t number_of_cores,
42 size_t max_payload_size));
43
44 MOCK_METHOD1(RegisterEncodeCompleteCallback, int32_t(EncodedImageCallback*));
45
46 MOCK_METHOD0(Release, int32_t());
47
48 MOCK_METHOD3(
49 Encode,
50 int32_t(const VideoFrame& inputImage,
51 const CodecSpecificInfo* codecSpecificInfo,
52 const std::vector<FrameType>* frame_types) /* override */);
53
54 MOCK_METHOD2(SetChannelParameters, int32_t(uint32_t packetLoss, int64_t rtt));
55
56 MOCK_CONST_METHOD0(ImplementationName, const char*());
57};
58
59class MockWebRtcVideoEncoderFactory
60 : public cricket::WebRtcVideoEncoderFactory {
61 public:
62 MockWebRtcVideoEncoderFactory() {}
63 virtual ~MockWebRtcVideoEncoderFactory() {}
64
65 MOCK_METHOD1(CreateVideoEncoder,
66 webrtc::VideoEncoder*(const cricket::VideoCodec& codec));
67
68 MOCK_CONST_METHOD0(supported_codecs, std::vector<cricket::VideoCodec>&());
69
70 MOCK_METHOD1(DestroyVideoEncoder, void(webrtc::VideoEncoder*));
71};
72
73TEST(VP8EncoderSimulcastProxy, ChoosesCorrectImplementation) {
74 const std::string kImplementationName = "Fake";
75 const std::string kSimulcastAdaptedImplementationName =
76 "SimulcastEncoderAdapter (Fake, Fake, Fake)";
77 VideoCodec codec_settings;
78 webrtc::test::CodecSettings(kVideoCodecVP8, &codec_settings);
79 TemporalLayersFactory tl_factory;
80 codec_settings.VP8()->tl_factory = &tl_factory;
81 codec_settings.simulcastStream[0] = {
82 test::kTestWidth, test::kTestHeight, 2, 2000, 1000, 1000, 56};
83 codec_settings.simulcastStream[1] = {
84 test::kTestWidth, test::kTestHeight, 2, 3000, 1000, 1000, 56};
85 codec_settings.simulcastStream[2] = {
86 test::kTestWidth, test::kTestHeight, 2, 5000, 1000, 1000, 56};
87 codec_settings.numberOfSimulcastStreams = 3;
88
89 NiceMock<MockEncoder> mock_encoder;
90 NiceMock<MockWebRtcVideoEncoderFactory> simulcast_factory;
91
92 EXPECT_CALL(mock_encoder, InitEncode(_, _, _))
93 .WillOnce(Return(WEBRTC_VIDEO_CODEC_OK));
94 EXPECT_CALL(mock_encoder, ImplementationName())
95 .WillRepeatedly(Return(kImplementationName.c_str()));
96
97 EXPECT_CALL(simulcast_factory, CreateVideoEncoder(_))
98 .Times(1)
99 .WillOnce(Return(&mock_encoder));
100
101 VP8EncoderSimulcastProxy simulcast_enabled_proxy(&simulcast_factory);
102 EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
103 simulcast_enabled_proxy.InitEncode(&codec_settings, 4, 1200));
104 EXPECT_EQ(kImplementationName, simulcast_enabled_proxy.ImplementationName());
105
106 NiceMock<MockEncoder> mock_encoder1;
107 NiceMock<MockEncoder> mock_encoder2;
108 NiceMock<MockEncoder> mock_encoder3;
109 NiceMock<MockEncoder> mock_encoder4;
110 NiceMock<MockWebRtcVideoEncoderFactory> nonsimulcast_factory;
111
112 EXPECT_CALL(mock_encoder1, InitEncode(_, _, _))
113 .WillOnce(
114 Return(WEBRTC_VIDEO_CODEC_ERR_SIMULCAST_PARAMETERS_NOT_SUPPORTED));
115 EXPECT_CALL(mock_encoder1, ImplementationName())
116 .WillRepeatedly(Return(kImplementationName.c_str()));
117
118 EXPECT_CALL(mock_encoder2, InitEncode(_, _, _))
119 .WillOnce(Return(WEBRTC_VIDEO_CODEC_OK));
120 EXPECT_CALL(mock_encoder2, ImplementationName())
121 .WillRepeatedly(Return(kImplementationName.c_str()));
122
123 EXPECT_CALL(mock_encoder3, InitEncode(_, _, _))
124 .WillOnce(Return(WEBRTC_VIDEO_CODEC_OK));
125 EXPECT_CALL(mock_encoder3, ImplementationName())
126 .WillRepeatedly(Return(kImplementationName.c_str()));
127
128 EXPECT_CALL(mock_encoder4, InitEncode(_, _, _))
129 .WillOnce(Return(WEBRTC_VIDEO_CODEC_OK));
130 EXPECT_CALL(mock_encoder4, ImplementationName())
131 .WillRepeatedly(Return(kImplementationName.c_str()));
132
133 EXPECT_CALL(nonsimulcast_factory, CreateVideoEncoder(_))
134 .Times(4)
135 .WillOnce(Return(&mock_encoder1))
136 .WillOnce(Return(&mock_encoder2))
137 .WillOnce(Return(&mock_encoder3))
138 .WillOnce(Return(&mock_encoder4));
139
140 VP8EncoderSimulcastProxy simulcast_disabled_proxy(&nonsimulcast_factory);
141 EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
142 simulcast_disabled_proxy.InitEncode(&codec_settings, 4, 1200));
143 EXPECT_EQ(kSimulcastAdaptedImplementationName,
144 simulcast_disabled_proxy.ImplementationName());
145
146 // Cleanup.
147 simulcast_enabled_proxy.Release();
148 simulcast_disabled_proxy.Release();
149}
150
151} // namespace testing
152} // namespace webrtc