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