forked from flutter-webrtc/flutter-webrtc
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSurfaceTextureRenderer.java
More file actions
executable file
·197 lines (182 loc) · 7.01 KB
/
Copy pathSurfaceTextureRenderer.java
File metadata and controls
executable file
·197 lines (182 loc) · 7.01 KB
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
package io.getstream.webrtc.flutter;
import android.graphics.SurfaceTexture;
import android.view.Surface;
import org.webrtc.EglBase;
import org.webrtc.EglRenderer;
import org.webrtc.GlRectDrawer;
import org.webrtc.RendererCommon;
import org.webrtc.ThreadUtils;
import org.webrtc.VideoFrame;
import java.util.concurrent.CountDownLatch;
import io.flutter.view.TextureRegistry;
/**
* Display the video stream on a Surface.
* renderFrame() is asynchronous to avoid blocking the calling thread.
* This class is thread safe and handles access from potentially three different threads:
* Interaction from the main app in init, release and setMirror.
* Interaction from C++ rtc::VideoSinkInterface in renderFrame.
* Interaction from SurfaceHolder lifecycle in surfaceCreated, surfaceChanged, and surfaceDestroyed.
*/
public class SurfaceTextureRenderer extends EglRenderer {
// Callback for reporting renderer events. Read-only after initilization so no lock required.
private RendererCommon.RendererEvents rendererEvents;
private final Object layoutLock = new Object();
private boolean isRenderingPaused;
private boolean isFirstFrameRendered;
private int rotatedFrameWidth;
private int rotatedFrameHeight;
private int frameRotation;
/**
* In order to render something, you must first call init().
*/
public SurfaceTextureRenderer(String name) {
super(name);
}
public void init(final EglBase.Context sharedContext,
RendererCommon.RendererEvents rendererEvents) {
init(sharedContext, rendererEvents, EglBase.CONFIG_RGBA, new GlRectDrawer());
}
/**
* Initialize this class, sharing resources with |sharedContext|. The custom |drawer| will be used
* for drawing frames on the EGLSurface. This class is responsible for calling release() on
* |drawer|. It is allowed to call init() to reinitialize the renderer after a previous
* init()/release() cycle.
*/
public void init(final EglBase.Context sharedContext,
RendererCommon.RendererEvents rendererEvents, final int[] configAttributes,
RendererCommon.GlDrawer drawer) {
ThreadUtils.checkIsOnMainThread();
this.rendererEvents = rendererEvents;
synchronized (layoutLock) {
isFirstFrameRendered = false;
rotatedFrameWidth = 0;
rotatedFrameHeight = 0;
frameRotation = -1;
}
super.init(sharedContext, configAttributes, drawer);
}
@Override
public void init(final EglBase.Context sharedContext, final int[] configAttributes,
RendererCommon.GlDrawer drawer) {
init(sharedContext, null /* rendererEvents */, configAttributes, drawer);
}
/**
* Limit render framerate.
*
* @param fps Limit render framerate to this value, or use Float.POSITIVE_INFINITY to disable fps
* reduction.
*/
@Override
public void setFpsReduction(float fps) {
synchronized (layoutLock) {
isRenderingPaused = fps == 0f;
}
super.setFpsReduction(fps);
}
@Override
public void disableFpsReduction() {
synchronized (layoutLock) {
isRenderingPaused = false;
}
super.disableFpsReduction();
}
@Override
public void pauseVideo() {
synchronized (layoutLock) {
isRenderingPaused = true;
}
super.pauseVideo();
}
// VideoSink interface.
@Override
public void onFrame(VideoFrame frame) {
synchronized (surfaceLock) {
if(surface == null) {
producer.setSize(frame.getRotatedWidth(),frame.getRotatedHeight());
surface = producer.getSurface();
createEglSurface(surface);
} else if (frameSizeChanged(frame)) {
// The producer's backing buffers are fixed-size: setSize() only takes
// effect for a Surface obtained afterwards. Without recreating the EGL
// surface here, a simulcast layer upgrade keeps rendering into the old
// low-resolution buffer and the video stays blurry.
releaseEglSurface(() -> {});
// Clear the field before re-obtaining: if getSurface() throws, the
// next frame takes the surface == null path and recreates cleanly
// rather than rendering into the already-released surface.
surface = null;
producer.setSize(frame.getRotatedWidth(), frame.getRotatedHeight());
surface = producer.getSurface();
createEglSurface(surface);
}
}
updateFrameDimensionsAndReportEvents(frame);
super.onFrame(frame);
}
private boolean frameSizeChanged(VideoFrame frame) {
synchronized (layoutLock) {
return !isRenderingPaused
&& (rotatedFrameWidth != frame.getRotatedWidth()
|| rotatedFrameHeight != frame.getRotatedHeight());
}
}
// Guards surface lifecycle transitions: creation/recreation happens on the
// frame delivery thread while destruction arrives on the main thread via
// the producer's onSurfaceCleanup callback. Serializing the two prevents a
// frame from re-creating the EGL surface against a Surface the producer is
// concurrently invalidating. surfaceDestroyed() blocks on the EGL release
// while holding this lock; the latch is signaled by the EglRenderer render
// thread, which never acquires it, so the wait cannot deadlock.
private final Object surfaceLock = new Object();
private Surface surface = null;
private TextureRegistry.SurfaceProducer producer;
public void surfaceCreated(final TextureRegistry.SurfaceProducer producer) {
ThreadUtils.checkIsOnMainThread();
this.producer = producer;
this.producer.setCallback(
new TextureRegistry.SurfaceProducer.Callback() {
@Override
public void onSurfaceAvailable() {
// Do surface initialization here, and draw the current frame.
}
@Override
public void onSurfaceCleanup() {
onSurfaceCleanup();
}
});
}
public void onSurfaceCleanup() {
ThreadUtils.checkIsOnMainThread();
synchronized (surfaceLock) {
final CountDownLatch completionLatch = new CountDownLatch(1);
releaseEglSurface(completionLatch::countDown);
ThreadUtils.awaitUninterruptibly(completionLatch);
surface = null;
}
}
// Update frame dimensions and report any changes to |rendererEvents|.
private void updateFrameDimensionsAndReportEvents(VideoFrame frame) {
synchronized (layoutLock) {
if (isRenderingPaused) {
return;
}
if (!isFirstFrameRendered) {
isFirstFrameRendered = true;
if (rendererEvents != null) {
rendererEvents.onFirstFrameRendered();
}
}
if (rotatedFrameWidth != frame.getRotatedWidth()
|| rotatedFrameHeight != frame.getRotatedHeight()
|| frameRotation != frame.getRotation()) {
if (rendererEvents != null) {
rendererEvents.onFrameResolutionChanged(
frame.getBuffer().getWidth(), frame.getBuffer().getHeight(), frame.getRotation());
}
rotatedFrameWidth = frame.getRotatedWidth();
rotatedFrameHeight = frame.getRotatedHeight();
frameRotation = frame.getRotation();
}
}
}
}