OLD | NEW |
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 | |
15 #include "webrtc/base/criticalsection.h" | 14 #include "webrtc/base/criticalsection.h" |
16 #include "webrtc/base/optional.h" | 15 #include "webrtc/base/optional.h" |
17 #include "webrtc/base/sigslot.h" | |
18 #include "webrtc/media/base/videocommon.h" | 16 #include "webrtc/media/base/videocommon.h" |
19 | 17 |
20 namespace cricket { | 18 namespace cricket { |
21 | 19 |
22 class VideoFrame; | |
23 | |
24 // VideoAdapter adapts an input video frame to an output frame based on the | 20 // VideoAdapter adapts an input video frame to an output frame based on the |
25 // specified input and output formats. The adaptation includes dropping frames | 21 // specified input and output formats. The adaptation includes dropping frames |
26 // to reduce frame rate and scaling frames. VideoAdapter is thread safe. | 22 // to reduce frame rate and scaling frames. |
| 23 // VideoAdapter is thread safe. |
27 class VideoAdapter { | 24 class VideoAdapter { |
28 public: | 25 public: |
29 VideoAdapter(); | 26 VideoAdapter(); |
30 virtual ~VideoAdapter(); | 27 virtual ~VideoAdapter(); |
31 | 28 |
32 virtual void SetInputFormat(const VideoFormat& format); | 29 // Sets the expected frame interval. This controls how often frames should |
33 void SetOutputFormat(const VideoFormat& format); | 30 // be dropped if |OnOutputFormatRequest| is called with a lower frame |
34 // Constrain output resolution to this many pixels overall | 31 // interval. |
35 void SetOutputNumPixels(int num_pixels); | 32 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 | 33 |
44 // Return the adapted resolution given the input resolution. The returned | 34 // Return the adapted resolution given the input resolution. The returned |
45 // resolution will be 0x0 if the frame should be dropped. | 35 // resolution will be 0x0 if the frame should be dropped. |
46 VideoFormat AdaptFrameResolution(int in_width, int in_height); | 36 VideoFormat AdaptFrameResolution(int in_width, int in_height); |
47 | 37 |
48 void set_scale_third(bool enable); | 38 // Requests the output frame size and frame interval from |
49 bool scale_third() const { return scale_third_; } | 39 // |AdaptFrameResolution| to not be larger than |format|. |
| 40 void OnOutputFormatRequest(const VideoFormat& format); |
50 | 41 |
51 int adaptation_changes() const { return adaption_changes_; } | 42 // Requests the output frame size from |AdaptFrameResolution| to not have |
| 43 // more than |max_pixel_count| pixels and have "one step" up more pixels than |
| 44 // max_pixel_count_step_up. |
| 45 void OnResolutionRequest(rtc::Optional<int> max_pixel_count, |
| 46 rtc::Optional<int> max_pixel_count_step_up); |
52 | 47 |
53 protected: | 48 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 | 49 |
58 private: | 50 private: |
59 const float* GetViewScaleFactors() const; | 51 void SetInputFormat(const VideoFormat& format); |
60 float FindScale(const float* scale_factors, | 52 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 | 53 |
64 VideoFormat input_format_; | 54 VideoFormat input_format_; |
65 VideoFormat output_format_; | 55 VideoFormat output_format_; |
66 int output_num_pixels_; | 56 int output_num_pixels_; |
67 bool scale_third_; // True if adapter allows scaling to 1/3 and 2/3. | 57 int frames_in_; // Number of input frames. |
68 int frames_in_; // Number of input frames. | 58 int frames_out_; // Number of output frames. |
69 int frames_out_; // Number of output frames. | 59 int frames_scaled_; // Number of frames scaled. |
70 int frames_scaled_; // Number of frames scaled. | |
71 int adaption_changes_; // Number of changes in scale factor. | 60 int adaption_changes_; // Number of changes in scale factor. |
72 size_t previous_width_; // Previous adapter output width. | 61 int previous_width_; // Previous adapter output width. |
73 size_t previous_height_; // Previous adapter output height. | 62 int previous_height_; // Previous adapter output height. |
74 int64_t interval_next_frame_; | 63 int64_t interval_next_frame_; |
| 64 |
| 65 // Max number of pixels requested via calls to OnOutputFormatRequest, |
| 66 // OnResolutionRequest respectively. |
| 67 // The adapted output format is the minimum of these. |
| 68 int format_request_max_pixel_count_; |
| 69 int resolution_request_max_pixel_count_; |
| 70 |
75 // The critical section to protect the above variables. | 71 // The critical section to protect the above variables. |
76 rtc::CriticalSection critical_section_; | 72 rtc::CriticalSection critical_section_; |
77 | 73 |
78 RTC_DISALLOW_COPY_AND_ASSIGN(VideoAdapter); | 74 RTC_DISALLOW_COPY_AND_ASSIGN(VideoAdapter); |
79 }; | 75 }; |
80 | 76 |
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 | 77 } // namespace cricket |
194 | 78 |
195 #endif // WEBRTC_MEDIA_BASE_VIDEOADAPTER_H_ // NOLINT | 79 #endif // WEBRTC_MEDIA_BASE_VIDEOADAPTER_H_ |
OLD | NEW |