David Liu

use server provided ice servers if any

@@ -27,7 +27,7 @@ class RTCEngine @@ -27,7 +27,7 @@ class RTCEngine
27 @Inject 27 @Inject
28 constructor( 28 constructor(
29 val client: RTCClient, 29 val client: RTCClient,
30 - pctFactory: PeerConnectionTransport.Factory, 30 + private val pctFactory: PeerConnectionTransport.Factory,
31 @Named(InjectionNames.DISPATCHER_IO) ioDispatcher: CoroutineDispatcher, 31 @Named(InjectionNames.DISPATCHER_IO) ioDispatcher: CoroutineDispatcher,
32 ) : RTCClient.Listener { 32 ) : RTCClient.Listener {
33 33
@@ -49,26 +49,13 @@ constructor( @@ -49,26 +49,13 @@ constructor(
49 49
50 private val publisherObserver = PublisherTransportObserver(this) 50 private val publisherObserver = PublisherTransportObserver(this)
51 private val subscriberObserver = SubscriberTransportObserver(this) 51 private val subscriberObserver = SubscriberTransportObserver(this)
52 - internal val publisher: PeerConnectionTransport  
53 - private val subscriber: PeerConnectionTransport  
54 -  
55 - private var privateDataChannel: DataChannel 52 + internal lateinit var publisher: PeerConnectionTransport
  53 + private lateinit var subscriber: PeerConnectionTransport
  54 + private lateinit var privateDataChannel: DataChannel
56 55
57 private val coroutineScope = CloseableCoroutineScope(SupervisorJob() + ioDispatcher) 56 private val coroutineScope = CloseableCoroutineScope(SupervisorJob() + ioDispatcher)
58 init { 57 init {
59 - val rtcConfig = PeerConnection.RTCConfiguration(RTCClient.DEFAULT_ICE_SERVERS).apply {  
60 - sdpSemantics = PeerConnection.SdpSemantics.UNIFIED_PLAN  
61 - continualGatheringPolicy = PeerConnection.ContinualGatheringPolicy.GATHER_CONTINUALLY  
62 - }  
63 -  
64 - publisher = pctFactory.create(rtcConfig, publisherObserver)  
65 - subscriber = pctFactory.create(rtcConfig, subscriberObserver)  
66 client.listener = this 58 client.listener = this
67 -  
68 - privateDataChannel = publisher.peerConnection.createDataChannel(  
69 - PRIVATE_DATA_CHANNEL_LABEL,  
70 - DataChannel.Init()  
71 - )  
72 } 59 }
73 60
74 fun join(url: String, token: String) { 61 fun join(url: String, token: String) {
@@ -166,6 +153,35 @@ constructor( @@ -166,6 +153,35 @@ constructor(
166 override fun onJoin(info: LivekitRtc.JoinResponse) { 153 override fun onJoin(info: LivekitRtc.JoinResponse) {
167 joinResponse = info 154 joinResponse = info
168 155
  156 + val iceServers = mutableListOf<PeerConnection.IceServer>()
  157 + for(serverInfo in info.iceServersList){
  158 + val username = serverInfo.username ?: ""
  159 + val credential = serverInfo.credential ?: ""
  160 + iceServers.add(
  161 + PeerConnection.IceServer
  162 + .builder(serverInfo.urlsList)
  163 + .setUsername(username)
  164 + .setPassword(credential)
  165 + .createIceServer()
  166 + )
  167 + }
  168 +
  169 + if(iceServers.isEmpty()){
  170 + iceServers.addAll(RTCClient.DEFAULT_ICE_SERVERS)
  171 + }
  172 +
  173 + val rtcConfig = PeerConnection.RTCConfiguration(iceServers).apply {
  174 + sdpSemantics = PeerConnection.SdpSemantics.UNIFIED_PLAN
  175 + continualGatheringPolicy = PeerConnection.ContinualGatheringPolicy.GATHER_CONTINUALLY
  176 + }
  177 +
  178 + publisher = pctFactory.create(rtcConfig, publisherObserver)
  179 + subscriber = pctFactory.create(rtcConfig, subscriberObserver)
  180 +
  181 + privateDataChannel = publisher.peerConnection.createDataChannel(
  182 + PRIVATE_DATA_CHANNEL_LABEL,
  183 + DataChannel.Init()
  184 + )
169 coroutineScope.launch { 185 coroutineScope.launch {
170 val sdpOffer = 186 val sdpOffer =
171 when (val outcome = publisher.peerConnection.createOffer(OFFER_CONSTRAINTS)) { 187 when (val outcome = publisher.peerConnection.createOffer(OFFER_CONSTRAINTS)) {