1 /*
2 * Copyright 2020 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 "call/adaptation/video_source_restrictions.h"
12
13 #include <limits>
14
15 #include "rtc_base/checks.h"
16 #include "rtc_base/strings/string_builder.h"
17
18 namespace webrtc {
19
VideoSourceRestrictions()20 VideoSourceRestrictions::VideoSourceRestrictions()
21 : max_pixels_per_frame_(absl::nullopt),
22 target_pixels_per_frame_(absl::nullopt),
23 max_frame_rate_(absl::nullopt) {}
24
VideoSourceRestrictions(absl::optional<size_t> max_pixels_per_frame,absl::optional<size_t> target_pixels_per_frame,absl::optional<double> max_frame_rate)25 VideoSourceRestrictions::VideoSourceRestrictions(
26 absl::optional<size_t> max_pixels_per_frame,
27 absl::optional<size_t> target_pixels_per_frame,
28 absl::optional<double> max_frame_rate)
29 : max_pixels_per_frame_(std::move(max_pixels_per_frame)),
30 target_pixels_per_frame_(std::move(target_pixels_per_frame)),
31 max_frame_rate_(std::move(max_frame_rate)) {
32 RTC_DCHECK(!max_pixels_per_frame_.has_value() ||
33 max_pixels_per_frame_.value() <
34 static_cast<size_t>(std::numeric_limits<int>::max()));
35 RTC_DCHECK(!max_frame_rate_.has_value() ||
36 max_frame_rate_.value() < std::numeric_limits<int>::max());
37 RTC_DCHECK(!max_frame_rate_.has_value() || max_frame_rate_.value() > 0.0);
38 }
39
ToString() const40 std::string VideoSourceRestrictions::ToString() const {
41 rtc::StringBuilder ss;
42 ss << "{";
43 if (max_frame_rate_)
44 ss << " max_fps=" << max_frame_rate_.value();
45 if (max_pixels_per_frame_)
46 ss << " max_pixels_per_frame=" << max_pixels_per_frame_.value();
47 if (target_pixels_per_frame_)
48 ss << " target_pixels_per_frame=" << target_pixels_per_frame_.value();
49 ss << " }";
50 return ss.Release();
51 }
52
max_pixels_per_frame() const53 const absl::optional<size_t>& VideoSourceRestrictions::max_pixels_per_frame()
54 const {
55 return max_pixels_per_frame_;
56 }
57
target_pixels_per_frame() const58 const absl::optional<size_t>& VideoSourceRestrictions::target_pixels_per_frame()
59 const {
60 return target_pixels_per_frame_;
61 }
62
max_frame_rate() const63 const absl::optional<double>& VideoSourceRestrictions::max_frame_rate() const {
64 return max_frame_rate_;
65 }
66
set_max_pixels_per_frame(absl::optional<size_t> max_pixels_per_frame)67 void VideoSourceRestrictions::set_max_pixels_per_frame(
68 absl::optional<size_t> max_pixels_per_frame) {
69 max_pixels_per_frame_ = std::move(max_pixels_per_frame);
70 }
71
set_target_pixels_per_frame(absl::optional<size_t> target_pixels_per_frame)72 void VideoSourceRestrictions::set_target_pixels_per_frame(
73 absl::optional<size_t> target_pixels_per_frame) {
74 target_pixels_per_frame_ = std::move(target_pixels_per_frame);
75 }
76
set_max_frame_rate(absl::optional<double> max_frame_rate)77 void VideoSourceRestrictions::set_max_frame_rate(
78 absl::optional<double> max_frame_rate) {
79 max_frame_rate_ = std::move(max_frame_rate);
80 }
81
DidRestrictionsIncrease(VideoSourceRestrictions before,VideoSourceRestrictions after)82 bool DidRestrictionsIncrease(VideoSourceRestrictions before,
83 VideoSourceRestrictions after) {
84 bool decreased_resolution = DidDecreaseResolution(before, after);
85 bool decreased_framerate = DidDecreaseFrameRate(before, after);
86 bool same_resolution =
87 before.max_pixels_per_frame() == after.max_pixels_per_frame();
88 bool same_framerate = before.max_frame_rate() == after.max_frame_rate();
89
90 return (decreased_resolution && decreased_framerate) ||
91 (decreased_resolution && same_framerate) ||
92 (same_resolution && decreased_framerate);
93 }
94
DidRestrictionsDecrease(VideoSourceRestrictions before,VideoSourceRestrictions after)95 bool DidRestrictionsDecrease(VideoSourceRestrictions before,
96 VideoSourceRestrictions after) {
97 bool increased_resolution = DidIncreaseResolution(before, after);
98 bool increased_framerate = DidIncreaseFrameRate(before, after);
99 bool same_resolution =
100 before.max_pixels_per_frame() == after.max_pixels_per_frame();
101 bool same_framerate = before.max_frame_rate() == after.max_frame_rate();
102
103 return (increased_resolution && increased_framerate) ||
104 (increased_resolution && same_framerate) ||
105 (same_resolution && increased_framerate);
106 }
107
DidIncreaseResolution(VideoSourceRestrictions restrictions_before,VideoSourceRestrictions restrictions_after)108 bool DidIncreaseResolution(VideoSourceRestrictions restrictions_before,
109 VideoSourceRestrictions restrictions_after) {
110 if (!restrictions_before.max_pixels_per_frame().has_value())
111 return false;
112 if (!restrictions_after.max_pixels_per_frame().has_value())
113 return true;
114 return restrictions_after.max_pixels_per_frame().value() >
115 restrictions_before.max_pixels_per_frame().value();
116 }
117
DidDecreaseResolution(VideoSourceRestrictions restrictions_before,VideoSourceRestrictions restrictions_after)118 bool DidDecreaseResolution(VideoSourceRestrictions restrictions_before,
119 VideoSourceRestrictions restrictions_after) {
120 if (!restrictions_after.max_pixels_per_frame().has_value())
121 return false;
122 if (!restrictions_before.max_pixels_per_frame().has_value())
123 return true;
124 return restrictions_after.max_pixels_per_frame().value() <
125 restrictions_before.max_pixels_per_frame().value();
126 }
127
DidIncreaseFrameRate(VideoSourceRestrictions restrictions_before,VideoSourceRestrictions restrictions_after)128 bool DidIncreaseFrameRate(VideoSourceRestrictions restrictions_before,
129 VideoSourceRestrictions restrictions_after) {
130 if (!restrictions_before.max_frame_rate().has_value())
131 return false;
132 if (!restrictions_after.max_frame_rate().has_value())
133 return true;
134 return restrictions_after.max_frame_rate().value() >
135 restrictions_before.max_frame_rate().value();
136 }
137
DidDecreaseFrameRate(VideoSourceRestrictions restrictions_before,VideoSourceRestrictions restrictions_after)138 bool DidDecreaseFrameRate(VideoSourceRestrictions restrictions_before,
139 VideoSourceRestrictions restrictions_after) {
140 if (!restrictions_after.max_frame_rate().has_value())
141 return false;
142 if (!restrictions_before.max_frame_rate().has_value())
143 return true;
144 return restrictions_after.max_frame_rate().value() <
145 restrictions_before.max_frame_rate().value();
146 }
147
148 } // namespace webrtc
149