Created
April 12, 2020 14:47
-
-
Save ShwetaChauhan18/a2dce386ad587397b817f7adc1cd963a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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