Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / main / java / com / vayunmathur / communicate / data / whatsapp / call / WhatsAppVideoCallSession.kt
WhatsAppVideoCallSession.kt102 lines · 3.9 KiBRaw
| 1 | package com.vayunmathur.communicate.data.whatsapp.call |
| 2 | |
| 3 | import android.content.Context |
| 4 | import org.webrtc.Camera2Enumerator |
| 5 | import org.webrtc.CameraVideoCapturer |
| 6 | import org.webrtc.MediaConstraints |
| 7 | import org.webrtc.PeerConnectionFactory |
| 8 | import org.webrtc.SurfaceTextureHelper |
| 9 | import org.webrtc.SurfaceViewRenderer |
| 10 | import org.webrtc.VideoSource |
| 11 | import org.webrtc.VideoTrack |
| 12 | |
| 13 | /** |
| 14 | * Video extension of [WhatsAppCallSession] (Phase E / Feature 4). Adds a camera |
| 15 | * `VideoSource`/`VideoTrack` (WebRTC `Camera2Enumerator`), local/remote `SurfaceViewRenderer` |
| 16 | * rendering off the shared `EglBase`, camera switch, and `OfferToReceiveVideo=true`. |
| 17 | * |
| 18 | * Renderers are owned by the UI ([WhatsAppCallScreen]); it calls [attachRenderers] once the |
| 19 | * surfaces are laid out. Everything else (SDP/ICE, mute, speaker) is inherited from the base. |
| 20 | */ |
| 21 | class WhatsAppVideoCallSession(context: Context) : WhatsAppCallSession(context) { |
| 22 | |
| 23 | private var videoCapturer: CameraVideoCapturer? = null |
| 24 | private var videoSource: VideoSource? = null |
| 25 | private var localVideoTrack: VideoTrack? = null |
| 26 | private var surfaceHelper: SurfaceTextureHelper? = null |
| 27 | private var localRenderer: SurfaceViewRenderer? = null |
| 28 | private var remoteRenderer: SurfaceViewRenderer? = null |
| 29 | private var remoteVideoTrack: VideoTrack? = null |
| 30 | |
| 31 | private companion object { |
| 32 | private const val CAPTURE_WIDTH = 1280 |
| 33 | private const val CAPTURE_HEIGHT = 720 |
| 34 | private const val CAPTURE_FPS = 30 |
| 35 | } |
| 36 | |
| 37 | override fun offerConstraints(): MediaConstraints = MediaConstraints().apply { |
| 38 | mandatory.add(MediaConstraints.KeyValuePair("OfferToReceiveAudio", "true")) |
| 39 | mandatory.add(MediaConstraints.KeyValuePair("OfferToReceiveVideo", "true")) |
| 40 | } |
| 41 | |
| 42 | override fun onPeerConnectionCreated(f: PeerConnectionFactory) { |
| 43 | val capturer = createCameraCapturer() ?: return |
| 44 | videoCapturer = capturer |
| 45 | val helper = SurfaceTextureHelper.create("wa_capture", eglBase.eglBaseContext) |
| 46 | surfaceHelper = helper |
| 47 | val source = f.createVideoSource(false) |
| 48 | videoSource = source |
| 49 | capturer.initialize(helper, appContext, source.capturerObserver) |
| 50 | |
| 51 | runCatching { capturer.startCapture(CAPTURE_WIDTH, CAPTURE_HEIGHT, CAPTURE_FPS) } |
| 52 | val track = f.createVideoTrack("wa_video", source) |
| 53 | localVideoTrack = track |
| 54 | peerConnection?.addTrack(track, listOf("wa_stream")) |
| 55 | } |
| 56 | |
| 57 | /** Attach the UI renderers; also renders the local track immediately. */ |
| 58 | fun attachRenderers(local: SurfaceViewRenderer?, remote: SurfaceViewRenderer?) { |
| 59 | localRenderer = local |
| 60 | remoteRenderer = remote |
| 61 | runCatching { |
| 62 | local?.init(eglBase.eglBaseContext, null) |
| 63 | remote?.init(eglBase.eglBaseContext, null) |
| 64 | } |
| 65 | local?.let { localVideoTrack?.addSink(it) } |
| 66 | remoteVideoTrack?.let { t -> remote?.let { t.addSink(it) } } |
| 67 | } |
| 68 | |
| 69 | fun switchCamera() { |
| 70 | runCatching { videoCapturer?.switchCamera(null) } |
| 71 | } |
| 72 | |
| 73 | fun setVideoEnabled(enabled: Boolean) { |
| 74 | localVideoTrack?.setEnabled(enabled) |
| 75 | } |
| 76 | |
| 77 | private fun createCameraCapturer(): CameraVideoCapturer? { |
| 78 | val enumerator = Camera2Enumerator(appContext) |
| 79 | val names = enumerator.deviceNames |
| 80 | // Prefer the front camera for calls. |
| 81 | names.firstOrNull { enumerator.isFrontFacing(it) }?.let { |
| 82 | return enumerator.createCapturer(it, null) |
| 83 | } |
| 84 | names.firstOrNull()?.let { return enumerator.createCapturer(it, null) } |
| 85 | return null |
| 86 | } |
| 87 | |
| 88 | override fun close() { |
| 89 | runCatching { videoCapturer?.stopCapture() } |
| 90 | runCatching { videoCapturer?.dispose() } |
| 91 | runCatching { videoSource?.dispose() } |
| 92 | runCatching { surfaceHelper?.dispose() } |
| 93 | runCatching { localRenderer?.release() } |
| 94 | runCatching { remoteRenderer?.release() } |
| 95 | videoCapturer = null |
| 96 | videoSource = null |
| 97 | localVideoTrack = null |
| 98 | remoteVideoTrack = null |
| 99 | surfaceHelper = null |
| 100 | super.close() |
| 101 | } |
| 102 | } |