Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: webrtc/media/base/videoadapter.h

Issue 1836043004: Cleanup the VideoAdapter (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Removed scale size 0. Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2010 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2010 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #ifndef WEBRTC_MEDIA_BASE_VIDEOADAPTER_H_ // NOLINT 11 #ifndef WEBRTC_MEDIA_BASE_VIDEOADAPTER_H_
12 #define WEBRTC_MEDIA_BASE_VIDEOADAPTER_H_ 12 #define WEBRTC_MEDIA_BASE_VIDEOADAPTER_H_
13 13
14 #include "webrtc/base/common.h" // For ASSERT 14 #include <limits.h> // For INT_MAX
15
15 #include "webrtc/base/criticalsection.h" 16 #include "webrtc/base/criticalsection.h"
16 #include "webrtc/base/optional.h" 17 #include "webrtc/base/optional.h"
17 #include "webrtc/base/sigslot.h"
18 #include "webrtc/media/base/videocommon.h" 18 #include "webrtc/media/base/videocommon.h"
19 19
20 namespace cricket { 20 namespace cricket {
21 21
22 class VideoFrame;
23
24 // VideoAdapter adapts an input video frame to an output frame based on the 22 // VideoAdapter adapts an input video frame to an output frame based on the
25 // specified input and output formats. The adaptation includes dropping frames 23 // specified input and output formats. The adaptation includes dropping frames
26 // to reduce frame rate and scaling frames. VideoAdapter is thread safe. 24 // to reduce frame rate and scaling frames.
25 // VideoAdapter is thread safe.
27 class VideoAdapter { 26 class VideoAdapter {
28 public: 27 public:
29 VideoAdapter(); 28 VideoAdapter();
30 virtual ~VideoAdapter(); 29 virtual ~VideoAdapter();
31 30
32 virtual void SetInputFormat(const VideoFormat& format); 31 // Sets the expected frame interval. This controls how often frames should
33 void SetOutputFormat(const VideoFormat& format); 32 // be dropped if |OnOutputFormatRequest| is called with a lower frame
34 // Constrain output resolution to this many pixels overall 33 // interval.
35 void SetOutputNumPixels(int num_pixels); 34 void SetExpectedInputFrameInterval(int64_t interval);
36 int GetOutputNumPixels() const;
37
38 const VideoFormat& input_format();
39 // Returns true if the adapter will always return zero size from
40 // AdaptFrameResolution.
41 bool drops_all_frames() const;
42 const VideoFormat& output_format();
43 35
44 // Return the adapted resolution given the input resolution. The returned 36 // Return the adapted resolution given the input resolution. The returned
45 // resolution will be 0x0 if the frame should be dropped. 37 // resolution will be 0x0 if the frame should be dropped.
46 VideoFormat AdaptFrameResolution(int in_width, int in_height); 38 VideoFormat AdaptFrameResolution(int in_width, int in_height);
47 39
48 void set_scale_third(bool enable); 40 // Requests the output frame size and frame interval from
49 bool scale_third() const { return scale_third_; } 41 // |AdaptFrameResolution| to not be larger than |format|.
42 void OnOutputFormatRequest(const VideoFormat& format);
50 43
51 int adaptation_changes() const { return adaption_changes_; } 44 // Requests the output frame size from |AdaptFrameResolution| to not have
45 // more than |max_pixel_count| pixels and have "one step" up more pixels than
46 // max_pixel_count_step_up.
47 void OnResolutionRequest(rtc::Optional<int> max_pixel_count,
48 rtc::Optional<int> max_pixel_count_step_up);
52 49
53 protected: 50 const VideoFormat& input_format() const;
54 float FindClosestScale(int width, int height, int target_num_pixels);
55 float FindClosestViewScale(int width, int height, int target_num_pixels);
56 float FindLowerScale(int width, int height, int target_num_pixels);
57 51
58 private: 52 private:
59 const float* GetViewScaleFactors() const; 53 void SetInputFormat(const VideoFormat& format);
60 float FindScale(const float* scale_factors, 54 bool Adapt(int max_num_pixels, int max_pixel_count_step_up);
61 const float upbias, int width, int height,
62 int target_num_pixels);
63 55
64 VideoFormat input_format_; 56 VideoFormat input_format_;
65 VideoFormat output_format_; 57 VideoFormat output_format_;
66 int output_num_pixels_; 58 int output_num_pixels_ = INT_MAX;
67 bool scale_third_; // True if adapter allows scaling to 1/3 and 2/3. 59 int frames_in_ = 0; // Number of input frames.
68 int frames_in_; // Number of input frames. 60 int frames_out_ = 0; // Number of output frames.
69 int frames_out_; // Number of output frames. 61 int frames_scaled_ = 0; // Number of frames scaled.
70 int frames_scaled_; // Number of frames scaled. 62 int adaption_changes_ = 0; // Number of changes in scale factor.
71 int adaption_changes_; // Number of changes in scale factor. 63 int previous_width_ = 0; // Previous adapter output width.
72 size_t previous_width_; // Previous adapter output width. 64 int previous_height_ = 0; // Previous adapter output height.
73 size_t previous_height_; // Previous adapter output height. 65 int64_t interval_next_frame_ = 0;
74 int64_t interval_next_frame_; 66
67 // Max number of pixels requested via calls to OnOutputFormatRequest,
68 // OnResolutionRequest respectively.
69 // The adapted output format is the minimum of these.
70 int format_request_max_pixel_count_ = INT_MAX;
71 int resolution_request_max_pixel_count_ = INT_MAX;
72
75 // The critical section to protect the above variables. 73 // The critical section to protect the above variables.
76 rtc::CriticalSection critical_section_; 74 rtc::CriticalSection critical_section_;
77 75
78 RTC_DISALLOW_COPY_AND_ASSIGN(VideoAdapter); 76 RTC_DISALLOW_COPY_AND_ASSIGN(VideoAdapter);
79 }; 77 };
80 78
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.
84 class CoordinatedVideoAdapter
85 : public VideoAdapter, public sigslot::has_slots<> {
86 public:
87 enum AdaptRequest { UPGRADE, KEEP, DOWNGRADE };
88 enum AdaptReasonEnum {
89 ADAPTREASON_NONE = 0,
90 ADAPTREASON_CPU = 1,
91 ADAPTREASON_BANDWIDTH = 2,
92 ADAPTREASON_VIEW = 4
93 };
94 typedef int AdaptReason;
95
96 CoordinatedVideoAdapter();
97 virtual ~CoordinatedVideoAdapter() {}
98
99 virtual void SetInputFormat(const VideoFormat& format);
100
101 // 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_; }
104 // Enable or disable smoothing when doing CPU adaptation. When smoothing is
105 // enabled, system CPU load is tracked using an exponential weighted
106 // average.
107 void set_cpu_smoothing(bool enable);
108 bool cpu_smoothing() const { return cpu_smoothing_; }
109 // 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.
125 void set_cpu_load_min_samples(int cpu_load_min_samples);
126 int cpu_load_min_samples() const { return cpu_load_min_samples_; }
127 // CPU system load high threshold for reducing resolution. e.g. 0.85f
128 void set_high_system_threshold(float high_system_threshold);
129 float high_system_threshold() const { return high_system_threshold_; }
130 // CPU system load low threshold for increasing resolution. e.g. 0.70f
131 void set_low_system_threshold(float low_system_threshold);
132 float low_system_threshold() const { return low_system_threshold_; }
133 // CPU process load threshold for reducing resolution. e.g. 0.10f
134 void set_process_threshold(float process_threshold);
135 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);
141 // Handle the resolution request for CPU overuse.
142 void OnCpuResolutionRequest(AdaptRequest request);
143 void OnCpuResolutionRequest(rtc::Optional<int> max_pixel_count,
144 rtc::Optional<int> max_pixel_count_step_up);
145
146 // 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
154 // the encoder wants. Returns true if resolution changed.
155 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.
164 bool cpu_smoothing_; // True if cpu smoothing is enabled (with adaptation).
165 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_;
169 int cpu_load_min_samples_;
170 int cpu_load_num_samples_;
171 // 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_;
179 int64_t view_desired_interval_;
180 int encoder_desired_num_pixels_;
181 int cpu_desired_num_pixels_;
182 CoordinatedVideoAdapter::AdaptReason adapt_reason_;
183 // The critical section to protect handling requests.
184 rtc::CriticalSection request_critical_section_;
185
186 // 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
190 RTC_DISALLOW_COPY_AND_ASSIGN(CoordinatedVideoAdapter);
191 };
192
193 } // namespace cricket 79 } // namespace cricket
194 80
195 #endif // WEBRTC_MEDIA_BASE_VIDEOADAPTER_H_ // NOLINT 81 #endif // WEBRTC_MEDIA_BASE_VIDEOADAPTER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698