Skip to content

Instantly share code, notes, and snippets.

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 ShwetaChauhan18/a2dce386ad587397b817f7adc1cd963a to your computer and use it in GitHub Desktop.
Save ShwetaChauhan18/a2dce386ad587397b817f7adc1cd963a to your computer and use it in GitHub Desktop.
private fun createVideoTrackFromCameraAndShowIt() {
val videoCapturer: VideoCapturer? = createVideoCapturer()
//Create a VideoSource instance
videoCapturer?.let {
surfaceTextureHelper = SurfaceTextureHelper.create("CaptureThread", rootEglBase!!.eglBaseContext)
videoSource = factory?.createVideoSource(videoCapturer.isScreencast)
videoCapturer.initialize(surfaceTextureHelper, this, videoSource?.capturerObserver)
}
localVideoTrack = factory?.createVideoTrack("100", videoSource)
//Create MediaConstraints - Will be useful for specifying video and audio constraints.
audioConstraints = MediaConstraints()
videoConstraints = MediaConstraints()
//create an AudioSource instance
audioSource = factory?.createAudioSource(audioConstraints)
localAudioTrack = factory?.createAudioTrack("101", audioSource)
videoCapturer?.startCapture(1024, 720, 30)
binding?.localGlSurfaceView?.visibility = View.VISIBLE
// And finally, with our VideoRenderer ready, we
// can add our renderer to the VideoTrack.
localVideoTrack?.addSink(binding?.localGlSurfaceView)
}
private fun createVideoCapturer(): VideoCapturer? {
return if (useCamera2()) {
createCameraCapturer(Camera2Enumerator(this))
} else {
createCameraCapturer(Camera1Enumerator(true))
}
}
private fun createCameraCapturer(enumerator: CameraEnumerator): VideoCapturer? {
val deviceNames = enumerator.deviceNames
for (deviceName in deviceNames) {
if (enumerator.isFrontFacing(deviceName)) {
val videoCapturer: VideoCapturer? = enumerator.createCapturer(deviceName, null)
if (videoCapturer != null) {
return videoCapturer
}
}
}
for (deviceName in deviceNames) {
if (!enumerator.isFrontFacing(deviceName)) {
val videoCapturer: VideoCapturer? = enumerator.createCapturer(deviceName, null)
if (videoCapturer != null) {
return videoCapturer
}
}
}
return null
}
private fun useCamera2(): Boolean {
return Camera2Enumerator.isSupported(this)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment