LocalVideoTrack.kt
9.4 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
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 dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import io.livekit.android.room.DefaultsManager
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
@AssistedInject
constructor(
@Assisted private var capturer: VideoCapturer,
@Assisted private var source: VideoSource,
@Assisted name: String,
@Assisted var options: LocalVideoTrackOptions,
@Assisted rtcTrack: org.webrtc.VideoTrack,
private val peerConnectionFactory: PeerConnectionFactory,
private val context: Context,
private val eglBase: EglBase,
private val defaultsManager: DefaultsManager,
private val trackFactory: Factory,
) : 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 setDeviceId(deviceId: String) {
restartTrack(options.copy(deviceId = deviceId))
}
fun restartTrack(options: LocalVideoTrackOptions = defaultsManager.videoTrackCaptureDefaults.copy()) {
val newTrack = createTrack(
peerConnectionFactory,
context,
name,
options,
eglBase,
trackFactory
)
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)
}
@AssistedFactory
interface Factory {
fun create(
capturer: VideoCapturer,
source: VideoSource,
name: String,
options: LocalVideoTrackOptions,
rtcTrack: org.webrtc.VideoTrack,
): LocalVideoTrack
}
companion object {
internal fun createTrack(
peerConnectionFactory: PeerConnectionFactory,
context: Context,
name: String,
capturer: VideoCapturer,
options: LocalVideoTrackOptions = LocalVideoTrackOptions(),
rootEglBase: EglBase,
trackFactory: Factory
): LocalVideoTrack {
val source = peerConnectionFactory.createVideoSource(false)
capturer.initialize(
SurfaceTextureHelper.create("VideoCaptureThread", rootEglBase.eglBaseContext),
context,
source.capturerObserver
)
val track = peerConnectionFactory.createVideoTrack(UUID.randomUUID().toString(), source)
return trackFactory.create(
capturer = capturer,
source = source,
options = options,
name = name,
rtcTrack = track
)
}
internal fun createTrack(
peerConnectionFactory: PeerConnectionFactory,
context: Context,
name: String,
options: LocalVideoTrackOptions,
rootEglBase: EglBase,
trackFactory: Factory
): 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, newOptions) = createVideoCapturer(context, options) ?: TODO()
capturer.initialize(
SurfaceTextureHelper.create("VideoCaptureThread", rootEglBase.eglBaseContext),
context,
source.capturerObserver
)
val track = peerConnectionFactory.createVideoTrack(UUID.randomUUID().toString(), source)
return trackFactory.create(
capturer = capturer,
source = source,
options = newOptions,
name = name,
rtcTrack = track
)
}
private fun createVideoCapturer(
context: Context,
options: LocalVideoTrackOptions
): Pair<VideoCapturer, LocalVideoTrackOptions>? {
val pair = if (Camera2Enumerator.isSupported(context)) {
createCameraCapturer(context, Camera2Enumerator(context), options)
} else {
createCameraCapturer(context, Camera1Enumerator(true), options)
}
if (pair == null) {
LKLog.d { "Failed to open camera" }
return null
}
return pair
}
private fun createCameraCapturer(
context: Context,
enumerator: CameraEnumerator,
options: LocalVideoTrackOptions
): Pair<VideoCapturerWithSize, LocalVideoTrackOptions>? {
val deviceNames = enumerator.deviceNames
var targetDeviceName: String? = null
var targetVideoCapturer: VideoCapturer? = null
for (deviceName in deviceNames) {
if ((options.deviceId != null && deviceName == options.deviceId)
|| (enumerator.isFrontFacing(deviceName) && options.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 ((options.deviceId != null && deviceName == options.deviceId)
|| (enumerator.isBackFacing(deviceName) && options.position == CameraPosition.BACK)
) {
LKLog.v { "Creating back facing camera capturer." }
val videoCapturer = enumerator.createCapturer(deviceName, null)
if (videoCapturer != null) {
targetDeviceName = deviceName
targetVideoCapturer = videoCapturer
break
}
}
}
// back fill any missing information
val newOptions = options.copy(
deviceId = targetDeviceName,
position = enumerator.getCameraPosition(targetDeviceName!!)
)
if (targetVideoCapturer is Camera1Capturer) {
// Cache supported capture formats ahead of time to avoid future camera locks.
Camera1Helper.getSupportedFormats(Camera1Helper.getCameraId(newOptions.deviceId))
return Pair(
Camera1CapturerWithSize(targetVideoCapturer, targetDeviceName),
newOptions
)
}
if (targetVideoCapturer is Camera2Capturer) {
return Pair(
Camera2CapturerWithSize(
targetVideoCapturer,
context.getSystemService(Context.CAMERA_SERVICE) as CameraManager,
targetDeviceName
),
newOptions
)
}
return null
}
fun CameraEnumerator.getCameraPosition(deviceName: String): CameraPosition? {
if (isBackFacing(deviceName)) {
return CameraPosition.BACK
} else if (isFrontFacing(deviceName)) {
return CameraPosition.FRONT
}
return null
}
}
}