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: content/renderer/media/media_stream_video_source_unittest.cc

Issue 246433006: Change MediaStreamVideoSource to output different resolutions to different tracks depending on the … (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed review comments. Created 6 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <string> 5 #include <string>
6 #include <vector> 6 #include <vector>
7 7
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 int NumberOfSuccessConstraintsCallbacks() const { 87 int NumberOfSuccessConstraintsCallbacks() const {
88 return number_of_successful_constraints_applied_; 88 return number_of_successful_constraints_applied_;
89 } 89 }
90 90
91 int NumberOfFailedConstraintsCallbacks() const { 91 int NumberOfFailedConstraintsCallbacks() const {
92 return number_of_failed_constraints_applied_; 92 return number_of_failed_constraints_applied_;
93 } 93 }
94 94
95 MockMediaStreamVideoSource* mock_source() { return mock_source_; } 95 MockMediaStreamVideoSource* mock_source() { return mock_source_; }
96 96
97 // Test that the source crops to the requested max width and 97 // Test that the source crops/scales to the requested width and
98 // height even though the camera delivers a larger frame. 98 // height even though the camera delivers a larger frame.
99 void TestSourceCropFrame(int capture_width, 99 void TestSourceCropFrame(int capture_width,
100 int capture_height, 100 int capture_height,
101 const blink::WebMediaConstraints& constraints, 101 const blink::WebMediaConstraints& constraints,
102 int expected_width, 102 int expected_width,
103 int expected_height) { 103 int expected_height) {
104 // Expect the source to start capture with the supported resolution. 104 // Expect the source to start capture with the supported resolution.
105 blink::WebMediaStreamTrack track = 105 blink::WebMediaStreamTrack track =
106 CreateTrackAndStartSource(constraints, capture_width, capture_height, 106 CreateTrackAndStartSource(constraints, capture_width, capture_height,
107 30); 107 30);
(...skipping 15 matching lines...) Expand all
123 base::RunLoop run_loop; 123 base::RunLoop run_loop;
124 base::Closure quit_closure = run_loop.QuitClosure(); 124 base::Closure quit_closure = run_loop.QuitClosure();
125 EXPECT_CALL(*sink, OnVideoFrame()).WillOnce( 125 EXPECT_CALL(*sink, OnVideoFrame()).WillOnce(
126 RunClosure(quit_closure)); 126 RunClosure(quit_closure));
127 scoped_refptr<media::VideoFrame> frame = 127 scoped_refptr<media::VideoFrame> frame =
128 media::VideoFrame::CreateBlackFrame(gfx::Size(width, height)); 128 media::VideoFrame::CreateBlackFrame(gfx::Size(width, height));
129 mock_source()->DeliverVideoFrame(frame); 129 mock_source()->DeliverVideoFrame(frame);
130 run_loop.Run(); 130 run_loop.Run();
131 } 131 }
132 132
133 void DeliverVideoFrameAndWaitForTwoRenderers(
134 int width,
135 int height,
136 MockMediaStreamVideoSink* sink1,
137 MockMediaStreamVideoSink* sink2) {
138 base::RunLoop run_loop;
139 base::Closure quit_closure = run_loop.QuitClosure();
140 EXPECT_CALL(*sink1, OnVideoFrame());
141 EXPECT_CALL(*sink2, OnVideoFrame()).WillOnce(
142 RunClosure(quit_closure));
143 scoped_refptr<media::VideoFrame> frame =
144 media::VideoFrame::CreateBlackFrame(gfx::Size(width, height));
145 mock_source()->DeliverVideoFrame(frame);
146 run_loop.Run();
147 }
148
149 void TestTwoTracksWithDifferentConstraints(
150 const blink::WebMediaConstraints& constraints1,
151 const blink::WebMediaConstraints& constraints2,
152 int capture_width,
153 int capture_height,
154 int expected_width1,
155 int expected_height1,
156 int expected_width2,
157 int expected_height2) {
158 blink::WebMediaStreamTrack track1 =
159 CreateTrackAndStartSource(constraints1, capture_width, capture_height,
160 MediaStreamVideoSource::kDefaultFrameRate);
161
162 blink::WebMediaStreamTrack track2 =
163 CreateTrack("dummy", constraints2);
164
165 MockMediaStreamVideoSink sink1;
166 MediaStreamVideoSink::AddToVideoTrack(&sink1, track1);
167 EXPECT_EQ(0, sink1.number_of_frames());
168
169 MockMediaStreamVideoSink sink2;
170 MediaStreamVideoSink::AddToVideoTrack(&sink2, track2);
171 EXPECT_EQ(0, sink2.number_of_frames());
172
173 DeliverVideoFrameAndWaitForTwoRenderers(capture_width,
174 capture_height,
175 &sink1,
176 &sink2);
177
178 EXPECT_EQ(1, sink1.number_of_frames());
179 EXPECT_EQ(expected_width1, sink1.frame_size().width());
180 EXPECT_EQ(expected_height1, sink1.frame_size().height());
181
182 EXPECT_EQ(1, sink2.number_of_frames());
183 EXPECT_EQ(expected_width2, sink2.frame_size().width());
184 EXPECT_EQ(expected_height2, sink2.frame_size().height());
185
186 MediaStreamVideoSink::RemoveFromVideoTrack(&sink1, track1);
187 MediaStreamVideoSink::RemoveFromVideoTrack(&sink2, track2);
188 }
133 189
134 void ReleaseTrackAndSourceOnAddTrackCallback( 190 void ReleaseTrackAndSourceOnAddTrackCallback(
135 const blink::WebMediaStreamTrack& track_to_release) { 191 const blink::WebMediaStreamTrack& track_to_release) {
136 track_to_release_ = track_to_release; 192 track_to_release_ = track_to_release;
137 } 193 }
138 194
139 private: 195 private:
140 void OnConstraintsApplied(MediaStreamSource* source, bool success) { 196 void OnConstraintsApplied(MediaStreamSource* source, bool success) {
141 ASSERT_EQ(source, webkit_source_.extraData()); 197 ASSERT_EQ(source, webkit_source_.extraData());
142 198
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 // require it even if an optional constraint request a higher resolution 295 // require it even if an optional constraint request a higher resolution
240 // that don't have this aspect ratio. 296 // that don't have this aspect ratio.
241 TEST_F(MediaStreamVideoSourceTest, MandatoryAspectRatio4To3) { 297 TEST_F(MediaStreamVideoSourceTest, MandatoryAspectRatio4To3) {
242 MockMediaConstraintFactory factory; 298 MockMediaConstraintFactory factory;
243 factory.AddMandatory(MediaStreamVideoSource::kMinWidth, 640); 299 factory.AddMandatory(MediaStreamVideoSource::kMinWidth, 640);
244 factory.AddMandatory(MediaStreamVideoSource::kMinHeight, 480); 300 factory.AddMandatory(MediaStreamVideoSource::kMinHeight, 480);
245 factory.AddMandatory(MediaStreamVideoSource::kMaxAspectRatio, 301 factory.AddMandatory(MediaStreamVideoSource::kMaxAspectRatio,
246 640.0 / 480); 302 640.0 / 480);
247 factory.AddOptional(MediaStreamVideoSource::kMinWidth, 1280); 303 factory.AddOptional(MediaStreamVideoSource::kMinWidth, 1280);
248 304
249 CreateTrackAndStartSource(factory.CreateWebMediaConstraints(), 640, 480, 30); 305 TestSourceCropFrame(1280, 720,
306 factory.CreateWebMediaConstraints(), 960, 720);
250 } 307 }
251 308
252 // Test that AddTrack fail if the mandatory aspect ratio 309 // Test that AddTrack succeeds if the mandatory min aspect ratio it set to 2.
253 // is set higher than supported. 310 TEST_F(MediaStreamVideoSourceTest, MandatoryAspectRatio2) {
254 TEST_F(MediaStreamVideoSourceTest, MandatoryAspectRatioTooHigh) {
255 MockMediaConstraintFactory factory; 311 MockMediaConstraintFactory factory;
256 factory.AddMandatory(MediaStreamVideoSource::kMinAspectRatio, 2); 312 factory.AddMandatory(MediaStreamVideoSource::kMinAspectRatio, 2);
313
314 TestSourceCropFrame(MediaStreamVideoSource::kDefaultWidth,
315 MediaStreamVideoSource::kDefaultHeight,
316 factory.CreateWebMediaConstraints(), 640, 320);
317 }
318
319 TEST_F(MediaStreamVideoSourceTest, MinAspectRatioLargerThanMaxAspectRatio) {
320 MockMediaConstraintFactory factory;
321 factory.AddMandatory(MediaStreamVideoSource::kMinAspectRatio, 2);
322 factory.AddMandatory(MediaStreamVideoSource::kMaxAspectRatio, 1);
257 blink::WebMediaStreamTrack track = CreateTrack( 323 blink::WebMediaStreamTrack track = CreateTrack(
258 "123", factory.CreateWebMediaConstraints()); 324 "123", factory.CreateWebMediaConstraints());
259 mock_source()->CompleteGetSupportedFormats(); 325 mock_source()->CompleteGetSupportedFormats();
326 EXPECT_EQ(1, NumberOfFailedConstraintsCallbacks());
327 }
328
329 TEST_F(MediaStreamVideoSourceTest, MaxAspectRatioZero) {
330 MockMediaConstraintFactory factory;
331 factory.AddOptional(MediaStreamVideoSource::kMaxAspectRatio, 0);
332 blink::WebMediaStreamTrack track = CreateTrack(
333 "123", factory.CreateWebMediaConstraints());
334 mock_source()->CompleteGetSupportedFormats();
335 EXPECT_EQ(1, NumberOfFailedConstraintsCallbacks());
336 }
337
338 TEST_F(MediaStreamVideoSourceTest, MinWidthLargerThanMaxWidth) {
339 MockMediaConstraintFactory factory;
340 factory.AddMandatory(MediaStreamVideoSource::kMinWidth, 640);
341 factory.AddMandatory(MediaStreamVideoSource::kMaxWidth, 320);
342 blink::WebMediaStreamTrack track = CreateTrack(
343 "123", factory.CreateWebMediaConstraints());
344 mock_source()->CompleteGetSupportedFormats();
345 EXPECT_EQ(1, NumberOfFailedConstraintsCallbacks());
346 }
347
348 TEST_F(MediaStreamVideoSourceTest, MinHeightLargerThanMaxHeight) {
349 MockMediaConstraintFactory factory;
350 factory.AddMandatory(MediaStreamVideoSource::kMinHeight, 480);
351 factory.AddMandatory(MediaStreamVideoSource::kMaxHeight, 360);
352 blink::WebMediaStreamTrack track = CreateTrack(
353 "123", factory.CreateWebMediaConstraints());
354 mock_source()->CompleteGetSupportedFormats();
260 EXPECT_EQ(1, NumberOfFailedConstraintsCallbacks()); 355 EXPECT_EQ(1, NumberOfFailedConstraintsCallbacks());
261 } 356 }
262 357
263 // Test that its safe to release the last reference of a blink track and the 358 // Test that its safe to release the last reference of a blink track and the
264 // source during the callback if adding a track succeeds. 359 // source during the callback if adding a track succeeds.
265 TEST_F(MediaStreamVideoSourceTest, ReleaseTrackAndSourceOnSuccessCallBack) { 360 TEST_F(MediaStreamVideoSourceTest, ReleaseTrackAndSourceOnSuccessCallBack) {
266 MockMediaConstraintFactory factory; 361 MockMediaConstraintFactory factory;
267 { 362 {
268 blink::WebMediaStreamTrack track = 363 blink::WebMediaStreamTrack track =
269 CreateTrack("123", factory.CreateWebMediaConstraints()); 364 CreateTrack("123", factory.CreateWebMediaConstraints());
270 ReleaseTrackAndSourceOnAddTrackCallback(track); 365 ReleaseTrackAndSourceOnAddTrackCallback(track);
271 } 366 }
272 mock_source()->CompleteGetSupportedFormats(); 367 mock_source()->CompleteGetSupportedFormats();
273 mock_source()->StartMockedSource(); 368 mock_source()->StartMockedSource();
274 EXPECT_EQ(1, NumberOfSuccessConstraintsCallbacks()); 369 EXPECT_EQ(1, NumberOfSuccessConstraintsCallbacks());
275 } 370 }
276 371
277 // Test that its safe to release the last reference of a blink track and the 372 // Test that its safe to release the last reference of a blink track and the
278 // source during the callback if adding a track fails. 373 // source during the callback if adding a track fails.
279 TEST_F(MediaStreamVideoSourceTest, ReleaseTrackAndSourceOnFailureCallBack) { 374 TEST_F(MediaStreamVideoSourceTest, ReleaseTrackAndSourceOnFailureCallBack) {
280 MockMediaConstraintFactory factory; 375 MockMediaConstraintFactory factory;
281 factory.AddMandatory(MediaStreamVideoSource::kMinAspectRatio, 2); 376 factory.AddMandatory(MediaStreamVideoSource::kMinWidth, 99999);
282 { 377 {
283 blink::WebMediaStreamTrack track = 378 blink::WebMediaStreamTrack track =
284 CreateTrack("123", factory.CreateWebMediaConstraints()); 379 CreateTrack("123", factory.CreateWebMediaConstraints());
285 ReleaseTrackAndSourceOnAddTrackCallback(track); 380 ReleaseTrackAndSourceOnAddTrackCallback(track);
286 } 381 }
287 mock_source()->CompleteGetSupportedFormats(); 382 mock_source()->CompleteGetSupportedFormats();
288 EXPECT_EQ(1, NumberOfFailedConstraintsCallbacks()); 383 EXPECT_EQ(1, NumberOfFailedConstraintsCallbacks());
289 } 384 }
290 385
291 // Test that the source ignores an optional aspect ratio that is higher than 386 // Test that the source ignores an optional aspect ratio that is higher than
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
398 493
399 // Test that the source crops to the requested max width and 494 // Test that the source crops to the requested max width and
400 // height even though the requested frame has odd size. 495 // height even though the requested frame has odd size.
401 TEST_F(MediaStreamVideoSourceTest, DeliverCroppedVideoFrame637359) { 496 TEST_F(MediaStreamVideoSourceTest, DeliverCroppedVideoFrame637359) {
402 MockMediaConstraintFactory factory; 497 MockMediaConstraintFactory factory;
403 factory.AddOptional(MediaStreamVideoSource::kMaxWidth, 637); 498 factory.AddOptional(MediaStreamVideoSource::kMaxWidth, 637);
404 factory.AddOptional(MediaStreamVideoSource::kMaxHeight, 359); 499 factory.AddOptional(MediaStreamVideoSource::kMaxHeight, 359);
405 TestSourceCropFrame(640, 480, factory.CreateWebMediaConstraints(), 637, 359); 500 TestSourceCropFrame(640, 480, factory.CreateWebMediaConstraints(), 637, 359);
406 } 501 }
407 502
503 TEST_F(MediaStreamVideoSourceTest, DeliverCroppedVideoFrame320320) {
504 MockMediaConstraintFactory factory;
505 factory.AddMandatory(MediaStreamVideoSource::kMaxWidth, 320);
506 factory.AddMandatory(MediaStreamVideoSource::kMaxHeight, 320);
507 factory.AddMandatory(MediaStreamVideoSource::kMinHeight, 320);
508 factory.AddMandatory(MediaStreamVideoSource::kMinWidth, 320);
509 TestSourceCropFrame(640, 480, factory.CreateWebMediaConstraints(), 320, 320);
510 }
511
408 TEST_F(MediaStreamVideoSourceTest, DeliverSmallerSizeWhenTooLargeMax) { 512 TEST_F(MediaStreamVideoSourceTest, DeliverSmallerSizeWhenTooLargeMax) {
409 MockMediaConstraintFactory factory; 513 MockMediaConstraintFactory factory;
410 factory.AddOptional(MediaStreamVideoSource::kMaxWidth, 1920); 514 factory.AddOptional(MediaStreamVideoSource::kMaxWidth, 1920);
411 factory.AddOptional(MediaStreamVideoSource::kMaxHeight, 1080); 515 factory.AddOptional(MediaStreamVideoSource::kMaxHeight, 1080);
412 factory.AddOptional(MediaStreamVideoSource::kMinWidth, 1280); 516 factory.AddOptional(MediaStreamVideoSource::kMinWidth, 1280);
413 factory.AddOptional(MediaStreamVideoSource::kMinHeight, 720); 517 factory.AddOptional(MediaStreamVideoSource::kMinHeight, 720);
414 TestSourceCropFrame(1280, 720, factory.CreateWebMediaConstraints(), 518 TestSourceCropFrame(1280, 720, factory.CreateWebMediaConstraints(),
415 1280, 720); 519 1280, 720);
416 } 520 }
417 521
522 TEST_F(MediaStreamVideoSourceTest, TwoTracksWithVGAAndWVGA) {
523 MockMediaConstraintFactory factory1;
524 factory1.AddOptional(MediaStreamVideoSource::kMaxWidth, 640);
525 factory1.AddOptional(MediaStreamVideoSource::kMaxHeight, 480);
526
527 MockMediaConstraintFactory factory2;
528 factory2.AddOptional(MediaStreamVideoSource::kMaxHeight, 360);
529
530 TestTwoTracksWithDifferentConstraints(factory1.CreateWebMediaConstraints(),
531 factory2.CreateWebMediaConstraints(),
532 640, 480,
533 640, 480,
534 640, 360);
535 }
536
537 TEST_F(MediaStreamVideoSourceTest, TwoTracksWith720AndWVGA) {
538 MockMediaConstraintFactory factory1;
539 factory1.AddOptional(MediaStreamVideoSource::kMinWidth, 1280);
540 factory1.AddOptional(MediaStreamVideoSource::kMinHeight, 720);
541
542
543 MockMediaConstraintFactory factory2;
544 factory2.AddMandatory(MediaStreamVideoSource::kMaxWidth, 640);
545 factory2.AddMandatory(MediaStreamVideoSource::kMaxHeight, 360);
546
547 TestTwoTracksWithDifferentConstraints(factory1.CreateWebMediaConstraints(),
548 factory2.CreateWebMediaConstraints(),
549 1280, 720,
550 1280, 720,
551 640, 360);
552 }
553
554 TEST_F(MediaStreamVideoSourceTest, TwoTracksWith720AndW700H700) {
555 MockMediaConstraintFactory factory1;
556 factory1.AddOptional(MediaStreamVideoSource::kMinWidth, 1280);
557 factory1.AddOptional(MediaStreamVideoSource::kMinHeight, 720);
558
559 MockMediaConstraintFactory factory2;
560 factory2.AddMandatory(MediaStreamVideoSource::kMaxWidth, 700);
561 factory2.AddMandatory(MediaStreamVideoSource::kMaxHeight, 700);
562
563 TestTwoTracksWithDifferentConstraints(factory1.CreateWebMediaConstraints(),
564 factory2.CreateWebMediaConstraints(),
565 1280, 720,
566 1280, 720,
567 700, 700);
568 }
569
570 TEST_F(MediaStreamVideoSourceTest, TwoTracksWith720AndMaxAspectRatio4To3) {
571 MockMediaConstraintFactory factory1;
572 factory1.AddOptional(MediaStreamVideoSource::kMinWidth, 1280);
573 factory1.AddOptional(MediaStreamVideoSource::kMinHeight, 720);
574
575 MockMediaConstraintFactory factory2;
576 factory2.AddMandatory(MediaStreamVideoSource::kMaxAspectRatio, 640.0 / 480);
577
578 TestTwoTracksWithDifferentConstraints(factory1.CreateWebMediaConstraints(),
579 factory2.CreateWebMediaConstraints(),
580 1280, 720,
581 1280, 720,
582 960, 720);
583 }
584
585 TEST_F(MediaStreamVideoSourceTest, TwoTracksWithVgaAndMinAspectRatio) {
586 MockMediaConstraintFactory factory1;
587 factory1.AddOptional(MediaStreamVideoSource::kMaxWidth, 640);
588 factory1.AddOptional(MediaStreamVideoSource::kMaxHeight, 480);
589
590 MockMediaConstraintFactory factory2;
591 factory2.AddMandatory(MediaStreamVideoSource::kMinAspectRatio, 640.0 / 360);
592
593 TestTwoTracksWithDifferentConstraints(factory1.CreateWebMediaConstraints(),
594 factory2.CreateWebMediaConstraints(),
595 640, 480,
596 640, 480,
597 640, 360);
598 }
599
418 // Test that a source can change the frame resolution on the fly and that 600 // Test that a source can change the frame resolution on the fly and that
419 // tracks sinks get the new frame size unless constraints force the frame to be 601 // tracks sinks get the new frame size unless constraints force the frame to be
420 // cropped. 602 // cropped.
421 TEST_F(MediaStreamVideoSourceTest, SourceChangeFrameSize) { 603 TEST_F(MediaStreamVideoSourceTest, SourceChangeFrameSize) {
422 MockMediaConstraintFactory factory; 604 MockMediaConstraintFactory factory;
423 factory.AddOptional(MediaStreamVideoSource::kMaxWidth, 800); 605 factory.AddOptional(MediaStreamVideoSource::kMaxWidth, 800);
424 factory.AddOptional(MediaStreamVideoSource::kMaxHeight, 700); 606 factory.AddOptional(MediaStreamVideoSource::kMaxHeight, 700);
425 607
426 // Expect the source to start capture with the supported resolution. 608 // Expect the source to start capture with the supported resolution.
427 blink::WebMediaStreamTrack track = 609 blink::WebMediaStreamTrack track =
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
471 EXPECT_TRUE(MediaStreamVideoSource::IsConstraintSupported( 653 EXPECT_TRUE(MediaStreamVideoSource::IsConstraintSupported(
472 MediaStreamVideoSource::kMaxAspectRatio)); 654 MediaStreamVideoSource::kMaxAspectRatio));
473 EXPECT_TRUE(MediaStreamVideoSource::IsConstraintSupported( 655 EXPECT_TRUE(MediaStreamVideoSource::IsConstraintSupported(
474 MediaStreamVideoSource::kMinAspectRatio)); 656 MediaStreamVideoSource::kMinAspectRatio));
475 657
476 EXPECT_FALSE(MediaStreamVideoSource::IsConstraintSupported( 658 EXPECT_FALSE(MediaStreamVideoSource::IsConstraintSupported(
477 "something unsupported")); 659 "something unsupported"));
478 } 660 }
479 661
480 } // namespace content 662 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698