Henrik Boström | ce0ea49 | 2020-01-13 10:27:18 | [diff] [blame] | 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 "video/video_source_sink_controller.h" |
| 12 | |
| 13 | #include <algorithm> |
| 14 | #include <limits> |
| 15 | #include <utility> |
| 16 | |
Evan Shrubsole | 236e0ed | 2020-05-20 10:24:57 | [diff] [blame] | 17 | #include "rtc_base/logging.h" |
Henrik Boström | ce0ea49 | 2020-01-13 10:27:18 | [diff] [blame] | 18 | #include "rtc_base/numerics/safe_conversions.h" |
Henrik Boström | e2e8c17 | 2020-06-03 07:24:06 | [diff] [blame] | 19 | #include "rtc_base/strings/string_builder.h" |
Henrik Boström | ce0ea49 | 2020-01-13 10:27:18 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | |
| 23 | VideoSourceSinkController::VideoSourceSinkController( |
| 24 | rtc::VideoSinkInterface<VideoFrame>* sink, |
| 25 | rtc::VideoSourceInterface<VideoFrame>* source) |
Henrik Boström | 8234b92 | 2020-01-13 16:26:50 | [diff] [blame] | 26 | : sink_(sink), source_(source) { |
Henrik Boström | ce0ea49 | 2020-01-13 10:27:18 | [diff] [blame] | 27 | RTC_DCHECK(sink_); |
| 28 | } |
| 29 | |
Tomas Gunnarsson | 612445e | 2020-09-21 12:31:23 | [diff] [blame] | 30 | VideoSourceSinkController::~VideoSourceSinkController() { |
| 31 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 32 | } |
| 33 | |
Henrik Boström | ce0ea49 | 2020-01-13 10:27:18 | [diff] [blame] | 34 | void VideoSourceSinkController::SetSource( |
Henrik Boström | 8234b92 | 2020-01-13 16:26:50 | [diff] [blame] | 35 | rtc::VideoSourceInterface<VideoFrame>* source) { |
Tomas Gunnarsson | 612445e | 2020-09-21 12:31:23 | [diff] [blame] | 36 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 37 | |
| 38 | rtc::VideoSourceInterface<VideoFrame>* old_source = source_; |
| 39 | source_ = source; |
| 40 | |
Henrik Boström | ce0ea49 | 2020-01-13 10:27:18 | [diff] [blame] | 41 | if (old_source != source && old_source) |
| 42 | old_source->RemoveSink(sink_); |
Tomas Gunnarsson | 612445e | 2020-09-21 12:31:23 | [diff] [blame] | 43 | |
Henrik Boström | ce0ea49 | 2020-01-13 10:27:18 | [diff] [blame] | 44 | if (!source) |
| 45 | return; |
Tomas Gunnarsson | 612445e | 2020-09-21 12:31:23 | [diff] [blame] | 46 | |
| 47 | source->AddOrUpdateSink(sink_, CurrentSettingsToSinkWants()); |
| 48 | } |
| 49 | |
| 50 | bool VideoSourceSinkController::HasSource() const { |
| 51 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 52 | return source_ != nullptr; |
Henrik Boström | ce0ea49 | 2020-01-13 10:27:18 | [diff] [blame] | 53 | } |
| 54 | |
Markus Handell | 2e0f4f0 | 2021-12-21 18:14:58 | [diff] [blame] | 55 | void VideoSourceSinkController::RequestRefreshFrame() { |
| 56 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 57 | if (source_) |
| 58 | source_->RequestRefreshFrame(); |
| 59 | } |
| 60 | |
Henrik Boström | ce0ea49 | 2020-01-13 10:27:18 | [diff] [blame] | 61 | void VideoSourceSinkController::PushSourceSinkSettings() { |
Tomas Gunnarsson | 612445e | 2020-09-21 12:31:23 | [diff] [blame] | 62 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
Henrik Boström | ce0ea49 | 2020-01-13 10:27:18 | [diff] [blame] | 63 | if (!source_) |
| 64 | return; |
Evan Shrubsole | 236e0ed | 2020-05-20 10:24:57 | [diff] [blame] | 65 | rtc::VideoSinkWants wants = CurrentSettingsToSinkWants(); |
Evan Shrubsole | 236e0ed | 2020-05-20 10:24:57 | [diff] [blame] | 66 | source_->AddOrUpdateSink(sink_, wants); |
Henrik Boström | ce0ea49 | 2020-01-13 10:27:18 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | VideoSourceRestrictions VideoSourceSinkController::restrictions() const { |
Tomas Gunnarsson | 612445e | 2020-09-21 12:31:23 | [diff] [blame] | 70 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
Henrik Boström | ce0ea49 | 2020-01-13 10:27:18 | [diff] [blame] | 71 | return restrictions_; |
| 72 | } |
| 73 | |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 74 | std::optional<size_t> VideoSourceSinkController::pixels_per_frame_upper_limit() |
Henrik Boström | ce0ea49 | 2020-01-13 10:27:18 | [diff] [blame] | 75 | const { |
Tomas Gunnarsson | 612445e | 2020-09-21 12:31:23 | [diff] [blame] | 76 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
Henrik Boström | ce0ea49 | 2020-01-13 10:27:18 | [diff] [blame] | 77 | return pixels_per_frame_upper_limit_; |
| 78 | } |
| 79 | |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 80 | std::optional<double> VideoSourceSinkController::frame_rate_upper_limit() |
Henrik Boström | ce0ea49 | 2020-01-13 10:27:18 | [diff] [blame] | 81 | const { |
Tomas Gunnarsson | 612445e | 2020-09-21 12:31:23 | [diff] [blame] | 82 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
Henrik Boström | ce0ea49 | 2020-01-13 10:27:18 | [diff] [blame] | 83 | return frame_rate_upper_limit_; |
| 84 | } |
| 85 | |
| 86 | bool VideoSourceSinkController::rotation_applied() const { |
Tomas Gunnarsson | 612445e | 2020-09-21 12:31:23 | [diff] [blame] | 87 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
Henrik Boström | ce0ea49 | 2020-01-13 10:27:18 | [diff] [blame] | 88 | return rotation_applied_; |
| 89 | } |
| 90 | |
| 91 | int VideoSourceSinkController::resolution_alignment() const { |
Tomas Gunnarsson | 612445e | 2020-09-21 12:31:23 | [diff] [blame] | 92 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
Henrik Boström | ce0ea49 | 2020-01-13 10:27:18 | [diff] [blame] | 93 | return resolution_alignment_; |
| 94 | } |
| 95 | |
Henrik Boström | 1124ed1 | 2021-02-25 09:30:39 | [diff] [blame] | 96 | const std::vector<rtc::VideoSinkWants::FrameSize>& |
| 97 | VideoSourceSinkController::resolutions() const { |
| 98 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 99 | return resolutions_; |
| 100 | } |
| 101 | |
Jonas Oreland | 0deda15 | 2022-09-23 10:08:57 | [diff] [blame] | 102 | bool VideoSourceSinkController::active() const { |
| 103 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 104 | return active_; |
| 105 | } |
| 106 | |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 107 | std::optional<rtc::VideoSinkWants::FrameSize> |
Henrik Boström | e8c97c0 | 2024-10-25 07:51:44 | [diff] [blame] | 108 | VideoSourceSinkController::scale_resolution_down_to() const { |
Jonas Oreland | 0deda15 | 2022-09-23 10:08:57 | [diff] [blame] | 109 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
Henrik Boström | e8c97c0 | 2024-10-25 07:51:44 | [diff] [blame] | 110 | return scale_resolution_down_to_; |
Jonas Oreland | 0deda15 | 2022-09-23 10:08:57 | [diff] [blame] | 111 | } |
| 112 | |
Henrik Boström | ce0ea49 | 2020-01-13 10:27:18 | [diff] [blame] | 113 | void VideoSourceSinkController::SetRestrictions( |
| 114 | VideoSourceRestrictions restrictions) { |
Tomas Gunnarsson | 612445e | 2020-09-21 12:31:23 | [diff] [blame] | 115 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
Henrik Boström | ce0ea49 | 2020-01-13 10:27:18 | [diff] [blame] | 116 | restrictions_ = std::move(restrictions); |
| 117 | } |
| 118 | |
| 119 | void VideoSourceSinkController::SetPixelsPerFrameUpperLimit( |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 120 | std::optional<size_t> pixels_per_frame_upper_limit) { |
Tomas Gunnarsson | 612445e | 2020-09-21 12:31:23 | [diff] [blame] | 121 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
Henrik Boström | ce0ea49 | 2020-01-13 10:27:18 | [diff] [blame] | 122 | pixels_per_frame_upper_limit_ = std::move(pixels_per_frame_upper_limit); |
| 123 | } |
| 124 | |
| 125 | void VideoSourceSinkController::SetFrameRateUpperLimit( |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 126 | std::optional<double> frame_rate_upper_limit) { |
Tomas Gunnarsson | 612445e | 2020-09-21 12:31:23 | [diff] [blame] | 127 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
Henrik Boström | ce0ea49 | 2020-01-13 10:27:18 | [diff] [blame] | 128 | frame_rate_upper_limit_ = std::move(frame_rate_upper_limit); |
| 129 | } |
| 130 | |
| 131 | void VideoSourceSinkController::SetRotationApplied(bool rotation_applied) { |
Tomas Gunnarsson | 612445e | 2020-09-21 12:31:23 | [diff] [blame] | 132 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
Henrik Boström | ce0ea49 | 2020-01-13 10:27:18 | [diff] [blame] | 133 | rotation_applied_ = rotation_applied; |
| 134 | } |
| 135 | |
| 136 | void VideoSourceSinkController::SetResolutionAlignment( |
| 137 | int resolution_alignment) { |
Tomas Gunnarsson | 612445e | 2020-09-21 12:31:23 | [diff] [blame] | 138 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
Henrik Boström | ce0ea49 | 2020-01-13 10:27:18 | [diff] [blame] | 139 | resolution_alignment_ = resolution_alignment; |
| 140 | } |
| 141 | |
Henrik Boström | 1124ed1 | 2021-02-25 09:30:39 | [diff] [blame] | 142 | void VideoSourceSinkController::SetResolutions( |
| 143 | std::vector<rtc::VideoSinkWants::FrameSize> resolutions) { |
| 144 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 145 | resolutions_ = std::move(resolutions); |
| 146 | } |
| 147 | |
Jonas Oreland | 0deda15 | 2022-09-23 10:08:57 | [diff] [blame] | 148 | void VideoSourceSinkController::SetActive(bool active) { |
| 149 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 150 | active_ = active; |
| 151 | } |
| 152 | |
Henrik Boström | e8c97c0 | 2024-10-25 07:51:44 | [diff] [blame] | 153 | void VideoSourceSinkController::SetScaleResolutionDownTo( |
| 154 | std::optional<rtc::VideoSinkWants::FrameSize> scale_resolution_down_to) { |
Jonas Oreland | 0deda15 | 2022-09-23 10:08:57 | [diff] [blame] | 155 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
Henrik Boström | e8c97c0 | 2024-10-25 07:51:44 | [diff] [blame] | 156 | scale_resolution_down_to_ = std::move(scale_resolution_down_to); |
Jonas Oreland | 0deda15 | 2022-09-23 10:08:57 | [diff] [blame] | 157 | } |
| 158 | |
Tomas Gunnarsson | 612445e | 2020-09-21 12:31:23 | [diff] [blame] | 159 | // RTC_EXCLUSIVE_LOCKS_REQUIRED(sequence_checker_) |
Henrik Boström | ce0ea49 | 2020-01-13 10:27:18 | [diff] [blame] | 160 | rtc::VideoSinkWants VideoSourceSinkController::CurrentSettingsToSinkWants() |
| 161 | const { |
Henrik Boström | ce0ea49 | 2020-01-13 10:27:18 | [diff] [blame] | 162 | rtc::VideoSinkWants wants; |
| 163 | wants.rotation_applied = rotation_applied_; |
Artem Titov | cfea218 | 2021-08-09 23:22:31 | [diff] [blame] | 164 | // `wants.black_frames` is not used, it always has its default value false. |
Henrik Boström | ce0ea49 | 2020-01-13 10:27:18 | [diff] [blame] | 165 | wants.max_pixel_count = |
| 166 | rtc::dchecked_cast<int>(restrictions_.max_pixels_per_frame().value_or( |
| 167 | std::numeric_limits<int>::max())); |
| 168 | wants.target_pixel_count = |
| 169 | restrictions_.target_pixels_per_frame().has_value() |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 170 | ? std::optional<int>(rtc::dchecked_cast<int>( |
Henrik Boström | ce0ea49 | 2020-01-13 10:27:18 | [diff] [blame] | 171 | restrictions_.target_pixels_per_frame().value())) |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 172 | : std::nullopt; |
Henrik Boström | ce0ea49 | 2020-01-13 10:27:18 | [diff] [blame] | 173 | wants.max_framerate_fps = |
| 174 | restrictions_.max_frame_rate().has_value() |
| 175 | ? static_cast<int>(restrictions_.max_frame_rate().value()) |
| 176 | : std::numeric_limits<int>::max(); |
| 177 | wants.resolution_alignment = resolution_alignment_; |
Henrik Boström | ce0ea49 | 2020-01-13 10:27:18 | [diff] [blame] | 178 | wants.max_pixel_count = |
| 179 | std::min(wants.max_pixel_count, |
| 180 | rtc::dchecked_cast<int>(pixels_per_frame_upper_limit_.value_or( |
| 181 | std::numeric_limits<int>::max()))); |
| 182 | wants.max_framerate_fps = |
| 183 | std::min(wants.max_framerate_fps, |
| 184 | frame_rate_upper_limit_.has_value() |
| 185 | ? static_cast<int>(frame_rate_upper_limit_.value()) |
| 186 | : std::numeric_limits<int>::max()); |
Henrik Boström | 1124ed1 | 2021-02-25 09:30:39 | [diff] [blame] | 187 | wants.resolutions = resolutions_; |
Jonas Oreland | 0deda15 | 2022-09-23 10:08:57 | [diff] [blame] | 188 | wants.is_active = active_; |
Henrik Boström | e8c97c0 | 2024-10-25 07:51:44 | [diff] [blame] | 189 | wants.requested_resolution = scale_resolution_down_to_; |
Henrik Boström | ce0ea49 | 2020-01-13 10:27:18 | [diff] [blame] | 190 | return wants; |
| 191 | } |
| 192 | |
| 193 | } // namespace webrtc |