• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2015 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.webrtc;
12 
13 import static org.junit.Assert.assertArrayEquals;
14 import static org.junit.Assert.assertEquals;
15 import static org.webrtc.RendererCommon.ScalingType.SCALE_ASPECT_BALANCED;
16 import static org.webrtc.RendererCommon.ScalingType.SCALE_ASPECT_FILL;
17 import static org.webrtc.RendererCommon.ScalingType.SCALE_ASPECT_FIT;
18 import static org.webrtc.RendererCommon.getDisplaySize;
19 import static org.webrtc.RendererCommon.getLayoutMatrix;
20 
21 import android.graphics.Point;
22 import androidx.test.filters.SmallTest;
23 import org.junit.Test;
24 
25 public class RendererCommonTest {
26   @Test
27   @SmallTest
testDisplaySizeNoFrame()28   public void testDisplaySizeNoFrame() {
29     assertEquals(new Point(0, 0), getDisplaySize(SCALE_ASPECT_FIT, 0.0f, 0, 0));
30     assertEquals(new Point(0, 0), getDisplaySize(SCALE_ASPECT_FILL, 0.0f, 0, 0));
31     assertEquals(new Point(0, 0), getDisplaySize(SCALE_ASPECT_BALANCED, 0.0f, 0, 0));
32   }
33 
34   @Test
35   @SmallTest
testDisplaySizeDegenerateAspectRatio()36   public void testDisplaySizeDegenerateAspectRatio() {
37     assertEquals(new Point(1280, 720), getDisplaySize(SCALE_ASPECT_FIT, 0.0f, 1280, 720));
38     assertEquals(new Point(1280, 720), getDisplaySize(SCALE_ASPECT_FILL, 0.0f, 1280, 720));
39     assertEquals(new Point(1280, 720), getDisplaySize(SCALE_ASPECT_BALANCED, 0.0f, 1280, 720));
40   }
41 
42   @Test
43   @SmallTest
testZeroDisplaySize()44   public void testZeroDisplaySize() {
45     assertEquals(new Point(0, 0), getDisplaySize(SCALE_ASPECT_FIT, 16.0f / 9, 0, 0));
46     assertEquals(new Point(0, 0), getDisplaySize(SCALE_ASPECT_FILL, 16.0f / 9, 0, 0));
47     assertEquals(new Point(0, 0), getDisplaySize(SCALE_ASPECT_BALANCED, 16.0f / 9, 0, 0));
48   }
49 
50   @Test
51   @SmallTest
testDisplaySizePerfectFit()52   public void testDisplaySizePerfectFit() {
53     assertEquals(new Point(1280, 720), getDisplaySize(SCALE_ASPECT_FIT, 16.0f / 9, 1280, 720));
54     assertEquals(new Point(1280, 720), getDisplaySize(SCALE_ASPECT_FILL, 16.0f / 9, 1280, 720));
55     assertEquals(new Point(1280, 720), getDisplaySize(SCALE_ASPECT_BALANCED, 16.0f / 9, 1280, 720));
56     assertEquals(new Point(720, 1280), getDisplaySize(SCALE_ASPECT_FIT, 9.0f / 16, 720, 1280));
57     assertEquals(new Point(720, 1280), getDisplaySize(SCALE_ASPECT_FILL, 9.0f / 16, 720, 1280));
58     assertEquals(new Point(720, 1280), getDisplaySize(SCALE_ASPECT_BALANCED, 9.0f / 16, 720, 1280));
59   }
60 
61   @Test
62   @SmallTest
testLandscapeVideoInPortraitDisplay()63   public void testLandscapeVideoInPortraitDisplay() {
64     assertEquals(new Point(720, 405), getDisplaySize(SCALE_ASPECT_FIT, 16.0f / 9, 720, 1280));
65     assertEquals(new Point(720, 1280), getDisplaySize(SCALE_ASPECT_FILL, 16.0f / 9, 720, 1280));
66     assertEquals(new Point(720, 720), getDisplaySize(SCALE_ASPECT_BALANCED, 16.0f / 9, 720, 1280));
67   }
68 
69   @Test
70   @SmallTest
testPortraitVideoInLandscapeDisplay()71   public void testPortraitVideoInLandscapeDisplay() {
72     assertEquals(new Point(405, 720), getDisplaySize(SCALE_ASPECT_FIT, 9.0f / 16, 1280, 720));
73     assertEquals(new Point(1280, 720), getDisplaySize(SCALE_ASPECT_FILL, 9.0f / 16, 1280, 720));
74     assertEquals(new Point(720, 720), getDisplaySize(SCALE_ASPECT_BALANCED, 9.0f / 16, 1280, 720));
75   }
76 
77   @Test
78   @SmallTest
testFourToThreeVideoInSixteenToNineDisplay()79   public void testFourToThreeVideoInSixteenToNineDisplay() {
80     assertEquals(new Point(960, 720), getDisplaySize(SCALE_ASPECT_FIT, 4.0f / 3, 1280, 720));
81     assertEquals(new Point(1280, 720), getDisplaySize(SCALE_ASPECT_FILL, 4.0f / 3, 1280, 720));
82     assertEquals(new Point(1280, 720), getDisplaySize(SCALE_ASPECT_BALANCED, 4.0f / 3, 1280, 720));
83   }
84 
85   // Only keep 2 rounded decimals to make float comparison robust.
round(float[] array)86   private static double[] round(float[] array) {
87     assertEquals(16, array.length);
88     final double[] doubleArray = new double[16];
89     for (int i = 0; i < 16; ++i) {
90       doubleArray[i] = Math.round(100 * array[i]) / 100.0;
91     }
92     return doubleArray;
93   }
94 
95   // Brief summary about matrix transformations:
96   // A coordinate p = [u, v, 0, 1] is transformed by matrix m like this p' = [u', v', 0, 1] = m * p.
97   // OpenGL uses column-major order, so:
98   // u' = u * m[0] + v * m[4] + m[12].
99   // v' = u * m[1] + v * m[5] + m[13].
100 
101   @Test
102   @SmallTest
testLayoutMatrixDefault()103   public void testLayoutMatrixDefault() {
104     final float layoutMatrix[] = getLayoutMatrix(false, 1.0f, 1.0f);
105     // Assert:
106     // u' = u.
107     // v' = v.
108     // clang-format off
109     assertArrayEquals(new double[] {
110         1, 0, 0, 0,
111         0, 1, 0, 0,
112         0, 0, 1, 0,
113         0, 0, 0, 1}, round(layoutMatrix), 0.0);
114     // clang-format on
115   }
116 
117   @Test
118   @SmallTest
testLayoutMatrixMirror()119   public void testLayoutMatrixMirror() {
120     final float layoutMatrix[] = getLayoutMatrix(true, 1.0f, 1.0f);
121     // Assert:
122     // u' = 1 - u.
123     // v' = v.
124     // clang-format off
125     assertArrayEquals(new double[] {
126         -1, 0, 0, 0,
127          0, 1, 0, 0,
128          0, 0, 1, 0,
129          1, 0, 0, 1}, round(layoutMatrix), 0.0);
130     // clang-format on
131   }
132 
133   @Test
134   @SmallTest
testLayoutMatrixScale()135   public void testLayoutMatrixScale() {
136     // Video has aspect ratio 2, but layout is square. This will cause only the center part of the
137     // video to be visible, i.e. the u coordinate will go from 0.25 to 0.75 instead of from 0 to 1.
138     final float layoutMatrix[] = getLayoutMatrix(false, 2.0f, 1.0f);
139     // Assert:
140     // u' = 0.25 + 0.5 u.
141     // v' = v.
142     // clang-format off
143     assertArrayEquals(new double[] {
144          0.5, 0, 0, 0,
145            0, 1, 0, 0,
146            0, 0, 1, 0,
147         0.25, 0, 0, 1}, round(layoutMatrix), 0.0);
148     // clang-format on
149   }
150 }
151