OLD | NEW |
(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 renderer implementations. |
| 35 */ |
| 36 public class RendererCommon { |
| 37 /** Interface for reporting rendering events. */ |
| 38 public static interface RendererEvents { |
| 39 /** |
| 40 * Callback fired once first frame is rendered. |
| 41 */ |
| 42 public void onFirstFrameRendered(); |
| 43 |
| 44 /** |
| 45 * Callback fired when rendered frame resolution or rotation has changed. |
| 46 */ |
| 47 public void onFrameResolutionChanged(int videoWidth, int videoHeight, int ro
tation); |
| 48 } |
| 49 |
| 50 // Types of video scaling: |
| 51 // SCALE_ASPECT_FIT - video frame is scaled to fit the size of the view by |
| 52 // maintaining the aspect ratio (black borders may be displayed). |
| 53 // SCALE_ASPECT_FILL - video frame is scaled to fill the size of the view by |
| 54 // maintaining the aspect ratio. Some portion of the video frame may be |
| 55 // clipped. |
| 56 // SCALE_ASPECT_BALANCED - Compromise between FIT and FILL. Video frame will f
ill as much as |
| 57 // possible of the view while maintaining aspect ratio, under the constraint t
hat at least |
| 58 // |BALANCED_VISIBLE_FRACTION| of the frame content will be shown. |
| 59 public static enum ScalingType { SCALE_ASPECT_FIT, SCALE_ASPECT_FILL, SCALE_AS
PECT_BALANCED } |
| 60 // The minimum fraction of the frame content that will be shown for |SCALE_ASP
ECT_BALANCED|. |
| 61 // This limits excessive cropping when adjusting display size. |
| 62 private static float BALANCED_VISIBLE_FRACTION = 0.56f; |
| 63 |
| 64 /** |
| 65 * Calculates a texture transformation matrix based on rotation, mirror, and v
ideo vs display |
| 66 * aspect ratio. |
| 67 */ |
| 68 public static void getTextureMatrix(float[] outputTextureMatrix, float rotatio
nDegree, |
| 69 boolean mirror, float videoAspectRatio, float displayAspectRatio) { |
| 70 // The matrix stack is using post-multiplication, which means that matrix op
erations: |
| 71 // A; B; C; will end up as A * B * C. When you apply this to a vertex, it wi
ll result in: |
| 72 // v' = A * B * C * v, i.e. the last matrix operation is the first thing tha
t affects the |
| 73 // vertex. This is the opposite of what you might expect. |
| 74 Matrix.setIdentityM(outputTextureMatrix, 0); |
| 75 // Move coordinates back to [0,1]x[0,1]. |
| 76 Matrix.translateM(outputTextureMatrix, 0, 0.5f, 0.5f, 0.0f); |
| 77 // Rotate frame clockwise in the XY-plane (around the Z-axis). |
| 78 Matrix.rotateM(outputTextureMatrix, 0, -rotationDegree, 0, 0, 1); |
| 79 // Scale one dimension until video and display size have same aspect ratio. |
| 80 if (displayAspectRatio > videoAspectRatio) { |
| 81 Matrix.scaleM(outputTextureMatrix, 0, 1, videoAspectRatio / displayAspectR
atio, 1); |
| 82 } else { |
| 83 Matrix.scaleM(outputTextureMatrix, 0, displayAspectRatio / videoAspectRati
o, 1, 1); |
| 84 } |
| 85 // TODO(magjed): We currently ignore the texture transform matrix from the S
urfaceTexture. |
| 86 // It contains a vertical flip that is hardcoded here instead. |
| 87 Matrix.scaleM(outputTextureMatrix, 0, 1, -1, 1); |
| 88 // Apply optional horizontal flip. |
| 89 if (mirror) { |
| 90 Matrix.scaleM(outputTextureMatrix, 0, -1, 1, 1); |
| 91 } |
| 92 // Center coordinates around origin. |
| 93 Matrix.translateM(outputTextureMatrix, 0, -0.5f, -0.5f, 0.0f); |
| 94 } |
| 95 |
| 96 /** |
| 97 * Calculate display size based on scaling type, video aspect ratio, and maxim
um display size. |
| 98 */ |
| 99 public static Point getDisplaySize(ScalingType scalingType, float videoAspectR
atio, |
| 100 int maxDisplayWidth, int maxDisplayHeight) { |
| 101 return getDisplaySize(convertScalingTypeToVisibleFraction(scalingType), vide
oAspectRatio, |
| 102 maxDisplayWidth, maxDisplayHeight); |
| 103 } |
| 104 |
| 105 /** |
| 106 * Each scaling type has a one-to-one correspondence to a numeric minimum frac
tion of the video |
| 107 * that must remain visible. |
| 108 */ |
| 109 private static float convertScalingTypeToVisibleFraction(ScalingType scalingTy
pe) { |
| 110 switch (scalingType) { |
| 111 case SCALE_ASPECT_FIT: |
| 112 return 1.0f; |
| 113 case SCALE_ASPECT_FILL: |
| 114 return 0.0f; |
| 115 case SCALE_ASPECT_BALANCED: |
| 116 return BALANCED_VISIBLE_FRACTION; |
| 117 default: |
| 118 throw new IllegalArgumentException(); |
| 119 } |
| 120 } |
| 121 |
| 122 /** |
| 123 * Calculate display size based on minimum fraction of the video that must rem
ain visible, |
| 124 * video aspect ratio, and maximum display size. |
| 125 */ |
| 126 private static Point getDisplaySize(float minVisibleFraction, float videoAspec
tRatio, |
| 127 int maxDisplayWidth, int maxDisplayHeight) { |
| 128 // If there is no constraint on the amount of cropping, fill the allowed dis
play area. |
| 129 if (minVisibleFraction == 0 || videoAspectRatio == 0) { |
| 130 return new Point(maxDisplayWidth, maxDisplayHeight); |
| 131 } |
| 132 // Each dimension is constrained on max display size and how much we are all
owed to crop. |
| 133 final int width = Math.min(maxDisplayWidth, |
| 134 (int) (maxDisplayHeight / minVisibleFraction * videoAspectRatio)); |
| 135 final int height = Math.min(maxDisplayHeight, |
| 136 (int) (maxDisplayWidth / minVisibleFraction / videoAspectRatio)); |
| 137 return new Point(width, height); |
| 138 } |
| 139 } |
OLD | NEW |