blob: 13a342d86eaec0068b4f0f262df06f312a94d807 [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"
perkj2d5f0912016-02-29 00:04:41 -080016#include "webrtc/base/optional.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000017#include "webrtc/base/sigslot.h"
kjellandera96e2d72016-02-04 23:52:28 -080018#include "webrtc/media/base/videocommon.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019
20namespace cricket {
21
22class 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.
27class VideoAdapter {
28 public:
29 VideoAdapter();
30 virtual ~VideoAdapter();
31
mallinath@webrtc.org1b15f422013-09-06 22:56:28 +000032 virtual void SetInputFormat(const VideoFormat& format);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033 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 Jedvertac27e202015-03-24 15:18:39 +010039 // Returns true if the adapter will always return zero size from
40 // AdaptFrameResolution.
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +000041 bool drops_all_frames() const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042 const VideoFormat& output_format();
magjed@webrtc.orgf58b4552014-11-19 18:09:14 +000043
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.org28e20752013-07-10 00:45:36 +000047
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +000048 void set_scale_third(bool enable);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +000049 bool scale_third() const { return scale_third_; }
50
buildbot@webrtc.org71dffb72014-06-24 07:24:49 +000051 int adaptation_changes() const { return adaption_changes_; }
52
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053 protected:
54 float FindClosestScale(int width, int height, int target_num_pixels);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +000055 float FindClosestViewScale(int width, int height, int target_num_pixels);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056 float FindLowerScale(int width, int height, int target_num_pixels);
57
58 private:
wu@webrtc.orgcadf9042013-08-30 21:24:16 +000059 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.org28e20752013-07-10 00:45:36 +000063
64 VideoFormat input_format_;
65 VideoFormat output_format_;
66 int output_num_pixels_;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +000067 bool scale_third_; // True if adapter allows scaling to 1/3 and 2/3.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000068 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.orgcadf9042013-08-30 21:24:16 +000071 int adaption_changes_; // Number of changes in scale factor.
magjed@webrtc.orga73d7462014-11-14 13:25:25 +000072 size_t previous_width_; // Previous adapter output width.
73 size_t previous_height_; // Previous adapter output height.
Peter Boström0c4e06b2015-10-07 12:23:21 +020074 int64_t interval_next_frame_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075 // The critical section to protect the above variables.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000076 rtc::CriticalSection critical_section_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077
henrikg3c089d72015-09-16 05:37:44 -070078 RTC_DISALLOW_COPY_AND_ASSIGN(VideoAdapter);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079};
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.
84class CoordinatedVideoAdapter
85 : public VideoAdapter, public sigslot::has_slots<> {
86 public:
87 enum AdaptRequest { UPGRADE, KEEP, DOWNGRADE };
wu@webrtc.org364f2042013-11-20 21:49:41 +000088 enum AdaptReasonEnum {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +000089 ADAPTREASON_NONE = 0,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090 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.org1b15f422013-09-06 22:56:28 +000099 virtual void SetInputFormat(const VideoFormat& format);
100
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101 // 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.org28654cb2013-07-22 21:07:49 +0000104 // 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.orgd43aa9d2014-02-21 23:43:24 +0000107 void set_cpu_smoothing(bool enable);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000108 bool cpu_smoothing() const { return cpu_smoothing_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109 // 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.orgd43aa9d2014-02-21 23:43:24 +0000125 void set_cpu_load_min_samples(int cpu_load_min_samples);
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000126 int cpu_load_min_samples() const { return cpu_load_min_samples_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127 // CPU system load high threshold for reducing resolution. e.g. 0.85f
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000128 void set_high_system_threshold(float high_system_threshold);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129 float high_system_threshold() const { return high_system_threshold_; }
130 // CPU system load low threshold for increasing resolution. e.g. 0.70f
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000131 void set_low_system_threshold(float low_system_threshold);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132 float low_system_threshold() const { return low_system_threshold_; }
133 // CPU process load threshold for reducing resolution. e.g. 0.10f
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000134 void set_process_threshold(float process_threshold);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135 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.orgcadf9042013-08-30 21:24:16 +0000141 // Handle the resolution request for CPU overuse.
142 void OnCpuResolutionRequest(AdaptRequest request);
perkj2d5f0912016-02-29 00:04:41 -0800143 void OnCpuResolutionRequest(rtc::Optional<int> max_pixel_count,
144 rtc::Optional<int> max_pixel_count_step_up);
145
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146 // 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.org28654cb2013-07-22 21:07:49 +0000154 // the encoder wants. Returns true if resolution changed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155 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.org28654cb2013-07-22 21:07:49 +0000164 bool cpu_smoothing_; // True if cpu smoothing is enabled (with adaptation).
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165 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.org67ee6b92014-02-03 16:57:16 +0000169 int cpu_load_min_samples_;
170 int cpu_load_num_samples_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171 // 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öm0c4e06b2015-10-07 12:23:21 +0200179 int64_t view_desired_interval_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000180 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.orgd4e598d2014-07-29 17:36:52 +0000184 rtc::CriticalSection request_critical_section_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000186 // 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
henrikg3c089d72015-09-16 05:37:44 -0700190 RTC_DISALLOW_COPY_AND_ASSIGN(CoordinatedVideoAdapter);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000191};
192
193} // namespace cricket
194
kjellandera96e2d72016-02-04 23:52:28 -0800195#endif // WEBRTC_MEDIA_BASE_VIDEOADAPTER_H_ // NOLINT