balanced_degradation_settings.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright 2019 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. #ifndef RTC_BASE_EXPERIMENTS_BALANCED_DEGRADATION_SETTINGS_H_
  11. #define RTC_BASE_EXPERIMENTS_BALANCED_DEGRADATION_SETTINGS_H_
  12. #include <vector>
  13. #include "absl/types/optional.h"
  14. #include "api/video_codecs/video_encoder.h"
  15. namespace webrtc {
  16. class BalancedDegradationSettings {
  17. public:
  18. static constexpr int kNoFpsDiff = -100;
  19. BalancedDegradationSettings();
  20. ~BalancedDegradationSettings();
  21. struct CodecTypeSpecific {
  22. CodecTypeSpecific() {}
  23. CodecTypeSpecific(int qp_low, int qp_high, int fps, int kbps, int kbps_res)
  24. : qp_low(qp_low),
  25. qp_high(qp_high),
  26. fps(fps),
  27. kbps(kbps),
  28. kbps_res(kbps_res) {}
  29. bool operator==(const CodecTypeSpecific& o) const {
  30. return qp_low == o.qp_low && qp_high == o.qp_high && fps == o.fps &&
  31. kbps == o.kbps && kbps_res == o.kbps_res;
  32. }
  33. absl::optional<int> GetQpLow() const;
  34. absl::optional<int> GetQpHigh() const;
  35. absl::optional<int> GetFps() const;
  36. absl::optional<int> GetKbps() const;
  37. absl::optional<int> GetKbpsRes() const;
  38. // Optional settings.
  39. int qp_low = 0;
  40. int qp_high = 0;
  41. int fps = 0; // If unset, defaults to |fps| in Config.
  42. int kbps = 0; // If unset, defaults to |kbps| in Config.
  43. int kbps_res = 0; // If unset, defaults to |kbps_res| in Config.
  44. };
  45. struct Config {
  46. Config();
  47. Config(int pixels,
  48. int fps,
  49. int kbps,
  50. int kbps_res,
  51. int fps_diff,
  52. CodecTypeSpecific vp8,
  53. CodecTypeSpecific vp9,
  54. CodecTypeSpecific h264,
  55. CodecTypeSpecific av1,
  56. CodecTypeSpecific generic);
  57. bool operator==(const Config& o) const {
  58. return pixels == o.pixels && fps == o.fps && kbps == o.kbps &&
  59. kbps_res == o.kbps_res && fps_diff == o.fps_diff && vp8 == o.vp8 &&
  60. vp9 == o.vp9 && h264 == o.h264 && av1 == o.av1 &&
  61. generic == o.generic;
  62. }
  63. // Example:
  64. // WebRTC-Video-BalancedDegradationSettings/pixels:100|200|300,fps:5|15|25/
  65. // pixels <= 100 -> min framerate: 5 fps
  66. // pixels <= 200 -> min framerate: 15 fps
  67. // pixels <= 300 -> min framerate: 25 fps
  68. //
  69. // WebRTC-Video-BalancedDegradationSettings/pixels:100|200|300,
  70. // fps:5|15|25, // Min framerate.
  71. // kbps:0|60|70, // Min bitrate needed to adapt up.
  72. // kbps_res:0|65|75/ // Min bitrate needed to adapt up in resolution.
  73. //
  74. // pixels: fps: kbps: kbps_res:
  75. // 300 30 - -
  76. // 300 25 70 kbps 75 kbps
  77. // 200 25 70 kbps -
  78. // 200 15 60 kbps 65 kbps
  79. // 100 15 60 kbps -
  80. // 100 5
  81. // optional optional
  82. int pixels = 0; // Video frame size.
  83. // If the frame size is less than or equal to |pixels|:
  84. int fps = 0; // Min framerate to be used.
  85. int kbps = 0; // Min bitrate needed to adapt up (resolution/fps).
  86. int kbps_res = 0; // Min bitrate needed to adapt up in resolution.
  87. int fps_diff = kNoFpsDiff; // Min fps reduction needed (input fps - |fps|)
  88. // w/o triggering a new subsequent downgrade
  89. // check.
  90. CodecTypeSpecific vp8;
  91. CodecTypeSpecific vp9;
  92. CodecTypeSpecific h264;
  93. CodecTypeSpecific av1;
  94. CodecTypeSpecific generic;
  95. };
  96. // Returns configurations from field trial on success (default on failure).
  97. std::vector<Config> GetConfigs() const;
  98. // Gets the min/max framerate from |configs_| based on |pixels|.
  99. int MinFps(VideoCodecType type, int pixels) const;
  100. int MaxFps(VideoCodecType type, int pixels) const;
  101. // Checks if quality can be increased based on |pixels| and |bitrate_bps|.
  102. bool CanAdaptUp(VideoCodecType type, int pixels, uint32_t bitrate_bps) const;
  103. bool CanAdaptUpResolution(VideoCodecType type,
  104. int pixels,
  105. uint32_t bitrate_bps) const;
  106. // Gets the min framerate diff from |configs_| based on |pixels|.
  107. absl::optional<int> MinFpsDiff(int pixels) const;
  108. // Gets QpThresholds for the codec |type| based on |pixels|.
  109. absl::optional<VideoEncoder::QpThresholds> GetQpThresholds(
  110. VideoCodecType type,
  111. int pixels) const;
  112. private:
  113. absl::optional<Config> GetMinFpsConfig(int pixels) const;
  114. absl::optional<Config> GetMaxFpsConfig(int pixels) const;
  115. Config GetConfig(int pixels) const;
  116. std::vector<Config> configs_;
  117. };
  118. } // namespace webrtc
  119. #endif // RTC_BASE_EXPERIMENTS_BALANCED_DEGRADATION_SETTINGS_H_