blob: e9d6c26137bac64757566a4ae45abbd876d8de34 [file] [log] [blame]
Henrik Boströmce0ea492020-01-13 11:27:18 +01001/*
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"
Henrik Boströme2e8c172020-06-03 09:24:06 +020016#include "rtc_base/strings/string_builder.h"
Henrik Boströmce0ea492020-01-13 11:27:18 +010017
18namespace webrtc {
19
20VideoSourceRestrictions::VideoSourceRestrictions()
21 : max_pixels_per_frame_(absl::nullopt),
22 target_pixels_per_frame_(absl::nullopt),
23 max_frame_rate_(absl::nullopt) {}
24
25VideoSourceRestrictions::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
Henrik Boströme2e8c172020-06-03 09:24:06 +020040std::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
Henrik Boströmce0ea492020-01-13 11:27:18 +010053const absl::optional<size_t>& VideoSourceRestrictions::max_pixels_per_frame()
54 const {
55 return max_pixels_per_frame_;
56}
57
58const absl::optional<size_t>& VideoSourceRestrictions::target_pixels_per_frame()
59 const {
60 return target_pixels_per_frame_;
61}
62
63const absl::optional<double>& VideoSourceRestrictions::max_frame_rate() const {
64 return max_frame_rate_;
65}
66
67void 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
72void 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
77void VideoSourceRestrictions::set_max_frame_rate(
78 absl::optional<double> max_frame_rate) {
79 max_frame_rate_ = std::move(max_frame_rate);
80}
81
Evan Shrubsole3b6afee2020-05-07 13:55:54 +020082bool 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
95bool 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
Henrik Boströmb613e3a2020-04-17 13:48:21 +0200108bool DidIncreaseResolution(VideoSourceRestrictions restrictions_before,
109 VideoSourceRestrictions restrictions_after) {
Henrik Boström91aa7322020-04-28 12:24:33 +0200110 if (!restrictions_before.max_pixels_per_frame().has_value())
Henrik Boströmb613e3a2020-04-17 13:48:21 +0200111 return false;
Henrik Boströmb613e3a2020-04-17 13:48:21 +0200112 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
Evan Shrubsole3b6afee2020-05-07 13:55:54 +0200118bool 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
128bool 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
Henrik Boström91aa7322020-04-28 12:24:33 +0200138bool 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
Henrik Boströmce0ea492020-01-13 11:27:18 +0100148} // namespace webrtc