LocalVideoTrack.kt
4.9 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
package io.livekit.android.room.track
import android.content.Context
import io.livekit.android.util.LKLog
import org.webrtc.*
import java.util.*
/**
* A representation of a local video track (generally input coming from camera or screen).
*
* [startCapture] should be called before use.
*/
class LocalVideoTrack(
private var capturer: VideoCapturer,
private var source: VideoSource,
name: String,
var options: LocalVideoTrackOptions,
rtcTrack: org.webrtc.VideoTrack,
private val peerConnectionFactory: PeerConnectionFactory,
private val context: Context,
private val eglBase: EglBase,
) : VideoTrack(name, rtcTrack) {
override var rtcTrack: org.webrtc.VideoTrack = rtcTrack
internal set
/**
* Note: these dimensions are only requested params, and may differ
* from the actual capture format used by the camera.
*/
val dimensions: Dimensions
get() = Dimensions(options.captureParams.width, options.captureParams.height)
internal var transceiver: RtpTransceiver? = null
private val sender: RtpSender?
get() = transceiver?.sender
fun startCapture() {
capturer.startCapture(
options.captureParams.width,
options.captureParams.height,
options.captureParams.maxFps
)
}
override fun stop() {
capturer.stopCapture()
super.stop()
}
fun restartTrack(options: LocalVideoTrackOptions = LocalVideoTrackOptions()) {
val newTrack = createTrack(
peerConnectionFactory,
context,
name,
options,
eglBase
)
val oldCapturer = capturer
val oldSource = source
val oldRtcTrack = rtcTrack
oldCapturer.stopCapture()
oldCapturer.dispose()
oldSource.dispose()
// sender owns rtcTrack, so it'll take care of disposing it.
oldRtcTrack.setEnabled(false)
// migrate video sinks to the new track
for (sink in sinks) {
oldRtcTrack.removeSink(sink)
newTrack.addRenderer(sink)
}
capturer = newTrack.capturer
source = newTrack.source
rtcTrack = newTrack.rtcTrack
this.options = options
startCapture()
sender?.setTrack(newTrack.rtcTrack, true)
}
companion object {
internal fun createTrack(
peerConnectionFactory: PeerConnectionFactory,
context: Context,
name: String,
options: LocalVideoTrackOptions,
rootEglBase: EglBase,
): LocalVideoTrack {
val source = peerConnectionFactory.createVideoSource(options.isScreencast)
val capturer = createVideoCapturer(context, options.position) ?: TODO()
capturer.initialize(
SurfaceTextureHelper.create("VideoCaptureThread", rootEglBase.eglBaseContext),
context,
source.capturerObserver
)
val track = peerConnectionFactory.createVideoTrack(UUID.randomUUID().toString(), source)
return LocalVideoTrack(
capturer = capturer,
source = source,
options = options,
name = name,
rtcTrack = track,
peerConnectionFactory = peerConnectionFactory,
context = context,
eglBase = rootEglBase,
)
}
private fun createVideoCapturer(context: Context, position: CameraPosition): VideoCapturer? {
val videoCapturer: VideoCapturer? = if (Camera2Enumerator.isSupported(context)) {
createCameraCapturer(Camera2Enumerator(context), position)
} else {
createCameraCapturer(Camera1Enumerator(true), position)
}
if (videoCapturer == null) {
LKLog.d { "Failed to open camera" }
return null
}
return videoCapturer
}
private fun createCameraCapturer(enumerator: CameraEnumerator, position: CameraPosition): VideoCapturer? {
val deviceNames = enumerator.deviceNames
for (deviceName in deviceNames) {
if (enumerator.isFrontFacing(deviceName) && position == CameraPosition.FRONT) {
LKLog.v { "Creating front facing camera capturer." }
val videoCapturer = enumerator.createCapturer(deviceName, null)
if (videoCapturer != null) {
return videoCapturer
}
} else if (enumerator.isBackFacing(deviceName) && position == CameraPosition.BACK) {
LKLog.v { "Creating back facing camera capturer." }
val videoCapturer = enumerator.createCapturer(deviceName, null)
if (videoCapturer != null) {
return videoCapturer
}
}
}
return null
}
}
}