blob: 2c6cb04f65c21ecf68a719a2b8151871451bbc32 [file] [log] [blame]
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00001/*
2 * libjingle
3 * Copyright 2010 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
henrike@webrtc.org28e20752013-07-10 00:45:36 +000027
28#ifndef TALK_MEDIA_BASE_VIDEOADAPTER_H_ // NOLINT
29#define TALK_MEDIA_BASE_VIDEOADAPTER_H_
30
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000031#include "talk/media/base/videocommon.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000032#include "webrtc/base/common.h" // For ASSERT
33#include "webrtc/base/criticalsection.h"
34#include "webrtc/base/scoped_ptr.h"
35#include "webrtc/base/sigslot.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036
37namespace cricket {
38
39class VideoFrame;
40
41// VideoAdapter adapts an input video frame to an output frame based on the
42// specified input and output formats. The adaptation includes dropping frames
43// to reduce frame rate and scaling frames. VideoAdapter is thread safe.
44class VideoAdapter {
45 public:
46 VideoAdapter();
47 virtual ~VideoAdapter();
48
mallinath@webrtc.org1b15f422013-09-06 22:56:28 +000049 virtual void SetInputFormat(const VideoFormat& format);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000050 void SetOutputFormat(const VideoFormat& format);
51 // Constrain output resolution to this many pixels overall
52 void SetOutputNumPixels(int num_pixels);
53 int GetOutputNumPixels() const;
54
55 const VideoFormat& input_format();
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +000056 // Returns true if the adapter is dropping frames in calls to AdaptFrame.
57 bool drops_all_frames() const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058 const VideoFormat& output_format();
59 // If the parameter black is true, the adapted frames will be black.
60 void SetBlackOutput(bool black);
magjed@webrtc.orgf58b4552014-11-19 18:09:14 +000061 bool IsBlackOutput();
62
63 // Return the adapted resolution given the input resolution. The returned
64 // resolution will be 0x0 if the frame should be dropped.
65 VideoFormat AdaptFrameResolution(int in_width, int in_height);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066
67 // Adapt the input frame from the input format to the output format. Return
68 // true and set the output frame to NULL if the input frame is dropped. Return
69 // true and set the out frame to output_frame_ if the input frame is adapted
70 // successfully. Return false otherwise.
buildbot@webrtc.org0cdcd232014-06-04 01:31:14 +000071 // Note that, if no adaptation is required, |out_frame| will refer directly
72 // in_frame. If a copy is always required, the caller must do an explicit
73 // copy.
74 // If a copy has taken place, |output_frame_| is owned by the VideoAdapter
75 // and will remain usable until the adapter is destroyed or AdaptFrame is
76 // called again.
77 bool AdaptFrame(VideoFrame* in_frame, VideoFrame** out_frame);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +000079 void set_scale_third(bool enable);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +000080 bool scale_third() const { return scale_third_; }
81
buildbot@webrtc.org71dffb72014-06-24 07:24:49 +000082 int adaptation_changes() const { return adaption_changes_; }
83
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084 protected:
85 float FindClosestScale(int width, int height, int target_num_pixels);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +000086 float FindClosestViewScale(int width, int height, int target_num_pixels);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087 float FindLowerScale(int width, int height, int target_num_pixels);
88
89 private:
wu@webrtc.orgcadf9042013-08-30 21:24:16 +000090 const float* GetViewScaleFactors() const;
91 float FindScale(const float* scale_factors,
92 const float upbias, int width, int height,
93 int target_num_pixels);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094 bool StretchToOutputFrame(const VideoFrame* in_frame);
95
96 VideoFormat input_format_;
97 VideoFormat output_format_;
98 int output_num_pixels_;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +000099 bool scale_third_; // True if adapter allows scaling to 1/3 and 2/3.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000100 int frames_in_; // Number of input frames.
101 int frames_out_; // Number of output frames.
102 int frames_scaled_; // Number of frames scaled.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000103 int adaption_changes_; // Number of changes in scale factor.
magjed@webrtc.orga73d7462014-11-14 13:25:25 +0000104 size_t previous_width_; // Previous adapter output width.
105 size_t previous_height_; // Previous adapter output height.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106 bool black_output_; // Flag to tell if we need to black output_frame_.
107 bool is_black_; // Flag to tell if output_frame_ is currently black.
108 int64 interval_next_frame_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000109 rtc::scoped_ptr<VideoFrame> output_frame_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110 // The critical section to protect the above variables.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000111 rtc::CriticalSection critical_section_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112
113 DISALLOW_COPY_AND_ASSIGN(VideoAdapter);
114};
115
116// CoordinatedVideoAdapter adapts the video input to the encoder by coordinating
117// the format request from the server, the resolution request from the encoder,
118// and the CPU load.
119class CoordinatedVideoAdapter
120 : public VideoAdapter, public sigslot::has_slots<> {
121 public:
122 enum AdaptRequest { UPGRADE, KEEP, DOWNGRADE };
wu@webrtc.org364f2042013-11-20 21:49:41 +0000123 enum AdaptReasonEnum {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000124 ADAPTREASON_NONE = 0,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125 ADAPTREASON_CPU = 1,
126 ADAPTREASON_BANDWIDTH = 2,
127 ADAPTREASON_VIEW = 4
128 };
129 typedef int AdaptReason;
130
131 CoordinatedVideoAdapter();
132 virtual ~CoordinatedVideoAdapter() {}
133
mallinath@webrtc.org1b15f422013-09-06 22:56:28 +0000134 virtual void SetInputFormat(const VideoFormat& format);
135
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136 // Enable or disable video adaptation due to the change of the CPU load.
137 void set_cpu_adaptation(bool enable) { cpu_adaptation_ = enable; }
138 bool cpu_adaptation() const { return cpu_adaptation_; }
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000139 // Enable or disable smoothing when doing CPU adaptation. When smoothing is
140 // enabled, system CPU load is tracked using an exponential weighted
141 // average.
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000142 void set_cpu_smoothing(bool enable);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000143 bool cpu_smoothing() const { return cpu_smoothing_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144 // Enable or disable video adaptation due to the change of the GD
145 void set_gd_adaptation(bool enable) { gd_adaptation_ = enable; }
146 bool gd_adaptation() const { return gd_adaptation_; }
147 // Enable or disable video adaptation due to the change of the View
148 void set_view_adaptation(bool enable) { view_adaptation_ = enable; }
149 bool view_adaptation() const { return view_adaptation_; }
150 // Enable or disable video adaptation to fast switch View
151 void set_view_switch(bool enable) { view_switch_ = enable; }
152 bool view_switch() const { return view_switch_; }
153
154 CoordinatedVideoAdapter::AdaptReason adapt_reason() const {
155 return adapt_reason_;
156 }
157
158 // When the video is decreased, set the waiting time for CPU adaptation to
159 // decrease video again.
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000160 void set_cpu_load_min_samples(int cpu_load_min_samples);
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000161 int cpu_load_min_samples() const { return cpu_load_min_samples_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162 // CPU system load high threshold for reducing resolution. e.g. 0.85f
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000163 void set_high_system_threshold(float high_system_threshold);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164 float high_system_threshold() const { return high_system_threshold_; }
165 // CPU system load low threshold for increasing resolution. e.g. 0.70f
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000166 void set_low_system_threshold(float low_system_threshold);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167 float low_system_threshold() const { return low_system_threshold_; }
168 // CPU process load threshold for reducing resolution. e.g. 0.10f
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000169 void set_process_threshold(float process_threshold);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000170 float process_threshold() const { return process_threshold_; }
171
172 // Handle the format request from the server via Jingle update message.
173 void OnOutputFormatRequest(const VideoFormat& format);
174 // Handle the resolution request from the encoder due to bandwidth changes.
175 void OnEncoderResolutionRequest(int width, int height, AdaptRequest request);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000176 // Handle the resolution request for CPU overuse.
177 void OnCpuResolutionRequest(AdaptRequest request);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178 // Handle the CPU load provided by a CPU monitor.
179 void OnCpuLoadUpdated(int current_cpus, int max_cpus,
180 float process_load, float system_load);
181
182 sigslot::signal0<> SignalCpuAdaptationUnable;
183
184 private:
185 // Adapt to the minimum of the formats the server requests, the CPU wants, and
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000186 // the encoder wants. Returns true if resolution changed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187 bool AdaptToMinimumFormat(int* new_width, int* new_height);
188 bool IsMinimumFormat(int pixels);
189 void StepPixelCount(CoordinatedVideoAdapter::AdaptRequest request,
190 int* num_pixels);
191 CoordinatedVideoAdapter::AdaptRequest FindCpuRequest(
192 int current_cpus, int max_cpus,
193 float process_load, float system_load);
194
195 bool cpu_adaptation_; // True if cpu adaptation is enabled.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000196 bool cpu_smoothing_; // True if cpu smoothing is enabled (with adaptation).
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197 bool gd_adaptation_; // True if gd adaptation is enabled.
198 bool view_adaptation_; // True if view adaptation is enabled.
199 bool view_switch_; // True if view switch is enabled.
200 int cpu_downgrade_count_;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000201 int cpu_load_min_samples_;
202 int cpu_load_num_samples_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000203 // cpu system load thresholds relative to max cpus.
204 float high_system_threshold_;
205 float low_system_threshold_;
206 // cpu process load thresholds relative to current cpus.
207 float process_threshold_;
208 // Video formats that the server view requests, the CPU wants, and the encoder
209 // wants respectively. The adapted output format is the minimum of these.
210 int view_desired_num_pixels_;
211 int64 view_desired_interval_;
212 int encoder_desired_num_pixels_;
213 int cpu_desired_num_pixels_;
214 CoordinatedVideoAdapter::AdaptReason adapt_reason_;
215 // The critical section to protect handling requests.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000216 rtc::CriticalSection request_critical_section_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000217
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000218 // The weighted average of cpu load over time. It's always updated (if cpu
219 // adaptation is on), but only used if cpu_smoothing_ is set.
220 float system_load_average_;
221
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000222 DISALLOW_COPY_AND_ASSIGN(CoordinatedVideoAdapter);
223};
224
225} // namespace cricket
226
227#endif // TALK_MEDIA_BASE_VIDEOADAPTER_H_ // NOLINT