Florent Castelli | e7862cc | 2018-12-06 13:38:24 +0100 | [diff] [blame] | 1 | /* |
| 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/encoder_simulcast_proxy.h" |
| 13 | #include <string> |
| 14 | |
Erik Språng | 9e79e6b | 2019-04-25 16:01:03 +0200 | [diff] [blame] | 15 | #include "api/test/mock_video_encoder.h" |
Florent Castelli | e7862cc | 2018-12-06 13:38:24 +0100 | [diff] [blame] | 16 | #include "api/test/mock_video_encoder_factory.h" |
Elad Alon | 370f93a | 2019-06-11 14:57:57 +0200 | [diff] [blame] | 17 | #include "api/video_codecs/video_encoder.h" |
Florent Castelli | e7862cc | 2018-12-06 13:38:24 +0100 | [diff] [blame] | 18 | #include "api/video_codecs/vp8_temporal_layers.h" |
Florent Castelli | e7862cc | 2018-12-06 13:38:24 +0100 | [diff] [blame] | 19 | #include "modules/video_coding/include/video_codec_interface.h" |
| 20 | #include "test/gmock.h" |
| 21 | #include "test/gtest.h" |
| 22 | #include "test/video_codec_settings.h" |
| 23 | |
| 24 | namespace webrtc { |
| 25 | namespace testing { |
Elad Alon | 370f93a | 2019-06-11 14:57:57 +0200 | [diff] [blame] | 26 | namespace { |
| 27 | const VideoEncoder::Capabilities kCapabilities(false); |
| 28 | const VideoEncoder::Settings kSettings(kCapabilities, 4, 1200); |
| 29 | } // namespace |
Florent Castelli | e7862cc | 2018-12-06 13:38:24 +0100 | [diff] [blame] | 30 | |
| 31 | using ::testing::_; |
| 32 | using ::testing::NiceMock; |
| 33 | using ::testing::Return; |
| 34 | |
Florent Castelli | e7862cc | 2018-12-06 13:38:24 +0100 | [diff] [blame] | 35 | TEST(EncoderSimulcastProxy, ChoosesCorrectImplementation) { |
| 36 | const std::string kImplementationName = "Fake"; |
| 37 | const std::string kSimulcastAdaptedImplementationName = |
| 38 | "SimulcastEncoderAdapter (Fake, Fake, Fake)"; |
| 39 | VideoCodec codec_settings; |
| 40 | webrtc::test::CodecSettings(kVideoCodecVP8, &codec_settings); |
| 41 | codec_settings.simulcastStream[0] = {test::kTestWidth, |
| 42 | test::kTestHeight, |
| 43 | test::kTestFrameRate, |
| 44 | 2, |
| 45 | 2000, |
| 46 | 1000, |
| 47 | 1000, |
| 48 | 56}; |
| 49 | codec_settings.simulcastStream[1] = {test::kTestWidth, |
| 50 | test::kTestHeight, |
| 51 | test::kTestFrameRate, |
| 52 | 2, |
| 53 | 3000, |
| 54 | 1000, |
| 55 | 1000, |
| 56 | 56}; |
| 57 | codec_settings.simulcastStream[2] = {test::kTestWidth, |
| 58 | test::kTestHeight, |
| 59 | test::kTestFrameRate, |
| 60 | 2, |
| 61 | 5000, |
| 62 | 1000, |
| 63 | 1000, |
| 64 | 56}; |
| 65 | codec_settings.numberOfSimulcastStreams = 3; |
| 66 | |
Erik Språng | 9e79e6b | 2019-04-25 16:01:03 +0200 | [diff] [blame] | 67 | NiceMock<MockVideoEncoder>* mock_encoder = new NiceMock<MockVideoEncoder>(); |
Florent Castelli | e7862cc | 2018-12-06 13:38:24 +0100 | [diff] [blame] | 68 | NiceMock<MockVideoEncoderFactory> simulcast_factory; |
| 69 | |
Elad Alon | 370f93a | 2019-06-11 14:57:57 +0200 | [diff] [blame] | 70 | EXPECT_CALL(*mock_encoder, InitEncode(_, _)) |
Florent Castelli | e7862cc | 2018-12-06 13:38:24 +0100 | [diff] [blame] | 71 | .WillOnce(Return(WEBRTC_VIDEO_CODEC_OK)); |
| 72 | VideoEncoder::EncoderInfo encoder_info; |
| 73 | encoder_info.implementation_name = kImplementationName; |
| 74 | EXPECT_CALL(*mock_encoder, GetEncoderInfo()) |
| 75 | .WillRepeatedly(Return(encoder_info)); |
| 76 | |
| 77 | EXPECT_CALL(simulcast_factory, CreateVideoEncoderProxy(_)) |
| 78 | .Times(1) |
| 79 | .WillOnce(Return(mock_encoder)); |
| 80 | |
| 81 | EncoderSimulcastProxy simulcast_enabled_proxy(&simulcast_factory, |
| 82 | SdpVideoFormat("VP8")); |
| 83 | EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, |
Elad Alon | 370f93a | 2019-06-11 14:57:57 +0200 | [diff] [blame] | 84 | simulcast_enabled_proxy.InitEncode(&codec_settings, kSettings)); |
Florent Castelli | e7862cc | 2018-12-06 13:38:24 +0100 | [diff] [blame] | 85 | EXPECT_EQ(kImplementationName, |
| 86 | simulcast_enabled_proxy.GetEncoderInfo().implementation_name); |
| 87 | |
Erik Språng | 9e79e6b | 2019-04-25 16:01:03 +0200 | [diff] [blame] | 88 | NiceMock<MockVideoEncoder>* mock_encoder1 = new NiceMock<MockVideoEncoder>(); |
| 89 | NiceMock<MockVideoEncoder>* mock_encoder2 = new NiceMock<MockVideoEncoder>(); |
| 90 | NiceMock<MockVideoEncoder>* mock_encoder3 = new NiceMock<MockVideoEncoder>(); |
| 91 | NiceMock<MockVideoEncoder>* mock_encoder4 = new NiceMock<MockVideoEncoder>(); |
Florent Castelli | e7862cc | 2018-12-06 13:38:24 +0100 | [diff] [blame] | 92 | NiceMock<MockVideoEncoderFactory> nonsimulcast_factory; |
| 93 | |
Elad Alon | 370f93a | 2019-06-11 14:57:57 +0200 | [diff] [blame] | 94 | EXPECT_CALL(*mock_encoder1, InitEncode(_, _)) |
Florent Castelli | e7862cc | 2018-12-06 13:38:24 +0100 | [diff] [blame] | 95 | .WillOnce( |
| 96 | Return(WEBRTC_VIDEO_CODEC_ERR_SIMULCAST_PARAMETERS_NOT_SUPPORTED)); |
| 97 | EXPECT_CALL(*mock_encoder1, GetEncoderInfo()) |
| 98 | .WillRepeatedly(Return(encoder_info)); |
| 99 | |
Elad Alon | 370f93a | 2019-06-11 14:57:57 +0200 | [diff] [blame] | 100 | EXPECT_CALL(*mock_encoder2, InitEncode(_, _)) |
Florent Castelli | e7862cc | 2018-12-06 13:38:24 +0100 | [diff] [blame] | 101 | .WillOnce(Return(WEBRTC_VIDEO_CODEC_OK)); |
| 102 | EXPECT_CALL(*mock_encoder2, GetEncoderInfo()) |
| 103 | .WillRepeatedly(Return(encoder_info)); |
| 104 | |
Elad Alon | 370f93a | 2019-06-11 14:57:57 +0200 | [diff] [blame] | 105 | EXPECT_CALL(*mock_encoder3, InitEncode(_, _)) |
Florent Castelli | e7862cc | 2018-12-06 13:38:24 +0100 | [diff] [blame] | 106 | .WillOnce(Return(WEBRTC_VIDEO_CODEC_OK)); |
| 107 | EXPECT_CALL(*mock_encoder3, GetEncoderInfo()) |
| 108 | .WillRepeatedly(Return(encoder_info)); |
| 109 | |
Elad Alon | 370f93a | 2019-06-11 14:57:57 +0200 | [diff] [blame] | 110 | EXPECT_CALL(*mock_encoder4, InitEncode(_, _)) |
Florent Castelli | e7862cc | 2018-12-06 13:38:24 +0100 | [diff] [blame] | 111 | .WillOnce(Return(WEBRTC_VIDEO_CODEC_OK)); |
| 112 | EXPECT_CALL(*mock_encoder4, GetEncoderInfo()) |
| 113 | .WillRepeatedly(Return(encoder_info)); |
| 114 | |
| 115 | EXPECT_CALL(nonsimulcast_factory, CreateVideoEncoderProxy(_)) |
| 116 | .Times(4) |
| 117 | .WillOnce(Return(mock_encoder1)) |
| 118 | .WillOnce(Return(mock_encoder2)) |
| 119 | .WillOnce(Return(mock_encoder3)) |
| 120 | .WillOnce(Return(mock_encoder4)); |
| 121 | |
| 122 | EncoderSimulcastProxy simulcast_disabled_proxy(&nonsimulcast_factory, |
| 123 | SdpVideoFormat("VP8")); |
| 124 | EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, |
Elad Alon | 370f93a | 2019-06-11 14:57:57 +0200 | [diff] [blame] | 125 | simulcast_disabled_proxy.InitEncode(&codec_settings, kSettings)); |
Florent Castelli | e7862cc | 2018-12-06 13:38:24 +0100 | [diff] [blame] | 126 | EXPECT_EQ(kSimulcastAdaptedImplementationName, |
| 127 | simulcast_disabled_proxy.GetEncoderInfo().implementation_name); |
| 128 | |
| 129 | // Cleanup. |
| 130 | simulcast_enabled_proxy.Release(); |
| 131 | simulcast_disabled_proxy.Release(); |
| 132 | } |
| 133 | |
| 134 | TEST(EncoderSimulcastProxy, ForwardsTrustedSetting) { |
Erik Språng | 9e79e6b | 2019-04-25 16:01:03 +0200 | [diff] [blame] | 135 | NiceMock<MockVideoEncoder>* mock_encoder = new NiceMock<MockVideoEncoder>(); |
Florent Castelli | e7862cc | 2018-12-06 13:38:24 +0100 | [diff] [blame] | 136 | NiceMock<MockVideoEncoderFactory> simulcast_factory; |
| 137 | |
Elad Alon | 370f93a | 2019-06-11 14:57:57 +0200 | [diff] [blame] | 138 | EXPECT_CALL(*mock_encoder, InitEncode(_, _)) |
Florent Castelli | e7862cc | 2018-12-06 13:38:24 +0100 | [diff] [blame] | 139 | .WillOnce(Return(WEBRTC_VIDEO_CODEC_OK)); |
| 140 | |
| 141 | EXPECT_CALL(simulcast_factory, CreateVideoEncoderProxy(_)) |
| 142 | .Times(1) |
| 143 | .WillOnce(Return(mock_encoder)); |
| 144 | |
| 145 | EncoderSimulcastProxy simulcast_enabled_proxy(&simulcast_factory, |
| 146 | SdpVideoFormat("VP8")); |
| 147 | VideoCodec codec_settings; |
| 148 | webrtc::test::CodecSettings(kVideoCodecVP8, &codec_settings); |
| 149 | EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, |
Elad Alon | 370f93a | 2019-06-11 14:57:57 +0200 | [diff] [blame] | 150 | simulcast_enabled_proxy.InitEncode(&codec_settings, kSettings)); |
Florent Castelli | e7862cc | 2018-12-06 13:38:24 +0100 | [diff] [blame] | 151 | |
| 152 | VideoEncoder::EncoderInfo info; |
| 153 | info.has_trusted_rate_controller = true; |
| 154 | EXPECT_CALL(*mock_encoder, GetEncoderInfo()).WillRepeatedly(Return(info)); |
| 155 | |
| 156 | EXPECT_TRUE( |
| 157 | simulcast_enabled_proxy.GetEncoderInfo().has_trusted_rate_controller); |
| 158 | } |
| 159 | |
| 160 | TEST(EncoderSimulcastProxy, ForwardsHardwareAccelerated) { |
Erik Språng | 9e79e6b | 2019-04-25 16:01:03 +0200 | [diff] [blame] | 161 | NiceMock<MockVideoEncoder>* mock_encoder = new NiceMock<MockVideoEncoder>(); |
Florent Castelli | e7862cc | 2018-12-06 13:38:24 +0100 | [diff] [blame] | 162 | NiceMock<MockVideoEncoderFactory> simulcast_factory; |
| 163 | |
Elad Alon | 370f93a | 2019-06-11 14:57:57 +0200 | [diff] [blame] | 164 | EXPECT_CALL(*mock_encoder, InitEncode(_, _)) |
Florent Castelli | e7862cc | 2018-12-06 13:38:24 +0100 | [diff] [blame] | 165 | .WillOnce(Return(WEBRTC_VIDEO_CODEC_OK)); |
| 166 | |
| 167 | EXPECT_CALL(simulcast_factory, CreateVideoEncoderProxy(_)) |
| 168 | .Times(1) |
| 169 | .WillOnce(Return(mock_encoder)); |
| 170 | |
| 171 | EncoderSimulcastProxy simulcast_enabled_proxy(&simulcast_factory, |
| 172 | SdpVideoFormat("VP8")); |
| 173 | VideoCodec codec_settings; |
| 174 | webrtc::test::CodecSettings(kVideoCodecVP8, &codec_settings); |
| 175 | EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, |
Elad Alon | 370f93a | 2019-06-11 14:57:57 +0200 | [diff] [blame] | 176 | simulcast_enabled_proxy.InitEncode(&codec_settings, kSettings)); |
Florent Castelli | e7862cc | 2018-12-06 13:38:24 +0100 | [diff] [blame] | 177 | |
| 178 | VideoEncoder::EncoderInfo info; |
| 179 | |
| 180 | info.is_hardware_accelerated = false; |
| 181 | EXPECT_CALL(*mock_encoder, GetEncoderInfo()).WillOnce(Return(info)); |
| 182 | EXPECT_FALSE( |
| 183 | simulcast_enabled_proxy.GetEncoderInfo().is_hardware_accelerated); |
| 184 | |
| 185 | info.is_hardware_accelerated = true; |
| 186 | EXPECT_CALL(*mock_encoder, GetEncoderInfo()).WillOnce(Return(info)); |
| 187 | EXPECT_TRUE(simulcast_enabled_proxy.GetEncoderInfo().is_hardware_accelerated); |
| 188 | } |
| 189 | |
| 190 | TEST(EncoderSimulcastProxy, ForwardsInternalSource) { |
Erik Språng | 9e79e6b | 2019-04-25 16:01:03 +0200 | [diff] [blame] | 191 | NiceMock<MockVideoEncoder>* mock_encoder = new NiceMock<MockVideoEncoder>(); |
Florent Castelli | e7862cc | 2018-12-06 13:38:24 +0100 | [diff] [blame] | 192 | NiceMock<MockVideoEncoderFactory> simulcast_factory; |
| 193 | |
Elad Alon | 370f93a | 2019-06-11 14:57:57 +0200 | [diff] [blame] | 194 | EXPECT_CALL(*mock_encoder, InitEncode(_, _)) |
Florent Castelli | e7862cc | 2018-12-06 13:38:24 +0100 | [diff] [blame] | 195 | .WillOnce(Return(WEBRTC_VIDEO_CODEC_OK)); |
| 196 | |
| 197 | EXPECT_CALL(simulcast_factory, CreateVideoEncoderProxy(_)) |
| 198 | .Times(1) |
| 199 | .WillOnce(Return(mock_encoder)); |
| 200 | |
| 201 | EncoderSimulcastProxy simulcast_enabled_proxy(&simulcast_factory, |
| 202 | SdpVideoFormat("VP8")); |
| 203 | VideoCodec codec_settings; |
| 204 | webrtc::test::CodecSettings(kVideoCodecVP8, &codec_settings); |
| 205 | EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, |
Elad Alon | 370f93a | 2019-06-11 14:57:57 +0200 | [diff] [blame] | 206 | simulcast_enabled_proxy.InitEncode(&codec_settings, kSettings)); |
Florent Castelli | e7862cc | 2018-12-06 13:38:24 +0100 | [diff] [blame] | 207 | |
| 208 | VideoEncoder::EncoderInfo info; |
| 209 | |
| 210 | info.has_internal_source = false; |
| 211 | EXPECT_CALL(*mock_encoder, GetEncoderInfo()).WillOnce(Return(info)); |
| 212 | EXPECT_FALSE(simulcast_enabled_proxy.GetEncoderInfo().has_internal_source); |
| 213 | |
| 214 | info.has_internal_source = true; |
| 215 | EXPECT_CALL(*mock_encoder, GetEncoderInfo()).WillOnce(Return(info)); |
| 216 | EXPECT_TRUE(simulcast_enabled_proxy.GetEncoderInfo().has_internal_source); |
| 217 | } |
| 218 | |
| 219 | } // namespace testing |
| 220 | } // namespace webrtc |