PeerConnectionTransport.kt
1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package io.livekit.android.room
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import io.livekit.android.room.util.CoroutineSdpObserver
import io.livekit.android.util.Either
import org.webrtc.IceCandidate
import org.webrtc.PeerConnection
import org.webrtc.PeerConnectionFactory
import org.webrtc.SessionDescription
/**
* @suppress
*/
class PeerConnectionTransport
@AssistedInject
constructor(
@Assisted config: PeerConnection.RTCConfiguration,
@Assisted listener: PeerConnection.Observer,
connectionFactory: PeerConnectionFactory
) {
val peerConnection: PeerConnection = connectionFactory.createPeerConnection(
config,
RTCEngine.CONN_CONSTRAINTS,
listener
) ?: throw IllegalStateException("peer connection creation failed?")
val pendingCandidates = mutableListOf<IceCandidate>()
fun addIceCandidate(candidate: IceCandidate) {
if (peerConnection.remoteDescription != null) {
peerConnection.addIceCandidate(candidate)
} else {
pendingCandidates.add(candidate)
}
}
suspend fun setRemoteDescription(sd: SessionDescription): Either<Unit, String?> {
val observer = object : CoroutineSdpObserver() {
override fun onSetSuccess() {
pendingCandidates.forEach { pending ->
peerConnection.addIceCandidate(pending)
}
pendingCandidates.clear()
super.onSetSuccess()
}
}
peerConnection.setRemoteDescription(observer, sd)
return observer.awaitSet()
}
fun close() {
peerConnection.close()
}
@AssistedFactory
interface Factory {
fun create(
config: PeerConnection.RTCConfiguration,
listener: PeerConnection.Observer
): PeerConnectionTransport
}
}