OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. | |
3 * | |
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 | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 package org.appspot.apprtc.test; | |
12 | |
13 import static org.junit.Assert.assertEquals; | |
14 import static org.junit.Assert.assertTrue; | |
15 | |
16 import android.os.Environment; | |
17 import android.support.test.filters.LargeTest; | |
18 import android.support.test.filters.MediumTest; | |
19 import android.support.test.filters.SmallTest; | |
20 import java.io.IOException; | |
21 import java.lang.Thread; | |
22 import java.nio.charset.StandardCharsets; | |
23 import java.util.ArrayList; | |
24 import java.util.Arrays; | |
25 import org.chromium.base.test.BaseJUnit4ClassRunner; | |
26 import org.junit.Test; | |
27 import org.junit.runner.RunWith; | |
28 import org.webrtc.FileVideoCapturer; | |
29 import org.webrtc.VideoCapturer; | |
30 | |
31 @RunWith(BaseJUnit4ClassRunner.class) | |
32 public class FileVideoCapturerTest { | |
33 private static class Frame { | |
34 public byte[] data; | |
35 public int width; | |
36 public int height; | |
37 } | |
38 | |
39 public class MockCapturerObserver implements VideoCapturer.CapturerObserver { | |
40 private final ArrayList<Frame> frameDatas = new ArrayList<Frame>(); | |
41 | |
42 @Override | |
43 public void onCapturerStarted(boolean success) { | |
44 assertTrue(success); | |
45 } | |
46 | |
47 @Override | |
48 public void onCapturerStopped() { | |
49 // Empty on purpose. | |
50 } | |
51 | |
52 @Override | |
53 public synchronized void onByteBufferFrameCaptured( | |
54 byte[] data, int width, int height, int rotation, long timeStamp) { | |
55 Frame frame = new Frame(); | |
56 frame.data = data; | |
57 frame.width = width; | |
58 frame.height = height; | |
59 assertTrue(data.length != 0); | |
60 frameDatas.add(frame); | |
61 notify(); | |
62 } | |
63 | |
64 @Override | |
65 public void onTextureFrameCaptured(int width, int height, int oesTextureId, | |
66 float[] transformMatrix, int rotation, long timestamp) { | |
67 // Empty on purpose. | |
68 } | |
69 | |
70 public synchronized ArrayList<Frame> getMinimumFramesBlocking(int minFrames) | |
71 throws InterruptedException { | |
72 while (frameDatas.size() < minFrames) { | |
73 wait(); | |
74 } | |
75 return new ArrayList<Frame>(frameDatas); | |
76 } | |
77 } | |
78 | |
79 @Test | |
80 @SmallTest | |
81 public void testVideoCaptureFromFile() throws InterruptedException, IOExceptio
n { | |
82 final int FRAME_WIDTH = 4; | |
83 final int FRAME_HEIGHT = 4; | |
84 final FileVideoCapturer fileVideoCapturer = | |
85 new FileVideoCapturer(Environment.getExternalStorageDirectory().getPath(
) | |
86 + "/chromium_tests_root/webrtc/examples/androidtests/src/org/appspot
/apprtc/test/" | |
87 + "capturetestvideo.y4m"); | |
88 final MockCapturerObserver capturerObserver = new MockCapturerObserver(); | |
89 fileVideoCapturer.initialize(null, null, capturerObserver); | |
90 fileVideoCapturer.startCapture(FRAME_WIDTH, FRAME_HEIGHT, 33); | |
91 | |
92 final String[] expectedFrames = { | |
93 "THIS IS JUST SOME TEXT x", "THE SECOND FRAME qwerty.", "HERE IS THE THR
ID FRAME!"}; | |
94 | |
95 final ArrayList<Frame> frameDatas; | |
96 frameDatas = capturerObserver.getMinimumFramesBlocking(expectedFrames.length
); | |
97 | |
98 assertEquals(expectedFrames.length, frameDatas.size()); | |
99 | |
100 fileVideoCapturer.stopCapture(); | |
101 fileVideoCapturer.dispose(); | |
102 | |
103 for (int i = 0; i < expectedFrames.length; ++i) { | |
104 Frame frame = frameDatas.get(i); | |
105 | |
106 assertEquals(FRAME_WIDTH, frame.width); | |
107 assertEquals(FRAME_HEIGHT, frame.height); | |
108 assertEquals(FRAME_WIDTH * FRAME_HEIGHT * 3 / 2, frame.data.length); | |
109 | |
110 byte[] expectedNV12Bytes = new byte[frame.data.length]; | |
111 FileVideoCapturer.nativeI420ToNV21(expectedFrames[i].getBytes(StandardChar
sets.US_ASCII), | |
112 FRAME_WIDTH, FRAME_HEIGHT, expectedNV12Bytes); | |
113 | |
114 assertTrue(Arrays.equals(expectedNV12Bytes, frame.data)); | |
115 } | |
116 } | |
117 } | |
OLD | NEW |