rk
昨天 4a8ff39b0fab0627ef8f7459587d514cc01c3676
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package com.example.datalibrary.deptrum;
 
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
 
import java.nio.ByteBuffer;
 
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
 
/**
 * 渲染
 */
public class GLFrameRenderer implements Renderer {
    private static final int SFPS = 25;
 
    private ISimplePlayer mParentAct;
    private GLSurfaceView mTargetSurface;
    private RGB24GLProgram rgbProg = new RGB24GLProgram(0);
    private YUV420PGLProgram yuvProg = new YUV420PGLProgram(0);
    private int mScreenWidth;
    private int mScreenHeight;
    private int mVideoWidth;
    private int mVideoHeight;
    private ByteBuffer y;
    private ByteBuffer u;
    private ByteBuffer v;
    private ByteBuffer blackUV;
    private ByteBuffer rgb24;
 
    private int mDisplayDegrees;
    private boolean mNeedMirror = false;
    private long mLastFrameTime = 0;
    private long mStanderDelta;
    private int dataType = 0; // 0:yuv420p 1:rgb24
 
    public GLFrameRenderer(GLSurfaceView surface) {
        // mParentAct = callback;
        mTargetSurface = surface;
        mDisplayDegrees = 0;
        mNeedMirror = false;
        mStanderDelta = 1000 / SFPS;
    }
 
    public void setResolution(int nWidth, int nHeight) {
        mScreenWidth = nWidth;
        mScreenHeight = nHeight;
    }
 
    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        // Utils.LOGD("GLFrameRenderer :: onSurfaceCreated");
        if (!rgbProg.isProgramBuilt()) {
            rgbProg.buildProgram();
        }
        if (!yuvProg.isProgramBuilt()) {
            yuvProg.buildProgram();
        }
    }
 
    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        // Utils.LOGD("GLFrameRenderer :: onSurfaceChanged");
        GLES20.glViewport(0, 0, width, height);
    }
 
    @Override
    public void onDrawFrame(GL10 gl) {
        synchronized (this) {
            if (mLastFrameTime == 0) {
                mLastFrameTime = System.currentTimeMillis();
            } else {
                long currentTime = System.currentTimeMillis();
                long delta = currentTime - mLastFrameTime;
                if (delta < mStanderDelta) {
                    try {
                        Thread.sleep(mStanderDelta - delta);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                mLastFrameTime = currentTime;
            }
            switch (dataType) {
                case 0: {
                    // 绿帧则不渲染
                    if (y != null) {
                        if (isNullFrame(y, u, v)) {
                            return;
                        } else {
                            y.position(0);
                            u.position(0);
                            v.position(0);
                            yuvProg.buildTextures(y, u, v, mVideoWidth, mVideoHeight);
                            GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
                            GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
                            yuvProg.drawFrame();
                        }
                    }
                    break;
                }
                case 1: {
                    if (rgb24 != null) {
                        rgb24.position(0);
                        rgbProg.buildTextures(rgb24, mVideoWidth, mVideoHeight, false);
                        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
                        GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
                        rgbProg.drawFrame();
                    }
                    break;
                }
            }
        }
    }
 
    public void clear() {
        switch (dataType) {
            case 0: {
                if (y == null || u == null || v == null) {
                    return;
                }
                int size = this.mVideoWidth * this.mVideoHeight;
                byte[] b = new byte[size];
                initBlackUV();
                synchronized (this) {
                    y.clear();
                    u.clear();
                    v.clear();
                    y.put(b);
                    blackUV.position(0);
                    u.put(blackUV);
                    blackUV.position(0);
                    v.put(blackUV);
                }
                mTargetSurface.requestRender();
                break;
            }
            case 1: {
                rgb24.clear();
                rgb24.put(new byte[rgb24.capacity()]);
                mTargetSurface.requestRender();
                break;
            }
        }
    }
 
    /**
     * this method will be called from native code, it happens when the video is about to play or the
     * video size changes.
     */
    public void update(int w, int h, int dataType) {
        this.dataType = dataType;
        // Utils.LOGD("INIT E");
        if (w > 0 && h > 0) {
 
            if (mScreenWidth > 0 && mScreenHeight > 0) {
                float f1 = 1f * mScreenHeight / mScreenWidth;
                float f2 = 1f * h / w;
                if (f1 == f2) {
                    rgbProg.createBuffers(RGB24GLProgram.squareVertices);
                    yuvProg.createBuffers(YUV420PGLProgram.squareVertices);
                } else if (f1 < f2) {
                    float widScale = f1 / f2;
                    rgbProg.createBuffers(
                            new float[]{
                                    -widScale, -1.0f, widScale, -1.0f, -widScale, 1.0f, widScale, 1.0f,
                            });
                    yuvProg.createBuffers(
                            new float[]{
                                    -widScale, -1.0f, widScale, -1.0f, -widScale, 1.0f, widScale, 1.0f,
                            });
                } else {
                    float heightScale = f2 / f1;
                    rgbProg.createBuffers(
                            new float[]{
                                    -1.0f, -heightScale, 1.0f, -heightScale, -1.0f, heightScale, 1.0f, heightScale,
                            });
                    yuvProg.createBuffers(
                            new float[]{
                                    -1.0f, -heightScale, 1.0f, -heightScale, -1.0f, heightScale, 1.0f, heightScale,
                            });
                }
            }
 
            if (w != mVideoWidth && h != mVideoHeight) {
                this.mVideoWidth = w;
                this.mVideoHeight = h;
                int yarraySize = w * h;
                if (dataType == 0) {
                    int uvarraySize = yarraySize / 4;
                    synchronized (this) {
                        y = ByteBuffer.allocate(yarraySize);
                        u = ByteBuffer.allocate(uvarraySize);
                        v = ByteBuffer.allocate(uvarraySize);
                    }
                } else if (dataType == 1) {
                    synchronized (this) {
                        rgb24 = ByteBuffer.allocate(yarraySize * 3);
                    }
                }
            }
        }
 
        // mParentAct.onPlayStart();
        // Utils.LOGD("INIT X");
    }
 
    /**
     * this method will be called from native code, it's used for passing yuv data to me.
     */
    public void update(byte[] ydata, byte[] udata, byte[] vdata) {
        synchronized (this) {
            y.clear();
            u.clear();
            v.clear();
            y.put(ydata, 0, ydata.length);
            u.put(udata, 0, udata.length);
            v.put(vdata, 0, vdata.length);
        }
        // request to render
        mTargetSurface.requestRender();
    }
 
    /**
     * @param dataType 0:yuv420p 1:rgb24
     */
    public void update(byte[] data, int dataType) {
        this.dataType = dataType;
        int size = this.mVideoWidth * this.mVideoHeight;
        synchronized (this) {
            switch (this.dataType) {
                case 0: {
                    y.clear();
                    u.clear();
                    v.clear();
                    y.put(data, 0, size);
                    u.put(data, size, size / 4);
                    v.put(data, size * 5 / 4, size / 4);
                    break;
                }
                case 1: {
                    if (rgb24 == null) {
                        rgb24 = ByteBuffer.allocate(size * 3);
                    }
                    rgb24.clear();
                    rgb24.put(data, 0, size * 3);
                    break;
                }
            }
        }
        // request to render
        mTargetSurface.requestRender();
    }
 
    /*
     * 取图像四角与中心判定
     * */
    private boolean isNullFrame(ByteBuffer y, ByteBuffer u, ByteBuffer v) {
        switch (dataType) {
            case 0: {
                int size = mVideoWidth * mVideoHeight;
                if (y == null
                        || u == null
                        || v == null
                        || y.capacity() < size
                        || u.capacity() < size / 4
                        || v.capacity() < size / 4) {
                    return true;
                }
                int ypos = y.position();
                int upos = u.position();
                int vpos = v.position();
                int[] points = {0, mVideoWidth - 2, size / 2, size - mScreenWidth + 2, size - 2};
                int flag = 2; // 最少两点为绿则判断为绿帧
 
                for (int i = 0; i < 5; i++) {
                    if (y.get(points[i]) == 0 && u.get(points[i] / 4) == 0 && v.get(points[i] / 4) == 0) {
                        flag--;
                        if (flag <= 0) {
                            y.position(ypos);
                            u.position(upos);
                            v.position(vpos);
                            return true;
                        }
                    }
                }
                y.position(ypos);
                u.position(upos);
                v.position(vpos);
                return false;
            }
            case 1: {
                break;
            }
        }
        return false;
    }
 
    private void initBlackUV() {
        int size = mVideoWidth * mVideoHeight;
        if (blackUV == null || blackUV.capacity() < size / 4) {
            blackUV = ByteBuffer.allocate(size / 4);
            blackUV.position(0);
            for (int i = 0; i < size / 4; i++) {
                blackUV.put((byte) 128);
            }
            blackUV.position(0);
        } else {
            blackUV.position(0);
        }
    }
 
    /**
     * this method will be called from native code, it's used for passing play state to activity.
     */
    public void updateState(int state) {
        // Utils.LOGD("updateState E = " + state);
        if (mParentAct != null) {
            mParentAct.onReceiveState(state);
        }
        // Utils.LOGD("updateState X");
    }
 
    public void setDisplayOrientation(int displayOrientation) {
        mDisplayDegrees = displayOrientation;
        rgbProg.setDisplayOrientation(displayOrientation, mNeedMirror);
        yuvProg.setDisplayOrientation(displayOrientation, mNeedMirror);
    }
 
    public void displayMirror(boolean mirror) {
        mNeedMirror = mirror;
        rgbProg.setDisplayOrientation(mDisplayDegrees, mNeedMirror);
        yuvProg.setDisplayOrientation(mDisplayDegrees, mNeedMirror);
    }
 
    public void release() {
        rgbProg.releaseProgram();
        yuvProg.releaseProgram();
    }
}