jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 1 | /* |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 2 | * Copyright (c) 2010 The WebRTC project authors. All Rights Reserved. |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 3 | * |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 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. |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 9 | */ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 10 | |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 11 | #ifndef WEBRTC_MEDIA_BASE_VIDEOADAPTER_H_ // NOLINT |
| 12 | #define WEBRTC_MEDIA_BASE_VIDEOADAPTER_H_ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 13 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 14 | #include "webrtc/base/common.h" // For ASSERT |
| 15 | #include "webrtc/base/criticalsection.h" |
perkj | 2d5f091 | 2016-02-29 00:04:41 -0800 | [diff] [blame] | 16 | #include "webrtc/base/optional.h" |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 17 | #include "webrtc/base/sigslot.h" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 18 | #include "webrtc/media/base/videocommon.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 19 | |
| 20 | namespace cricket { |
| 21 | |
| 22 | class VideoFrame; |
| 23 | |
| 24 | // VideoAdapter adapts an input video frame to an output frame based on the |
| 25 | // specified input and output formats. The adaptation includes dropping frames |
| 26 | // to reduce frame rate and scaling frames. VideoAdapter is thread safe. |
| 27 | class VideoAdapter { |
| 28 | public: |
| 29 | VideoAdapter(); |
| 30 | virtual ~VideoAdapter(); |
| 31 | |
mallinath@webrtc.org | 1b15f42 | 2013-09-06 22:56:28 +0000 | [diff] [blame] | 32 | virtual void SetInputFormat(const VideoFormat& format); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 33 | void SetOutputFormat(const VideoFormat& format); |
| 34 | // Constrain output resolution to this many pixels overall |
| 35 | void SetOutputNumPixels(int num_pixels); |
| 36 | int GetOutputNumPixels() const; |
| 37 | |
| 38 | const VideoFormat& input_format(); |
Magnus Jedvert | ac27e20 | 2015-03-24 15:18:39 +0100 | [diff] [blame] | 39 | // Returns true if the adapter will always return zero size from |
| 40 | // AdaptFrameResolution. |
henrike@webrtc.org | 704bf9e | 2014-02-27 17:52:04 +0000 | [diff] [blame] | 41 | bool drops_all_frames() const; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 42 | const VideoFormat& output_format(); |
magjed@webrtc.org | f58b455 | 2014-11-19 18:09:14 +0000 | [diff] [blame] | 43 | |
| 44 | // Return the adapted resolution given the input resolution. The returned |
| 45 | // resolution will be 0x0 if the frame should be dropped. |
| 46 | VideoFormat AdaptFrameResolution(int in_width, int in_height); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 47 | |
henrike@webrtc.org | d43aa9d | 2014-02-21 23:43:24 +0000 | [diff] [blame] | 48 | void set_scale_third(bool enable); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 49 | bool scale_third() const { return scale_third_; } |
| 50 | |
buildbot@webrtc.org | 71dffb7 | 2014-06-24 07:24:49 +0000 | [diff] [blame] | 51 | int adaptation_changes() const { return adaption_changes_; } |
| 52 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 53 | protected: |
| 54 | float FindClosestScale(int width, int height, int target_num_pixels); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 55 | float FindClosestViewScale(int width, int height, int target_num_pixels); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 56 | float FindLowerScale(int width, int height, int target_num_pixels); |
| 57 | |
| 58 | private: |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 59 | const float* GetViewScaleFactors() const; |
| 60 | float FindScale(const float* scale_factors, |
| 61 | const float upbias, int width, int height, |
| 62 | int target_num_pixels); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 63 | |
| 64 | VideoFormat input_format_; |
| 65 | VideoFormat output_format_; |
| 66 | int output_num_pixels_; |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 67 | bool scale_third_; // True if adapter allows scaling to 1/3 and 2/3. |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 68 | int frames_in_; // Number of input frames. |
| 69 | int frames_out_; // Number of output frames. |
| 70 | int frames_scaled_; // Number of frames scaled. |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 71 | int adaption_changes_; // Number of changes in scale factor. |
magjed@webrtc.org | a73d746 | 2014-11-14 13:25:25 +0000 | [diff] [blame] | 72 | size_t previous_width_; // Previous adapter output width. |
| 73 | size_t previous_height_; // Previous adapter output height. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 74 | int64_t interval_next_frame_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 75 | // The critical section to protect the above variables. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 76 | rtc::CriticalSection critical_section_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 77 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 78 | RTC_DISALLOW_COPY_AND_ASSIGN(VideoAdapter); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | // CoordinatedVideoAdapter adapts the video input to the encoder by coordinating |
| 82 | // the format request from the server, the resolution request from the encoder, |
| 83 | // and the CPU load. |
| 84 | class CoordinatedVideoAdapter |
| 85 | : public VideoAdapter, public sigslot::has_slots<> { |
| 86 | public: |
| 87 | enum AdaptRequest { UPGRADE, KEEP, DOWNGRADE }; |
wu@webrtc.org | 364f204 | 2013-11-20 21:49:41 +0000 | [diff] [blame] | 88 | enum AdaptReasonEnum { |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 89 | ADAPTREASON_NONE = 0, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 90 | ADAPTREASON_CPU = 1, |
| 91 | ADAPTREASON_BANDWIDTH = 2, |
| 92 | ADAPTREASON_VIEW = 4 |
| 93 | }; |
| 94 | typedef int AdaptReason; |
| 95 | |
| 96 | CoordinatedVideoAdapter(); |
| 97 | virtual ~CoordinatedVideoAdapter() {} |
| 98 | |
mallinath@webrtc.org | 1b15f42 | 2013-09-06 22:56:28 +0000 | [diff] [blame] | 99 | virtual void SetInputFormat(const VideoFormat& format); |
| 100 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 101 | // Enable or disable video adaptation due to the change of the CPU load. |
| 102 | void set_cpu_adaptation(bool enable) { cpu_adaptation_ = enable; } |
| 103 | bool cpu_adaptation() const { return cpu_adaptation_; } |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 104 | // Enable or disable smoothing when doing CPU adaptation. When smoothing is |
| 105 | // enabled, system CPU load is tracked using an exponential weighted |
| 106 | // average. |
henrike@webrtc.org | d43aa9d | 2014-02-21 23:43:24 +0000 | [diff] [blame] | 107 | void set_cpu_smoothing(bool enable); |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 108 | bool cpu_smoothing() const { return cpu_smoothing_; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 109 | // Enable or disable video adaptation due to the change of the GD |
| 110 | void set_gd_adaptation(bool enable) { gd_adaptation_ = enable; } |
| 111 | bool gd_adaptation() const { return gd_adaptation_; } |
| 112 | // Enable or disable video adaptation due to the change of the View |
| 113 | void set_view_adaptation(bool enable) { view_adaptation_ = enable; } |
| 114 | bool view_adaptation() const { return view_adaptation_; } |
| 115 | // Enable or disable video adaptation to fast switch View |
| 116 | void set_view_switch(bool enable) { view_switch_ = enable; } |
| 117 | bool view_switch() const { return view_switch_; } |
| 118 | |
| 119 | CoordinatedVideoAdapter::AdaptReason adapt_reason() const { |
| 120 | return adapt_reason_; |
| 121 | } |
| 122 | |
| 123 | // When the video is decreased, set the waiting time for CPU adaptation to |
| 124 | // decrease video again. |
henrike@webrtc.org | d43aa9d | 2014-02-21 23:43:24 +0000 | [diff] [blame] | 125 | void set_cpu_load_min_samples(int cpu_load_min_samples); |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 126 | int cpu_load_min_samples() const { return cpu_load_min_samples_; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 127 | // CPU system load high threshold for reducing resolution. e.g. 0.85f |
henrike@webrtc.org | d43aa9d | 2014-02-21 23:43:24 +0000 | [diff] [blame] | 128 | void set_high_system_threshold(float high_system_threshold); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 129 | float high_system_threshold() const { return high_system_threshold_; } |
| 130 | // CPU system load low threshold for increasing resolution. e.g. 0.70f |
henrike@webrtc.org | d43aa9d | 2014-02-21 23:43:24 +0000 | [diff] [blame] | 131 | void set_low_system_threshold(float low_system_threshold); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 132 | float low_system_threshold() const { return low_system_threshold_; } |
| 133 | // CPU process load threshold for reducing resolution. e.g. 0.10f |
henrike@webrtc.org | d43aa9d | 2014-02-21 23:43:24 +0000 | [diff] [blame] | 134 | void set_process_threshold(float process_threshold); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 135 | float process_threshold() const { return process_threshold_; } |
| 136 | |
| 137 | // Handle the format request from the server via Jingle update message. |
| 138 | void OnOutputFormatRequest(const VideoFormat& format); |
| 139 | // Handle the resolution request from the encoder due to bandwidth changes. |
| 140 | void OnEncoderResolutionRequest(int width, int height, AdaptRequest request); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 141 | // Handle the resolution request for CPU overuse. |
| 142 | void OnCpuResolutionRequest(AdaptRequest request); |
perkj | 2d5f091 | 2016-02-29 00:04:41 -0800 | [diff] [blame] | 143 | void OnCpuResolutionRequest(rtc::Optional<int> max_pixel_count, |
| 144 | rtc::Optional<int> max_pixel_count_step_up); |
| 145 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 146 | // Handle the CPU load provided by a CPU monitor. |
| 147 | void OnCpuLoadUpdated(int current_cpus, int max_cpus, |
| 148 | float process_load, float system_load); |
| 149 | |
| 150 | sigslot::signal0<> SignalCpuAdaptationUnable; |
| 151 | |
| 152 | private: |
| 153 | // Adapt to the minimum of the formats the server requests, the CPU wants, and |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 154 | // the encoder wants. Returns true if resolution changed. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 155 | bool AdaptToMinimumFormat(int* new_width, int* new_height); |
| 156 | bool IsMinimumFormat(int pixels); |
| 157 | void StepPixelCount(CoordinatedVideoAdapter::AdaptRequest request, |
| 158 | int* num_pixels); |
| 159 | CoordinatedVideoAdapter::AdaptRequest FindCpuRequest( |
| 160 | int current_cpus, int max_cpus, |
| 161 | float process_load, float system_load); |
| 162 | |
| 163 | bool cpu_adaptation_; // True if cpu adaptation is enabled. |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 164 | bool cpu_smoothing_; // True if cpu smoothing is enabled (with adaptation). |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 165 | bool gd_adaptation_; // True if gd adaptation is enabled. |
| 166 | bool view_adaptation_; // True if view adaptation is enabled. |
| 167 | bool view_switch_; // True if view switch is enabled. |
| 168 | int cpu_downgrade_count_; |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 169 | int cpu_load_min_samples_; |
| 170 | int cpu_load_num_samples_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 171 | // cpu system load thresholds relative to max cpus. |
| 172 | float high_system_threshold_; |
| 173 | float low_system_threshold_; |
| 174 | // cpu process load thresholds relative to current cpus. |
| 175 | float process_threshold_; |
| 176 | // Video formats that the server view requests, the CPU wants, and the encoder |
| 177 | // wants respectively. The adapted output format is the minimum of these. |
| 178 | int view_desired_num_pixels_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 179 | int64_t view_desired_interval_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 180 | int encoder_desired_num_pixels_; |
| 181 | int cpu_desired_num_pixels_; |
| 182 | CoordinatedVideoAdapter::AdaptReason adapt_reason_; |
| 183 | // The critical section to protect handling requests. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 184 | rtc::CriticalSection request_critical_section_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 185 | |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 186 | // The weighted average of cpu load over time. It's always updated (if cpu |
| 187 | // adaptation is on), but only used if cpu_smoothing_ is set. |
| 188 | float system_load_average_; |
| 189 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 190 | RTC_DISALLOW_COPY_AND_ASSIGN(CoordinatedVideoAdapter); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | } // namespace cricket |
| 194 | |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 195 | #endif // WEBRTC_MEDIA_BASE_VIDEOADAPTER_H_ // NOLINT |