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

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

Issue 1982983003: VideoAdapter: Drop frames based on actual fps instead of expected fps (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 4 years, 7 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
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 111
112 namespace cricket { 112 namespace cricket {
113 113
114 VideoAdapter::VideoAdapter() 114 VideoAdapter::VideoAdapter()
115 : frames_in_(0), 115 : frames_in_(0),
116 frames_out_(0), 116 frames_out_(0),
117 frames_scaled_(0), 117 frames_scaled_(0),
118 adaption_changes_(0), 118 adaption_changes_(0),
119 previous_width_(0), 119 previous_width_(0),
120 previous_height_(0), 120 previous_height_(0),
121 input_interval_(0), 121 previous_timestamp_ns_(0),
nisse-webrtc 2016/05/18 11:44:00 Please make it an rtc::Optional, instead of using
magjed_webrtc 2016/05/18 16:35:22 Done.
122 interval_next_frame_(0), 122 interval_ns_next_frame_(0),
nisse-webrtc 2016/05/18 11:44:00 There should be a comment somewhere explaining the
magjed_webrtc 2016/05/18 16:35:22 Done.
123 resolution_request_max_pixel_count_(std::numeric_limits<int>::max()), 123 resolution_request_max_pixel_count_(std::numeric_limits<int>::max()),
124 resolution_request_max_pixel_count_step_up_(0) {} 124 resolution_request_max_pixel_count_step_up_(0) {}
125 125
126 VideoAdapter::~VideoAdapter() {} 126 VideoAdapter::~VideoAdapter() {}
127 127
128 void VideoAdapter::SetExpectedInputFrameInterval(int64_t interval) { 128 bool VideoAdapter::ShouldDrop(int64_t in_timestamp_ns) {
nisse-webrtc 2016/05/18 11:44:00 I'd prefer if false meant that the frame should be
magjed_webrtc 2016/05/18 16:35:23 Done.
129 // TODO(perkj): Consider measuring input frame rate instead.
130 // Frame rate typically varies depending on lighting.
131 rtc::CritScope cs(&critical_section_); 129 rtc::CritScope cs(&critical_section_);
132 input_interval_ = interval; 130 if (!requested_format_ || requested_format_->interval == 0)
131 return false;
132
133 // Reset counters if it's the first frame, if input fps is higher than
134 // requested fps, or if timestamps are out of order. Always output the
135 // first frame after a reset.
136 const int64_t frame_delta_ns = (in_timestamp_ns - previous_timestamp_ns_);
137 if (previous_timestamp_ns_ == 0 || frame_delta_ns <= 0 ||
138 frame_delta_ns >= requested_format_->interval) {
139 interval_ns_next_frame_ = 0;
140 previous_timestamp_ns_ = in_timestamp_ns;
141 return false;
142 }
143
144 // Increment counters.
145 interval_ns_next_frame_ += frame_delta_ns;
146 previous_timestamp_ns_ = in_timestamp_ns;
147
148 // Drop if elapsed time is less then requested.
149 if (interval_ns_next_frame_ < requested_format_->interval)
150 return true;
151
152 // Time to output new frame.
153 interval_ns_next_frame_ -= requested_format_->interval;
154 return false;
133 } 155 }
134 156
135 void VideoAdapter::AdaptFrameResolution(int in_width, 157 void VideoAdapter::AdaptFrameResolution(int in_width,
136 int in_height, 158 int in_height,
159 int64_t in_timestamp_ns,
137 int* cropped_width, 160 int* cropped_width,
138 int* cropped_height, 161 int* cropped_height,
139 int* out_width, 162 int* out_width,
140 int* out_height) { 163 int* out_height) {
141 rtc::CritScope cs(&critical_section_); 164 rtc::CritScope cs(&critical_section_);
142 ++frames_in_; 165 ++frames_in_;
143 166
144 // The max output pixel count is the minimum of the requests from 167 // The max output pixel count is the minimum of the requests from
145 // OnOutputFormatRequest and OnResolutionRequest. 168 // OnOutputFormatRequest and OnResolutionRequest.
146 int max_pixel_count = resolution_request_max_pixel_count_; 169 int max_pixel_count = resolution_request_max_pixel_count_;
147 if (requested_format_) { 170 if (requested_format_) {
148 max_pixel_count = std::min( 171 max_pixel_count = std::min(
149 max_pixel_count, requested_format_->width * requested_format_->height); 172 max_pixel_count, requested_format_->width * requested_format_->height);
150 } 173 }
151 174
152 // Drop the input frame if necessary. 175 // Drop the input frame if necessary.
153 bool should_drop = false; 176 if (max_pixel_count == 0 || ShouldDrop(in_timestamp_ns)) {
154 if (max_pixel_count == 0) {
155 // Drop all frames as the output format is 0x0.
156 should_drop = true;
157 } else if (requested_format_ && requested_format_->interval > 0) {
158 // Drop some frames based on input fps and output fps.
159 // Normally output fps is less than input fps.
160 interval_next_frame_ += input_interval_;
161 if (interval_next_frame_ >= requested_format_->interval) {
162 interval_next_frame_ -= requested_format_->interval;
163 // Reset |interval_next_frame_| if it accumulates too much to avoid
164 // "catching up" behaviour.
165 if (interval_next_frame_ >= requested_format_->interval)
166 interval_next_frame_ = 0;
167 } else {
168 should_drop = true;
169 }
170 }
171 if (should_drop) {
172 // Show VAdapt log every 90 frames dropped. (3 seconds) 177 // Show VAdapt log every 90 frames dropped. (3 seconds)
173 if ((frames_in_ - frames_out_) % 90 == 0) { 178 if ((frames_in_ - frames_out_) % 90 == 0) {
174 // TODO(fbarchard): Reduce to LS_VERBOSE when adapter info is not needed 179 // TODO(fbarchard): Reduce to LS_VERBOSE when adapter info is not needed
175 // in default calls. 180 // in default calls.
176 LOG(LS_INFO) << "VAdapt Drop Frame: scaled " << frames_scaled_ 181 LOG(LS_INFO) << "VAdapt Drop Frame: scaled " << frames_scaled_
177 << " / out " << frames_out_ 182 << " / out " << frames_out_
178 << " / in " << frames_in_ 183 << " / in " << frames_in_
179 << " Changes: " << adaption_changes_ 184 << " Changes: " << adaption_changes_
180 << " Input: " << in_width 185 << " Input: " << in_width
181 << "x" << in_height 186 << "x" << in_height
182 << " i" << input_interval_ 187 << " timestamp: " << in_timestamp_ns
183 << " Output: i" 188 << " Output: i"
184 << (requested_format_ ? requested_format_->interval : 0); 189 << (requested_format_ ? requested_format_->interval : 0);
185 } 190 }
186 191
187 // Drop frame. 192 // Drop frame.
188 *cropped_width = 0; 193 *cropped_width = 0;
189 *cropped_height = 0; 194 *cropped_height = 0;
190 *out_width = 0; 195 *out_width = 0;
191 *out_height = 0; 196 *out_height = 0;
192 return; 197 return;
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 ++frames_out_; 236 ++frames_out_;
232 if (scale.numerator != scale.denominator) 237 if (scale.numerator != scale.denominator)
233 ++frames_scaled_; 238 ++frames_scaled_;
234 239
235 if (previous_width_ && (previous_width_ != *out_width || 240 if (previous_width_ && (previous_width_ != *out_width ||
236 previous_height_ != *out_height)) { 241 previous_height_ != *out_height)) {
237 ++adaption_changes_; 242 ++adaption_changes_;
238 LOG(LS_INFO) << "Frame size changed: scaled " << frames_scaled_ << " / out " 243 LOG(LS_INFO) << "Frame size changed: scaled " << frames_scaled_ << " / out "
239 << frames_out_ << " / in " << frames_in_ 244 << frames_out_ << " / in " << frames_in_
240 << " Changes: " << adaption_changes_ << " Input: " << in_width 245 << " Changes: " << adaption_changes_ << " Input: " << in_width
241 << "x" << in_height << " i" << input_interval_ 246 << "x" << in_height
242 << " Scale: " << scale.numerator << "/" << scale.denominator 247 << " Scale: " << scale.numerator << "/" << scale.denominator
243 << " Output: " << *out_width << "x" << *out_height << " i" 248 << " Output: " << *out_width << "x" << *out_height << " i"
244 << (requested_format_ ? requested_format_->interval : 0); 249 << (requested_format_ ? requested_format_->interval : 0);
245 } 250 }
246 251
247 previous_width_ = *out_width; 252 previous_width_ = *out_width;
248 previous_height_ = *out_height; 253 previous_height_ = *out_height;
249 } 254 }
250 255
251 void VideoAdapter::OnOutputFormatRequest(const VideoFormat& format) { 256 void VideoAdapter::OnOutputFormatRequest(const VideoFormat& format) {
252 rtc::CritScope cs(&critical_section_); 257 rtc::CritScope cs(&critical_section_);
253 requested_format_ = rtc::Optional<VideoFormat>(format); 258 requested_format_ = rtc::Optional<VideoFormat>(format);
254 interval_next_frame_ = 0; 259 previous_timestamp_ns_ = 0;
260 interval_ns_next_frame_ = 0;
255 } 261 }
256 262
257 void VideoAdapter::OnResolutionRequest( 263 void VideoAdapter::OnResolutionRequest(
258 rtc::Optional<int> max_pixel_count, 264 rtc::Optional<int> max_pixel_count,
259 rtc::Optional<int> max_pixel_count_step_up) { 265 rtc::Optional<int> max_pixel_count_step_up) {
260 rtc::CritScope cs(&critical_section_); 266 rtc::CritScope cs(&critical_section_);
261 resolution_request_max_pixel_count_ = 267 resolution_request_max_pixel_count_ =
262 max_pixel_count.value_or(std::numeric_limits<int>::max()); 268 max_pixel_count.value_or(std::numeric_limits<int>::max());
263 resolution_request_max_pixel_count_step_up_ = 269 resolution_request_max_pixel_count_step_up_ =
264 max_pixel_count_step_up.value_or(0); 270 max_pixel_count_step_up.value_or(0);
265 } 271 }
266 272
267 } // namespace cricket 273 } // namespace cricket
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698