-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRtcTextureView.kt
More file actions
85 lines (75 loc) · 2.53 KB
/
RtcTextureView.kt
File metadata and controls
85 lines (75 loc) · 2.53 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
package io.agora.rtc.base
import android.content.Context
import android.view.TextureView
import android.widget.FrameLayout
import io.agora.rtc.RtcChannel
import io.agora.rtc.RtcEngine
import io.agora.rtc.video.VideoCanvas
import java.lang.ref.WeakReference
class RtcTextureView(
context: Context
) : FrameLayout(context) {
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)
}
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)
}
}
private fun setupVideoCanvas(engine: RtcEngine) {
removeAllViews()
texture = RtcEngine.CreateTextureView(context.applicationContext)
addView(texture)
texture.layout(0, 0, width, height)
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 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)
}
}
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)
}
}