OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2017 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 android.graphics.Matrix; | |
14 import java.nio.ByteBuffer; | |
15 | |
16 /** | |
17 * Android texture buffer backed by a SurfaceTextureHelper's texture. The buffer calls | |
18 * |releaseCallback| when it is released. | |
19 */ | |
20 class TextureBufferImpl implements VideoFrame.TextureBuffer { | |
21 private final int width; | |
22 private final int height; | |
23 private final Type type; | |
24 private final int id; | |
25 private final Matrix transformMatrix; | |
26 private final SurfaceTextureHelper surfaceTextureHelper; | |
27 private final Runnable releaseCallback; | |
28 private int refCount; | |
29 | |
30 public TextureBufferImpl(int width, int height, Type type, int id, Matrix tran sformMatrix, | |
31 SurfaceTextureHelper surfaceTextureHelper, Runnable releaseCallback) { | |
32 this.width = width; | |
33 this.height = height; | |
34 this.type = type; | |
35 this.id = id; | |
36 this.transformMatrix = transformMatrix; | |
37 this.surfaceTextureHelper = surfaceTextureHelper; | |
38 this.releaseCallback = releaseCallback; | |
39 this.refCount = 1; // Creator implicitly holds a reference. | |
40 } | |
41 | |
42 @Override | |
43 public VideoFrame.TextureBuffer.Type getType() { | |
44 return VideoFrame.TextureBuffer.Type.OES; | |
magjed_webrtc
2017/07/27 14:02:29
Is it possible to return just Type.OES?
sakal
2017/07/28 08:01:36
This should return type.
| |
45 } | |
46 | |
47 @Override | |
48 public int getTextureId() { | |
49 return id; | |
50 } | |
51 | |
52 @Override | |
53 public Matrix getTransformMatrix() { | |
54 return transformMatrix; | |
55 } | |
56 | |
57 @Override | |
58 public int getWidth() { | |
59 return width; | |
60 } | |
61 | |
62 @Override | |
63 public int getHeight() { | |
64 return height; | |
65 } | |
66 | |
67 @Override | |
68 public VideoFrame.I420Buffer toI420() { | |
69 // SurfaceTextureHelper requires a stride that is divisible by 8. Round wid th up. | |
70 // See SurfaceTextureHelper for details on the size and format. | |
71 int stride = ((width + 7) / 8) * 8; | |
72 int uvHeight = (height + 1) / 2; | |
73 // Due to the layout used by SurfaceTextureHelper, vPos + stride * uvHeight would overrun the | |
74 // buffer. Add one row at the bottom to compensate for this. There will ne ver be data in the | |
75 // extra row, but now other code does not have to deal with v stride * v hei ght exceeding the | |
76 // buffer's capacity. | |
77 int size = stride * (height + uvHeight + 1); | |
78 ByteBuffer buffer = ByteBuffer.allocateDirect(size); | |
79 surfaceTextureHelper.textureToYUV(buffer, width, height, stride, id, | |
80 RendererCommon.convertMatrixFromAndroidGraphicsMatrix(transformMatrix)); | |
81 | |
82 int yPos = 0; | |
83 int uPos = yPos + stride * height; | |
84 // Rows of U and V alternate in the buffer, so V data starts after the first row of U. | |
85 int vPos = uPos + stride / 2; | |
86 | |
87 // SurfaceTextureHelper uses the same stride for Y, U, and V data. | |
88 return new I420BufferImpl( | |
89 buffer, width, height, yPos, stride, uPos, stride, vPos, stride, null); | |
90 } | |
91 | |
92 @Override | |
93 public void retain() { | |
94 ++refCount; | |
95 } | |
96 | |
97 @Override | |
98 public void release() { | |
99 if (--refCount == 0) { | |
100 releaseCallback.run(); | |
101 } | |
102 } | |
103 | |
104 @Override | |
105 public VideoFrame.Buffer cropAndScale( | |
106 int cropX, int cropY, int cropWidth, int cropHeight, int scaleWidth, int s caleHeight) { | |
107 retain(); | |
108 Matrix newMatrix = new Matrix(transformMatrix); | |
109 newMatrix.postScale(cropWidth / (float) width, cropHeight / (float) height); | |
110 newMatrix.postTranslate(cropX / (float) width, cropY / (float) height); | |
111 | |
112 return new TextureBufferImpl( | |
113 scaleWidth, scaleHeight, type, id, newMatrix, surfaceTextureHelper, new Runnable() { | |
114 @Override | |
115 public void run() { | |
116 release(); | |
117 } | |
118 }); | |
119 } | |
120 } | |
OLD | NEW |