|
| 1 | +package com.piasy.kmp.webrtc.web |
| 2 | + |
| 3 | +import com.piasy.kmp.webrtc.* |
| 4 | +import com.piasy.kmp.webrtc.data.IceCandidate |
| 5 | +import com.piasy.kmp.webrtc.data.RtcStatsReport |
| 6 | +import com.piasy.kmp.webrtc.data.SessionDescription |
| 7 | +import kotlinx.browser.document |
| 8 | +import kotlinx.browser.window |
| 9 | +import org.w3c.dom.HTMLButtonElement |
| 10 | +import org.w3c.dom.events.Event |
| 11 | + |
| 12 | +var isLoopback = false |
| 13 | +var loopback: HTMLButtonElement? = null |
| 14 | + |
| 15 | +var pcClientFactory: PeerConnectionClientFactory? = null |
| 16 | +// on web, we can't disable encryption, so we need two pc clients. |
| 17 | +var localPcClient: PeerConnectionClient? = null |
| 18 | +var remotePcClient: PeerConnectionClient? = null |
| 19 | + |
| 20 | +fun main() { |
| 21 | + window.onload = { |
| 22 | + loopback = document.getElementById("loopbackButton") as HTMLButtonElement |
| 23 | + loopback?.addEventListener("click", ::handleLoopbackClick) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +fun handleLoopbackClick(event: Event) { |
| 28 | + isLoopback = !isLoopback |
| 29 | + loopback?.innerHTML = if (isLoopback) "Stop" else "Loopback" |
| 30 | + if (isLoopback) { |
| 31 | + startLoopback() |
| 32 | + } else { |
| 33 | + stopLoopback() |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +fun startLoopback() { |
| 38 | + // 1. initialize |
| 39 | + initializeWebRTC(null, "", true) |
| 40 | + |
| 41 | + // 2. create PcClientFactory |
| 42 | + val privateConfig = JsPrivateConfig(js("createBlackStream")()) |
| 43 | + val config = PeerConnectionClientFactory.Config( |
| 44 | + PeerConnectionClientFactory.VIDEO_CAPTURE_IMPL_SYSTEM_CAMERA, 1280, 720, 30, |
| 45 | + PeerConnectionClientFactory.CAMERA_FACE_FRONT, privateConfig, |
| 46 | + ) |
| 47 | + pcClientFactory = createPeerConnectionClientFactory(config) { code, msg -> |
| 48 | + println("PeerConnectionClientFactory error: $code, $msg") |
| 49 | + } |
| 50 | + |
| 51 | + // 3. create local tracks |
| 52 | + pcClientFactory?.createLocalTracks() |
| 53 | + |
| 54 | + // 4. add local preview & start camera capture |
| 55 | + pcClientFactory?.addLocalTrackRenderer(document.getElementById("localVideo")!!) |
| 56 | + pcClientFactory?.startVideoCapture() |
| 57 | + |
| 58 | + // 5. create PcClient |
| 59 | + localPcClient = pcClientFactory?.createPeerConnectionClient( |
| 60 | + "test_local", PeerConnectionClientFactory.DIR_SEND_ONLY, true, |
| 61 | + 800_000, 30, localPcClientCallback |
| 62 | + ) |
| 63 | + remotePcClient = pcClientFactory?.createPeerConnectionClient( |
| 64 | + "test_remote", PeerConnectionClientFactory.DIR_RECV_ONLY, true, |
| 65 | + 800_000, 30, remotePcClientCallback |
| 66 | + ) |
| 67 | + |
| 68 | + // 6. create pc |
| 69 | + localPcClient?.createPeerConnection(emptyList()) |
| 70 | + remotePcClient?.createPeerConnection(emptyList()) |
| 71 | + |
| 72 | + // 7. create offer |
| 73 | + localPcClient?.createOffer() |
| 74 | +} |
| 75 | + |
| 76 | +val localPcClientCallback = object : PeerConnectionClientCallback { |
| 77 | + override fun onLocalDescription(peerUid: String, sdp: SessionDescription) { |
| 78 | + // 8.1. send offer to remote |
| 79 | + remotePcClient?.setRemoteDescription(sdp) |
| 80 | + remotePcClient?.createAnswer() |
| 81 | + } |
| 82 | + |
| 83 | + override fun onIceCandidate(peerUid: String, candidate: IceCandidate) { |
| 84 | + // 9. send ice candidate to remote, get ice candidate from remote, add ice candidate |
| 85 | + remotePcClient?.addIceCandidate(candidate) |
| 86 | + } |
| 87 | + |
| 88 | + override fun onIceConnected(peerUid: String) { |
| 89 | + } |
| 90 | + |
| 91 | + override fun onPreferCodecs(peerUid: String, sdp: String): String { |
| 92 | + return sdp |
| 93 | + } |
| 94 | + |
| 95 | + override fun onIceCandidatesRemoved(peerUid: String, candidates: List<IceCandidate>) { |
| 96 | + } |
| 97 | + |
| 98 | + override fun onPeerConnectionStatsReady(peerUid: String, report: RtcStatsReport) { |
| 99 | + } |
| 100 | + |
| 101 | + override fun onIceDisconnected(peerUid: String) { |
| 102 | + } |
| 103 | + |
| 104 | + override fun onError(peerUid: String, code: Int) { |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +val remotePcClientCallback = object : PeerConnectionClientCallback { |
| 109 | + override fun onLocalDescription(peerUid: String, sdp: SessionDescription) { |
| 110 | + // 8.2. get answer from remote, and set answer |
| 111 | + localPcClient?.setRemoteDescription(sdp); |
| 112 | + } |
| 113 | + |
| 114 | + override fun onIceCandidate(peerUid: String, candidate: IceCandidate) { |
| 115 | + // 9. send ice candidate to remote, get ice candidate from remote, add ice candidate |
| 116 | + localPcClient?.addIceCandidate(candidate); |
| 117 | + } |
| 118 | + |
| 119 | + override fun onIceConnected(peerUid: String) { |
| 120 | + // 10. on ice connected, add renderer for remote stream |
| 121 | + remotePcClient?.addRemoteTrackRenderer(document.getElementById("remoteVideo")!!) |
| 122 | + } |
| 123 | + |
| 124 | + override fun onPreferCodecs(peerUid: String, sdp: String): String { |
| 125 | + return sdp |
| 126 | + } |
| 127 | + |
| 128 | + override fun onIceCandidatesRemoved(peerUid: String, candidates: List<IceCandidate>) { |
| 129 | + } |
| 130 | + |
| 131 | + override fun onPeerConnectionStatsReady(peerUid: String, report: RtcStatsReport) { |
| 132 | + } |
| 133 | + |
| 134 | + override fun onIceDisconnected(peerUid: String) { |
| 135 | + } |
| 136 | + |
| 137 | + override fun onError(peerUid: String, code: Int) { |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +fun stopLoopback() { |
| 142 | + pcClientFactory?.stopVideoCapture() |
| 143 | + localPcClient?.close() |
| 144 | + remotePcClient?.close() |
| 145 | + pcClientFactory?.destroyPeerConnectionFactory() |
| 146 | + pcClientFactory = null |
| 147 | + localPcClient = null |
| 148 | + remotePcClient = null |
| 149 | +} |
0 commit comments