LocalVideoTrack.kt
6.6 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
package io.livekit.android.room.track
import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.hardware.camera2.CameraManager
import androidx.core.content.ContextCompat
import io.livekit.android.room.track.video.Camera1CapturerWithSize
import io.livekit.android.room.track.video.Camera2CapturerWithSize
import io.livekit.android.room.track.video.VideoCapturerWithSize
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.
*/
open 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
val dimensions: Dimensions
get() {
(capturer as? VideoCapturerWithSize)?.let { capturerWithSize ->
val size = capturerWithSize.findCaptureFormat(
options.captureParams.width,
options.captureParams.height
)
return Dimensions(size.width, size.height)
}
return Dimensions(options.captureParams.width, options.captureParams.height)
}
internal var transceiver: RtpTransceiver? = null
private val sender: RtpSender?
get() = transceiver?.sender
open 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 {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) !=
PackageManager.PERMISSION_GRANTED
) {
throw SecurityException("Camera permissions are required to create a camera video track.")
}
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(context, Camera2Enumerator(context), position)
} else {
createCameraCapturer(context, Camera1Enumerator(true), position)
}
if (videoCapturer == null) {
LKLog.d { "Failed to open camera" }
return null
}
return videoCapturer
}
private fun createCameraCapturer(
context: Context,
enumerator: CameraEnumerator,
position: CameraPosition
): VideoCapturer? {
val deviceNames = enumerator.deviceNames
var targetDeviceName: String? = null
var targetVideoCapturer: VideoCapturer? = null
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) {
targetDeviceName = deviceName
targetVideoCapturer = videoCapturer
break
}
} else if (enumerator.isBackFacing(deviceName) && position == CameraPosition.BACK) {
LKLog.v { "Creating back facing camera capturer." }
val videoCapturer = enumerator.createCapturer(deviceName, null)
if (videoCapturer != null) {
targetDeviceName = deviceName
targetVideoCapturer = videoCapturer
break
}
}
}
if (targetVideoCapturer is Camera1Capturer) {
return Camera1CapturerWithSize(targetVideoCapturer, targetDeviceName)
}
if (targetVideoCapturer is Camera2Capturer) {
return Camera2CapturerWithSize(
targetVideoCapturer,
context.getSystemService(Context.CAMERA_SERVICE) as CameraManager,
targetDeviceName
)
}
return null
}
}
}