Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2015 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 package org.webrtc; | 10 package org.webrtc; |
| 11 | 11 |
| 12 import static junit.framework.Assert.*; | 12 import static junit.framework.Assert.*; |
| 13 | 13 |
| 14 import org.webrtc.CameraEnumerationAndroid.CaptureFormat; | 14 import org.webrtc.CameraEnumerationAndroid.CaptureFormat; |
| 15 import org.webrtc.VideoRenderer.I420Frame; | 15 import org.webrtc.VideoRenderer.I420Frame; |
| 16 | 16 |
| 17 import android.content.Context; | 17 import android.content.Context; |
| 18 | 18 |
| 19 import java.util.ArrayList; | 19 import java.util.ArrayList; |
| 20 import java.util.List; | 20 import java.util.List; |
| 21 import java.util.concurrent.CountDownLatch; | 21 import java.util.concurrent.CountDownLatch; |
| 22 | 22 |
| 23 class CameraVideoCapturerTestFixtures { | 23 class CameraVideoCapturerTestFixtures { |
| 24 static final String TAG = "CameraVideoCapturerTestFixtures"; | 24 static final String TAG = "CameraVideoCapturerTestFixtures"; |
| 25 // Default values used for starting capturing | |
| 26 static final int DEFAULT_WIDTH = 640; | |
| 27 static final int DEFAULT_HEIGHT = 480; | |
| 28 static final int DEFAULT_FPS = 15; | |
| 25 | 29 |
| 26 static private class RendererCallbacks implements VideoRenderer.Callbacks { | 30 static private class RendererCallbacks implements VideoRenderer.Callbacks { |
| 27 private int framesRendered = 0; | 31 private int framesRendered = 0; |
| 28 private Object frameLock = 0; | 32 private Object frameLock = 0; |
| 29 private int width = 0; | 33 private int width = 0; |
| 30 private int height = 0; | 34 private int height = 0; |
| 31 | 35 |
| 32 @Override | 36 @Override |
| 33 public void renderFrame(I420Frame frame) { | 37 public void renderFrame(I420Frame frame) { |
| 34 synchronized (frameLock) { | 38 synchronized (frameLock) { |
| (...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 337 | 341 |
| 338 private void startCapture(CapturerInstance instance, int formatIndex) { | 342 private void startCapture(CapturerInstance instance, int formatIndex) { |
| 339 final CameraEnumerationAndroid.CaptureFormat format = | 343 final CameraEnumerationAndroid.CaptureFormat format = |
| 340 instance.supportedFormats.get(formatIndex); | 344 instance.supportedFormats.get(formatIndex); |
| 341 | 345 |
| 342 instance.capturer.startCapture(format.width, format.height, format.framerate .max); | 346 instance.capturer.startCapture(format.width, format.height, format.framerate .max); |
| 343 instance.format = format; | 347 instance.format = format; |
| 344 } | 348 } |
| 345 | 349 |
| 346 private void disposeCapturer(CapturerInstance instance) { | 350 private void disposeCapturer(CapturerInstance instance) { |
| 351 try { | |
| 352 instance.capturer.stopCapture(); | |
| 353 } catch (InterruptedException e) { | |
| 354 // TODO(sakal): Remove this once stopCapture no longer throws InterruptedE xception | |
| 355 } | |
| 347 instance.capturer.dispose(); | 356 instance.capturer.dispose(); |
| 348 instance.surfaceTextureHelper.returnTextureFrame(); | 357 instance.surfaceTextureHelper.returnTextureFrame(); |
| 349 instance.surfaceTextureHelper.dispose(); | 358 instance.surfaceTextureHelper.dispose(); |
| 350 } | 359 } |
| 351 | 360 |
| 352 private VideoTrackWithRenderer createVideoTrackWithRenderer(CameraVideoCapture r capturer, | 361 private VideoTrackWithRenderer createVideoTrackWithRenderer(CameraVideoCapture r capturer, |
| 353 VideoRenderer.Callbacks rendererCallbacks) { | 362 VideoRenderer.Callbacks rendererCallbacks) { |
| 354 VideoTrackWithRenderer videoTrackWithRenderer = new VideoTrackWithRenderer() ; | 363 VideoTrackWithRenderer videoTrackWithRenderer = new VideoTrackWithRenderer() ; |
| 355 videoTrackWithRenderer.source = | 364 videoTrackWithRenderer.source = |
| 356 peerConnectionFactory.createVideoSource(capturer, new MediaConstraints() ); | 365 peerConnectionFactory.createVideoSource(capturer); |
|
magjed_webrtc
2016/07/26 09:15:13
This fits on the line above now.
sakal
2016/07/26 11:50:48
Done.
| |
| 366 capturer.startCapture(DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_FPS); | |
| 357 videoTrackWithRenderer.track = | 367 videoTrackWithRenderer.track = |
| 358 peerConnectionFactory.createVideoTrack("dummy", videoTrackWithRenderer.s ource); | 368 peerConnectionFactory.createVideoTrack("dummy", videoTrackWithRenderer.s ource); |
| 359 videoTrackWithRenderer.track.addRenderer(new VideoRenderer(rendererCallbacks )); | 369 videoTrackWithRenderer.track.addRenderer(new VideoRenderer(rendererCallbacks )); |
| 360 return videoTrackWithRenderer; | 370 return videoTrackWithRenderer; |
| 361 } | 371 } |
| 362 | 372 |
| 363 private VideoTrackWithRenderer createVideoTrackWithRenderer(CameraVideoCapture r capturer) { | 373 private VideoTrackWithRenderer createVideoTrackWithRenderer(CameraVideoCapture r capturer) { |
| 364 RendererCallbacks rendererCallbacks = new RendererCallbacks(); | 374 RendererCallbacks rendererCallbacks = new RendererCallbacks(); |
| 365 VideoTrackWithRenderer videoTrackWithRenderer = | 375 VideoTrackWithRenderer videoTrackWithRenderer = |
| 366 createVideoTrackWithRenderer(capturer, rendererCallbacks); | 376 createVideoTrackWithRenderer(capturer, rendererCallbacks); |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 395 private void createCapturerAndRender(String name) throws InterruptedException { | 405 private void createCapturerAndRender(String name) throws InterruptedException { |
| 396 if (name == null) { | 406 if (name == null) { |
| 397 Logging.w(TAG, "Skipping video capturer test because device name is null." ); | 407 Logging.w(TAG, "Skipping video capturer test because device name is null." ); |
| 398 return; | 408 return; |
| 399 } | 409 } |
| 400 | 410 |
| 401 final CapturerInstance capturerInstance = createCapturer(name, false /* init ialize */); | 411 final CapturerInstance capturerInstance = createCapturer(name, false /* init ialize */); |
| 402 final VideoTrackWithRenderer videoTrackWithRenderer = | 412 final VideoTrackWithRenderer videoTrackWithRenderer = |
| 403 createVideoTrackWithRenderer(capturerInstance.capturer); | 413 createVideoTrackWithRenderer(capturerInstance.capturer); |
| 404 assertTrue(videoTrackWithRenderer.rendererCallbacks.waitForNextFrameToRender () > 0); | 414 assertTrue(videoTrackWithRenderer.rendererCallbacks.waitForNextFrameToRender () > 0); |
| 415 disposeCapturer(capturerInstance); | |
| 405 disposeVideoTrackWithRenderer(videoTrackWithRenderer); | 416 disposeVideoTrackWithRenderer(videoTrackWithRenderer); |
| 406 disposeCapturer(capturerInstance); | |
| 407 } | 417 } |
| 408 | 418 |
| 409 // Test methods | 419 // Test methods |
| 410 public void createCapturerAndDispose() { | 420 public void createCapturerAndDispose() { |
| 411 disposeCapturer(createCapturer(true /* initialize */)); | 421 disposeCapturer(createCapturer(true /* initialize */)); |
| 412 } | 422 } |
| 413 | 423 |
| 414 public void createNonExistingCamera() { | 424 public void createNonExistingCamera() { |
| 415 try { | 425 try { |
| 416 disposeCapturer(createCapturer("non-existing camera", false /* initialize */)); | 426 disposeCapturer(createCapturer("non-existing camera", false /* initialize */)); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 459 barrier.countDown(); | 469 barrier.countDown(); |
| 460 } | 470 } |
| 461 }); | 471 }); |
| 462 // Wait until the camera has been switched. | 472 // Wait until the camera has been switched. |
| 463 barrier.await(); | 473 barrier.await(); |
| 464 | 474 |
| 465 // Check result. | 475 // Check result. |
| 466 assertTrue(cameraSwitchSuccessful[0]); | 476 assertTrue(cameraSwitchSuccessful[0]); |
| 467 // Ensure that frames are received. | 477 // Ensure that frames are received. |
| 468 assertTrue(videoTrackWithRenderer.rendererCallbacks.waitForNextFrameToRender () > 0); | 478 assertTrue(videoTrackWithRenderer.rendererCallbacks.waitForNextFrameToRender () > 0); |
| 479 disposeCapturer(capturerInstance); | |
| 469 disposeVideoTrackWithRenderer(videoTrackWithRenderer); | 480 disposeVideoTrackWithRenderer(videoTrackWithRenderer); |
| 470 disposeCapturer(capturerInstance); | |
| 471 } | 481 } |
| 472 | 482 |
| 473 public void cameraEventsInvoked() throws InterruptedException { | 483 public void cameraEventsInvoked() throws InterruptedException { |
| 474 final CapturerInstance capturerInstance = createCapturer(true /* initialize */); | 484 final CapturerInstance capturerInstance = createCapturer(true /* initialize */); |
| 475 startCapture(capturerInstance); | 485 startCapture(capturerInstance); |
| 476 // Make sure camera is started and first frame is received and then stop it. | 486 // Make sure camera is started and first frame is received and then stop it. |
| 477 assertTrue(capturerInstance.observer.waitForCapturerToStart()); | 487 assertTrue(capturerInstance.observer.waitForCapturerToStart()); |
| 478 capturerInstance.observer.waitForNextCapturedFrame(); | 488 capturerInstance.observer.waitForNextCapturedFrame(); |
| 479 capturerInstance.capturer.stopCapture(); | 489 capturerInstance.capturer.stopCapture(); |
| 480 disposeCapturer(capturerInstance); | 490 disposeCapturer(capturerInstance); |
| 481 | 491 |
| 482 assertTrue(capturerInstance.cameraEvents.onCameraOpeningCalled); | 492 assertTrue(capturerInstance.cameraEvents.onCameraOpeningCalled); |
| 483 assertTrue(capturerInstance.cameraEvents.onFirstFrameAvailableCalled); | 493 assertTrue(capturerInstance.cameraEvents.onFirstFrameAvailableCalled); |
| 484 } | 494 } |
| 485 | 495 |
| 486 public void cameraCallsAfterStop() throws InterruptedException { | 496 public void cameraCallsAfterStop() throws InterruptedException { |
| 487 final CapturerInstance capturerInstance = createCapturer(true /* initialize */); | 497 final CapturerInstance capturerInstance = createCapturer(true /* initialize */); |
| 488 startCapture(capturerInstance); | 498 startCapture(capturerInstance); |
| 489 // Make sure camera is started and then stop it. | 499 // Make sure camera is started and then stop it. |
| 490 assertTrue(capturerInstance.observer.waitForCapturerToStart()); | 500 assertTrue(capturerInstance.observer.waitForCapturerToStart()); |
| 491 capturerInstance.capturer.stopCapture(); | 501 capturerInstance.capturer.stopCapture(); |
| 492 capturerInstance.surfaceTextureHelper.returnTextureFrame(); | 502 capturerInstance.surfaceTextureHelper.returnTextureFrame(); |
| 493 | 503 |
| 494 // We can't change |capturer| at this point, but we should not crash. | 504 // We can't change |capturer| at this point, but we should not crash. |
| 495 capturerInstance.capturer.switchCamera(null /* switchEventsHandler */); | 505 capturerInstance.capturer.switchCamera(null /* switchEventsHandler */); |
| 496 capturerInstance.capturer.onOutputFormatRequest(640, 480, 15); | 506 capturerInstance.capturer.onOutputFormatRequest(DEFAULT_WIDTH, DEFAULT_HEIGH T, DEFAULT_FPS); |
| 497 capturerInstance.capturer.changeCaptureFormat(640, 480, 15); | 507 capturerInstance.capturer.changeCaptureFormat(DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_FPS); |
| 498 | 508 |
| 499 disposeCapturer(capturerInstance); | 509 disposeCapturer(capturerInstance); |
| 500 } | 510 } |
| 501 | 511 |
| 502 public void stopRestartVideoSource() throws InterruptedException { | 512 public void stopRestartVideoSource() throws InterruptedException { |
| 503 final CapturerInstance capturerInstance = createCapturer(false /* initialize */); | 513 final CapturerInstance capturerInstance = createCapturer(false /* initialize */); |
| 504 final VideoTrackWithRenderer videoTrackWithRenderer = | 514 final VideoTrackWithRenderer videoTrackWithRenderer = |
| 505 createVideoTrackWithRenderer(capturerInstance.capturer); | 515 createVideoTrackWithRenderer(capturerInstance.capturer); |
| 506 | 516 |
| 507 assertTrue(videoTrackWithRenderer.rendererCallbacks.waitForNextFrameToRender () > 0); | 517 assertTrue(videoTrackWithRenderer.rendererCallbacks.waitForNextFrameToRender () > 0); |
| 508 assertEquals(MediaSource.State.LIVE, videoTrackWithRenderer.source.state()); | 518 assertEquals(MediaSource.State.LIVE, videoTrackWithRenderer.source.state()); |
| 509 | 519 |
| 510 videoTrackWithRenderer.source.stop(); | 520 capturerInstance.capturer.stopCapture(); |
| 511 assertEquals(MediaSource.State.ENDED, videoTrackWithRenderer.source.state()) ; | 521 assertEquals(MediaSource.State.ENDED, videoTrackWithRenderer.source.state()) ; |
| 512 | 522 |
| 513 videoTrackWithRenderer.source.restart(); | 523 startCapture(capturerInstance); |
| 514 assertTrue(videoTrackWithRenderer.rendererCallbacks.waitForNextFrameToRender () > 0); | 524 assertTrue(videoTrackWithRenderer.rendererCallbacks.waitForNextFrameToRender () > 0); |
| 515 assertEquals(MediaSource.State.LIVE, videoTrackWithRenderer.source.state()); | 525 assertEquals(MediaSource.State.LIVE, videoTrackWithRenderer.source.state()); |
| 516 | 526 |
| 527 disposeCapturer(capturerInstance); | |
| 517 disposeVideoTrackWithRenderer(videoTrackWithRenderer); | 528 disposeVideoTrackWithRenderer(videoTrackWithRenderer); |
| 518 disposeCapturer(capturerInstance); | |
| 519 } | 529 } |
| 520 | 530 |
| 521 public void startStopWithDifferentResolutions() throws InterruptedException { | 531 public void startStopWithDifferentResolutions() throws InterruptedException { |
| 522 final CapturerInstance capturerInstance = createCapturer(true /* initialize */); | 532 final CapturerInstance capturerInstance = createCapturer(true /* initialize */); |
| 523 | 533 |
| 524 for(int i = 0; i < 3 ; ++i) { | 534 for(int i = 0; i < 3 ; ++i) { |
| 525 startCapture(capturerInstance, i); | 535 startCapture(capturerInstance, i); |
| 526 assertTrue(capturerInstance.observer.waitForCapturerToStart()); | 536 assertTrue(capturerInstance.observer.waitForCapturerToStart()); |
| 527 capturerInstance.observer.waitForNextCapturedFrame(); | 537 capturerInstance.observer.waitForNextCapturedFrame(); |
| 528 | 538 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 578 throws InterruptedException { | 588 throws InterruptedException { |
| 579 final CapturerInstance capturerInstance = createCapturer(false /* initialize */); | 589 final CapturerInstance capturerInstance = createCapturer(false /* initialize */); |
| 580 final VideoTrackWithRenderer videoTrackWithRenderer = | 590 final VideoTrackWithRenderer videoTrackWithRenderer = |
| 581 createVideoTrackWithFakeAsyncRenderer(capturerInstance.capturer); | 591 createVideoTrackWithFakeAsyncRenderer(capturerInstance.capturer); |
| 582 // Wait for at least one frame that has not been returned. | 592 // Wait for at least one frame that has not been returned. |
| 583 assertFalse(videoTrackWithRenderer.fakeAsyncRenderer.waitForPendingFrames(). isEmpty()); | 593 assertFalse(videoTrackWithRenderer.fakeAsyncRenderer.waitForPendingFrames(). isEmpty()); |
| 584 | 594 |
| 585 capturerInstance.capturer.stopCapture(); | 595 capturerInstance.capturer.stopCapture(); |
| 586 | 596 |
| 587 // Dispose everything. | 597 // Dispose everything. |
| 598 disposeCapturer(capturerInstance); | |
| 588 disposeVideoTrackWithRenderer(videoTrackWithRenderer); | 599 disposeVideoTrackWithRenderer(videoTrackWithRenderer); |
| 589 disposeCapturer(capturerInstance); | |
| 590 | 600 |
| 591 // Return the frame(s), on a different thread out of spite. | 601 // Return the frame(s), on a different thread out of spite. |
| 592 final List<I420Frame> pendingFrames = | 602 final List<I420Frame> pendingFrames = |
| 593 videoTrackWithRenderer.fakeAsyncRenderer.waitForPendingFrames(); | 603 videoTrackWithRenderer.fakeAsyncRenderer.waitForPendingFrames(); |
| 594 final Thread returnThread = new Thread(new Runnable() { | 604 final Thread returnThread = new Thread(new Runnable() { |
| 595 @Override | 605 @Override |
| 596 public void run() { | 606 public void run() { |
| 597 for (I420Frame frame : pendingFrames) { | 607 for (I420Frame frame : pendingFrames) { |
| 598 VideoRenderer.renderFrameDone(frame); | 608 VideoRenderer.renderFrameDone(frame); |
| 599 } | 609 } |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 636 int numberOfInspectedFrames = 0; | 646 int numberOfInspectedFrames = 0; |
| 637 | 647 |
| 638 do { | 648 do { |
| 639 videoTrackWithRenderer.rendererCallbacks.waitForNextFrameToRender(); | 649 videoTrackWithRenderer.rendererCallbacks.waitForNextFrameToRender(); |
| 640 ++numberOfInspectedFrames; | 650 ++numberOfInspectedFrames; |
| 641 | 651 |
| 642 gotExpectedResolution = (videoTrackWithRenderer.rendererCallbacks.frameWid th() == scaledWidth | 652 gotExpectedResolution = (videoTrackWithRenderer.rendererCallbacks.frameWid th() == scaledWidth |
| 643 && videoTrackWithRenderer.rendererCallbacks.frameHeight() == scaledHe ight); | 653 && videoTrackWithRenderer.rendererCallbacks.frameHeight() == scaledHe ight); |
| 644 } while (!gotExpectedResolution && numberOfInspectedFrames < 30); | 654 } while (!gotExpectedResolution && numberOfInspectedFrames < 30); |
| 645 | 655 |
| 656 disposeCapturer(capturerInstance); | |
| 646 disposeVideoTrackWithRenderer(videoTrackWithRenderer); | 657 disposeVideoTrackWithRenderer(videoTrackWithRenderer); |
| 647 disposeCapturer(capturerInstance); | |
| 648 | 658 |
| 649 assertTrue(gotExpectedResolution); | 659 assertTrue(gotExpectedResolution); |
| 650 } | 660 } |
| 651 | 661 |
| 652 public void startWhileCameraIsAlreadyOpen() throws InterruptedException { | 662 public void startWhileCameraIsAlreadyOpen() throws InterruptedException { |
| 653 final String cameraName = testObjectFactory.getNameOfBackFacingDevice(); | 663 final String cameraName = testObjectFactory.getNameOfBackFacingDevice(); |
| 654 // At this point camera is not actually opened. | 664 // At this point camera is not actually opened. |
| 655 final CapturerInstance capturerInstance = createCapturer(cameraName, true /* initialize */); | 665 final CapturerInstance capturerInstance = createCapturer(cameraName, true /* initialize */); |
| 656 | 666 |
| 657 final Object competingCamera = testObjectFactory.rawOpenCamera(cameraName); | 667 final Object competingCamera = testObjectFactory.rawOpenCamera(cameraName); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 702 final Object competingCamera = testObjectFactory.rawOpenCamera(cameraName); | 712 final Object competingCamera = testObjectFactory.rawOpenCamera(cameraName); |
| 703 | 713 |
| 704 startCapture(capturerInstance); | 714 startCapture(capturerInstance); |
| 705 | 715 |
| 706 capturerInstance.capturer.stopCapture(); | 716 capturerInstance.capturer.stopCapture(); |
| 707 disposeCapturer(capturerInstance); | 717 disposeCapturer(capturerInstance); |
| 708 | 718 |
| 709 testObjectFactory.rawCloseCamera(competingCamera); | 719 testObjectFactory.rawCloseCamera(competingCamera); |
| 710 } | 720 } |
| 711 } | 721 } |
| OLD | NEW |