Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(692)

Side by Side Diff: talk/app/webrtc/java/android/org/webrtc/RendererCommon.java

Issue 1257043004: AppRTCDemo: Render each video in a separate SurfaceView (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Change license header for PercentFrameLayout.java Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * libjingle
3 * Copyright 2015 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 package org.webrtc;
29
30 import android.graphics.Point;
31 import android.opengl.Matrix;
32
33 /**
34 * Static helper functions for VideoRendererGui and SurfaceViewRenderer.
35 */
36 public class RendererCommon {
37 // Types of video scaling:
38 // SCALE_ASPECT_FIT - video frame is scaled to fit the size of the view by
39 // maintaining the aspect ratio (black borders may be displayed).
40 // SCALE_ASPECT_FILL - video frame is scaled to fill the size of the view by
41 // maintaining the aspect ratio. Some portion of the video frame may be
42 // clipped.
43 // SCALE_ASPECT_BALANCED - Compromise between FIT and FILL. Video frame will f ill as much as
44 // possible of the view while maintaining aspect ratio, under the constraint t hat at least
45 // |BALANCED_VISIBLE_FRACTION| of the frame content will be shown.
46 public static enum ScalingType { SCALE_ASPECT_FIT, SCALE_ASPECT_FILL, SCALE_AS PECT_BALANCED }
47 // The minimum fraction of the frame content that will be shown for |SCALE_ASP ECT_BALANCED|.
48 // This limits excessive cropping when adjusting display size.
49 private static float BALANCED_VISIBLE_FRACTION = 0.56f;
50
51 /**
52 * Calculates a texture transformation matrix based on rotation, mirror, and v ideo vs display
53 * aspect ratio.
54 */
55 public static void getTextureMatrix(float[] outputTextureMatrix, float rotatio nDegree,
56 boolean mirror, float videoAspectRatio, float displayAspectRatio) {
57 // The matrix stack is using post-multiplication, which means that matrix op erations:
58 // A; B; C; will end up as A * B * C. When you apply this to a vertex, it wi ll result in:
59 // v' = A * B * C * v, i.e. the last matrix operation is the first thing tha t affects the
60 // vertex. This is the opposite of what you might expect.
61 Matrix.setIdentityM(outputTextureMatrix, 0);
62 // Move coordinates back to [0,1]x[0,1].
63 Matrix.translateM(outputTextureMatrix, 0, 0.5f, 0.5f, 0.0f);
64 // Rotate frame clockwise in the XY-plane (around the Z-axis).
65 Matrix.rotateM(outputTextureMatrix, 0, -rotationDegree, 0, 0, 1);
66 // Scale one dimension until video and display size have same aspect ratio.
67 if (displayAspectRatio > videoAspectRatio) {
68 Matrix.scaleM(outputTextureMatrix, 0, 1, videoAspectRatio / displayAspectR atio, 1);
69 } else {
70 Matrix.scaleM(outputTextureMatrix, 0, displayAspectRatio / videoAspectRati o, 1, 1);
71 }
72 // TODO(magjed): We currently ignore the texture transform matrix from the S urfaceTexture.
73 // It contains a vertical flip that is hardcoded here instead.
74 Matrix.scaleM(outputTextureMatrix, 0, 1, -1, 1);
75 // Apply optional horizontal flip.
76 if (mirror) {
77 Matrix.scaleM(outputTextureMatrix, 0, -1, 1, 1);
78 }
79 // Center coordinates around origin.
80 Matrix.translateM(outputTextureMatrix, 0, -0.5f, -0.5f, 0.0f);
81 }
82
83 /**
84 * Calculate display size based on scaling type, video aspect ratio, and maxim um display size.
85 */
86 public static Point getDisplaySize(ScalingType scalingType, float videoAspectR atio,
87 int maxDisplayWidth, int maxDisplayHeight) {
88 return getDisplaySize(convertScalingTypeToVisibleFraction(scalingType), vide oAspectRatio,
89 maxDisplayWidth, maxDisplayHeight);
90 }
91
92 /**
93 * Each scaling type has a one-to-one correspondence to a numeric minimum frac tion of the video
94 * that must remain visible.
95 */
96 private static float convertScalingTypeToVisibleFraction(ScalingType scalingTy pe) {
97 switch (scalingType) {
98 case SCALE_ASPECT_FIT:
99 return 1.0f;
100 case SCALE_ASPECT_FILL:
101 return 0.0f;
102 case SCALE_ASPECT_BALANCED:
103 return BALANCED_VISIBLE_FRACTION;
104 default:
105 throw new IllegalArgumentException();
106 }
107 }
108
109 /**
110 * Calculate display size based on minimum fraction of the video that must rem ain visible,
111 * video aspect ratio, and maximum display size.
112 */
113 private static Point getDisplaySize(float minVisibleFraction, float videoAspec tRatio,
114 int maxDisplayWidth, int maxDisplayHeight) {
115 // If there is no constraint on the amount of cropping, fill the allowed dis play area.
116 if (minVisibleFraction == 0 || videoAspectRatio == 0) {
117 return new Point(maxDisplayWidth, maxDisplayHeight);
118 }
119 // Each dimension is constrained on max display size and how much we are all owed to crop.
120 final int width = Math.min(maxDisplayWidth,
121 (int) (maxDisplayHeight / minVisibleFraction * videoAspectRatio));
122 final int height = Math.min(maxDisplayHeight,
123 (int) (maxDisplayWidth / minVisibleFraction / videoAspectRatio));
124 return new Point(width, height);
125 }
126 }
OLDNEW
« no previous file with comments | « talk/app/webrtc/java/android/org/webrtc/EglBase.java ('k') | talk/app/webrtc/java/android/org/webrtc/SurfaceViewRenderer.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698