blob: 0146182d1d9d300894c54b77b7b29a682263fea4 [file] [log] [blame]
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00001/*
kjellander1afca732016-02-07 20:46:45 -08002 * Copyright (c) 2010 The WebRTC project authors. All Rights Reserved.
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 *
kjellander1afca732016-02-07 20:46:45 -08004 * 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.org5f93d0a2015-01-20 21:36:13 +00009 */
henrike@webrtc.org28e20752013-07-10 00:45:36 +000010
kjellandera96e2d72016-02-04 23:52:28 -080011#ifndef WEBRTC_MEDIA_BASE_VIDEOADAPTER_H_ // NOLINT
12#define WEBRTC_MEDIA_BASE_VIDEOADAPTER_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000013
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000014#include "webrtc/base/common.h" // For ASSERT
15#include "webrtc/base/criticalsection.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000016#include "webrtc/base/sigslot.h"
kjellandera96e2d72016-02-04 23:52:28 -080017#include "webrtc/media/base/videocommon.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000018
19namespace cricket {
20
21class VideoFrame;
22
23// VideoAdapter adapts an input video frame to an output frame based on the
24// specified input and output formats. The adaptation includes dropping frames
25// to reduce frame rate and scaling frames. VideoAdapter is thread safe.
26class VideoAdapter {
27 public:
28 VideoAdapter();
29 virtual ~VideoAdapter();
30
mallinath@webrtc.org1b15f422013-09-06 22:56:28 +000031 virtual void SetInputFormat(const VideoFormat& format);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032 void SetOutputFormat(const VideoFormat& format);
33 // Constrain output resolution to this many pixels overall
34 void SetOutputNumPixels(int num_pixels);
35 int GetOutputNumPixels() const;
36
37 const VideoFormat& input_format();
Magnus Jedvertac27e202015-03-24 15:18:39 +010038 // Returns true if the adapter will always return zero size from
39 // AdaptFrameResolution.
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +000040 bool drops_all_frames() const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041 const VideoFormat& output_format();
magjed@webrtc.orgf58b4552014-11-19 18:09:14 +000042
43 // Return the adapted resolution given the input resolution. The returned
44 // resolution will be 0x0 if the frame should be dropped.
45 VideoFormat AdaptFrameResolution(int in_width, int in_height);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +000047 void set_scale_third(bool enable);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +000048 bool scale_third() const { return scale_third_; }
49
buildbot@webrtc.org71dffb72014-06-24 07:24:49 +000050 int adaptation_changes() const { return adaption_changes_; }
51
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052 protected:
53 float FindClosestScale(int width, int height, int target_num_pixels);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +000054 float FindClosestViewScale(int width, int height, int target_num_pixels);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055 float FindLowerScale(int width, int height, int target_num_pixels);
56
57 private:
wu@webrtc.orgcadf9042013-08-30 21:24:16 +000058 const float* GetViewScaleFactors() const;
59 float FindScale(const float* scale_factors,
60 const float upbias, int width, int height,
61 int target_num_pixels);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062
63 VideoFormat input_format_;
64 VideoFormat output_format_;
65 int output_num_pixels_;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +000066 bool scale_third_; // True if adapter allows scaling to 1/3 and 2/3.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000067 int frames_in_; // Number of input frames.
68 int frames_out_; // Number of output frames.
69 int frames_scaled_; // Number of frames scaled.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +000070 int adaption_changes_; // Number of changes in scale factor.
magjed@webrtc.orga73d7462014-11-14 13:25:25 +000071 size_t previous_width_; // Previous adapter output width.
72 size_t previous_height_; // Previous adapter output height.
Peter Boström0c4e06b2015-10-07 12:23:21 +020073 int64_t interval_next_frame_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074 // The critical section to protect the above variables.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000075 rtc::CriticalSection critical_section_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076
henrikg3c089d72015-09-16 05:37:44 -070077 RTC_DISALLOW_COPY_AND_ASSIGN(VideoAdapter);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078};
79
80// CoordinatedVideoAdapter adapts the video input to the encoder by coordinating
81// the format request from the server, the resolution request from the encoder,
82// and the CPU load.
83class CoordinatedVideoAdapter
84 : public VideoAdapter, public sigslot::has_slots<> {
85 public:
86 enum AdaptRequest { UPGRADE, KEEP, DOWNGRADE };
wu@webrtc.org364f2042013-11-20 21:49:41 +000087 enum AdaptReasonEnum {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000088 ADAPTREASON_NONE = 0,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089 ADAPTREASON_CPU = 1,
90 ADAPTREASON_BANDWIDTH = 2,
91 ADAPTREASON_VIEW = 4
92 };
93 typedef int AdaptReason;
94
95 CoordinatedVideoAdapter();
96 virtual ~CoordinatedVideoAdapter() {}
97
mallinath@webrtc.org1b15f422013-09-06 22:56:28 +000098 virtual void SetInputFormat(const VideoFormat& format);
99
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100 // Enable or disable video adaptation due to the change of the CPU load.
101 void set_cpu_adaptation(bool enable) { cpu_adaptation_ = enable; }
102 bool cpu_adaptation() const { return cpu_adaptation_; }
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000103 // Enable or disable smoothing when doing CPU adaptation. When smoothing is
104 // enabled, system CPU load is tracked using an exponential weighted
105 // average.
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000106 void set_cpu_smoothing(bool enable);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000107 bool cpu_smoothing() const { return cpu_smoothing_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000108 // Enable or disable video adaptation due to the change of the GD
109 void set_gd_adaptation(bool enable) { gd_adaptation_ = enable; }
110 bool gd_adaptation() const { return gd_adaptation_; }
111 // Enable or disable video adaptation due to the change of the View
112 void set_view_adaptation(bool enable) { view_adaptation_ = enable; }
113 bool view_adaptation() const { return view_adaptation_; }
114 // Enable or disable video adaptation to fast switch View
115 void set_view_switch(bool enable) { view_switch_ = enable; }
116 bool view_switch() const { return view_switch_; }
117
118 CoordinatedVideoAdapter::AdaptReason adapt_reason() const {
119 return adapt_reason_;
120 }
121
122 // When the video is decreased, set the waiting time for CPU adaptation to
123 // decrease video again.
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000124 void set_cpu_load_min_samples(int cpu_load_min_samples);
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000125 int cpu_load_min_samples() const { return cpu_load_min_samples_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126 // CPU system load high threshold for reducing resolution. e.g. 0.85f
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000127 void set_high_system_threshold(float high_system_threshold);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128 float high_system_threshold() const { return high_system_threshold_; }
129 // CPU system load low threshold for increasing resolution. e.g. 0.70f
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000130 void set_low_system_threshold(float low_system_threshold);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131 float low_system_threshold() const { return low_system_threshold_; }
132 // CPU process load threshold for reducing resolution. e.g. 0.10f
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000133 void set_process_threshold(float process_threshold);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134 float process_threshold() const { return process_threshold_; }
135
136 // Handle the format request from the server via Jingle update message.
137 void OnOutputFormatRequest(const VideoFormat& format);
138 // Handle the resolution request from the encoder due to bandwidth changes.
139 void OnEncoderResolutionRequest(int width, int height, AdaptRequest request);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000140 // Handle the resolution request for CPU overuse.
141 void OnCpuResolutionRequest(AdaptRequest request);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000142 // Handle the CPU load provided by a CPU monitor.
143 void OnCpuLoadUpdated(int current_cpus, int max_cpus,
144 float process_load, float system_load);
145
146 sigslot::signal0<> SignalCpuAdaptationUnable;
147
148 private:
149 // Adapt to the minimum of the formats the server requests, the CPU wants, and
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000150 // the encoder wants. Returns true if resolution changed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151 bool AdaptToMinimumFormat(int* new_width, int* new_height);
152 bool IsMinimumFormat(int pixels);
153 void StepPixelCount(CoordinatedVideoAdapter::AdaptRequest request,
154 int* num_pixels);
155 CoordinatedVideoAdapter::AdaptRequest FindCpuRequest(
156 int current_cpus, int max_cpus,
157 float process_load, float system_load);
158
159 bool cpu_adaptation_; // True if cpu adaptation is enabled.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000160 bool cpu_smoothing_; // True if cpu smoothing is enabled (with adaptation).
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161 bool gd_adaptation_; // True if gd adaptation is enabled.
162 bool view_adaptation_; // True if view adaptation is enabled.
163 bool view_switch_; // True if view switch is enabled.
164 int cpu_downgrade_count_;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000165 int cpu_load_min_samples_;
166 int cpu_load_num_samples_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167 // cpu system load thresholds relative to max cpus.
168 float high_system_threshold_;
169 float low_system_threshold_;
170 // cpu process load thresholds relative to current cpus.
171 float process_threshold_;
172 // Video formats that the server view requests, the CPU wants, and the encoder
173 // wants respectively. The adapted output format is the minimum of these.
174 int view_desired_num_pixels_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200175 int64_t view_desired_interval_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176 int encoder_desired_num_pixels_;
177 int cpu_desired_num_pixels_;
178 CoordinatedVideoAdapter::AdaptReason adapt_reason_;
179 // The critical section to protect handling requests.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000180 rtc::CriticalSection request_critical_section_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000182 // The weighted average of cpu load over time. It's always updated (if cpu
183 // adaptation is on), but only used if cpu_smoothing_ is set.
184 float system_load_average_;
185
henrikg3c089d72015-09-16 05:37:44 -0700186 RTC_DISALLOW_COPY_AND_ASSIGN(CoordinatedVideoAdapter);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187};
188
189} // namespace cricket
190
kjellandera96e2d72016-02-04 23:52:28 -0800191#endif // WEBRTC_MEDIA_BASE_VIDEOADAPTER_H_ // NOLINT