David Liu

create and send offer through peer connection transport

@@ -17,7 +17,7 @@ import javax.inject.Named @@ -17,7 +17,7 @@ import javax.inject.Named
17 /** 17 /**
18 * @suppress 18 * @suppress
19 */ 19 */
20 -class PeerConnectionTransport 20 +internal class PeerConnectionTransport
21 @AssistedInject 21 @AssistedInject
22 constructor( 22 constructor(
23 @Assisted config: PeerConnection.RTCConfiguration, 23 @Assisted config: PeerConnection.RTCConfiguration,
@@ -28,14 +28,14 @@ constructor( @@ -28,14 +28,14 @@ constructor(
28 connectionFactory: PeerConnectionFactory 28 connectionFactory: PeerConnectionFactory
29 ) { 29 ) {
30 private val coroutineScope = CoroutineScope(ioDispatcher + SupervisorJob()) 30 private val coroutineScope = CoroutineScope(ioDispatcher + SupervisorJob())
31 - val peerConnection: PeerConnection = connectionFactory.createPeerConnection( 31 + internal val peerConnection: PeerConnection = connectionFactory.createPeerConnection(
32 config, 32 config,
33 pcObserver 33 pcObserver
34 ) ?: throw IllegalStateException("peer connection creation failed?") 34 ) ?: throw IllegalStateException("peer connection creation failed?")
35 - val pendingCandidates = mutableListOf<IceCandidate>()  
36 - var restartingIce: Boolean = false 35 + private val pendingCandidates = mutableListOf<IceCandidate>()
  36 + private var restartingIce: Boolean = false
37 37
38 - var renegotiate = false 38 + private var renegotiate = false
39 39
40 interface Listener { 40 interface Listener {
41 fun onOffer(sd: SessionDescription) 41 fun onOffer(sd: SessionDescription)
@@ -97,11 +97,17 @@ constructor( @@ -97,11 +97,17 @@ constructor(
97 97
98 // actually negotiate 98 // actually negotiate
99 Timber.d { "starting to negotiate" } 99 Timber.d { "starting to negotiate" }
100 - val offer = peerConnection.createOffer(constraints)  
101 - if (offer is Either.Left) {  
102 - peerConnection.setLocalDescription(offer.value)  
103 - listener?.onOffer(offer.value) 100 + val sdpOffer = when (val outcome = peerConnection.createOffer(constraints)) {
  101 + is Either.Left -> outcome.value
  102 + is Either.Right -> {
  103 + Timber.d { "error creating offer: ${outcome.value}" }
  104 + return
  105 + }
104 } 106 }
  107 +
  108 + Timber.v { "sdp offer = $sdpOffer, description: ${sdpOffer.description}, type: ${sdpOffer.type}" }
  109 + peerConnection.setLocalDescription(sdpOffer)
  110 + listener?.onOffer(sdpOffer)
105 } 111 }
106 112
107 113
@@ -34,7 +34,7 @@ import kotlin.coroutines.suspendCoroutine @@ -34,7 +34,7 @@ import kotlin.coroutines.suspendCoroutine
34 @Singleton 34 @Singleton
35 class RTCEngine 35 class RTCEngine
36 @Inject 36 @Inject
37 -constructor( 37 +internal constructor(
38 val client: SignalClient, 38 val client: SignalClient,
39 private val pctFactory: PeerConnectionTransport.Factory, 39 private val pctFactory: PeerConnectionTransport.Factory,
40 @Named(InjectionNames.DISPATCHER_IO) ioDispatcher: CoroutineDispatcher, 40 @Named(InjectionNames.DISPATCHER_IO) ioDispatcher: CoroutineDispatcher,
@@ -269,7 +269,7 @@ constructor( @@ -269,7 +269,7 @@ constructor(
269 wsRetries = 0 269 wsRetries = 0
270 270
271 // trigger publisher reconnect 271 // trigger publisher reconnect
272 - subscriber.restartingIce = true 272 + subscriber.prepareForIceRestart()
273 // only restart publisher if it's needed 273 // only restart publisher if it's needed
274 if (hasPublished) { 274 if (hasPublished) {
275 publisher.createAndSendOffer( 275 publisher.createAndSendOffer(
@@ -294,24 +294,7 @@ constructor( @@ -294,24 +294,7 @@ constructor(
294 return 294 return
295 } 295 }
296 coroutineScope.launch { 296 coroutineScope.launch {
297 - val sdpOffer =  
298 - when (val outcome = publisher.peerConnection.createOffer(getPublisherOfferConstraints())) {  
299 - is Either.Left -> outcome.value  
300 - is Either.Right -> {  
301 - Timber.d { "error creating offer: ${outcome.value}" }  
302 - return@launch  
303 - }  
304 - }  
305 -  
306 - Timber.v { "sdp offer = $sdpOffer, description: ${sdpOffer.description}, type: ${sdpOffer.type}" }  
307 - when (val outcome = publisher.peerConnection.setLocalDescription(sdpOffer)) {  
308 - is Either.Right -> {  
309 - Timber.d { "error setting local description: ${outcome.value}" }  
310 - return@launch  
311 - }  
312 - }  
313 -  
314 - client.sendOffer(sdpOffer) 297 + publisher.createAndSendOffer(getPublisherOfferConstraints())
315 } 298 }
316 } 299 }
317 300