Skip to content

Instantly share code, notes, and snippets.

@skydoves
Last active January 27, 2023 05:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skydoves/373e6ca678a34833b7e8809fdcda404b to your computer and use it in GitHub Desktop.
Save skydoves/373e6ca678a34833b7e8809fdcda404b to your computer and use it in GitHub Desktop.
video_suource
// Copyright 2023 Stream.IO, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// getting front camera
private val videoCapturer: VideoCapturer by lazy { buildCameraCapturer() }
// we need it to initialize video capturer
private val surfaceTextureHelper = SurfaceTextureHelper.create(
"SurfaceTextureHelperThread",
peerConnectionFactory.eglBaseContext
)
private val videoSource by lazy {
peerConnectionFactory.makeVideoSource(videoCapturer.isScreencast).apply {
videoCapturer.initialize(surfaceTextureHelper, context, this.capturerObserver)
videoCapturer.startCapture(480, 720, 30)
}
}
private val localVideoTrack: VideoTrack by lazy {
peerConnectionFactory.makeVideoTrack(
source = videoSource,
trackId = "Video${UUID.randomUUID()}"
)
}
private fun buildCameraCapturer(): VideoCapturer {
val manager = cameraManager ?: throw RuntimeException("CameraManager was not initialized!")
val ids = manager.cameraIdList
var foundCamera = false
var cameraId = ""
for (id in ids) {
val characteristics = manager.getCameraCharacteristics(id)
val cameraLensFacing = characteristics.get(CameraCharacteristics.LENS_FACING)
if (cameraLensFacing == CameraMetadata.LENS_FACING_FRONT) {
foundCamera = true
cameraId = id
}
}
if (!foundCamera && ids.isNotEmpty()) {
cameraId = ids.first()
}
val camera2Capturer = Camera2Capturer(context, cameraId, null)
return camera2Capturer
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment