TextureViewRenderer.kt
14.1 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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
* Copyright 2015 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
package io.livekit.android.renderer
import android.annotation.SuppressLint
import android.content.Context
import android.content.res.Resources.NotFoundException
import android.graphics.Matrix
import android.graphics.SurfaceTexture
import android.os.Looper
import android.util.AttributeSet
import android.util.Log
import android.view.SurfaceHolder
import android.view.TextureView
import android.view.View
import io.livekit.android.room.track.video.ViewVisibility
import io.livekit.android.util.LKLog
import livekit.org.webrtc.EglBase
import livekit.org.webrtc.EglRenderer
import livekit.org.webrtc.GlRectDrawer
import livekit.org.webrtc.Logging
import livekit.org.webrtc.RendererCommon.GlDrawer
import livekit.org.webrtc.RendererCommon.RendererEvents
import livekit.org.webrtc.RendererCommon.ScalingType
import livekit.org.webrtc.RendererCommon.VideoLayoutMeasure
import livekit.org.webrtc.SurfaceEglRenderer
import livekit.org.webrtc.ThreadUtils
import livekit.org.webrtc.VideoFrame
import livekit.org.webrtc.VideoSink
import java.util.concurrent.CountDownLatch
/**
* Display the video stream on a TextureView.
*/
open class TextureViewRenderer :
TextureView,
SurfaceHolder.Callback,
TextureView.SurfaceTextureListener,
VideoSink,
RendererEvents,
ViewVisibility.Notifier {
// Cached resource name.
private val resourceName: String
private val videoLayoutMeasure = VideoLayoutMeasure()
private val eglRenderer: SurfaceEglRenderer
// Callback for reporting renderer events. Read-only after initialization so no lock required.
private var rendererEvents: RendererEvents? = null
// Accessed only on the main thread.
private var rotatedFrameWidth = 0
private var rotatedFrameHeight = 0
private var enableFixedSize = false
private var surfaceWidth = 0
private var surfaceHeight = 0
private var initialized = false
/**
* Standard View constructor. In order to render something, you must first call init().
*/
constructor(context: Context) : super(context) {
resourceName = getResourceName()
eglRenderer = SurfaceEglRenderer(resourceName)
surfaceTextureListener = this
}
/**
* Standard View constructor. In order to render something, you must first call init().
*/
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
resourceName = getResourceName()
eglRenderer = SurfaceEglRenderer(resourceName)
surfaceTextureListener = this
}
/**
* 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.
*/
/**
* Initialize this class, sharing resources with `sharedContext`. It is allowed to call init() to
* reinitialize the renderer after a previous init()/release() cycle.
*/
@JvmOverloads
fun init(
sharedContext: EglBase.Context?,
rendererEvents: RendererEvents?,
configAttributes: IntArray? = EglBase.CONFIG_PLAIN,
drawer: GlDrawer? = GlRectDrawer(),
) {
ThreadUtils.checkIsOnMainThread()
if (initialized) {
LKLog.w { "Reinitializing already initialized TextureViewRenderer." }
}
initialized = true
this.rendererEvents = rendererEvents
rotatedFrameWidth = 0
rotatedFrameHeight = 0
eglRenderer.init(sharedContext, this, configAttributes, drawer)
}
/**
* Block until any pending frame is returned and all GL resources released, even if an interrupt
* occurs. If an interrupt occurs during release(), the interrupt flag will be set. This function
* should be called before the Activity is destroyed and the EGLContext is still valid. If you
* don't call this function, the GL resources might leak.
*/
fun release() {
initialized = false
eglRenderer.release()
}
/**
* Register a callback to be invoked when a new video frame has been received.
*
* @param listener The callback to be invoked. The callback will be invoked on the render thread.
* It should be lightweight and must not call removeFrameListener.
* @param scale The scale of the Bitmap passed to the callback, or 0 if no Bitmap is
* required.
* @param drawerParam Custom drawer to use for this frame listener.
*/
fun addFrameListener(
listener: EglRenderer.FrameListener?,
scale: Float,
drawerParam: GlDrawer?,
) {
eglRenderer.addFrameListener(listener, scale, drawerParam)
}
/**
* Register a callback to be invoked when a new video frame has been received. This version uses
* the drawer of the EglRenderer that was passed in init.
*
* @param listener The callback to be invoked. The callback will be invoked on the render thread.
* It should be lightweight and must not call removeFrameListener.
* @param scale The scale of the Bitmap passed to the callback, or 0 if no Bitmap is
* required.
*/
fun addFrameListener(listener: EglRenderer.FrameListener?, scale: Float) {
eglRenderer.addFrameListener(listener, scale)
}
fun removeFrameListener(listener: EglRenderer.FrameListener?) {
eglRenderer.removeFrameListener(listener)
}
/**
* Enables fixed size for the surface. This provides better performance but might be buggy on some
* devices. By default this is turned off.
*/
fun setEnableHardwareScaler(enabled: Boolean) {
ThreadUtils.checkIsOnMainThread()
enableFixedSize = enabled
updateSurfaceSize()
}
/**
* Set if the video stream should be mirrored or not.
*/
fun setMirror(mirror: Boolean) {
eglRenderer.setMirror(mirror)
}
/**
* Set how the video will fill the allowed layout area.
*/
fun setScalingType(scalingType: ScalingType?) {
ThreadUtils.checkIsOnMainThread()
videoLayoutMeasure.setScalingType(scalingType)
requestLayout()
}
fun setScalingType(
scalingTypeMatchOrientation: ScalingType?,
scalingTypeMismatchOrientation: ScalingType?,
) {
ThreadUtils.checkIsOnMainThread()
videoLayoutMeasure.setScalingType(
scalingTypeMatchOrientation,
scalingTypeMismatchOrientation,
)
requestLayout()
}
/**
* Limit render framerate.
*
* @param fps Limit render framerate to this value, or use Float.POSITIVE_INFINITY to disable fps
* reduction.
*/
fun setFpsReduction(fps: Float) {
eglRenderer.setFpsReduction(fps)
}
fun disableFpsReduction() {
eglRenderer.disableFpsReduction()
}
fun pauseVideo() {
eglRenderer.pauseVideo()
}
// VideoSink interface.
@SuppressLint("LogNotTimber")
override fun onFrame(frame: VideoFrame) {
if (!initialized) {
Log.e("TextureViewRenderer", "Received frame when not initialized! You must call Room.initVideoRenderer(view) before using this view!")
}
eglRenderer.onFrame(frame)
}
// View layout interface.
override fun onMeasure(widthSpec: Int, heightSpec: Int) {
ThreadUtils.checkIsOnMainThread()
val size =
videoLayoutMeasure.measure(widthSpec, heightSpec, rotatedFrameWidth, rotatedFrameHeight)
setMeasuredDimension(size.x, size.y)
logD("onMeasure(). New size: " + size.x + "x" + size.y)
}
override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
ThreadUtils.checkIsOnMainThread()
eglRenderer.setLayoutAspectRatio((right - left) / (bottom - top).toFloat())
updateSurfaceSize()
}
private fun updateSurfaceSize() {
ThreadUtils.checkIsOnMainThread()
if (enableFixedSize && rotatedFrameWidth != 0 && rotatedFrameHeight != 0 && width != 0 && height != 0) {
val layoutAspectRatio = width / height.toFloat()
val frameAspectRatio = rotatedFrameWidth / rotatedFrameHeight.toFloat()
val drawnFrameWidth: Int
val drawnFrameHeight: Int
if (frameAspectRatio > layoutAspectRatio) {
drawnFrameWidth = (rotatedFrameHeight * layoutAspectRatio).toInt()
drawnFrameHeight = rotatedFrameHeight
} else {
drawnFrameWidth = rotatedFrameWidth
drawnFrameHeight = (rotatedFrameWidth / layoutAspectRatio).toInt()
}
// Aspect ratio of the drawn frame and the view is the same.
val width = Math.min(width, drawnFrameWidth)
val height = Math.min(height, drawnFrameHeight)
logD(
"updateSurfaceSize. Layout size: " + getWidth() + "x" + getHeight() + ", frame size: " +
rotatedFrameWidth + "x" + rotatedFrameHeight + ", requested surface size: " + width +
"x" + height + ", old surface size: " + surfaceWidth + "x" + surfaceHeight,
)
if (width != surfaceWidth || height != surfaceHeight) {
surfaceWidth = width
surfaceHeight = height
adjustAspectRatio(surfaceWidth, surfaceHeight)
}
} else {
surfaceHeight = 0
surfaceWidth = surfaceHeight
}
}
/**
* Sets the TextureView transform to preserve the aspect ratio of the video.
*/
private fun adjustAspectRatio(videoWidth: Int, videoHeight: Int) {
val viewWidth = width
val viewHeight = height
val aspectRatio = videoHeight.toDouble() / videoWidth
val newWidth: Int
val newHeight: Int
if (viewHeight > (viewWidth * aspectRatio).toInt()) {
// limited by narrow width; restrict height
newWidth = viewWidth
newHeight = (viewWidth * aspectRatio).toInt()
} else {
// limited by short height; restrict width
newWidth = (viewHeight / aspectRatio).toInt()
newHeight = viewHeight
}
val xoff = (viewWidth - newWidth) / 2
val yoff = (viewHeight - newHeight) / 2
logD(
"video=" + videoWidth + "x" + videoHeight + " view=" + viewWidth + "x" + viewHeight +
" newView=" + newWidth + "x" + newHeight + " off=" + xoff + "," + yoff,
)
val txform = Matrix()
getTransform(txform)
txform.setScale(newWidth.toFloat() / viewWidth, newHeight.toFloat() / viewHeight)
// txform.postRotate(10); // just for fun
txform.postTranslate(xoff.toFloat(), yoff.toFloat())
setTransform(txform)
}
// SurfaceHolder.Callback interface.
override fun surfaceCreated(holder: SurfaceHolder) {
ThreadUtils.checkIsOnMainThread()
surfaceHeight = 0
surfaceWidth = surfaceHeight
updateSurfaceSize()
}
override fun surfaceDestroyed(holder: SurfaceHolder) {}
override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {}
// TextureView.SurfaceTextureListener implementation
override fun onSurfaceTextureAvailable(surface: SurfaceTexture, i: Int, i1: Int) {
ThreadUtils.checkIsOnMainThread()
eglRenderer.createEglSurface(surfaceTexture)
surfaceHeight = 0
surfaceWidth = surfaceHeight
updateSurfaceSize()
}
override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture, width: Int, height: Int) {
ThreadUtils.checkIsOnMainThread()
logD("surfaceChanged: size: " + width + "x" + height)
}
override fun onSurfaceTextureDestroyed(surface: SurfaceTexture): Boolean {
ThreadUtils.checkIsOnMainThread()
val completionLatch = CountDownLatch(1)
eglRenderer.releaseEglSurface { completionLatch.countDown() }
ThreadUtils.awaitUninterruptibly(completionLatch)
return true
}
override fun onSurfaceTextureUpdated(surface: SurfaceTexture) {}
private fun getResourceName(): String {
return try {
resources.getResourceEntryName(id)
} catch (e: NotFoundException) {
""
}
}
/**
* Post a task to clear the SurfaceView to a transparent uniform color.
*/
fun clearImage() {
eglRenderer.clearImage()
}
override fun onFirstFrameRendered() {
if (rendererEvents != null) {
rendererEvents!!.onFirstFrameRendered()
}
}
override fun onFrameResolutionChanged(videoWidth: Int, videoHeight: Int, rotation: Int) {
if (rendererEvents != null) {
rendererEvents!!.onFrameResolutionChanged(videoWidth, videoHeight, rotation)
}
val rotatedWidth = if (rotation == 0 || rotation == 180) videoWidth else videoHeight
val rotatedHeight = if (rotation == 0 || rotation == 180) videoHeight else videoWidth
// run immediately if possible for ui thread tests
postOrRun {
rotatedFrameWidth = rotatedWidth
rotatedFrameHeight = rotatedHeight
updateSurfaceSize()
requestLayout()
}
}
private fun postOrRun(r: Runnable) {
if (Thread.currentThread() === Looper.getMainLooper().thread) {
r.run()
} else {
post(r)
}
}
private fun logD(string: String) {
Logging.d(TAG, "$resourceName: $string")
}
companion object {
private const val TAG = "TextureViewRenderer"
}
override var viewVisibility: ViewVisibility? = null
override fun onVisibilityChanged(changedView: View, visibility: Int) {
super.onVisibilityChanged(changedView, visibility)
viewVisibility?.recalculate()
}
}