I made a change in VideoTextureViewRenderer to enable removing the publisher view from 1 fragment and putting it in another #65
UrielFrankel
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The idea is to NOT call release when detached from window.
/*
*/
import android.content.Context
import android.content.res.Resources
import android.graphics.Point
import android.graphics.SurfaceTexture
import android.os.Handler
import android.os.Looper
import android.util.AttributeSet
import android.view.TextureView
import android.view.TextureView.SurfaceTextureListener
import org.webrtc.EglBase
import org.webrtc.EglRenderer
import org.webrtc.GlRectDrawer
import org.webrtc.RendererCommon.RendererEvents
import org.webrtc.RendererCommon.ScalingType
import org.webrtc.RendererCommon.VideoLayoutMeasure
import org.webrtc.ThreadUtils
import org.webrtc.VideoFrame
import org.webrtc.VideoSink
import java.util.concurrent.CountDownLatch
/**
Custom [TextureView] used to render local/incoming videos on the screen.
*/
public open class VideoTextureViewRenderer @jvmoverloads constructor(
context: Context,
attrs: AttributeSet? = null
) : TextureView(context, attrs), VideoSink, SurfaceTextureListener {
val TAG = "VideoTextureViewRenderer"
/**
*/
private val resourceName: String = getResourceName()
/**
*/
private val videoLayoutMeasure = VideoLayoutMeasure()
/**
*/
private val eglRenderer: EglRenderer = EglRenderer("hello_uriel")
/**
*/
private var rendererEvents: RendererEvents? = null
/**
*/
private val uiThreadHandler = Handler(Looper.getMainLooper())
/**
*/
private var isFirstFrameRendered = false
/**
*/
private var rotatedFrameWidth = 0
/**
*/
private var rotatedFrameHeight = 0
/**
*/
private var frameRotation = 0
init {
surfaceTextureListener = this
}
/**
*/
public fun setMirror(mirror: Boolean) {
eglRenderer.setMirror(mirror)
}
/**
*/
public fun setScalingType(scalingType: ScalingType?) {
ThreadUtils.checkIsOnMainThread()
videoLayoutMeasure.setScalingType(scalingType)
requestLayout()
}
/**
*/
public fun setScalingType(
scalingTypeMatchOrientation: ScalingType?,
scalingTypeMismatchOrientation: ScalingType?
) {
ThreadUtils.checkIsOnMainThread()
videoLayoutMeasure.setScalingType(scalingTypeMatchOrientation, scalingTypeMismatchOrientation)
requestLayout()
}
/**
*/
override fun onFrame(videoFrame: VideoFrame) {
eglRenderer.onFrame(videoFrame)
updateFrameData(videoFrame)
}
// View layout interface.
override fun onMeasure(widthSpec: Int, heightSpec: Int) {
ThreadUtils.checkIsOnMainThread()
val size: Point =
videoLayoutMeasure.measure(widthSpec, heightSpec, rotatedFrameWidth, rotatedFrameHeight)
setMeasuredDimension(size.x, size.y)
}
/**
Updates the frame data and notifies [rendererEvents] about the changes.
*/
private fun updateFrameData(videoFrame: VideoFrame) {
if (!isFirstFrameRendered) {
rendererEvents?.onFirstFrameRendered()
isFirstFrameRendered = true
}
if (videoFrame.rotatedWidth != rotatedFrameWidth ||
videoFrame.rotatedHeight != rotatedFrameHeight ||
videoFrame.rotation != frameRotation
) {
rotatedFrameWidth = videoFrame.rotatedWidth
rotatedFrameHeight = videoFrame.rotatedHeight
frameRotation = videoFrame.rotation
}
}
/**
*/
override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
eglRenderer.setLayoutAspectRatio((right - left) / (bottom.toFloat() - top))
}
/**
*/
public fun init(
sharedContext: EglBase.Context,
rendererEvents: RendererEvents
) {
ThreadUtils.checkIsOnMainThread()
this.rendererEvents = rendererEvents
eglRenderer.init(sharedContext, EglBase.CONFIG_PLAIN, GlRectDrawer())
}
/**
*/
override fun onSurfaceTextureAvailable(surfaceTexture: SurfaceTexture, width: Int, height: Int) {
eglRenderer.createEglSurface(surfaceTexture)
}
/**
*/
override fun onSurfaceTextureDestroyed(surfaceTexture: SurfaceTexture): Boolean {
Log.d(TAG, "onSurfaceTextureDestroyed() called with: surfaceTexture = $surfaceTexture")
val completionLatch = CountDownLatch(1)
eglRenderer.releaseEglSurface { completionLatch.countDown() }
ThreadUtils.awaitUninterruptibly(completionLatch)
return true
}
override fun onSurfaceTextureSizeChanged(
surfaceTexture: SurfaceTexture,
width: Int,
height: Int
) {
}
override fun onSurfaceTextureUpdated(surfaceTexture: SurfaceTexture) {
}
override fun onDetachedFromWindow() {
// eglRenderer.release()
super.onDetachedFromWindow()
}
private fun getResourceName(): String {
return try {
resources.getResourceEntryName(id) + ": "
} catch (e: Resources.NotFoundException) {
""
}
}
public fun release() {
eglRenderer.release()
}
}
Beta Was this translation helpful? Give feedback.
All reactions