tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

video_source_restrictions.cc (6700B)


      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 <algorithm>
     14 #include <cstddef>
     15 #include <limits>
     16 #include <optional>
     17 #include <string>
     18 #include <utility>
     19 
     20 #include "rtc_base/checks.h"
     21 #include "rtc_base/strings/string_builder.h"
     22 
     23 namespace webrtc {
     24 
     25 VideoSourceRestrictions::VideoSourceRestrictions()
     26    : max_pixels_per_frame_(std::nullopt),
     27      target_pixels_per_frame_(std::nullopt),
     28      max_frame_rate_(std::nullopt) {}
     29 
     30 VideoSourceRestrictions::VideoSourceRestrictions(
     31    std::optional<size_t> max_pixels_per_frame,
     32    std::optional<size_t> target_pixels_per_frame,
     33    std::optional<double> max_frame_rate)
     34    : max_pixels_per_frame_(std::move(max_pixels_per_frame)),
     35      target_pixels_per_frame_(std::move(target_pixels_per_frame)),
     36      max_frame_rate_(std::move(max_frame_rate)) {
     37  RTC_DCHECK(!max_pixels_per_frame_.has_value() ||
     38             max_pixels_per_frame_.value() <
     39                 static_cast<size_t>(std::numeric_limits<int>::max()));
     40  RTC_DCHECK(!max_frame_rate_.has_value() ||
     41             max_frame_rate_.value() < std::numeric_limits<int>::max());
     42  RTC_DCHECK(!max_frame_rate_.has_value() || max_frame_rate_.value() > 0.0);
     43 }
     44 
     45 std::string VideoSourceRestrictions::ToString() const {
     46  StringBuilder ss;
     47  ss << "{";
     48  if (max_frame_rate_)
     49    ss << " max_fps=" << max_frame_rate_.value();
     50  if (max_pixels_per_frame_)
     51    ss << " max_pixels_per_frame=" << max_pixels_per_frame_.value();
     52  if (target_pixels_per_frame_)
     53    ss << " target_pixels_per_frame=" << target_pixels_per_frame_.value();
     54  ss << " }";
     55  return ss.Release();
     56 }
     57 
     58 const std::optional<size_t>& VideoSourceRestrictions::max_pixels_per_frame()
     59    const {
     60  return max_pixels_per_frame_;
     61 }
     62 
     63 const std::optional<size_t>& VideoSourceRestrictions::target_pixels_per_frame()
     64    const {
     65  return target_pixels_per_frame_;
     66 }
     67 
     68 const std::optional<double>& VideoSourceRestrictions::max_frame_rate() const {
     69  return max_frame_rate_;
     70 }
     71 
     72 void VideoSourceRestrictions::set_max_pixels_per_frame(
     73    std::optional<size_t> max_pixels_per_frame) {
     74  max_pixels_per_frame_ = std::move(max_pixels_per_frame);
     75 }
     76 
     77 void VideoSourceRestrictions::set_target_pixels_per_frame(
     78    std::optional<size_t> target_pixels_per_frame) {
     79  target_pixels_per_frame_ = std::move(target_pixels_per_frame);
     80 }
     81 
     82 void VideoSourceRestrictions::set_max_frame_rate(
     83    std::optional<double> max_frame_rate) {
     84  max_frame_rate_ = std::move(max_frame_rate);
     85 }
     86 
     87 void VideoSourceRestrictions::UpdateMin(const VideoSourceRestrictions& other) {
     88  if (max_pixels_per_frame_.has_value()) {
     89    max_pixels_per_frame_ = std::min(*max_pixels_per_frame_,
     90                                     other.max_pixels_per_frame().value_or(
     91                                         std::numeric_limits<size_t>::max()));
     92  } else {
     93    max_pixels_per_frame_ = other.max_pixels_per_frame();
     94  }
     95  if (target_pixels_per_frame_.has_value()) {
     96    target_pixels_per_frame_ = std::min(
     97        *target_pixels_per_frame_, other.target_pixels_per_frame().value_or(
     98                                       std::numeric_limits<size_t>::max()));
     99  } else {
    100    target_pixels_per_frame_ = other.target_pixels_per_frame();
    101  }
    102  if (max_frame_rate_.has_value()) {
    103    max_frame_rate_ = std::min(
    104        *max_frame_rate_,
    105        other.max_frame_rate().value_or(std::numeric_limits<double>::max()));
    106  } else {
    107    max_frame_rate_ = other.max_frame_rate();
    108  }
    109 }
    110 
    111 bool DidRestrictionsIncrease(VideoSourceRestrictions before,
    112                             VideoSourceRestrictions after) {
    113  bool decreased_resolution = DidDecreaseResolution(before, after);
    114  bool decreased_framerate = DidDecreaseFrameRate(before, after);
    115  bool same_resolution =
    116      before.max_pixels_per_frame() == after.max_pixels_per_frame();
    117  bool same_framerate = before.max_frame_rate() == after.max_frame_rate();
    118 
    119  return (decreased_resolution && decreased_framerate) ||
    120         (decreased_resolution && same_framerate) ||
    121         (same_resolution && decreased_framerate);
    122 }
    123 
    124 bool DidRestrictionsDecrease(VideoSourceRestrictions before,
    125                             VideoSourceRestrictions after) {
    126  bool increased_resolution = DidIncreaseResolution(before, after);
    127  bool increased_framerate = DidIncreaseFrameRate(before, after);
    128  bool same_resolution =
    129      before.max_pixels_per_frame() == after.max_pixels_per_frame();
    130  bool same_framerate = before.max_frame_rate() == after.max_frame_rate();
    131 
    132  return (increased_resolution && increased_framerate) ||
    133         (increased_resolution && same_framerate) ||
    134         (same_resolution && increased_framerate);
    135 }
    136 
    137 bool DidIncreaseResolution(VideoSourceRestrictions restrictions_before,
    138                           VideoSourceRestrictions restrictions_after) {
    139  if (!restrictions_before.max_pixels_per_frame().has_value())
    140    return false;
    141  if (!restrictions_after.max_pixels_per_frame().has_value())
    142    return true;
    143  return restrictions_after.max_pixels_per_frame().value() >
    144         restrictions_before.max_pixels_per_frame().value();
    145 }
    146 
    147 bool DidDecreaseResolution(VideoSourceRestrictions restrictions_before,
    148                           VideoSourceRestrictions restrictions_after) {
    149  if (!restrictions_after.max_pixels_per_frame().has_value())
    150    return false;
    151  if (!restrictions_before.max_pixels_per_frame().has_value())
    152    return true;
    153  return restrictions_after.max_pixels_per_frame().value() <
    154         restrictions_before.max_pixels_per_frame().value();
    155 }
    156 
    157 bool DidIncreaseFrameRate(VideoSourceRestrictions restrictions_before,
    158                          VideoSourceRestrictions restrictions_after) {
    159  if (!restrictions_before.max_frame_rate().has_value())
    160    return false;
    161  if (!restrictions_after.max_frame_rate().has_value())
    162    return true;
    163  return restrictions_after.max_frame_rate().value() >
    164         restrictions_before.max_frame_rate().value();
    165 }
    166 
    167 bool DidDecreaseFrameRate(VideoSourceRestrictions restrictions_before,
    168                          VideoSourceRestrictions restrictions_after) {
    169  if (!restrictions_after.max_frame_rate().has_value())
    170    return false;
    171  if (!restrictions_before.max_frame_rate().has_value())
    172    return true;
    173  return restrictions_after.max_frame_rate().value() <
    174         restrictions_before.max_frame_rate().value();
    175 }
    176 
    177 }  // namespace webrtc