1 /*
2 * Copyright (c) 2022 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 #include "video/config/encoder_stream_factory.h"
12
13 #include "call/adaptation/video_source_restrictions.h"
14 #include "test/gtest.h"
15
16 namespace webrtc {
17
18 using cricket::EncoderStreamFactory;
19 constexpr int kMaxQp = 48;
20
21 namespace {
22
GetStreamResolutions(const std::vector<VideoStream> & streams)23 std::vector<Resolution> GetStreamResolutions(
24 const std::vector<VideoStream>& streams) {
25 std::vector<Resolution> res;
26 for (const auto& s : streams) {
27 if (s.active) {
28 res.push_back(
29 {rtc::checked_cast<int>(s.width), rtc::checked_cast<int>(s.height)});
30 }
31 }
32 return res;
33 }
34
LayerWithRequestedResolution(Resolution res)35 VideoStream LayerWithRequestedResolution(Resolution res) {
36 VideoStream s;
37 s.requested_resolution = res;
38 return s;
39 }
40
41 } // namespace
42
TEST(EncoderStreamFactory,SinglecastRequestedResolution)43 TEST(EncoderStreamFactory, SinglecastRequestedResolution) {
44 VideoEncoder::EncoderInfo encoder_info;
45 auto factory = rtc::make_ref_counted<EncoderStreamFactory>(
46 "VP8", kMaxQp,
47 /* is_screenshare= */ false,
48 /* conference_mode= */ false, encoder_info);
49 VideoEncoderConfig encoder_config;
50 encoder_config.number_of_streams = 1;
51 encoder_config.simulcast_layers.push_back(
52 LayerWithRequestedResolution({.width = 640, .height = 360}));
53 auto streams = factory->CreateEncoderStreams(1280, 720, encoder_config);
54 EXPECT_EQ(streams[0].requested_resolution,
55 (Resolution{.width = 640, .height = 360}));
56 EXPECT_EQ(GetStreamResolutions(streams), (std::vector<Resolution>{
57 {.width = 640, .height = 360},
58 }));
59 }
60
TEST(EncoderStreamFactory,SinglecastRequestedResolutionWithAdaptation)61 TEST(EncoderStreamFactory, SinglecastRequestedResolutionWithAdaptation) {
62 VideoSourceRestrictions restrictions(
63 /* max_pixels_per_frame= */ (320 * 320),
64 /* target_pixels_per_frame= */ absl::nullopt,
65 /* max_frame_rate= */ absl::nullopt);
66 VideoEncoder::EncoderInfo encoder_info;
67 auto factory = rtc::make_ref_counted<EncoderStreamFactory>(
68 "VP8", kMaxQp,
69 /* is_screenshare= */ false,
70 /* conference_mode= */ false, encoder_info, restrictions);
71 VideoEncoderConfig encoder_config;
72 encoder_config.number_of_streams = 1;
73 encoder_config.simulcast_layers.push_back(
74 LayerWithRequestedResolution({.width = 640, .height = 360}));
75 auto streams = factory->CreateEncoderStreams(1280, 720, encoder_config);
76 EXPECT_EQ(streams[0].requested_resolution,
77 (Resolution{.width = 640, .height = 360}));
78 EXPECT_EQ(GetStreamResolutions(streams), (std::vector<Resolution>{
79 {.width = 320, .height = 180},
80 }));
81 }
82
83 } // namespace webrtc
84