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 | 10 |
11 package org.webrtc; | 11 package org.webrtc; |
12 | 12 |
13 import static org.junit.Assert.assertEquals; | 13 import static org.junit.Assert.assertEquals; |
14 import static org.junit.Assert.assertFalse; | 14 import static org.junit.Assert.assertFalse; |
15 import static org.junit.Assert.assertTrue; | 15 import static org.junit.Assert.assertTrue; |
16 import static org.junit.Assert.fail; | 16 import static org.junit.Assert.fail; |
17 | 17 |
| 18 import android.annotation.TargetApi; |
18 import android.content.Context; | 19 import android.content.Context; |
| 20 import android.media.CamcorderProfile; |
| 21 import android.media.MediaRecorder; |
| 22 import android.os.Environment; |
| 23 import java.io.File; |
| 24 import java.io.IOException; |
19 import java.util.ArrayList; | 25 import java.util.ArrayList; |
20 import java.util.List; | 26 import java.util.List; |
21 import java.util.concurrent.CountDownLatch; | 27 import java.util.concurrent.CountDownLatch; |
22 import org.chromium.base.test.BaseJUnit4ClassRunner; | 28 import org.chromium.base.test.BaseJUnit4ClassRunner; |
23 import org.junit.runner.RunWith; | 29 import org.junit.runner.RunWith; |
24 import org.webrtc.CameraEnumerationAndroid.CaptureFormat; | 30 import org.webrtc.CameraEnumerationAndroid.CaptureFormat; |
25 import org.webrtc.VideoRenderer.I420Frame; | 31 import org.webrtc.VideoRenderer.I420Frame; |
26 | 32 |
27 class CameraVideoCapturerTestFixtures { | 33 class CameraVideoCapturerTestFixtures { |
28 static final String TAG = "CameraVideoCapturerTestFixtures"; | 34 static final String TAG = "CameraVideoCapturerTestFixtures"; |
(...skipping 466 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
495 barrier.await(); | 501 barrier.await(); |
496 | 502 |
497 // Check result. | 503 // Check result. |
498 assertTrue(cameraSwitchSuccessful[0]); | 504 assertTrue(cameraSwitchSuccessful[0]); |
499 // Ensure that frames are received. | 505 // Ensure that frames are received. |
500 assertTrue(videoTrackWithRenderer.rendererCallbacks.waitForNextFrameToRender
() > 0); | 506 assertTrue(videoTrackWithRenderer.rendererCallbacks.waitForNextFrameToRender
() > 0); |
501 disposeCapturer(capturerInstance); | 507 disposeCapturer(capturerInstance); |
502 disposeVideoTrackWithRenderer(videoTrackWithRenderer); | 508 disposeVideoTrackWithRenderer(videoTrackWithRenderer); |
503 } | 509 } |
504 | 510 |
| 511 @TargetApi(21) |
| 512 public void updateMediaRecorder(boolean useSurfaceCapture) |
| 513 throws InterruptedException, IOException { |
| 514 final CapturerInstance capturerInstance = createCapturer(false /* initialize
*/); |
| 515 final VideoTrackWithRenderer videoTrackWithRenderer = |
| 516 createVideoTrackWithRenderer(capturerInstance.capturer); |
| 517 // Wait for the camera to start so we can add and remove MediaRecorder. |
| 518 assertTrue(videoTrackWithRenderer.rendererCallbacks.waitForNextFrameToRender
() > 0); |
| 519 |
| 520 final String videoOutPath = Environment.getExternalStorageDirectory().getPat
h() |
| 521 + "/chromium_tests_root/testmediarecorder.mp4"; |
| 522 File outputFile = new File(videoOutPath); |
| 523 |
| 524 // Create MediaRecorder object |
| 525 MediaRecorder mediaRecorder = new MediaRecorder(); |
| 526 mediaRecorder.setVideoSource( |
| 527 useSurfaceCapture ? MediaRecorder.VideoSource.SURFACE : MediaRecorder.Vi
deoSource.CAMERA); |
| 528 CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_480
P); |
| 529 profile.videoCodec = MediaRecorder.VideoEncoder.H264; |
| 530 profile.videoBitRate = 2500000; |
| 531 profile.videoFrameWidth = 640; |
| 532 profile.videoFrameHeight = 480; |
| 533 mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); |
| 534 mediaRecorder.setVideoFrameRate(profile.videoFrameRate); |
| 535 mediaRecorder.setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight
); |
| 536 mediaRecorder.setVideoEncodingBitRate(profile.videoBitRate); |
| 537 mediaRecorder.setVideoEncoder(profile.videoCodec); |
| 538 mediaRecorder.setOutputFile(outputFile.getPath()); |
| 539 mediaRecorder.prepare(); |
| 540 |
| 541 // Add MediaRecorder to camera pipeline. |
| 542 final boolean[] addMediaRecorderSuccessful = new boolean[1]; |
| 543 final CountDownLatch addBarrier = new CountDownLatch(1); |
| 544 CameraVideoCapturer.MediaRecorderHandler addMediaRecorderHandler = |
| 545 new CameraVideoCapturer.MediaRecorderHandler() { |
| 546 @Override |
| 547 public void onMediaRecorderSuccess() { |
| 548 addMediaRecorderSuccessful[0] = true; |
| 549 addBarrier.countDown(); |
| 550 } |
| 551 @Override |
| 552 public void onMediaRecorderError(String errorDescription) { |
| 553 addMediaRecorderSuccessful[0] = false; |
| 554 addBarrier.countDown(); |
| 555 } |
| 556 }; |
| 557 capturerInstance.capturer.addMediaRecorderToCamera(mediaRecorder, addMediaRe
corderHandler); |
| 558 // Wait until MediaRecoder has been added. |
| 559 addBarrier.await(); |
| 560 // Check result. |
| 561 assertTrue(addMediaRecorderSuccessful[0]); |
| 562 |
| 563 // Start MediaRecorder and wait for a few frames to capture. |
| 564 mediaRecorder.start(); |
| 565 for (int i = 0; i < 5; i++) { |
| 566 assertTrue(videoTrackWithRenderer.rendererCallbacks.waitForNextFrameToRend
er() > 0); |
| 567 } |
| 568 mediaRecorder.stop(); |
| 569 |
| 570 // Remove MediaRecorder from camera pipeline. |
| 571 final boolean[] removeMediaRecorderSuccessful = new boolean[1]; |
| 572 final CountDownLatch removeBarrier = new CountDownLatch(1); |
| 573 CameraVideoCapturer.MediaRecorderHandler removeMediaRecorderHandler = |
| 574 new CameraVideoCapturer.MediaRecorderHandler() { |
| 575 @Override |
| 576 public void onMediaRecorderSuccess() { |
| 577 removeMediaRecorderSuccessful[0] = true; |
| 578 removeBarrier.countDown(); |
| 579 } |
| 580 @Override |
| 581 public void onMediaRecorderError(String errorDescription) { |
| 582 removeMediaRecorderSuccessful[0] = false; |
| 583 removeBarrier.countDown(); |
| 584 } |
| 585 }; |
| 586 capturerInstance.capturer.removeMediaRecorderFromCamera(removeMediaRecorderH
andler); |
| 587 // Wait until MediaRecoder has been removed. |
| 588 removeBarrier.await(); |
| 589 // Check result. |
| 590 assertTrue(removeMediaRecorderSuccessful[0]); |
| 591 // Ensure that frames are received after removing MediaRecorder. |
| 592 assertTrue(videoTrackWithRenderer.rendererCallbacks.waitForNextFrameToRender
() > 0); |
| 593 // Check that recorded file contains some data. |
| 594 assertTrue(outputFile.length() > 0); |
| 595 |
| 596 disposeCapturer(capturerInstance); |
| 597 disposeVideoTrackWithRenderer(videoTrackWithRenderer); |
| 598 } |
| 599 |
505 public void cameraEventsInvoked() throws InterruptedException { | 600 public void cameraEventsInvoked() throws InterruptedException { |
506 final CapturerInstance capturerInstance = createCapturer(true /* initialize
*/); | 601 final CapturerInstance capturerInstance = createCapturer(true /* initialize
*/); |
507 startCapture(capturerInstance); | 602 startCapture(capturerInstance); |
508 // Make sure camera is started and first frame is received and then stop it. | 603 // Make sure camera is started and first frame is received and then stop it. |
509 assertTrue(capturerInstance.observer.waitForCapturerToStart()); | 604 assertTrue(capturerInstance.observer.waitForCapturerToStart()); |
510 capturerInstance.observer.waitForNextCapturedFrame(); | 605 capturerInstance.observer.waitForNextCapturedFrame(); |
511 disposeCapturer(capturerInstance); | 606 disposeCapturer(capturerInstance); |
512 | 607 |
513 assertTrue(capturerInstance.cameraEvents.onCameraOpeningCalled); | 608 assertTrue(capturerInstance.cameraEvents.onCameraOpeningCalled); |
514 assertTrue(capturerInstance.cameraEvents.onFirstFrameAvailableCalled); | 609 assertTrue(capturerInstance.cameraEvents.onFirstFrameAvailableCalled); |
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
727 final CapturerInstance capturerInstance = createCapturer(cameraName, true /*
initialize */); | 822 final CapturerInstance capturerInstance = createCapturer(cameraName, true /*
initialize */); |
728 | 823 |
729 final Object competingCamera = testObjectFactory.rawOpenCamera(cameraName); | 824 final Object competingCamera = testObjectFactory.rawOpenCamera(cameraName); |
730 | 825 |
731 startCapture(capturerInstance); | 826 startCapture(capturerInstance); |
732 disposeCapturer(capturerInstance); | 827 disposeCapturer(capturerInstance); |
733 | 828 |
734 testObjectFactory.rawCloseCamera(competingCamera); | 829 testObjectFactory.rawCloseCamera(competingCamera); |
735 } | 830 } |
736 } | 831 } |
OLD | NEW |