blob: 98c64d6a435db1b8d84c94e19a56039190b10b37 [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"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000031#include "talk/base/scoped_ptr.h"
32#include "talk/base/sigslot.h"
33#include "talk/media/base/videocommon.h"
34
35namespace cricket {
36
37class VideoFrame;
38
39// VideoAdapter adapts an input video frame to an output frame based on the
40// specified input and output formats. The adaptation includes dropping frames
41// to reduce frame rate and scaling frames. VideoAdapter is thread safe.
42class VideoAdapter {
43 public:
44 VideoAdapter();
45 virtual ~VideoAdapter();
46
mallinath@webrtc.org1b15f422013-09-06 22:56:28 +000047 virtual void SetInputFormat(const VideoFormat& format);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048 void SetOutputFormat(const VideoFormat& format);
49 // Constrain output resolution to this many pixels overall
50 void SetOutputNumPixels(int num_pixels);
51 int GetOutputNumPixels() const;
52
53 const VideoFormat& input_format();
54 const VideoFormat& output_format();
55 // If the parameter black is true, the adapted frames will be black.
56 void SetBlackOutput(bool black);
57
58 // Adapt the input frame from the input format to the output format. Return
59 // true and set the output frame to NULL if the input frame is dropped. Return
60 // true and set the out frame to output_frame_ if the input frame is adapted
61 // successfully. Return false otherwise.
62 // output_frame_ is owned by the VideoAdapter that has the best knowledge on
63 // the output frame.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +000064 bool AdaptFrame(const VideoFrame* in_frame, VideoFrame** out_frame);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +000066 void set_scale_third(bool enable);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +000067 bool scale_third() const { return scale_third_; }
68
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069 protected:
70 float FindClosestScale(int width, int height, int target_num_pixels);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +000071 float FindClosestViewScale(int width, int height, int target_num_pixels);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072 float FindLowerScale(int width, int height, int target_num_pixels);
73
74 private:
wu@webrtc.orgcadf9042013-08-30 21:24:16 +000075 const float* GetViewScaleFactors() const;
76 float FindScale(const float* scale_factors,
77 const float upbias, int width, int height,
78 int target_num_pixels);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079 bool StretchToOutputFrame(const VideoFrame* in_frame);
80
81 VideoFormat input_format_;
82 VideoFormat output_format_;
83 int output_num_pixels_;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +000084 bool scale_third_; // True if adapter allows scaling to 1/3 and 2/3.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000085 int frames_in_; // Number of input frames.
86 int frames_out_; // Number of output frames.
87 int frames_scaled_; // Number of frames scaled.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +000088 int adaption_changes_; // Number of changes in scale factor.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +000089 size_t previous_width_; // Previous adapter output width.
90 size_t previous_height_; // Previous adapter output height.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091 bool black_output_; // Flag to tell if we need to black output_frame_.
92 bool is_black_; // Flag to tell if output_frame_ is currently black.
93 int64 interval_next_frame_;
94 talk_base::scoped_ptr<VideoFrame> output_frame_;
95 // The critical section to protect the above variables.
96 talk_base::CriticalSection critical_section_;
97
98 DISALLOW_COPY_AND_ASSIGN(VideoAdapter);
99};
100
101// CoordinatedVideoAdapter adapts the video input to the encoder by coordinating
102// the format request from the server, the resolution request from the encoder,
103// and the CPU load.
104class CoordinatedVideoAdapter
105 : public VideoAdapter, public sigslot::has_slots<> {
106 public:
107 enum AdaptRequest { UPGRADE, KEEP, DOWNGRADE };
wu@webrtc.org364f2042013-11-20 21:49:41 +0000108 enum AdaptReasonEnum {
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000109 ADAPTREASON_NONE = 0,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110 ADAPTREASON_CPU = 1,
111 ADAPTREASON_BANDWIDTH = 2,
112 ADAPTREASON_VIEW = 4
113 };
114 typedef int AdaptReason;
115
116 CoordinatedVideoAdapter();
117 virtual ~CoordinatedVideoAdapter() {}
118
mallinath@webrtc.org1b15f422013-09-06 22:56:28 +0000119 virtual void SetInputFormat(const VideoFormat& format);
120
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000121 // Enable or disable video adaptation due to the change of the CPU load.
122 void set_cpu_adaptation(bool enable) { cpu_adaptation_ = enable; }
123 bool cpu_adaptation() const { return cpu_adaptation_; }
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000124 // Enable or disable smoothing when doing CPU adaptation. When smoothing is
125 // enabled, system CPU load is tracked using an exponential weighted
126 // average.
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000127 void set_cpu_smoothing(bool enable);
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000128 bool cpu_smoothing() const { return cpu_smoothing_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129 // Enable or disable video adaptation due to the change of the GD
130 void set_gd_adaptation(bool enable) { gd_adaptation_ = enable; }
131 bool gd_adaptation() const { return gd_adaptation_; }
132 // Enable or disable video adaptation due to the change of the View
133 void set_view_adaptation(bool enable) { view_adaptation_ = enable; }
134 bool view_adaptation() const { return view_adaptation_; }
135 // Enable or disable video adaptation to fast switch View
136 void set_view_switch(bool enable) { view_switch_ = enable; }
137 bool view_switch() const { return view_switch_; }
138
139 CoordinatedVideoAdapter::AdaptReason adapt_reason() const {
140 return adapt_reason_;
141 }
142
143 // When the video is decreased, set the waiting time for CPU adaptation to
144 // decrease video again.
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000145 void set_cpu_load_min_samples(int cpu_load_min_samples);
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000146 int cpu_load_min_samples() const { return cpu_load_min_samples_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000147 // CPU system load high threshold for reducing resolution. e.g. 0.85f
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000148 void set_high_system_threshold(float high_system_threshold);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000149 float high_system_threshold() const { return high_system_threshold_; }
150 // CPU system load low threshold for increasing resolution. e.g. 0.70f
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000151 void set_low_system_threshold(float low_system_threshold);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152 float low_system_threshold() const { return low_system_threshold_; }
153 // CPU process load threshold for reducing resolution. e.g. 0.10f
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000154 void set_process_threshold(float process_threshold);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155 float process_threshold() const { return process_threshold_; }
156
157 // Handle the format request from the server via Jingle update message.
158 void OnOutputFormatRequest(const VideoFormat& format);
159 // Handle the resolution request from the encoder due to bandwidth changes.
160 void OnEncoderResolutionRequest(int width, int height, AdaptRequest request);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000161 // Handle the resolution request for CPU overuse.
162 void OnCpuResolutionRequest(AdaptRequest request);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163 // Handle the CPU load provided by a CPU monitor.
164 void OnCpuLoadUpdated(int current_cpus, int max_cpus,
165 float process_load, float system_load);
166
167 sigslot::signal0<> SignalCpuAdaptationUnable;
168
169 private:
170 // Adapt to the minimum of the formats the server requests, the CPU wants, and
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000171 // the encoder wants. Returns true if resolution changed.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000172 bool AdaptToMinimumFormat(int* new_width, int* new_height);
173 bool IsMinimumFormat(int pixels);
174 void StepPixelCount(CoordinatedVideoAdapter::AdaptRequest request,
175 int* num_pixels);
176 CoordinatedVideoAdapter::AdaptRequest FindCpuRequest(
177 int current_cpus, int max_cpus,
178 float process_load, float system_load);
179
180 bool cpu_adaptation_; // True if cpu adaptation is enabled.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000181 bool cpu_smoothing_; // True if cpu smoothing is enabled (with adaptation).
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 bool gd_adaptation_; // True if gd adaptation is enabled.
183 bool view_adaptation_; // True if view adaptation is enabled.
184 bool view_switch_; // True if view switch is enabled.
185 int cpu_downgrade_count_;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000186 int cpu_load_min_samples_;
187 int cpu_load_num_samples_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000188 // cpu system load thresholds relative to max cpus.
189 float high_system_threshold_;
190 float low_system_threshold_;
191 // cpu process load thresholds relative to current cpus.
192 float process_threshold_;
193 // Video formats that the server view requests, the CPU wants, and the encoder
194 // wants respectively. The adapted output format is the minimum of these.
195 int view_desired_num_pixels_;
196 int64 view_desired_interval_;
197 int encoder_desired_num_pixels_;
198 int cpu_desired_num_pixels_;
199 CoordinatedVideoAdapter::AdaptReason adapt_reason_;
200 // The critical section to protect handling requests.
201 talk_base::CriticalSection request_critical_section_;
202
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000203 // The weighted average of cpu load over time. It's always updated (if cpu
204 // adaptation is on), but only used if cpu_smoothing_ is set.
205 float system_load_average_;
206
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000207 DISALLOW_COPY_AND_ASSIGN(CoordinatedVideoAdapter);
208};
209
210} // namespace cricket
211
212#endif // TALK_MEDIA_BASE_VIDEOADAPTER_H_ // NOLINT