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 android.graphics.Point; | 13 import android.graphics.Point; |
14 import android.test.ActivityTestCase; | 14 import android.test.ActivityTestCase; |
15 import android.test.suitebuilder.annotation.MediumTest; | 15 import android.test.suitebuilder.annotation.MediumTest; |
16 import android.view.View.MeasureSpec; | 16 import android.view.View.MeasureSpec; |
17 | 17 |
18 import java.nio.ByteBuffer; | 18 import java.nio.ByteBuffer; |
19 import java.util.Arrays; | 19 import java.util.Arrays; |
20 import java.util.List; | 20 import java.util.List; |
21 | 21 |
22 public final class SurfaceViewRendererOnMeasureTest extends ActivityTestCase { | 22 public final class SurfaceViewRendererOnMeasureTest extends ActivityTestCase { |
23 /** | 23 /** |
24 * List with all possible scaling types. | 24 * List with all possible scaling types. |
25 */ | 25 */ |
26 private static final List<RendererCommon.ScalingType> scalingTypes = Arrays.as
List( | 26 private static final List<RendererCommon.ScalingType> scalingTypes = Arrays.as
List( |
27 RendererCommon.ScalingType.SCALE_ASPECT_FIT, | 27 RendererCommon.ScalingType.SCALE_ASPECT_FIT, RendererCommon.ScalingType.SC
ALE_ASPECT_FILL, |
28 RendererCommon.ScalingType.SCALE_ASPECT_FILL, | |
29 RendererCommon.ScalingType.SCALE_ASPECT_BALANCED); | 28 RendererCommon.ScalingType.SCALE_ASPECT_BALANCED); |
30 | 29 |
31 /** | 30 /** |
32 * List with MeasureSpec modes. | 31 * List with MeasureSpec modes. |
33 */ | 32 */ |
34 private static final List<Integer> measureSpecModes = | 33 private static final List<Integer> measureSpecModes = |
35 Arrays.asList(MeasureSpec.EXACTLY, MeasureSpec.AT_MOST); | 34 Arrays.asList(MeasureSpec.EXACTLY, MeasureSpec.AT_MOST); |
36 | 35 |
37 /** | 36 /** |
38 * Returns a dummy YUV frame. | 37 * Returns a dummy YUV frame. |
39 */ | 38 */ |
40 static VideoRenderer.I420Frame createFrame(int width, int height, int rotation
Degree) { | 39 static VideoRenderer.I420Frame createFrame(int width, int height, int rotation
Degree) { |
41 final int[] yuvStrides = new int[] {width, (width + 1) / 2, (width + 1) / 2}
; | 40 final int[] yuvStrides = new int[] {width, (width + 1) / 2, (width + 1) / 2}
; |
42 final int[] yuvHeights = new int[] {height, (height + 1) / 2, (height + 1) /
2}; | 41 final int[] yuvHeights = new int[] {height, (height + 1) / 2, (height + 1) /
2}; |
43 final ByteBuffer[] yuvPlanes = new ByteBuffer[3]; | 42 final ByteBuffer[] yuvPlanes = new ByteBuffer[3]; |
44 for (int i = 0; i < 3; ++i) { | 43 for (int i = 0; i < 3; ++i) { |
45 yuvPlanes[i] = ByteBuffer.allocateDirect(yuvStrides[i] * yuvHeights[i]); | 44 yuvPlanes[i] = ByteBuffer.allocateDirect(yuvStrides[i] * yuvHeights[i]); |
46 } | 45 } |
47 return new VideoRenderer.I420Frame(width, height, rotationDegree, yuvStrides
, yuvPlanes, 0); | 46 return new VideoRenderer.I420Frame(width, height, rotationDegree, yuvStrides
, yuvPlanes, 0); |
48 } | 47 } |
49 | 48 |
50 /** | 49 /** |
51 * Assert onMeasure() with given parameters will result in expected measured s
ize. | 50 * Assert onMeasure() with given parameters will result in expected measured s
ize. |
52 */ | 51 */ |
53 private static void assertMeasuredSize( | 52 private static void assertMeasuredSize(SurfaceViewRenderer surfaceViewRenderer
, |
54 SurfaceViewRenderer surfaceViewRenderer, RendererCommon.ScalingType scalin
gType, | 53 RendererCommon.ScalingType scalingType, String frameDimensions, int expect
edWidth, |
55 String frameDimensions, | 54 int expectedHeight, int widthSpec, int heightSpec) { |
56 int expectedWidth, int expectedHeight, | |
57 int widthSpec, int heightSpec) { | |
58 surfaceViewRenderer.setScalingType(scalingType); | 55 surfaceViewRenderer.setScalingType(scalingType); |
59 surfaceViewRenderer.onMeasure(widthSpec, heightSpec); | 56 surfaceViewRenderer.onMeasure(widthSpec, heightSpec); |
60 final int measuredWidth = surfaceViewRenderer.getMeasuredWidth(); | 57 final int measuredWidth = surfaceViewRenderer.getMeasuredWidth(); |
61 final int measuredHeight = surfaceViewRenderer.getMeasuredHeight(); | 58 final int measuredHeight = surfaceViewRenderer.getMeasuredHeight(); |
62 if (measuredWidth != expectedWidth || measuredHeight != expectedHeight) { | 59 if (measuredWidth != expectedWidth || measuredHeight != expectedHeight) { |
63 fail("onMeasure(" | 60 fail("onMeasure(" + MeasureSpec.toString(widthSpec) + ", " + MeasureSpec.t
oString(heightSpec) |
64 + MeasureSpec.toString(widthSpec) + ", " + MeasureSpec.toString(height
Spec) + ")" | 61 + ")" |
65 + " with scaling type " + scalingType | 62 + " with scaling type " + scalingType + " and frame: " + frameDimensio
ns |
66 + " and frame: " + frameDimensions | 63 + " expected measured size " + expectedWidth + "x" + expectedHeight +
", but was " |
67 + " expected measured size " + expectedWidth + "x" + expectedHeight | 64 + measuredWidth + "x" + measuredHeight); |
68 + ", but was " + measuredWidth + "x" + measuredHeight); | |
69 } | 65 } |
70 } | 66 } |
71 | 67 |
72 /** | 68 /** |
73 * Test how SurfaceViewRenderer.onMeasure() behaves when no frame has been del
ivered. | 69 * Test how SurfaceViewRenderer.onMeasure() behaves when no frame has been del
ivered. |
74 */ | 70 */ |
75 @MediumTest | 71 @MediumTest |
76 public void testNoFrame() { | 72 public void testNoFrame() { |
77 final SurfaceViewRenderer surfaceViewRenderer = | 73 final SurfaceViewRenderer surfaceViewRenderer = |
78 new SurfaceViewRenderer(getInstrumentation().getContext()); | 74 new SurfaceViewRenderer(getInstrumentation().getContext()); |
79 final String frameDimensions = "null"; | 75 final String frameDimensions = "null"; |
80 | 76 |
81 // Test behaviour before SurfaceViewRenderer.init() is called. | 77 // Test behaviour before SurfaceViewRenderer.init() is called. |
82 for (RendererCommon.ScalingType scalingType : scalingTypes) { | 78 for (RendererCommon.ScalingType scalingType : scalingTypes) { |
83 for (int measureSpecMode : measureSpecModes) { | 79 for (int measureSpecMode : measureSpecModes) { |
84 final int zeroMeasureSize = MeasureSpec.makeMeasureSpec(0, measureSpecMo
de); | 80 final int zeroMeasureSize = MeasureSpec.makeMeasureSpec(0, measureSpecMo
de); |
85 assertMeasuredSize(surfaceViewRenderer, scalingType, frameDimensions, | 81 assertMeasuredSize(surfaceViewRenderer, scalingType, frameDimensions, 0,
0, zeroMeasureSize, |
86 0, 0, zeroMeasureSize, zeroMeasureSize); | 82 zeroMeasureSize); |
87 assertMeasuredSize(surfaceViewRenderer, scalingType, frameDimensions, | 83 assertMeasuredSize(surfaceViewRenderer, scalingType, frameDimensions, 12
80, 720, |
88 1280, 720, | |
89 MeasureSpec.makeMeasureSpec(1280, measureSpecMode), | 84 MeasureSpec.makeMeasureSpec(1280, measureSpecMode), |
90 MeasureSpec.makeMeasureSpec(720, measureSpecMode)); | 85 MeasureSpec.makeMeasureSpec(720, measureSpecMode)); |
91 } | 86 } |
92 } | 87 } |
93 | 88 |
94 // Test behaviour after SurfaceViewRenderer.init() is called, but still no fr
ame. | 89 // Test behaviour after SurfaceViewRenderer.init() is called, but still no f
rame. |
95 surfaceViewRenderer.init((EglBase.Context) null, null); | 90 surfaceViewRenderer.init((EglBase.Context) null, null); |
96 for (RendererCommon.ScalingType scalingType : scalingTypes) { | 91 for (RendererCommon.ScalingType scalingType : scalingTypes) { |
97 for (int measureSpecMode : measureSpecModes) { | 92 for (int measureSpecMode : measureSpecModes) { |
98 final int zeroMeasureSize = MeasureSpec.makeMeasureSpec(0, measureSpecMo
de); | 93 final int zeroMeasureSize = MeasureSpec.makeMeasureSpec(0, measureSpecMo
de); |
99 assertMeasuredSize(surfaceViewRenderer, scalingType, frameDimensions, | 94 assertMeasuredSize(surfaceViewRenderer, scalingType, frameDimensions, 0,
0, zeroMeasureSize, |
100 0, 0, zeroMeasureSize, zeroMeasureSize); | 95 zeroMeasureSize); |
101 assertMeasuredSize(surfaceViewRenderer, scalingType, frameDimensions, | 96 assertMeasuredSize(surfaceViewRenderer, scalingType, frameDimensions, 12
80, 720, |
102 1280, 720, | |
103 MeasureSpec.makeMeasureSpec(1280, measureSpecMode), | 97 MeasureSpec.makeMeasureSpec(1280, measureSpecMode), |
104 MeasureSpec.makeMeasureSpec(720, measureSpecMode)); | 98 MeasureSpec.makeMeasureSpec(720, measureSpecMode)); |
105 } | 99 } |
106 } | 100 } |
107 | 101 |
108 surfaceViewRenderer.release(); | 102 surfaceViewRenderer.release(); |
109 } | 103 } |
110 | 104 |
111 /** | 105 /** |
112 * Test how SurfaceViewRenderer.onMeasure() behaves with a 1280x720 frame. | 106 * Test how SurfaceViewRenderer.onMeasure() behaves with a 1280x720 frame. |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 assertEquals(rotatedHeight, frame.rotatedHeight()); | 150 assertEquals(rotatedHeight, frame.rotatedHeight()); |
157 final String frameDimensions = | 151 final String frameDimensions = |
158 unrotatedWidth + "x" + unrotatedHeight + " with rotation " + rotationD
egree; | 152 unrotatedWidth + "x" + unrotatedHeight + " with rotation " + rotationD
egree; |
159 surfaceViewRenderer.renderFrame(frame); | 153 surfaceViewRenderer.renderFrame(frame); |
160 rendererEvents.waitForFrameSize(unrotatedWidth, unrotatedHeight, rotationD
egree); | 154 rendererEvents.waitForFrameSize(unrotatedWidth, unrotatedHeight, rotationD
egree); |
161 | 155 |
162 // Test forcing to zero size. | 156 // Test forcing to zero size. |
163 for (RendererCommon.ScalingType scalingType : scalingTypes) { | 157 for (RendererCommon.ScalingType scalingType : scalingTypes) { |
164 for (int measureSpecMode : measureSpecModes) { | 158 for (int measureSpecMode : measureSpecModes) { |
165 final int zeroMeasureSize = MeasureSpec.makeMeasureSpec(0, measureSpec
Mode); | 159 final int zeroMeasureSize = MeasureSpec.makeMeasureSpec(0, measureSpec
Mode); |
166 assertMeasuredSize(surfaceViewRenderer, scalingType, frameDimensions, | 160 assertMeasuredSize(surfaceViewRenderer, scalingType, frameDimensions,
0, 0, |
167 0, 0, zeroMeasureSize, zeroMeasureSize); | 161 zeroMeasureSize, zeroMeasureSize); |
168 } | 162 } |
169 } | 163 } |
170 | 164 |
171 // Test perfect fit. | 165 // Test perfect fit. |
172 for (RendererCommon.ScalingType scalingType : scalingTypes) { | 166 for (RendererCommon.ScalingType scalingType : scalingTypes) { |
173 for (int measureSpecMode : measureSpecModes) { | 167 for (int measureSpecMode : measureSpecModes) { |
174 assertMeasuredSize(surfaceViewRenderer, scalingType, frameDimensions, | 168 assertMeasuredSize(surfaceViewRenderer, scalingType, frameDimensions,
rotatedWidth, |
175 rotatedWidth, rotatedHeight, | 169 rotatedHeight, MeasureSpec.makeMeasureSpec(rotatedWidth, measureSp
ecMode), |
176 MeasureSpec.makeMeasureSpec(rotatedWidth, measureSpecMode), | |
177 MeasureSpec.makeMeasureSpec(rotatedHeight, measureSpecMode)); | 170 MeasureSpec.makeMeasureSpec(rotatedHeight, measureSpecMode)); |
178 } | 171 } |
179 } | 172 } |
180 | 173 |
181 // Force spec size with different aspect ratio than frame aspect ratio. | 174 // Force spec size with different aspect ratio than frame aspect ratio. |
182 for (RendererCommon.ScalingType scalingType : scalingTypes) { | 175 for (RendererCommon.ScalingType scalingType : scalingTypes) { |
183 assertMeasuredSize(surfaceViewRenderer, scalingType, frameDimensions, | 176 assertMeasuredSize(surfaceViewRenderer, scalingType, frameDimensions, 72
0, 1280, |
184 720, 1280, | |
185 MeasureSpec.makeMeasureSpec(720, MeasureSpec.EXACTLY), | 177 MeasureSpec.makeMeasureSpec(720, MeasureSpec.EXACTLY), |
186 MeasureSpec.makeMeasureSpec(1280, MeasureSpec.EXACTLY)); | 178 MeasureSpec.makeMeasureSpec(1280, MeasureSpec.EXACTLY)); |
187 } | 179 } |
188 | 180 |
189 final float videoAspectRatio = (float) rotatedWidth / rotatedHeight; | 181 final float videoAspectRatio = (float) rotatedWidth / rotatedHeight; |
190 { | 182 { |
191 // Relax both width and height constraints. | 183 // Relax both width and height constraints. |
192 final int widthSpec = MeasureSpec.makeMeasureSpec(720, MeasureSpec.AT_MO
ST); | 184 final int widthSpec = MeasureSpec.makeMeasureSpec(720, MeasureSpec.AT_MO
ST); |
193 final int heightSpec = MeasureSpec.makeMeasureSpec(1280, MeasureSpec.AT_
MOST); | 185 final int heightSpec = MeasureSpec.makeMeasureSpec(1280, MeasureSpec.AT_
MOST); |
194 for (RendererCommon.ScalingType scalingType : scalingTypes) { | 186 for (RendererCommon.ScalingType scalingType : scalingTypes) { |
195 final Point expectedSize = | 187 final Point expectedSize = |
196 RendererCommon.getDisplaySize(scalingType, videoAspectRatio, 720,
1280); | 188 RendererCommon.getDisplaySize(scalingType, videoAspectRatio, 720,
1280); |
197 assertMeasuredSize(surfaceViewRenderer, scalingType, frameDimensions, | 189 assertMeasuredSize(surfaceViewRenderer, scalingType, frameDimensions,
expectedSize.x, |
198 expectedSize.x, expectedSize.y, widthSpec, heightSpec); | 190 expectedSize.y, widthSpec, heightSpec); |
199 } | 191 } |
200 } | 192 } |
201 { | 193 { |
202 // Force width to 720, but relax height constraint. This will give the s
ame result as | 194 // Force width to 720, but relax height constraint. This will give the s
ame result as |
203 // above, because width is already the limiting factor and will be maxed
out. | 195 // above, because width is already the limiting factor and will be maxed
out. |
204 final int widthSpec = MeasureSpec.makeMeasureSpec(720, MeasureSpec.EXACT
LY); | 196 final int widthSpec = MeasureSpec.makeMeasureSpec(720, MeasureSpec.EXACT
LY); |
205 final int heightSpec = MeasureSpec.makeMeasureSpec(1280, MeasureSpec.AT_
MOST); | 197 final int heightSpec = MeasureSpec.makeMeasureSpec(1280, MeasureSpec.AT_
MOST); |
206 for (RendererCommon.ScalingType scalingType : scalingTypes) { | 198 for (RendererCommon.ScalingType scalingType : scalingTypes) { |
207 final Point expectedSize = | 199 final Point expectedSize = |
208 RendererCommon.getDisplaySize(scalingType, videoAspectRatio, 720,
1280); | 200 RendererCommon.getDisplaySize(scalingType, videoAspectRatio, 720,
1280); |
209 assertMeasuredSize(surfaceViewRenderer, scalingType, frameDimensions, | 201 assertMeasuredSize(surfaceViewRenderer, scalingType, frameDimensions,
expectedSize.x, |
210 expectedSize.x, expectedSize.y, widthSpec, heightSpec); | 202 expectedSize.y, widthSpec, heightSpec); |
211 } | 203 } |
212 } | 204 } |
213 { | 205 { |
214 // Force height, but relax width constraint. This will force a bad layou
t size. | 206 // Force height, but relax width constraint. This will force a bad layou
t size. |
215 final int widthSpec = MeasureSpec.makeMeasureSpec(720, MeasureSpec.AT_MO
ST); | 207 final int widthSpec = MeasureSpec.makeMeasureSpec(720, MeasureSpec.AT_MO
ST); |
216 final int heightSpec = MeasureSpec.makeMeasureSpec(1280, MeasureSpec.EXA
CTLY); | 208 final int heightSpec = MeasureSpec.makeMeasureSpec(1280, MeasureSpec.EXA
CTLY); |
217 for (RendererCommon.ScalingType scalingType : scalingTypes) { | 209 for (RendererCommon.ScalingType scalingType : scalingTypes) { |
218 assertMeasuredSize(surfaceViewRenderer, scalingType, frameDimensions, | 210 assertMeasuredSize( |
219 720, 1280, widthSpec, heightSpec); | 211 surfaceViewRenderer, scalingType, frameDimensions, 720, 1280, widt
hSpec, heightSpec); |
220 } | 212 } |
221 } | 213 } |
222 } | 214 } |
223 | 215 |
224 surfaceViewRenderer.release(); | 216 surfaceViewRenderer.release(); |
225 } | 217 } |
226 } | 218 } |
OLD | NEW |