blob: 12e564db9aea77c3afc6e1e3f129a99dd27fc7ba [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001// libjingle
2// Copyright 2010 Google Inc.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are met:
6//
7// 1. Redistributions of source code must retain the above copyright notice,
8// this list of conditions and the following disclaimer.
9// 2. Redistributions in binary form must reproduce the above copyright notice,
10// this list of conditions and the following disclaimer in the documentation
11// and/or other materials provided with the distribution.
12// 3. The name of the author may not be used to endorse or promote products
13// derived from this software without specific prior written permission.
14//
15// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26#ifndef TALK_MEDIA_BASE_VIDEOADAPTER_H_ // NOLINT
27#define TALK_MEDIA_BASE_VIDEOADAPTER_H_
28
29#include "talk/base/common.h" // For ASSERT
30#include "talk/base/criticalsection.h"
31#include "talk/base/logging.h"
32#include "talk/base/scoped_ptr.h"
33#include "talk/base/sigslot.h"
34#include "talk/media/base/videocommon.h"
35
36namespace cricket {
37
38class VideoFrame;
39
40// VideoAdapter adapts an input video frame to an output frame based on the
41// specified input and output formats. The adaptation includes dropping frames
42// to reduce frame rate and scaling frames. VideoAdapter is thread safe.
43class VideoAdapter {
44 public:
45 VideoAdapter();
46 virtual ~VideoAdapter();
47
48 void SetInputFormat(const VideoFrame& in_frame);
49 void SetInputFormat(const VideoFormat& format);
50 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();
56 const VideoFormat& output_format();
57 // If the parameter black is true, the adapted frames will be black.
58 void SetBlackOutput(bool black);
59
60 // Adapt the input frame from the input format to the output format. Return
61 // true and set the output frame to NULL if the input frame is dropped. Return
62 // true and set the out frame to output_frame_ if the input frame is adapted
63 // successfully. Return false otherwise.
64 // output_frame_ is owned by the VideoAdapter that has the best knowledge on
65 // the output frame.
66 bool AdaptFrame(const VideoFrame* in_frame, const VideoFrame** out_frame);
67
wu@webrtc.orgcadf9042013-08-30 21:24:16 +000068 void set_scale_third(bool enable) {
69 LOG(LS_INFO) << "Video Adapter third scaling is now "
70 << (enable ? "enabled" : "disabled");
71 scale_third_ = enable;
72 }
73 bool scale_third() const { return scale_third_; }
74
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075 protected:
76 float FindClosestScale(int width, int height, int target_num_pixels);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +000077 float FindClosestViewScale(int width, int height, int target_num_pixels);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078 float FindLowerScale(int width, int height, int target_num_pixels);
79
80 private:
wu@webrtc.orgcadf9042013-08-30 21:24:16 +000081 const float* GetViewScaleFactors() const;
82 float FindScale(const float* scale_factors,
83 const float upbias, int width, int height,
84 int target_num_pixels);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085 bool StretchToOutputFrame(const VideoFrame* in_frame);
86
87 VideoFormat input_format_;
88 VideoFormat output_format_;
89 int output_num_pixels_;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +000090 bool scale_third_; // True if adapter allows scaling to 1/3 and 2/3.
91 int frames_; // Number of input frames.
92 int adapted_frames_; // Number of frames scaled.
93 int adaption_changes_; // Number of changes in scale factor.
94 size_t previous_width; // Previous adapter output width.
95 size_t previous_height; // Previous adapter output height.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096 bool black_output_; // Flag to tell if we need to black output_frame_.
97 bool is_black_; // Flag to tell if output_frame_ is currently black.
98 int64 interval_next_frame_;
99 talk_base::scoped_ptr<VideoFrame> output_frame_;
100 // The critical section to protect the above variables.
101 talk_base::CriticalSection critical_section_;
102
103 DISALLOW_COPY_AND_ASSIGN(VideoAdapter);
104};
105
106// CoordinatedVideoAdapter adapts the video input to the encoder by coordinating
107// the format request from the server, the resolution request from the encoder,
108// and the CPU load.
109class CoordinatedVideoAdapter
110 : public VideoAdapter, public sigslot::has_slots<> {
111 public:
112 enum AdaptRequest { UPGRADE, KEEP, DOWNGRADE };
113 enum {
114 ADAPTREASON_CPU = 1,
115 ADAPTREASON_BANDWIDTH = 2,
116 ADAPTREASON_VIEW = 4
117 };
118 typedef int AdaptReason;
119
120 CoordinatedVideoAdapter();
121 virtual ~CoordinatedVideoAdapter() {}
122
123 // Enable or disable video adaptation due to the change of the CPU load.
124 void set_cpu_adaptation(bool enable) { cpu_adaptation_ = enable; }
125 bool cpu_adaptation() const { return cpu_adaptation_; }
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000126 // Enable or disable smoothing when doing CPU adaptation. When smoothing is
127 // enabled, system CPU load is tracked using an exponential weighted
128 // average.
129 void set_cpu_smoothing(bool enable) {
130 LOG(LS_INFO) << "CPU smoothing is now "
131 << (enable ? "enabled" : "disabled");
132 cpu_smoothing_ = enable;
133 }
134 bool cpu_smoothing() const { return cpu_smoothing_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135 // Enable or disable video adaptation due to the change of the GD
136 void set_gd_adaptation(bool enable) { gd_adaptation_ = enable; }
137 bool gd_adaptation() const { return gd_adaptation_; }
138 // Enable or disable video adaptation due to the change of the View
139 void set_view_adaptation(bool enable) { view_adaptation_ = enable; }
140 bool view_adaptation() const { return view_adaptation_; }
141 // Enable or disable video adaptation to fast switch View
142 void set_view_switch(bool enable) { view_switch_ = enable; }
143 bool view_switch() const { return view_switch_; }
144
145 CoordinatedVideoAdapter::AdaptReason adapt_reason() const {
146 return adapt_reason_;
147 }
148
149 // When the video is decreased, set the waiting time for CPU adaptation to
150 // decrease video again.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000151 void set_cpu_adapt_wait_time(uint32 cpu_adapt_wait_time) {
152 if (cpu_adapt_wait_time_ != static_cast<int>(cpu_adapt_wait_time)) {
153 LOG(LS_INFO) << "VAdapt Change Cpu Adapt Wait Time from: "
154 << cpu_adapt_wait_time_ << " to "
155 << cpu_adapt_wait_time;
156 cpu_adapt_wait_time_ = static_cast<int>(cpu_adapt_wait_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000157 }
158 }
159 // CPU system load high threshold for reducing resolution. e.g. 0.85f
160 void set_high_system_threshold(float high_system_threshold) {
161 ASSERT(high_system_threshold <= 1.0f);
162 ASSERT(high_system_threshold >= 0.0f);
163 if (high_system_threshold_ != high_system_threshold) {
164 LOG(LS_INFO) << "VAdapt Change High System Threshold from: "
165 << high_system_threshold_ << " to " << high_system_threshold;
166 high_system_threshold_ = high_system_threshold;
167 }
168 }
169 float high_system_threshold() const { return high_system_threshold_; }
170 // CPU system load low threshold for increasing resolution. e.g. 0.70f
171 void set_low_system_threshold(float low_system_threshold) {
172 ASSERT(low_system_threshold <= 1.0f);
173 ASSERT(low_system_threshold >= 0.0f);
174 if (low_system_threshold_ != low_system_threshold) {
175 LOG(LS_INFO) << "VAdapt Change Low System Threshold from: "
176 << low_system_threshold_ << " to " << low_system_threshold;
177 low_system_threshold_ = low_system_threshold;
178 }
179 }
180 float low_system_threshold() const { return low_system_threshold_; }
181 // CPU process load threshold for reducing resolution. e.g. 0.10f
182 void set_process_threshold(float process_threshold) {
183 ASSERT(process_threshold <= 1.0f);
184 ASSERT(process_threshold >= 0.0f);
185 if (process_threshold_ != process_threshold) {
186 LOG(LS_INFO) << "VAdapt Change High Process Threshold from: "
187 << process_threshold_ << " to " << process_threshold;
188 process_threshold_ = process_threshold;
189 }
190 }
191 float process_threshold() const { return process_threshold_; }
192
193 // Handle the format request from the server via Jingle update message.
194 void OnOutputFormatRequest(const VideoFormat& format);
195 // Handle the resolution request from the encoder due to bandwidth changes.
196 void OnEncoderResolutionRequest(int width, int height, AdaptRequest request);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000197 // Handle the resolution request for CPU overuse.
198 void OnCpuResolutionRequest(AdaptRequest request);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199 // Handle the CPU load provided by a CPU monitor.
200 void OnCpuLoadUpdated(int current_cpus, int max_cpus,
201 float process_load, float system_load);
202
203 sigslot::signal0<> SignalCpuAdaptationUnable;
204
205 private:
206 // Adapt to the minimum of the formats the server requests, the CPU wants, and
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000207 // the encoder wants. Returns true if resolution changed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208 bool AdaptToMinimumFormat(int* new_width, int* new_height);
209 bool IsMinimumFormat(int pixels);
210 void StepPixelCount(CoordinatedVideoAdapter::AdaptRequest request,
211 int* num_pixels);
212 CoordinatedVideoAdapter::AdaptRequest FindCpuRequest(
213 int current_cpus, int max_cpus,
214 float process_load, float system_load);
215
216 bool cpu_adaptation_; // True if cpu adaptation is enabled.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000217 bool cpu_smoothing_; // True if cpu smoothing is enabled (with adaptation).
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218 bool gd_adaptation_; // True if gd adaptation is enabled.
219 bool view_adaptation_; // True if view adaptation is enabled.
220 bool view_switch_; // True if view switch is enabled.
221 int cpu_downgrade_count_;
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000222 int cpu_adapt_wait_time_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000223 // cpu system load thresholds relative to max cpus.
224 float high_system_threshold_;
225 float low_system_threshold_;
226 // cpu process load thresholds relative to current cpus.
227 float process_threshold_;
228 // Video formats that the server view requests, the CPU wants, and the encoder
229 // wants respectively. The adapted output format is the minimum of these.
230 int view_desired_num_pixels_;
231 int64 view_desired_interval_;
232 int encoder_desired_num_pixels_;
233 int cpu_desired_num_pixels_;
234 CoordinatedVideoAdapter::AdaptReason adapt_reason_;
235 // The critical section to protect handling requests.
236 talk_base::CriticalSection request_critical_section_;
237
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000238 // The weighted average of cpu load over time. It's always updated (if cpu
239 // adaptation is on), but only used if cpu_smoothing_ is set.
240 float system_load_average_;
241
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000242 DISALLOW_COPY_AND_ASSIGN(CoordinatedVideoAdapter);
243};
244
245} // namespace cricket
246
247#endif // TALK_MEDIA_BASE_VIDEOADAPTER_H_ // NOLINT