Skip to content

Commit

Permalink
fix: some rendering problems when change render widget order
Browse files Browse the repository at this point in the history
  • Loading branch information
LichKing-2234 committed Dec 17, 2020
1 parent 2520e7a commit c737102
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 126 deletions.
152 changes: 81 additions & 71 deletions RtcSurfaceView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,92 +9,102 @@ import io.agora.rtc.video.VideoCanvas
import java.lang.ref.WeakReference

class RtcSurfaceView(
context: Context
context: Context
) : FrameLayout(context) {
private var surface: SurfaceView
private var canvas: VideoCanvas
private var channel: WeakReference<RtcChannel>? = null
private var surface: SurfaceView
private var canvas: VideoCanvas
private var isMediaOverlay = false
private var onTop = false
private var channel: WeakReference<RtcChannel>? = null

init {
try {
surface = RtcEngine.CreateRendererView(context)
} catch (e: UnsatisfiedLinkError) {
throw RuntimeException("Please init RtcEngine first!")
}
canvas = VideoCanvas(surface)
addView(surface)
init {
try {
surface = RtcEngine.CreateRendererView(context)
} catch (e: UnsatisfiedLinkError) {
throw RuntimeException("Please init RtcEngine first!")
}
canvas = VideoCanvas(surface)
addView(surface)
}

fun setZOrderMediaOverlay(isMediaOverlay: Boolean) {
try {
removeView(surface)
surface.setZOrderMediaOverlay(isMediaOverlay)
addView(surface)
} catch (e: Exception) {
e.printStackTrace()
}
fun setZOrderMediaOverlay(isMediaOverlay: Boolean) {
this.isMediaOverlay = isMediaOverlay
try {
removeView(surface)
surface.setZOrderMediaOverlay(isMediaOverlay)
addView(surface)
} catch (e: Exception) {
e.printStackTrace()
}
}

fun setZOrderOnTop(onTop: Boolean) {
try {
removeView(surface)
surface.setZOrderOnTop(onTop)
addView(surface)
} catch (e: Exception) {
e.printStackTrace()
}
fun setZOrderOnTop(onTop: Boolean) {
this.onTop = onTop
try {
removeView(surface)
surface.setZOrderOnTop(onTop)
addView(surface)
} catch (e: Exception) {
e.printStackTrace()
}
}

fun setData(engine: RtcEngine, channel: RtcChannel?, uid: Int) {
this.channel = if (channel != null) WeakReference(channel) else null
canvas.channelId = this.channel?.get()?.channelId()
canvas.uid = uid
setupVideoCanvas(engine)
}
fun setData(engine: RtcEngine, channel: RtcChannel?, uid: Int) {
this.channel = if (channel != null) WeakReference(channel) else null
canvas.channelId = this.channel?.get()?.channelId()
canvas.uid = uid
setupVideoCanvas(engine)
}

fun resetVideoCanvas(engine: RtcEngine) {
val canvas = VideoCanvas(null, canvas.renderMode, canvas.channelId, canvas.uid, canvas.mirrorMode)
if (canvas.uid == 0) {
engine.setupLocalVideo(canvas)
} else {
engine.setupRemoteVideo(canvas)
}
fun resetVideoCanvas(engine: RtcEngine) {
val canvas = VideoCanvas(null, canvas.renderMode, canvas.channelId, canvas.uid, canvas.mirrorMode)
if (canvas.uid == 0) {
engine.setupLocalVideo(canvas)
} else {
engine.setupRemoteVideo(canvas)
}
}

private fun setupVideoCanvas(engine: RtcEngine) {
if (canvas.uid == 0) {
engine.setupLocalVideo(canvas)
} else {
engine.setupRemoteVideo(canvas)
}
private fun setupVideoCanvas(engine: RtcEngine) {
removeAllViews()
surface = RtcEngine.CreateRendererView(context.applicationContext)
surface.setZOrderMediaOverlay(isMediaOverlay)
surface.setZOrderOnTop(onTop)
addView(surface)
canvas.view = surface
if (canvas.uid == 0) {
engine.setupLocalVideo(canvas)
} else {
engine.setupRemoteVideo(canvas)
}
}

fun setRenderMode(engine: RtcEngine, @Annotations.AgoraVideoRenderMode renderMode: Int) {
canvas.renderMode = renderMode
setupRenderMode(engine)
}
fun setRenderMode(engine: RtcEngine, @Annotations.AgoraVideoRenderMode renderMode: Int) {
canvas.renderMode = renderMode
setupRenderMode(engine)
}

fun setMirrorMode(engine: RtcEngine, @Annotations.AgoraVideoMirrorMode mirrorMode: Int) {
canvas.mirrorMode = mirrorMode
setupRenderMode(engine)
}
fun setMirrorMode(engine: RtcEngine, @Annotations.AgoraVideoMirrorMode mirrorMode: Int) {
canvas.mirrorMode = mirrorMode
setupRenderMode(engine)
}

private fun setupRenderMode(engine: RtcEngine) {
if (canvas.uid == 0) {
engine.setLocalRenderMode(canvas.renderMode, canvas.mirrorMode)
} else {
channel?.get()?.let {
it.setRemoteRenderMode(canvas.uid, canvas.renderMode, canvas.mirrorMode)
return@setupRenderMode
}
engine.setRemoteRenderMode(canvas.uid, canvas.renderMode, canvas.mirrorMode)
}
private fun setupRenderMode(engine: RtcEngine) {
if (canvas.uid == 0) {
engine.setLocalRenderMode(canvas.renderMode, canvas.mirrorMode)
} else {
channel?.get()?.let {
it.setRemoteRenderMode(canvas.uid, canvas.renderMode, canvas.mirrorMode)
return@setupRenderMode
}
engine.setRemoteRenderMode(canvas.uid, canvas.renderMode, canvas.mirrorMode)
}
}

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
val width: Int = MeasureSpec.getSize(widthMeasureSpec)
val height: Int = MeasureSpec.getSize(heightMeasureSpec)
surface.layout(0, 0, width, height)
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
val width: Int = MeasureSpec.getSize(widthMeasureSpec)
val height: Int = MeasureSpec.getSize(heightMeasureSpec)
surface.layout(0, 0, width, height)
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
}
}
114 changes: 59 additions & 55 deletions RtcTextureView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,72 +9,76 @@ import io.agora.rtc.video.VideoCanvas
import java.lang.ref.WeakReference

class RtcTextureView(
context: Context
context: Context
) : FrameLayout(context) {
private var texture: TextureView
private var canvas: VideoCanvas
private var channel: WeakReference<RtcChannel>? = null
private var texture: TextureView
private var canvas: VideoCanvas
private var channel: WeakReference<RtcChannel>? = null

init {
try {
texture = RtcEngine.CreateTextureView(context)
} catch (e: UnsatisfiedLinkError) {
throw RuntimeException("Please init RtcEngine first!")
}
canvas = VideoCanvas(texture)
addView(texture)
init {
try {
texture = RtcEngine.CreateTextureView(context)
} catch (e: UnsatisfiedLinkError) {
throw RuntimeException("Please init RtcEngine first!")
}
canvas = VideoCanvas(texture)
addView(texture)
}

fun setData(engine: RtcEngine, channel: RtcChannel?, uid: Int) {
this.channel = if (channel != null) WeakReference(channel) else null
canvas.channelId = this.channel?.get()?.channelId()
canvas.uid = uid
setupVideoCanvas(engine)
}
fun setData(engine: RtcEngine, channel: RtcChannel?, uid: Int) {
this.channel = if (channel != null) WeakReference(channel) else null
canvas.channelId = this.channel?.get()?.channelId()
canvas.uid = uid
setupVideoCanvas(engine)
}

fun resetVideoCanvas(engine: RtcEngine) {
val canvas = VideoCanvas(null, canvas.renderMode, canvas.channelId, canvas.uid, canvas.mirrorMode)
if (canvas.uid == 0) {
engine.setupLocalVideo(canvas)
} else {
engine.setupRemoteVideo(canvas)
}
fun resetVideoCanvas(engine: RtcEngine) {
val canvas = VideoCanvas(null, canvas.renderMode, canvas.channelId, canvas.uid, canvas.mirrorMode)
if (canvas.uid == 0) {
engine.setupLocalVideo(canvas)
} else {
engine.setupRemoteVideo(canvas)
}
}

private fun setupVideoCanvas(engine: RtcEngine) {
if (canvas.uid == 0) {
engine.setupLocalVideo(canvas)
} else {
engine.setupRemoteVideo(canvas)
}
private fun setupVideoCanvas(engine: RtcEngine) {
removeAllViews()
texture = RtcEngine.CreateTextureView(context.applicationContext)
addView(texture)
canvas.view = texture
if (canvas.uid == 0) {
engine.setupLocalVideo(canvas)
} else {
engine.setupRemoteVideo(canvas)
}
}

fun setRenderMode(engine: RtcEngine, @Annotations.AgoraVideoRenderMode renderMode: Int) {
canvas.renderMode = renderMode
setupRenderMode(engine)
}
fun setRenderMode(engine: RtcEngine, @Annotations.AgoraVideoRenderMode renderMode: Int) {
canvas.renderMode = renderMode
setupRenderMode(engine)
}

fun setMirrorMode(engine: RtcEngine, @Annotations.AgoraVideoMirrorMode mirrorMode: Int) {
canvas.mirrorMode = mirrorMode
setupRenderMode(engine)
}
fun setMirrorMode(engine: RtcEngine, @Annotations.AgoraVideoMirrorMode mirrorMode: Int) {
canvas.mirrorMode = mirrorMode
setupRenderMode(engine)
}

private fun setupRenderMode(engine: RtcEngine) {
if (canvas.uid == 0) {
engine.setLocalRenderMode(canvas.renderMode, canvas.mirrorMode)
} else {
channel?.get()?.let {
it.setRemoteRenderMode(canvas.uid, canvas.renderMode, canvas.mirrorMode)
return@setupRenderMode
}
engine.setRemoteRenderMode(canvas.uid, canvas.renderMode, canvas.mirrorMode)
}
private fun setupRenderMode(engine: RtcEngine) {
if (canvas.uid == 0) {
engine.setLocalRenderMode(canvas.renderMode, canvas.mirrorMode)
} else {
channel?.get()?.let {
it.setRemoteRenderMode(canvas.uid, canvas.renderMode, canvas.mirrorMode)
return@setupRenderMode
}
engine.setRemoteRenderMode(canvas.uid, canvas.renderMode, canvas.mirrorMode)
}
}

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
val width: Int = MeasureSpec.getSize(widthMeasureSpec)
val height: Int = MeasureSpec.getSize(heightMeasureSpec)
texture.layout(0, 0, width, height)
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
val width: Int = MeasureSpec.getSize(widthMeasureSpec)
val height: Int = MeasureSpec.getSize(heightMeasureSpec)
texture.layout(0, 0, width, height)
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
}
}

0 comments on commit c737102

Please sign in to comment.