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

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

Issue 1966273002: VideoAdapter: Add cropping based on OnOutputFormatRequest() (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Remove unused variable 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
« no previous file with comments | « webrtc/media/base/videoadapter.cc ('k') | webrtc/media/base/videocapturer.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
43 } 43 }
44 44
45 protected: 45 protected:
46 class VideoCapturerListener: public sigslot::has_slots<> { 46 class VideoCapturerListener: public sigslot::has_slots<> {
47 public: 47 public:
48 struct Stats { 48 struct Stats {
49 int captured_frames; 49 int captured_frames;
50 int dropped_frames; 50 int dropped_frames;
51 bool last_adapt_was_no_op; 51 bool last_adapt_was_no_op;
52 52
53 int adapted_width; 53 int cropped_width;
54 int adapted_height; 54 int cropped_height;
55 int out_width;
56 int out_height;
55 }; 57 };
56 58
57 explicit VideoCapturerListener(VideoAdapter* adapter) 59 explicit VideoCapturerListener(VideoAdapter* adapter)
58 : video_adapter_(adapter), 60 : video_adapter_(adapter),
59 captured_frames_(0), 61 captured_frames_(0),
60 dropped_frames_(0), 62 dropped_frames_(0),
61 last_adapt_was_no_op_(false) { 63 last_adapt_was_no_op_(false) {
62 } 64 }
63 65
64 void OnFrameCaptured(VideoCapturer* capturer, 66 void OnFrameCaptured(VideoCapturer* capturer,
65 const CapturedFrame* captured_frame) { 67 const CapturedFrame* captured_frame) {
66 rtc::CritScope lock(&crit_); 68 rtc::CritScope lock(&crit_);
67 const int in_width = captured_frame->width; 69 const int in_width = captured_frame->width;
68 const int in_height = abs(captured_frame->height); 70 const int in_height = abs(captured_frame->height);
69 const VideoFormat adapted_format = 71 int cropped_width;
70 video_adapter_->AdaptFrameResolution(in_width, in_height); 72 int cropped_height;
71 if (!adapted_format.IsSize0x0()) { 73 int out_width;
72 adapted_format_ = adapted_format; 74 int out_height;
73 last_adapt_was_no_op_ = (in_width == adapted_format.width && 75 video_adapter_->AdaptFrameResolution(in_width, in_height,
74 in_height == adapted_format.height); 76 &cropped_width, &cropped_height,
77 &out_width, &out_height);
78 if (out_width != 0 && out_height != 0) {
79 cropped_width_ = cropped_width;
80 cropped_height_ = cropped_height;
81 out_width_ = out_width;
82 out_height_ = out_height;
83 last_adapt_was_no_op_ =
84 (in_width == cropped_width && in_height == cropped_height &&
85 in_width == out_width && in_height == out_height);
75 } else { 86 } else {
76 ++dropped_frames_; 87 ++dropped_frames_;
77 } 88 }
78 ++captured_frames_; 89 ++captured_frames_;
79 } 90 }
80 91
81 Stats GetStats() { 92 Stats GetStats() {
82 rtc::CritScope lock(&crit_); 93 rtc::CritScope lock(&crit_);
83 Stats stats; 94 Stats stats;
84 stats.captured_frames = captured_frames_; 95 stats.captured_frames = captured_frames_;
85 stats.dropped_frames = dropped_frames_; 96 stats.dropped_frames = dropped_frames_;
86 stats.last_adapt_was_no_op = last_adapt_was_no_op_; 97 stats.last_adapt_was_no_op = last_adapt_was_no_op_;
87 if (!adapted_format_.IsSize0x0()) { 98 stats.cropped_width = cropped_width_;
88 stats.adapted_width = adapted_format_.width; 99 stats.cropped_height = cropped_height_;
89 stats.adapted_height = adapted_format_.height; 100 stats.out_width = out_width_;
90 } else { 101 stats.out_height = out_height_;
91 stats.adapted_width = stats.adapted_height = -1;
92 }
93
94 return stats; 102 return stats;
95 } 103 }
96 104
97 private: 105 private:
98 rtc::CriticalSection crit_; 106 rtc::CriticalSection crit_;
99 VideoAdapter* video_adapter_; 107 VideoAdapter* video_adapter_;
100 VideoFormat adapted_format_; 108 int cropped_width_;
109 int cropped_height_;
110 int out_width_;
111 int out_height_;
101 int captured_frames_; 112 int captured_frames_;
102 int dropped_frames_; 113 int dropped_frames_;
103 bool last_adapt_was_no_op_; 114 bool last_adapt_was_no_op_;
104 }; 115 };
105 116
106 117
107 void VerifyAdaptedResolution(const VideoCapturerListener::Stats& stats, 118 void VerifyAdaptedResolution(const VideoCapturerListener::Stats& stats,
108 int width, 119 int cropped_width,
109 int height) { 120 int cropped_height,
110 EXPECT_EQ(width, stats.adapted_width); 121 int out_width,
111 EXPECT_EQ(height, stats.adapted_height); 122 int out_height) {
123 EXPECT_EQ(cropped_width, stats.cropped_width);
124 EXPECT_EQ(cropped_height, stats.cropped_height);
125 EXPECT_EQ(out_width, stats.out_width);
126 EXPECT_EQ(out_height, stats.out_height);
112 } 127 }
113 128
114 std::unique_ptr<FakeVideoCapturer> capturer_; 129 std::unique_ptr<FakeVideoCapturer> capturer_;
115 VideoAdapter adapter_; 130 VideoAdapter adapter_;
131 int cropped_width_;
132 int cropped_height_;
133 int out_width_;
134 int out_height_;
116 std::unique_ptr<VideoCapturerListener> listener_; 135 std::unique_ptr<VideoCapturerListener> listener_;
117 VideoFormat capture_format_; 136 VideoFormat capture_format_;
118 }; 137 };
119 138
120 // Do not adapt the frame rate or the resolution. Expect no frame drop and no 139 // Do not adapt the frame rate or the resolution. Expect no frame drop, no
121 // resolution change. 140 // cropping, and no resolution change.
122 TEST_F(VideoAdapterTest, AdaptNothing) { 141 TEST_F(VideoAdapterTest, AdaptNothing) {
123 EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_)); 142 EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_));
124 for (int i = 0; i < 10; ++i) 143 for (int i = 0; i < 10; ++i)
125 capturer_->CaptureFrame(); 144 capturer_->CaptureFrame();
126 145
127 // Verify no frame drop and no resolution change. 146 // Verify no frame drop and no resolution change.
128 VideoCapturerListener::Stats stats = listener_->GetStats(); 147 VideoCapturerListener::Stats stats = listener_->GetStats();
129 EXPECT_GE(stats.captured_frames, 10); 148 EXPECT_GE(stats.captured_frames, 10);
130 EXPECT_EQ(0, stats.dropped_frames); 149 EXPECT_EQ(0, stats.dropped_frames);
131 VerifyAdaptedResolution(stats, capture_format_.width, capture_format_.height); 150 VerifyAdaptedResolution(stats, capture_format_.width, capture_format_.height,
151 capture_format_.width, capture_format_.height);
132 EXPECT_TRUE(stats.last_adapt_was_no_op); 152 EXPECT_TRUE(stats.last_adapt_was_no_op);
133 } 153 }
134 154
135 TEST_F(VideoAdapterTest, AdaptZeroInterval) { 155 TEST_F(VideoAdapterTest, AdaptZeroInterval) {
136 VideoFormat format = capturer_->GetSupportedFormats()->at(0); 156 VideoFormat format = capturer_->GetSupportedFormats()->at(0);
137 format.interval = 0; 157 format.interval = 0;
138 adapter_.OnOutputFormatRequest(format); 158 adapter_.OnOutputFormatRequest(format);
139 EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_)); 159 EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_));
140 for (int i = 0; i < 10; ++i) 160 for (int i = 0; i < 10; ++i)
141 capturer_->CaptureFrame(); 161 capturer_->CaptureFrame();
142 162
143 // Verify no crash and that frames aren't dropped. 163 // Verify no crash and that frames aren't dropped.
144 VideoCapturerListener::Stats stats = listener_->GetStats(); 164 VideoCapturerListener::Stats stats = listener_->GetStats();
145 EXPECT_GE(stats.captured_frames, 10); 165 EXPECT_GE(stats.captured_frames, 10);
146 EXPECT_EQ(0, stats.dropped_frames); 166 EXPECT_EQ(0, stats.dropped_frames);
147 VerifyAdaptedResolution(stats, capture_format_.width, capture_format_.height); 167 VerifyAdaptedResolution(stats, capture_format_.width, capture_format_.height,
168 capture_format_.width, capture_format_.height);
148 } 169 }
149 170
150 // Adapt the frame rate to be half of the capture rate at the beginning. Expect 171 // Adapt the frame rate to be half of the capture rate at the beginning. Expect
151 // the number of dropped frames to be half of the number the captured frames. 172 // the number of dropped frames to be half of the number the captured frames.
152 TEST_F(VideoAdapterTest, AdaptFramerate) { 173 TEST_F(VideoAdapterTest, AdaptFramerate) {
153 VideoFormat request_format = capture_format_; 174 VideoFormat request_format = capture_format_;
154 request_format.interval *= 2; 175 request_format.interval *= 2;
155 adapter_.OnOutputFormatRequest(request_format); 176 adapter_.OnOutputFormatRequest(request_format);
156 EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_)); 177 EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_));
157 for (int i = 0; i < 10; ++i) 178 for (int i = 0; i < 10; ++i)
158 capturer_->CaptureFrame(); 179 capturer_->CaptureFrame();
159 180
160 // Verify frame drop and no resolution change. 181 // Verify frame drop and no resolution change.
161 VideoCapturerListener::Stats stats = listener_->GetStats(); 182 VideoCapturerListener::Stats stats = listener_->GetStats();
162 EXPECT_GE(stats.captured_frames, 10); 183 EXPECT_GE(stats.captured_frames, 10);
163 EXPECT_EQ(stats.captured_frames / 2, stats.dropped_frames); 184 EXPECT_EQ(stats.captured_frames / 2, stats.dropped_frames);
164 VerifyAdaptedResolution(stats, capture_format_.width, capture_format_.height); 185 VerifyAdaptedResolution(stats, capture_format_.width, capture_format_.height,
186 capture_format_.width, capture_format_.height);
165 } 187 }
166 188
167 // Adapt the frame rate to be half of the capture rate at the beginning. Expect 189 // Adapt the frame rate to be half of the capture rate at the beginning. Expect
168 // the number of dropped frames to be half of the number the captured frames. 190 // the number of dropped frames to be half of the number the captured frames.
169 TEST_F(VideoAdapterTest, AdaptFramerateVariable) { 191 TEST_F(VideoAdapterTest, AdaptFramerateVariable) {
170 VideoFormat request_format = capture_format_; 192 VideoFormat request_format = capture_format_;
171 request_format.interval = request_format.interval * 3 / 2; 193 request_format.interval = request_format.interval * 3 / 2;
172 adapter_.OnOutputFormatRequest(request_format); 194 adapter_.OnOutputFormatRequest(request_format);
173 EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_)); 195 EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_));
174 for (int i = 0; i < 30; ++i) 196 for (int i = 0; i < 30; ++i)
175 capturer_->CaptureFrame(); 197 capturer_->CaptureFrame();
176 198
177 // Verify frame drop and no resolution change. 199 // Verify frame drop and no resolution change.
178 VideoCapturerListener::Stats stats = listener_->GetStats(); 200 VideoCapturerListener::Stats stats = listener_->GetStats();
179 EXPECT_GE(stats.captured_frames, 30); 201 EXPECT_GE(stats.captured_frames, 30);
180 // Verify 2 / 3 kept (20) and 1 / 3 dropped (10). 202 // Verify 2 / 3 kept (20) and 1 / 3 dropped (10).
181 EXPECT_EQ(stats.captured_frames * 1 / 3, stats.dropped_frames); 203 EXPECT_EQ(stats.captured_frames * 1 / 3, stats.dropped_frames);
182 VerifyAdaptedResolution(stats, capture_format_.width, capture_format_.height); 204 VerifyAdaptedResolution(stats, capture_format_.width, capture_format_.height,
205 capture_format_.width, capture_format_.height);
183 } 206 }
184 207
185 // Adapt the frame rate to be half of the capture rate after capturing no less 208 // Adapt the frame rate to be half of the capture rate after capturing no less
186 // than 10 frames. Expect no frame dropped before adaptation and frame dropped 209 // than 10 frames. Expect no frame dropped before adaptation and frame dropped
187 // after adaptation. 210 // after adaptation.
188 TEST_F(VideoAdapterTest, AdaptFramerateOntheFly) { 211 TEST_F(VideoAdapterTest, AdaptFramerateOntheFly) {
189 VideoFormat request_format = capture_format_; 212 VideoFormat request_format = capture_format_;
190 adapter_.OnOutputFormatRequest(request_format); 213 adapter_.OnOutputFormatRequest(request_format);
191 EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_)); 214 EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_));
192 for (int i = 0; i < 10; ++i) 215 for (int i = 0; i < 10; ++i)
193 capturer_->CaptureFrame(); 216 capturer_->CaptureFrame();
194 217
195 // Verify no frame drop before adaptation. 218 // Verify no frame drop before adaptation.
196 EXPECT_EQ(0, listener_->GetStats().dropped_frames); 219 EXPECT_EQ(0, listener_->GetStats().dropped_frames);
197 220
198 // Adapat the frame rate. 221 // Adapat the frame rate.
199 request_format.interval *= 2; 222 request_format.interval *= 2;
200 adapter_.OnOutputFormatRequest(request_format); 223 adapter_.OnOutputFormatRequest(request_format);
201 224
202 for (int i = 0; i < 20; ++i) 225 for (int i = 0; i < 20; ++i)
203 capturer_->CaptureFrame(); 226 capturer_->CaptureFrame();
204 227
205 // Verify frame drop after adaptation. 228 // Verify frame drop after adaptation.
206 EXPECT_GT(listener_->GetStats().dropped_frames, 0); 229 EXPECT_GT(listener_->GetStats().dropped_frames, 0);
207 } 230 }
208 231
209 // Set a very high output pixel resolution. Expect no resolution change. 232 // Set a very high output pixel resolution. Expect no cropping or resolution
233 // change.
210 TEST_F(VideoAdapterTest, AdaptFrameResolutionHighLimit) { 234 TEST_F(VideoAdapterTest, AdaptFrameResolutionHighLimit) {
211 VideoFormat output_format = capture_format_; 235 VideoFormat output_format = capture_format_;
212 output_format.width = 2560; 236 output_format.width *= 10;
213 output_format.height = 2560; 237 output_format.height *= 10;
214 adapter_.OnOutputFormatRequest(output_format); 238 adapter_.OnOutputFormatRequest(output_format);
215 VideoFormat adapted_format = adapter_.AdaptFrameResolution( 239 adapter_.AdaptFrameResolution(capture_format_.width, capture_format_.height,
216 capture_format_.width, capture_format_.height); 240 &cropped_width_, &cropped_height_,
217 EXPECT_EQ(capture_format_.width, adapted_format.width); 241 &out_width_, &out_height_);
218 EXPECT_EQ(capture_format_.height, adapted_format.height); 242 EXPECT_EQ(capture_format_.width, cropped_width_);
243 EXPECT_EQ(capture_format_.height, cropped_height_);
244 EXPECT_EQ(capture_format_.width, out_width_);
245 EXPECT_EQ(capture_format_.height, out_height_);
219 } 246 }
220 247
221 // Adapt the frame resolution to be the same as capture resolution. Expect no 248 // Adapt the frame resolution to be the same as capture resolution. Expect no
222 // resolution change. 249 // cropping or resolution change.
223 TEST_F(VideoAdapterTest, AdaptFrameResolutionIdentical) { 250 TEST_F(VideoAdapterTest, AdaptFrameResolutionIdentical) {
224 adapter_.OnOutputFormatRequest(capture_format_); 251 adapter_.OnOutputFormatRequest(capture_format_);
225 const VideoFormat adapted_format = adapter_.AdaptFrameResolution( 252 adapter_.AdaptFrameResolution(capture_format_.width, capture_format_.height,
226 capture_format_.width, capture_format_.height); 253 &cropped_width_, &cropped_height_,
227 EXPECT_EQ(capture_format_.width, adapted_format.width); 254 &out_width_, &out_height_);
228 EXPECT_EQ(capture_format_.height, adapted_format.height); 255 EXPECT_EQ(capture_format_.width, cropped_width_);
256 EXPECT_EQ(capture_format_.height, cropped_height_);
257 EXPECT_EQ(capture_format_.width, out_width_);
258 EXPECT_EQ(capture_format_.height, out_height_);
229 } 259 }
230 260
231 // Adapt the frame resolution to be a quarter of the capture resolution. Expect 261 // Adapt the frame resolution to be a quarter of the capture resolution. Expect
232 // resolution change. 262 // no cropping, but a resolution change.
233 TEST_F(VideoAdapterTest, AdaptFrameResolutionQuarter) { 263 TEST_F(VideoAdapterTest, AdaptFrameResolutionQuarter) {
234 VideoFormat request_format = capture_format_; 264 VideoFormat request_format = capture_format_;
235 request_format.width /= 2; 265 request_format.width /= 2;
236 request_format.height /= 2; 266 request_format.height /= 2;
237 adapter_.OnOutputFormatRequest(request_format); 267 adapter_.OnOutputFormatRequest(request_format);
238 const VideoFormat adapted_format = adapter_.AdaptFrameResolution( 268 adapter_.AdaptFrameResolution(capture_format_.width, capture_format_.height,
239 request_format.width, request_format.height); 269 &cropped_width_, &cropped_height_,
240 EXPECT_EQ(request_format.width, adapted_format.width); 270 &out_width_, &out_height_);
241 EXPECT_EQ(request_format.height, adapted_format.height); 271 EXPECT_EQ(capture_format_.width, cropped_width_);
272 EXPECT_EQ(capture_format_.height, cropped_height_);
273 EXPECT_EQ(request_format.width, out_width_);
274 EXPECT_EQ(request_format.height, out_height_);
242 } 275 }
243 276
244 // Adapt the pixel resolution to 0. Expect frame drop. 277 // Adapt the pixel resolution to 0. Expect frame drop.
245 TEST_F(VideoAdapterTest, AdaptFrameResolutionDrop) { 278 TEST_F(VideoAdapterTest, AdaptFrameResolutionDrop) {
246 VideoFormat output_format = capture_format_; 279 VideoFormat output_format = capture_format_;
247 output_format.width = 0; 280 output_format.width = 0;
248 output_format.height = 0; 281 output_format.height = 0;
249 adapter_.OnOutputFormatRequest(output_format); 282 adapter_.OnOutputFormatRequest(output_format);
250 EXPECT_TRUE( 283 adapter_.AdaptFrameResolution(capture_format_.width, capture_format_.height,
251 adapter_ 284 &cropped_width_, &cropped_height_,
252 .AdaptFrameResolution(capture_format_.width, capture_format_.height) 285 &out_width_, &out_height_);
253 .IsSize0x0()); 286 EXPECT_EQ(0, out_width_);
287 EXPECT_EQ(0, out_height_);
254 } 288 }
255 289
256 // Adapt the frame resolution to be a quarter of the capture resolution at the 290 // Adapt the frame resolution to be a quarter of the capture resolution at the
257 // beginning. Expect resolution change. 291 // beginning. Expect no cropping but a resolution change.
258 TEST_F(VideoAdapterTest, AdaptResolution) { 292 TEST_F(VideoAdapterTest, AdaptResolution) {
259 VideoFormat request_format = capture_format_; 293 VideoFormat request_format = capture_format_;
260 request_format.width /= 2; 294 request_format.width /= 2;
261 request_format.height /= 2; 295 request_format.height /= 2;
262 adapter_.OnOutputFormatRequest(request_format); 296 adapter_.OnOutputFormatRequest(request_format);
263 EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_)); 297 EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_));
264 for (int i = 0; i < 10; ++i) 298 for (int i = 0; i < 10; ++i)
265 capturer_->CaptureFrame(); 299 capturer_->CaptureFrame();
266 300
267 // Verify no frame drop and resolution change. 301 // Verify no frame drop, no cropping, and resolution change.
268 VideoCapturerListener::Stats stats = listener_->GetStats(); 302 VideoCapturerListener::Stats stats = listener_->GetStats();
269 EXPECT_EQ(0, stats.dropped_frames); 303 EXPECT_EQ(0, stats.dropped_frames);
270 VerifyAdaptedResolution(stats, request_format.width, request_format.height); 304 VerifyAdaptedResolution(stats, capture_format_.width, capture_format_.height,
305 request_format.width, request_format.height);
271 } 306 }
272 307
273 // Adapt the frame resolution to be a quarter of the capture resolution after 308 // Adapt the frame resolution to be a quarter of the capture resolution after
274 // capturing no less than 10 frames. Expect no resolution change before 309 // capturing no less than 10 frames. Expect no resolution change before
275 // adaptation and resolution change after adaptation. 310 // adaptation and resolution change after adaptation.
276 TEST_F(VideoAdapterTest, AdaptResolutionOnTheFly) { 311 TEST_F(VideoAdapterTest, AdaptResolutionOnTheFly) {
277 VideoFormat request_format = capture_format_; 312 VideoFormat request_format = capture_format_;
278 adapter_.OnOutputFormatRequest(request_format); 313 adapter_.OnOutputFormatRequest(request_format);
279 EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_)); 314 EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_));
280 for (int i = 0; i < 10; ++i) 315 for (int i = 0; i < 10; ++i)
281 capturer_->CaptureFrame(); 316 capturer_->CaptureFrame();
282 317
283 // Verify no resolution change before adaptation. 318 // Verify no resolution change before adaptation.
284 VerifyAdaptedResolution( 319 VerifyAdaptedResolution(listener_->GetStats(),
285 listener_->GetStats(), request_format.width, request_format.height); 320 capture_format_.width, capture_format_.height,
321 request_format.width, request_format.height);
286 322
287 // Adapt the frame resolution. 323 // Adapt the frame resolution.
288 request_format.width /= 2; 324 request_format.width /= 2;
289 request_format.height /= 2; 325 request_format.height /= 2;
290 adapter_.OnOutputFormatRequest(request_format); 326 adapter_.OnOutputFormatRequest(request_format);
291 for (int i = 0; i < 10; ++i) 327 for (int i = 0; i < 10; ++i)
292 capturer_->CaptureFrame(); 328 capturer_->CaptureFrame();
293 329
294 // Verify resolution change after adaptation. 330 // Verify resolution change after adaptation.
295 VerifyAdaptedResolution( 331 VerifyAdaptedResolution(listener_->GetStats(),
296 listener_->GetStats(), request_format.width, request_format.height); 332 capture_format_.width, capture_format_.height,
333 request_format.width, request_format.height);
297 } 334 }
298 335
299 // Drop all frames. 336 // Drop all frames.
300 TEST_F(VideoAdapterTest, DropAllFrames) { 337 TEST_F(VideoAdapterTest, DropAllFrames) {
301 VideoFormat format; // with resolution 0x0. 338 VideoFormat format; // with resolution 0x0.
302 adapter_.OnOutputFormatRequest(format); 339 adapter_.OnOutputFormatRequest(format);
303 EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_)); 340 EXPECT_EQ(CS_RUNNING, capturer_->Start(capture_format_));
304 for (int i = 0; i < 10; ++i) 341 for (int i = 0; i < 10; ++i)
305 capturer_->CaptureFrame(); 342 capturer_->CaptureFrame();
306 343
307 // Verify all frames are dropped. 344 // Verify all frames are dropped.
308 VideoCapturerListener::Stats stats = listener_->GetStats(); 345 VideoCapturerListener::Stats stats = listener_->GetStats();
309 EXPECT_GE(stats.captured_frames, 10); 346 EXPECT_GE(stats.captured_frames, 10);
310 EXPECT_EQ(stats.captured_frames, stats.dropped_frames); 347 EXPECT_EQ(stats.captured_frames, stats.dropped_frames);
311 } 348 }
312 349
313 TEST_F(VideoAdapterTest, TestOnOutputFormatRequest) { 350 TEST_F(VideoAdapterTest, TestOnOutputFormatRequest) {
314 VideoFormat format(640, 400, VideoFormat::FpsToInterval(30), 0); 351 VideoFormat format(640, 400, VideoFormat::FpsToInterval(30), 0);
315 adapter_.SetExpectedInputFrameInterval(VideoFormat::FpsToInterval(30)); 352 adapter_.SetExpectedInputFrameInterval(VideoFormat::FpsToInterval(30));
316 VideoFormat out_format = 353 adapter_.AdaptFrameResolution(640, 400,
317 adapter_.AdaptFrameResolution(format.width, format.height); 354 &cropped_width_, &cropped_height_,
318 EXPECT_EQ(format, adapter_.input_format()); 355 &out_width_, &out_height_);
319 EXPECT_EQ(format, out_format); 356 EXPECT_EQ(640, cropped_width_);
357 EXPECT_EQ(400, cropped_height_);
358 EXPECT_EQ(640, out_width_);
359 EXPECT_EQ(400, out_height_);
320 360
321 // Format request 640x400. 361 // Format request 640x400.
322 format.height = 400; 362 format.height = 400;
323 adapter_.OnOutputFormatRequest(format); 363 adapter_.OnOutputFormatRequest(format);
324 out_format = adapter_.AdaptFrameResolution(640, 400); 364 adapter_.AdaptFrameResolution(640, 400,
325 EXPECT_EQ(640, out_format.width); 365 &cropped_width_, &cropped_height_,
326 EXPECT_EQ(400, out_format.height); 366 &out_width_, &out_height_);
367 EXPECT_EQ(640, cropped_width_);
368 EXPECT_EQ(400, cropped_height_);
369 EXPECT_EQ(640, out_width_);
370 EXPECT_EQ(400, out_height_);
327 371
328 // Request 1280x720, higher than input. Adapt nothing. 372 // Request 1280x720, higher than input, but aspect 16:9. Expect cropping but
373 // no scaling.
329 format.width = 1280; 374 format.width = 1280;
330 format.height = 720; 375 format.height = 720;
331 adapter_.OnOutputFormatRequest(format); 376 adapter_.OnOutputFormatRequest(format);
332 out_format = adapter_.AdaptFrameResolution(640, 400); 377 adapter_.AdaptFrameResolution(640, 400,
333 EXPECT_EQ(640, out_format.width); 378 &cropped_width_, &cropped_height_,
334 EXPECT_EQ(400, out_format.height); 379 &out_width_, &out_height_);
380 EXPECT_EQ(640, cropped_width_);
381 EXPECT_EQ(360, cropped_height_);
382 EXPECT_EQ(640, out_width_);
383 EXPECT_EQ(360, out_height_);
335 384
336 // Request 0x0. 385 // Request 0x0.
337 format.width = 0; 386 format.width = 0;
338 format.height = 0; 387 format.height = 0;
339 adapter_.OnOutputFormatRequest(format); 388 adapter_.OnOutputFormatRequest(format);
340 out_format = adapter_.AdaptFrameResolution(640, 400); 389 adapter_.AdaptFrameResolution(640, 400,
341 EXPECT_TRUE(out_format.IsSize0x0()); 390 &cropped_width_, &cropped_height_,
391 &out_width_, &out_height_);
392 EXPECT_EQ(0, out_width_);
393 EXPECT_EQ(0, out_height_);
342 394
343 // Request 320x200. 395 // Request 320x200. Expect scaling, but no cropping.
344 format.width = 320; 396 format.width = 320;
345 format.height = 200; 397 format.height = 200;
346 adapter_.OnOutputFormatRequest(format); 398 adapter_.OnOutputFormatRequest(format);
347 out_format = adapter_.AdaptFrameResolution(640, 400); 399 adapter_.AdaptFrameResolution(640, 400,
348 EXPECT_EQ(320, out_format.width); 400 &cropped_width_, &cropped_height_,
349 EXPECT_EQ(200, out_format.height); 401 &out_width_, &out_height_);
402 EXPECT_EQ(640, cropped_width_);
403 EXPECT_EQ(400, cropped_height_);
404 EXPECT_EQ(320, out_width_);
405 EXPECT_EQ(200, out_height_);
350 406
351 // Request resolution of 2 / 3. Expect adapt down. Scaling to 1/3 is not 407 // Request resolution close to 2/3 scale. Expect adapt down. Scaling to 2/3
352 // optimized and not allowed. 408 // is not optimized and not allowed, therefore 1/2 scaling will be used
353 format.width = (640 * 2 + 1) / 3; 409 // instead.
354 format.height = (400 * 2 + 1) / 3; 410 format.width = 424;
411 format.height = 265;
355 adapter_.OnOutputFormatRequest(format); 412 adapter_.OnOutputFormatRequest(format);
356 out_format = adapter_.AdaptFrameResolution(640, 400); 413 adapter_.AdaptFrameResolution(640, 400,
357 EXPECT_EQ(320, out_format.width); 414 &cropped_width_, &cropped_height_,
358 EXPECT_EQ(200, out_format.height); 415 &out_width_, &out_height_);
416 EXPECT_EQ(640, cropped_width_);
417 EXPECT_EQ(400, cropped_height_);
418 EXPECT_EQ(320, out_width_);
419 EXPECT_EQ(200, out_height_);
359 420
360 // Request resolution of 3 / 8. Expect adapt down. 421 // Request resolution of 3 / 8. Expect adapt down.
361 format.width = 640 * 3 / 8; 422 format.width = 640 * 3 / 8;
362 format.height = 400 * 3 / 8; 423 format.height = 400 * 3 / 8;
363 adapter_.OnOutputFormatRequest(format); 424 adapter_.OnOutputFormatRequest(format);
364 out_format = adapter_.AdaptFrameResolution(640, 400); 425 adapter_.AdaptFrameResolution(640, 400,
365 EXPECT_EQ(640 * 3 / 8, out_format.width); 426 &cropped_width_, &cropped_height_,
366 EXPECT_EQ(400 * 3 / 8, out_format.height); 427 &out_width_, &out_height_);
428 EXPECT_EQ(640, cropped_width_);
429 EXPECT_EQ(400, cropped_height_);
430 EXPECT_EQ(640 * 3 / 8, out_width_);
431 EXPECT_EQ(400 * 3 / 8, out_height_);
367 432
368 // Switch back up. Expect adapt. 433 // Switch back up. Expect adapt.
369 format.width = 320; 434 format.width = 320;
370 format.height = 200; 435 format.height = 200;
371 adapter_.OnOutputFormatRequest(format); 436 adapter_.OnOutputFormatRequest(format);
372 out_format = adapter_.AdaptFrameResolution(640, 400); 437 adapter_.AdaptFrameResolution(640, 400,
373 EXPECT_EQ(320, out_format.width); 438 &cropped_width_, &cropped_height_,
374 EXPECT_EQ(200, out_format.height); 439 &out_width_, &out_height_);
440 EXPECT_EQ(640, cropped_width_);
441 EXPECT_EQ(400, cropped_height_);
442 EXPECT_EQ(320, out_width_);
443 EXPECT_EQ(200, out_height_);
375 444
376 // Format request 480x300. 445 // Format request 480x300.
377 format.width = 480; 446 format.width = 480;
378 format.height = 300; 447 format.height = 300;
379 adapter_.OnOutputFormatRequest(format); 448 adapter_.OnOutputFormatRequest(format);
380 out_format = adapter_.AdaptFrameResolution(640, 400); 449 adapter_.AdaptFrameResolution(640, 400,
381 EXPECT_EQ(480, out_format.width); 450 &cropped_width_, &cropped_height_,
382 EXPECT_EQ(300, out_format.height); 451 &out_width_, &out_height_);
452 EXPECT_EQ(640, cropped_width_);
453 EXPECT_EQ(400, cropped_height_);
454 EXPECT_EQ(480, out_width_);
455 EXPECT_EQ(300, out_height_);
383 } 456 }
384 457
385 TEST_F(VideoAdapterTest, TestViewRequestPlusCameraSwitch) { 458 TEST_F(VideoAdapterTest, TestViewRequestPlusCameraSwitch) {
386 // Start at HD. 459 // Start at HD.
387 VideoFormat format(1280, 720, VideoFormat::FpsToInterval(30), 0); 460 VideoFormat format(1280, 720, VideoFormat::FpsToInterval(30), 0);
388 adapter_.SetExpectedInputFrameInterval(VideoFormat::FpsToInterval(30)); 461 adapter_.SetExpectedInputFrameInterval(VideoFormat::FpsToInterval(30));
389 VideoFormat out_format = 462 adapter_.AdaptFrameResolution(1280, 720,
390 adapter_.AdaptFrameResolution(format.width, format.height); 463 &cropped_width_, &cropped_height_,
391 EXPECT_EQ(format, adapter_.input_format()); 464 &out_width_, &out_height_);
392 EXPECT_EQ(out_format, adapter_.input_format()); 465 EXPECT_EQ(1280, cropped_width_);
466 EXPECT_EQ(720, cropped_height_);
467 EXPECT_EQ(1280, out_width_);
468 EXPECT_EQ(720, out_height_);
393 469
394 // Format request for VGA. 470 // Format request for VGA.
395 format.width = 640; 471 format.width = 640;
396 format.height = 360; 472 format.height = 360;
397 adapter_.OnOutputFormatRequest(format); 473 adapter_.OnOutputFormatRequest(format);
398 out_format = adapter_.AdaptFrameResolution(1280, 720); 474 adapter_.AdaptFrameResolution(1280, 720,
399 EXPECT_EQ(640, out_format.width); 475 &cropped_width_, &cropped_height_,
400 EXPECT_EQ(360, out_format.height); 476 &out_width_, &out_height_);
477 EXPECT_EQ(1280, cropped_width_);
478 EXPECT_EQ(720, cropped_height_);
479 EXPECT_EQ(640, out_width_);
480 EXPECT_EQ(360, out_height_);
401 481
402 // Now, the camera reopens at VGA. 482 // Now, the camera reopens at VGA.
403 // Both the frame and the output format should be 640x360. 483 // Both the frame and the output format should be 640x360.
404 out_format = adapter_.AdaptFrameResolution(640, 360); 484 adapter_.AdaptFrameResolution(640, 360,
405 EXPECT_EQ(640, out_format.width); 485 &cropped_width_, &cropped_height_,
406 EXPECT_EQ(360, out_format.height); 486 &out_width_, &out_height_);
487 EXPECT_EQ(640, cropped_width_);
488 EXPECT_EQ(360, cropped_height_);
489 EXPECT_EQ(640, out_width_);
490 EXPECT_EQ(360, out_height_);
407 491
408 // And another view request comes in for 640x360, which should have no 492 // And another view request comes in for 640x360, which should have no
409 // real impact. 493 // real impact.
410 adapter_.OnOutputFormatRequest(format); 494 adapter_.OnOutputFormatRequest(format);
411 out_format = adapter_.AdaptFrameResolution(640, 360); 495 adapter_.AdaptFrameResolution(640, 360,
412 EXPECT_EQ(640, out_format.width); 496 &cropped_width_, &cropped_height_,
413 EXPECT_EQ(360, out_format.height); 497 &out_width_, &out_height_);
498 EXPECT_EQ(640, cropped_width_);
499 EXPECT_EQ(360, cropped_height_);
500 EXPECT_EQ(640, out_width_);
501 EXPECT_EQ(360, out_height_);
414 } 502 }
415 503
416 TEST_F(VideoAdapterTest, TestVGAWidth) { 504 TEST_F(VideoAdapterTest, TestVGAWidth) {
417 // Reqeuested Output format is 640x360. 505 // Reqeuested Output format is 640x360.
418 VideoFormat format(640, 360, VideoFormat::FpsToInterval(30), FOURCC_I420); 506 VideoFormat format(640, 360, VideoFormat::FpsToInterval(30), FOURCC_I420);
419 adapter_.SetExpectedInputFrameInterval(VideoFormat::FpsToInterval(30)); 507 adapter_.SetExpectedInputFrameInterval(VideoFormat::FpsToInterval(30));
420 adapter_.OnOutputFormatRequest(format); 508 adapter_.OnOutputFormatRequest(format);
421 509
422 VideoFormat out_format = adapter_.AdaptFrameResolution(640, 480); 510 adapter_.AdaptFrameResolution(640, 480,
423 // At this point, we have to adapt down to something lower. 511 &cropped_width_, &cropped_height_,
424 EXPECT_EQ(480, out_format.width); 512 &out_width_, &out_height_);
425 EXPECT_EQ(360, out_format.height); 513 // Expect cropping.
514 EXPECT_EQ(640, cropped_width_);
515 EXPECT_EQ(360, cropped_height_);
516 EXPECT_EQ(640, out_width_);
517 EXPECT_EQ(360, out_height_);
426 518
427 // But if frames come in at 640x360, we shouldn't adapt them down. 519 // But if frames come in at 640x360, we shouldn't adapt them down.
428 out_format = adapter_.AdaptFrameResolution(640, 360); 520 adapter_.AdaptFrameResolution(640, 360,
429 EXPECT_EQ(640, out_format.width); 521 &cropped_width_, &cropped_height_,
430 EXPECT_EQ(360, out_format.height); 522 &out_width_, &out_height_);
431 523 EXPECT_EQ(640, cropped_width_);
432 out_format = adapter_.AdaptFrameResolution(640, 480); 524 EXPECT_EQ(360, cropped_height_);
433 EXPECT_EQ(480, out_format.width); 525 EXPECT_EQ(640, out_width_);
434 EXPECT_EQ(360, out_format.height); 526 EXPECT_EQ(360, out_height_);
527
528 adapter_.AdaptFrameResolution(640, 480,
529 &cropped_width_, &cropped_height_,
530 &out_width_, &out_height_);
531 EXPECT_EQ(640, cropped_width_);
532 EXPECT_EQ(360, cropped_height_);
533 EXPECT_EQ(640, out_width_);
534 EXPECT_EQ(360, out_height_);
435 } 535 }
436 536
437 TEST_F(VideoAdapterTest, TestOnResolutionRequestInSmallSteps) { 537 TEST_F(VideoAdapterTest, TestOnResolutionRequestInSmallSteps) {
438 VideoFormat out_format = adapter_.AdaptFrameResolution(1280, 720); 538 adapter_.AdaptFrameResolution(1280, 720,
439 EXPECT_EQ(1280, out_format.width); 539 &cropped_width_, &cropped_height_,
440 EXPECT_EQ(720, out_format.height); 540 &out_width_, &out_height_);
541 EXPECT_EQ(1280, cropped_width_);
542 EXPECT_EQ(720, cropped_height_);
543 EXPECT_EQ(1280, out_width_);
544 EXPECT_EQ(720, out_height_);
441 545
442 // Adapt down one step. 546 // Adapt down one step.
443 adapter_.OnResolutionRequest(rtc::Optional<int>(1280 * 720 - 1), 547 adapter_.OnResolutionRequest(rtc::Optional<int>(1280 * 720 - 1),
444 rtc::Optional<int>()); 548 rtc::Optional<int>());
445 out_format = adapter_.AdaptFrameResolution(1280, 720); 549 adapter_.AdaptFrameResolution(1280, 720,
446 EXPECT_EQ(960, out_format.width); 550 &cropped_width_, &cropped_height_,
447 EXPECT_EQ(540, out_format.height); 551 &out_width_, &out_height_);
552 EXPECT_EQ(1280, cropped_width_);
553 EXPECT_EQ(720, cropped_height_);
554 EXPECT_EQ(960, out_width_);
555 EXPECT_EQ(540, out_height_);
448 556
449 // Adapt down one step more. 557 // Adapt down one step more.
450 adapter_.OnResolutionRequest(rtc::Optional<int>(960 * 540 - 1), 558 adapter_.OnResolutionRequest(rtc::Optional<int>(960 * 540 - 1),
451 rtc::Optional<int>()); 559 rtc::Optional<int>());
452 out_format = adapter_.AdaptFrameResolution(1280, 720); 560 adapter_.AdaptFrameResolution(1280, 720,
453 EXPECT_EQ(640, out_format.width); 561 &cropped_width_, &cropped_height_,
454 EXPECT_EQ(360, out_format.height); 562 &out_width_, &out_height_);
563 EXPECT_EQ(1280, cropped_width_);
564 EXPECT_EQ(720, cropped_height_);
565 EXPECT_EQ(640, out_width_);
566 EXPECT_EQ(360, out_height_);
455 567
456 // Adapt down one step more. 568 // Adapt down one step more.
457 adapter_.OnResolutionRequest(rtc::Optional<int>(640 * 360 - 1), 569 adapter_.OnResolutionRequest(rtc::Optional<int>(640 * 360 - 1),
458 rtc::Optional<int>()); 570 rtc::Optional<int>());
459 out_format = adapter_.AdaptFrameResolution(1280, 720); 571 adapter_.AdaptFrameResolution(1280, 720,
460 EXPECT_EQ(480, out_format.width); 572 &cropped_width_, &cropped_height_,
461 EXPECT_EQ(270, out_format.height); 573 &out_width_, &out_height_);
574 EXPECT_EQ(1280, cropped_width_);
575 EXPECT_EQ(720, cropped_height_);
576 EXPECT_EQ(480, out_width_);
577 EXPECT_EQ(270, out_height_);
462 578
463 // Adapt up one step. 579 // Adapt up one step.
464 adapter_.OnResolutionRequest(rtc::Optional<int>(), 580 adapter_.OnResolutionRequest(rtc::Optional<int>(),
465 rtc::Optional<int>(480 * 270)); 581 rtc::Optional<int>(480 * 270));
466 out_format = adapter_.AdaptFrameResolution(1280, 720); 582 adapter_.AdaptFrameResolution(1280, 720,
467 EXPECT_EQ(640, out_format.width); 583 &cropped_width_, &cropped_height_,
468 EXPECT_EQ(360, out_format.height); 584 &out_width_, &out_height_);
585 EXPECT_EQ(1280, cropped_width_);
586 EXPECT_EQ(720, cropped_height_);
587 EXPECT_EQ(640, out_width_);
588 EXPECT_EQ(360, out_height_);
469 589
470 // Adapt up one step more. 590 // Adapt up one step more.
471 adapter_.OnResolutionRequest(rtc::Optional<int>(), 591 adapter_.OnResolutionRequest(rtc::Optional<int>(),
472 rtc::Optional<int>(640 * 360)); 592 rtc::Optional<int>(640 * 360));
473 out_format = adapter_.AdaptFrameResolution(1280, 720); 593 adapter_.AdaptFrameResolution(1280, 720,
474 EXPECT_EQ(960, out_format.width); 594 &cropped_width_, &cropped_height_,
475 EXPECT_EQ(540, out_format.height); 595 &out_width_, &out_height_);
596 EXPECT_EQ(1280, cropped_width_);
597 EXPECT_EQ(720, cropped_height_);
598 EXPECT_EQ(960, out_width_);
599 EXPECT_EQ(540, out_height_);
476 600
477 // Adapt up one step more. 601 // Adapt up one step more.
478 adapter_.OnResolutionRequest(rtc::Optional<int>(), 602 adapter_.OnResolutionRequest(rtc::Optional<int>(),
479 rtc::Optional<int>(960 * 720)); 603 rtc::Optional<int>(960 * 720));
480 out_format = adapter_.AdaptFrameResolution(1280, 720); 604 adapter_.AdaptFrameResolution(1280, 720,
481 EXPECT_EQ(1280, out_format.width); 605 &cropped_width_, &cropped_height_,
482 EXPECT_EQ(720, out_format.height); 606 &out_width_, &out_height_);
607 EXPECT_EQ(1280, cropped_width_);
608 EXPECT_EQ(720, cropped_height_);
609 EXPECT_EQ(1280, out_width_);
610 EXPECT_EQ(720, out_height_);
483 } 611 }
484 612
485 TEST_F(VideoAdapterTest, TestOnResolutionRequestMaxZero) { 613 TEST_F(VideoAdapterTest, TestOnResolutionRequestMaxZero) {
486 VideoFormat out_format = adapter_.AdaptFrameResolution(1280, 720); 614 adapter_.AdaptFrameResolution(1280, 720,
487 EXPECT_EQ(1280, out_format.width); 615 &cropped_width_, &cropped_height_,
488 EXPECT_EQ(720, out_format.height); 616 &out_width_, &out_height_);
617 EXPECT_EQ(1280, cropped_width_);
618 EXPECT_EQ(720, cropped_height_);
619 EXPECT_EQ(1280, out_width_);
620 EXPECT_EQ(720, out_height_);
489 621
490 adapter_.OnResolutionRequest(rtc::Optional<int>(0), rtc::Optional<int>()); 622 adapter_.OnResolutionRequest(rtc::Optional<int>(0), rtc::Optional<int>());
491 out_format = adapter_.AdaptFrameResolution(1280, 720); 623 adapter_.AdaptFrameResolution(1280, 720,
492 EXPECT_EQ(0, out_format.width); 624 &cropped_width_, &cropped_height_,
493 EXPECT_EQ(0, out_format.height); 625 &out_width_, &out_height_);
626 EXPECT_EQ(0, out_width_);
627 EXPECT_EQ(0, out_height_);
494 } 628 }
495 629
496 TEST_F(VideoAdapterTest, TestOnResolutionRequestInLargeSteps) { 630 TEST_F(VideoAdapterTest, TestOnResolutionRequestInLargeSteps) {
497 adapter_.OnResolutionRequest(rtc::Optional<int>(640 * 360 - 1), 631 adapter_.OnResolutionRequest(rtc::Optional<int>(640 * 360 - 1),
498 rtc::Optional<int>()); 632 rtc::Optional<int>());
499 VideoFormat out_format = adapter_.AdaptFrameResolution(1280, 720); 633 adapter_.AdaptFrameResolution(1280, 720,
500 EXPECT_EQ(480, out_format.width); 634 &cropped_width_, &cropped_height_,
501 EXPECT_EQ(270, out_format.height); 635 &out_width_, &out_height_);
636 EXPECT_EQ(1280, cropped_width_);
637 EXPECT_EQ(720, cropped_height_);
638 EXPECT_EQ(480, out_width_);
639 EXPECT_EQ(270, out_height_);
502 640
503 adapter_.OnResolutionRequest(rtc::Optional<int>(), 641 adapter_.OnResolutionRequest(rtc::Optional<int>(),
504 rtc::Optional<int>(960 * 720)); 642 rtc::Optional<int>(960 * 720));
505 out_format = adapter_.AdaptFrameResolution(1280, 720); 643 adapter_.AdaptFrameResolution(1280, 720,
506 EXPECT_EQ(1280, out_format.width); 644 &cropped_width_, &cropped_height_,
507 EXPECT_EQ(720, out_format.height); 645 &out_width_, &out_height_);
646 EXPECT_EQ(1280, cropped_width_);
647 EXPECT_EQ(720, cropped_height_);
648 EXPECT_EQ(1280, out_width_);
649 EXPECT_EQ(720, out_height_);
508 } 650 }
509 651
510 TEST_F(VideoAdapterTest, TestOnOutputFormatRequestCapsMaxResolution) { 652 TEST_F(VideoAdapterTest, TestOnOutputFormatRequestCapsMaxResolution) {
511 adapter_.OnResolutionRequest(rtc::Optional<int>(640 * 360 - 1), 653 adapter_.OnResolutionRequest(rtc::Optional<int>(640 * 360 - 1),
512 rtc::Optional<int>()); 654 rtc::Optional<int>());
513 VideoFormat out_format = adapter_.AdaptFrameResolution(1280, 720); 655 adapter_.AdaptFrameResolution(1280, 720,
514 EXPECT_EQ(480, out_format.width); 656 &cropped_width_, &cropped_height_,
515 EXPECT_EQ(270, out_format.height); 657 &out_width_, &out_height_);
658 EXPECT_EQ(1280, cropped_width_);
659 EXPECT_EQ(720, cropped_height_);
660 EXPECT_EQ(480, out_width_);
661 EXPECT_EQ(270, out_height_);
516 662
517 VideoFormat new_format(640, 360, VideoFormat::FpsToInterval(30), FOURCC_I420); 663 VideoFormat new_format(640, 360, VideoFormat::FpsToInterval(30), FOURCC_I420);
518 adapter_.SetExpectedInputFrameInterval(VideoFormat::FpsToInterval(30)); 664 adapter_.SetExpectedInputFrameInterval(VideoFormat::FpsToInterval(30));
519 adapter_.OnOutputFormatRequest(new_format); 665 adapter_.OnOutputFormatRequest(new_format);
520 out_format = adapter_.AdaptFrameResolution(1280, 720); 666 adapter_.AdaptFrameResolution(1280, 720,
521 EXPECT_EQ(480, out_format.width); 667 &cropped_width_, &cropped_height_,
522 EXPECT_EQ(270, out_format.height); 668 &out_width_, &out_height_);
669 EXPECT_EQ(1280, cropped_width_);
670 EXPECT_EQ(720, cropped_height_);
671 EXPECT_EQ(480, out_width_);
672 EXPECT_EQ(270, out_height_);
523 673
524 adapter_.OnResolutionRequest(rtc::Optional<int>(), 674 adapter_.OnResolutionRequest(rtc::Optional<int>(),
525 rtc::Optional<int>(960 * 720)); 675 rtc::Optional<int>(960 * 720));
526 out_format = adapter_.AdaptFrameResolution(1280, 720); 676 adapter_.AdaptFrameResolution(1280, 720,
527 EXPECT_EQ(640, out_format.width); 677 &cropped_width_, &cropped_height_,
528 EXPECT_EQ(360, out_format.height); 678 &out_width_, &out_height_);
679 EXPECT_EQ(1280, cropped_width_);
680 EXPECT_EQ(720, cropped_height_);
681 EXPECT_EQ(640, out_width_);
682 EXPECT_EQ(360, out_height_);
529 } 683 }
530 684
531 TEST_F(VideoAdapterTest, TestOnResolutionRequestReset) { 685 TEST_F(VideoAdapterTest, TestOnResolutionRequestReset) {
532 VideoFormat out_format = adapter_.AdaptFrameResolution(1280, 720); 686 adapter_.AdaptFrameResolution(1280, 720,
533 EXPECT_EQ(1280, out_format.width); 687 &cropped_width_, &cropped_height_,
534 EXPECT_EQ(720, out_format.height); 688 &out_width_, &out_height_);
535 689 EXPECT_EQ(1280, cropped_width_);
536 adapter_.OnResolutionRequest(rtc::Optional<int>(640 * 360 - 1), 690 EXPECT_EQ(720, cropped_height_);
537 rtc::Optional<int>()); 691 EXPECT_EQ(1280, out_width_);
538 out_format = adapter_.AdaptFrameResolution(1280, 720); 692 EXPECT_EQ(720, out_height_);
539 EXPECT_EQ(480, out_format.width); 693
540 EXPECT_EQ(270, out_format.height); 694 adapter_.OnResolutionRequest(rtc::Optional<int>(640 * 360 - 1),
695 rtc::Optional<int>());
696 adapter_.AdaptFrameResolution(1280, 720,
697 &cropped_width_, &cropped_height_,
698 &out_width_, &out_height_);
699 EXPECT_EQ(1280, cropped_width_);
700 EXPECT_EQ(720, cropped_height_);
701 EXPECT_EQ(480, out_width_);
702 EXPECT_EQ(270, out_height_);
541 703
542 adapter_.OnResolutionRequest(rtc::Optional<int>(), rtc::Optional<int>()); 704 adapter_.OnResolutionRequest(rtc::Optional<int>(), rtc::Optional<int>());
543 out_format = adapter_.AdaptFrameResolution(1280, 720); 705 adapter_.AdaptFrameResolution(1280, 720,
544 EXPECT_EQ(1280, out_format.width); 706 &cropped_width_, &cropped_height_,
545 EXPECT_EQ(720, out_format.height); 707 &out_width_, &out_height_);
708 EXPECT_EQ(1280, cropped_width_);
709 EXPECT_EQ(720, cropped_height_);
710 EXPECT_EQ(1280, out_width_);
711 EXPECT_EQ(720, out_height_);
712 }
713
714 TEST_F(VideoAdapterTest, TestCroppingWithResolutionRequest) {
715 // Ask for 640x360 (16:9 aspect).
716 adapter_.SetExpectedInputFrameInterval(VideoFormat::FpsToInterval(30));
717 adapter_.OnOutputFormatRequest(
718 VideoFormat(640, 360, VideoFormat::FpsToInterval(30), FOURCC_I420));
719 // Send 640x480 (4:3 aspect).
720 adapter_.AdaptFrameResolution(640, 480,
721 &cropped_width_, &cropped_height_,
722 &out_width_, &out_height_);
723 // Expect cropping to 16:9 format and no scaling.
724 EXPECT_EQ(640, cropped_width_);
725 EXPECT_EQ(360, cropped_height_);
726 EXPECT_EQ(640, out_width_);
727 EXPECT_EQ(360, out_height_);
728
729 // Adapt down one step.
730 adapter_.OnResolutionRequest(rtc::Optional<int>(640 * 360 - 1),
731 rtc::Optional<int>());
732 // Expect cropping to 16:9 format and 3/4 scaling.
733 adapter_.AdaptFrameResolution(640, 480,
734 &cropped_width_, &cropped_height_,
735 &out_width_, &out_height_);
736 EXPECT_EQ(640, cropped_width_);
737 EXPECT_EQ(360, cropped_height_);
738 EXPECT_EQ(480, out_width_);
739 EXPECT_EQ(270, out_height_);
740
741 // Adapt down one step more.
742 adapter_.OnResolutionRequest(rtc::Optional<int>(480 * 270 - 1),
743 rtc::Optional<int>());
744 // Expect cropping to 16:9 format and 1/2 scaling.
745 adapter_.AdaptFrameResolution(640, 480,
746 &cropped_width_, &cropped_height_,
747 &out_width_, &out_height_);
748 EXPECT_EQ(640, cropped_width_);
749 EXPECT_EQ(360, cropped_height_);
750 EXPECT_EQ(320, out_width_);
751 EXPECT_EQ(180, out_height_);
752
753 // Adapt up one step.
754 adapter_.OnResolutionRequest(rtc::Optional<int>(),
755 rtc::Optional<int>(320 * 180));
756 // Expect cropping to 16:9 format and 3/4 scaling.
757 adapter_.AdaptFrameResolution(640, 480,
758 &cropped_width_, &cropped_height_,
759 &out_width_, &out_height_);
760 EXPECT_EQ(640, cropped_width_);
761 EXPECT_EQ(360, cropped_height_);
762 EXPECT_EQ(480, out_width_);
763 EXPECT_EQ(270, out_height_);
764
765 // Adapt up one step more.
766 adapter_.OnResolutionRequest(rtc::Optional<int>(),
767 rtc::Optional<int>(480 * 270));
768 // Expect cropping to 16:9 format and no scaling.
769 adapter_.AdaptFrameResolution(640, 480,
770 &cropped_width_, &cropped_height_,
771 &out_width_, &out_height_);
772 EXPECT_EQ(640, cropped_width_);
773 EXPECT_EQ(360, cropped_height_);
774 EXPECT_EQ(640, out_width_);
775 EXPECT_EQ(360, out_height_);
776
777 // Try to adapt up one step more.
778 adapter_.OnResolutionRequest(rtc::Optional<int>(),
779 rtc::Optional<int>(640 * 360));
780 // Expect cropping to 16:9 format and no scaling.
781 adapter_.AdaptFrameResolution(640, 480,
782 &cropped_width_, &cropped_height_,
783 &out_width_, &out_height_);
784 EXPECT_EQ(640, cropped_width_);
785 EXPECT_EQ(360, cropped_height_);
786 EXPECT_EQ(640, out_width_);
787 EXPECT_EQ(360, out_height_);
788 }
789
790 TEST_F(VideoAdapterTest, TestCroppingOddResolution) {
791 // Ask for 640x360 (16:9 aspect), with 3/16 scaling.
792 adapter_.SetExpectedInputFrameInterval(VideoFormat::FpsToInterval(30));
793 adapter_.OnOutputFormatRequest(
794 VideoFormat(640, 360, VideoFormat::FpsToInterval(30), FOURCC_I420));
795 adapter_.OnResolutionRequest(rtc::Optional<int>(640 * 360 * 3 / 16 * 3 / 16),
796 rtc::Optional<int>());
797
798 // Send 640x480 (4:3 aspect).
799 adapter_.AdaptFrameResolution(640, 480,
800 &cropped_width_, &cropped_height_,
801 &out_width_, &out_height_);
802
803 // Instead of getting the exact aspect ratio with cropped resolution 640x360,
804 // the resolution should be adjusted to get a perfect scale factor instead.
805 EXPECT_EQ(640, cropped_width_);
806 EXPECT_EQ(368, cropped_height_);
807 EXPECT_EQ(120, out_width_);
808 EXPECT_EQ(69, out_height_);
546 } 809 }
547 810
548 } // namespace cricket 811 } // namespace cricket
OLDNEW
« no previous file with comments | « webrtc/media/base/videoadapter.cc ('k') | webrtc/media/base/videocapturer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698