@@ -61,6 +61,8 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
6161 this . _sdpResolvers = [ ] ; // [{finish, resolve, reject}]
6262 this . _sdpResolveNum = 0 ;
6363 this . _remoteMediaStreams = new Map ( ) ; // Key is subscription ID, value is MediaStream.
64+
65+ this . _createPeerConnection ( ) ;
6466 }
6567
6668 /**
@@ -129,14 +131,29 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
129131 mid : t . mid ,
130132 } ;
131133 } ) ;
132- const publicationId =
133- await this . _signaling . sendSignalingMessage ( 'publish' , {
134- media : { tracks : trackOptions } ,
135- attributes : stream . attributes ,
136- transport : { id : this . _id , type : 'webrtc' } ,
137- } ) . id ;
134+ const publicationResp =
135+ await this . _signaling . sendSignalingMessage ( 'publish' , {
136+ media : { tracks : trackOptions } ,
137+ attributes : stream . attributes ,
138+ transport : { id : this . _id , type : 'webrtc' } ,
139+ } ) ;
140+ const publicationId = publicationResp . id ;
138141 this . _publishTransceivers . get ( internalId ) . id = publicationId ;
139142 this . _reverseIdMap . set ( publicationId , internalId ) ;
143+
144+ if ( this . _id && this . _id !== publicationResp . transportId ) {
145+ Logger . warning ( 'Server returns conflict ID: ' + publicationResp
146+ . transportId ) ;
147+ return ;
148+ }
149+ this . _id = publicationResp . transportId ;
150+
151+ const messageEvent = new MessageEvent ( 'id' , {
152+ message : publicationId ,
153+ origin : this . _remoteId ,
154+ } ) ;
155+ this . dispatchEvent ( messageEvent ) ;
156+
140157 await this . _signaling . sendSignalingMessage (
141158 'soac' , { id : this . _id , signaling : offer } ) ;
142159 return new Promise ( ( resolve , reject ) => {
@@ -223,7 +240,6 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
223240 }
224241 }
225242 const mediaOptions = { } ;
226- this . _createPeerConnection ( ) ;
227243 if ( stream . mediaStream . getAudioTracks ( ) . length > 0 && options . audio !==
228244 false && options . audio !== null ) {
229245 if ( stream . mediaStream . getAudioTracks ( ) . length > 1 ) {
@@ -373,20 +389,21 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
373389 } ) ;
374390 } ) . then ( ( data ) => {
375391 const publicationId = data . id ;
376- const messageEvent = new MessageEvent ( 'id' , {
377- message : publicationId ,
378- origin : this . _remoteId ,
379- } ) ;
380- this . dispatchEvent ( messageEvent ) ;
381-
382392 this . _publishTransceivers . get ( internalId ) . id = publicationId ;
383393 this . _reverseIdMap . set ( publicationId , internalId ) ;
384394
385395 if ( this . _id && this . _id !== data . transportId ) {
386396 Logger . warning ( 'Server returns conflict ID: ' + data . transportId ) ;
397+ return ;
387398 }
388399 this . _id = data . transportId ;
389400
401+ const messageEvent = new MessageEvent ( 'id' , {
402+ message : publicationId ,
403+ origin : this . _remoteId ,
404+ } ) ;
405+ this . dispatchEvent ( messageEvent ) ;
406+
390407 // Modify local SDP before sending
391408 if ( options ) {
392409 transceivers . forEach ( ( { type, transceiver, option} ) => {
@@ -511,7 +528,6 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
511528
512529 const offerOptions = { } ;
513530 const transceivers = [ ] ;
514- this . _createPeerConnection ( ) ;
515531 if ( typeof this . pc . addTransceiver === 'function' ) {
516532 // |direction| seems not working on Safari.
517533 if ( mediaOptions . audio ) {
@@ -577,19 +593,20 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
577593 } ) ;
578594 } ) . then ( ( data ) => {
579595 const subscriptionId = data . id ;
580- const messageEvent = new MessageEvent ( 'id' , {
581- message : subscriptionId ,
582- origin : this . _remoteId ,
583- } ) ;
584- this . dispatchEvent ( messageEvent ) ;
585-
586596 this . _subscribeTransceivers . get ( internalId ) . id = subscriptionId ;
587597 this . _reverseIdMap . set ( subscriptionId , internalId ) ;
588598 if ( this . _id && this . _id !== data . transportId ) {
589599 Logger . warning ( 'Server returns conflict ID: ' + data . transportId ) ;
600+ return ;
590601 }
591602 this . _id = data . transportId ;
592603
604+ const messageEvent = new MessageEvent ( 'id' , {
605+ message : subscriptionId ,
606+ origin : this . _remoteId ,
607+ } ) ;
608+ this . dispatchEvent ( messageEvent ) ;
609+
593610 // Modify local SDP before sending
594611 if ( options ) {
595612 transceivers . forEach ( ( { type, transceiver, option} ) => {
@@ -898,6 +915,8 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
898915
899916 _createPeerConnection ( ) {
900917 if ( this . pc ) {
918+ Logger . warning ( 'A PeerConnection was created. Cannot create again for ' +
919+ 'the same PeerConnectionChannel.' ) ;
901920 return ;
902921 }
903922
0 commit comments