OLD | NEW |
1 /* | 1 /* |
2 * libjingle | 2 * libjingle |
3 * Copyright 2015 Google Inc. | 3 * Copyright 2015 Google Inc. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions are met: | 6 * modification, are permitted provided that the following conditions are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright notice, | 8 * 1. Redistributions of source code must retain the above copyright notice, |
9 * this list of conditions and the following disclaimer. | 9 * this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright notice, | 10 * 2. Redistributions in binary form must reproduce the above copyright notice, |
(...skipping 10 matching lines...) Expand all Loading... |
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | 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, | 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | 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 | 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 */ | 26 */ |
27 | 27 |
28 package org.webrtc; | 28 package org.webrtc; |
29 | 29 |
30 import android.graphics.Point; | 30 import android.graphics.Point; |
| 31 import android.opengl.GLES20; |
31 import android.opengl.Matrix; | 32 import android.opengl.Matrix; |
32 | 33 |
| 34 import java.nio.ByteBuffer; |
| 35 |
33 /** | 36 /** |
34 * Static helper functions for renderer implementations. | 37 * Static helper functions for renderer implementations. |
35 */ | 38 */ |
36 public class RendererCommon { | 39 public class RendererCommon { |
37 /** Interface for reporting rendering events. */ | 40 /** Interface for reporting rendering events. */ |
38 public static interface RendererEvents { | 41 public static interface RendererEvents { |
39 /** | 42 /** |
40 * Callback fired once first frame is rendered. | 43 * Callback fired once first frame is rendered. |
41 */ | 44 */ |
42 public void onFirstFrameRendered(); | 45 public void onFirstFrameRendered(); |
43 | 46 |
44 /** | 47 /** |
45 * Callback fired when rendered frame resolution or rotation has changed. | 48 * Callback fired when rendered frame resolution or rotation has changed. |
46 */ | 49 */ |
47 public void onFrameResolutionChanged(int videoWidth, int videoHeight, int ro
tation); | 50 public void onFrameResolutionChanged(int videoWidth, int videoHeight, int ro
tation); |
48 } | 51 } |
49 | 52 |
| 53 /** Interface for rendering frames on an EGLSurface. */ |
| 54 public static interface GlDrawer { |
| 55 /** |
| 56 * Functions for drawing frames with different sources. The rendering surfac
e target is |
| 57 * implied by the current EGL context of the calling thread and requires no
explicit argument. |
| 58 * The coordinates specify the viewport location on the surface target. |
| 59 */ |
| 60 void drawOes(int oesTextureId, float[] texMatrix, int x, int y, int width, i
nt height); |
| 61 void drawRgb(int textureId, float[] texMatrix, int x, int y, int width, int
height); |
| 62 void drawYuv(int[] yuvTextures, float[] texMatrix, int x, int y, int width,
int height); |
| 63 |
| 64 /** |
| 65 * Release all GL resources. This needs to be done manually, otherwise resou
rces may leak. |
| 66 */ |
| 67 void release(); |
| 68 } |
| 69 |
| 70 /** |
| 71 * Helper class for uploading YUV bytebuffer frames to textures that handles s
tride > width. This |
| 72 * class keeps an internal ByteBuffer to avoid unnecessary allocations for int
ermediate copies. |
| 73 */ |
| 74 public static class YuvUploader { |
| 75 // Intermediate copy buffer for uploading yuv frames that are not packed, i.
e. stride > width. |
| 76 // TODO(magjed): Investigate when GL_UNPACK_ROW_LENGTH is available, or make
a custom shader |
| 77 // that handles stride and compare performance with intermediate copy. |
| 78 private ByteBuffer copyBuffer; |
| 79 |
| 80 /** |
| 81 * Upload |planes| into |outputYuvTextures|, taking stride into consideratio
n. |
| 82 * |outputYuvTextures| must have been generated in advance. |
| 83 */ |
| 84 public void uploadYuvData( |
| 85 int[] outputYuvTextures, int width, int height, int[] strides, ByteBuffe
r[] planes) { |
| 86 final int[] planeWidths = new int[] {width, width / 2, width / 2}; |
| 87 final int[] planeHeights = new int[] {height, height / 2, height / 2}; |
| 88 // Make a first pass to see if we need a temporary copy buffer. |
| 89 int copyCapacityNeeded = 0; |
| 90 for (int i = 0; i < 3; ++i) { |
| 91 if (strides[i] > planeWidths[i]) { |
| 92 copyCapacityNeeded = Math.max(copyCapacityNeeded, planeWidths[i] * pla
neHeights[i]); |
| 93 } |
| 94 } |
| 95 // Allocate copy buffer if necessary. |
| 96 if (copyCapacityNeeded > 0 |
| 97 && (copyBuffer == null || copyBuffer.capacity() < copyCapacityNeeded))
{ |
| 98 copyBuffer = ByteBuffer.allocateDirect(copyCapacityNeeded); |
| 99 } |
| 100 // Upload each plane. |
| 101 for (int i = 0; i < 3; ++i) { |
| 102 GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + i); |
| 103 GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, outputYuvTextures[i]); |
| 104 // GLES only accepts packed data, i.e. stride == planeWidth. |
| 105 final ByteBuffer packedByteBuffer; |
| 106 if (strides[i] == planeWidths[i]) { |
| 107 // Input is packed already. |
| 108 packedByteBuffer = planes[i]; |
| 109 } else { |
| 110 VideoRenderer.nativeCopyPlane( |
| 111 planes[i], planeWidths[i], planeHeights[i], strides[i], copyBuffer
, planeWidths[i]); |
| 112 packedByteBuffer = copyBuffer; |
| 113 } |
| 114 GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_LUMINANCE, planeW
idths[i], |
| 115 planeHeights[i], 0, GLES20.GL_LUMINANCE, GLES20.GL_UNSIGNED_BYTE, pa
ckedByteBuffer); |
| 116 } |
| 117 } |
| 118 } |
| 119 |
50 // Types of video scaling: | 120 // Types of video scaling: |
51 // SCALE_ASPECT_FIT - video frame is scaled to fit the size of the view by | 121 // 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). | 122 // 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 | 123 // 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 | 124 // maintaining the aspect ratio. Some portion of the video frame may be |
55 // clipped. | 125 // clipped. |
56 // SCALE_ASPECT_BALANCED - Compromise between FIT and FILL. Video frame will f
ill as much as | 126 // 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 | 127 // 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. | 128 // |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 } | 129 public static enum ScalingType { SCALE_ASPECT_FIT, SCALE_ASPECT_FILL, SCALE_AS
PECT_BALANCED } |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 return new Point(maxDisplayWidth, maxDisplayHeight); | 251 return new Point(maxDisplayWidth, maxDisplayHeight); |
182 } | 252 } |
183 // Each dimension is constrained on max display size and how much we are all
owed to crop. | 253 // Each dimension is constrained on max display size and how much we are all
owed to crop. |
184 final int width = Math.min(maxDisplayWidth, | 254 final int width = Math.min(maxDisplayWidth, |
185 Math.round(maxDisplayHeight / minVisibleFraction * videoAspectRatio)); | 255 Math.round(maxDisplayHeight / minVisibleFraction * videoAspectRatio)); |
186 final int height = Math.min(maxDisplayHeight, | 256 final int height = Math.min(maxDisplayHeight, |
187 Math.round(maxDisplayWidth / minVisibleFraction / videoAspectRatio)); | 257 Math.round(maxDisplayWidth / minVisibleFraction / videoAspectRatio)); |
188 return new Point(width, height); | 258 return new Point(width, height); |
189 } | 259 } |
190 } | 260 } |
OLD | NEW |