@@ -48,11 +48,6 @@ const SignalingType = {
4848 UA : 'chat-ua' ,
4949} ;
5050
51- const offerOptions = {
52- 'offerToReceiveAudio' : true ,
53- 'offerToReceiveVideo' : true ,
54- } ;
55-
5651const sysInfo = Utils . sysInfo ( ) ;
5752
5853/**
@@ -83,7 +78,6 @@ class P2PPeerConnectionChannel extends EventDispatcher {
8378 this . _publishingStreamTracks = new Map ( ) ; // Key is MediaStream's ID, value is an array of the ID of its MediaStreamTracks that haven't been acked.
8479 this . _publishedStreamTracks = new Map ( ) ; // Key is MediaStream's ID, value is an array of the ID of its MediaStreamTracks that haven't been removed.
8580 this . _isNegotiationNeeded = false ;
86- this . _negotiating = false ;
8781 this . _remoteSideSupportsRemoveStream = true ;
8882 this . _remoteSideSupportsPlanB = true ;
8983 this . _remoteSideSupportsUnifiedPlan = true ;
@@ -122,7 +116,10 @@ class P2PPeerConnectionChannel extends EventDispatcher {
122116 this . _sendStreamInfo ( stream ) ] ) . then ( ( ) => {
123117 return new Promise ( ( resolve , reject ) => {
124118 // Replace |addStream| with PeerConnection.addTrack when all browsers are ready.
125- this . _pc . addStream ( stream . mediaStream ) ;
119+ for ( const track of stream . mediaStream . getTracks ( ) ) {
120+ this . _pc . addTrack ( track , stream . mediaStream ) ;
121+ }
122+ this . _onNegotiationneeded ( ) ;
126123 this . _publishingStreams . push ( stream ) ;
127124 const trackIds = Array . from ( stream . mediaStream . getTracks ( ) ,
128125 ( track ) => track . id ) ;
@@ -132,9 +129,6 @@ class P2PPeerConnectionChannel extends EventDispatcher {
132129 resolve : resolve ,
133130 reject : reject ,
134131 } ) ;
135- if ( ! this . _dataChannels . has ( DataChannelLabel . MESSAGE ) ) {
136- this . _createDataChannel ( DataChannelLabel . MESSAGE ) ;
137- }
138132 } ) ;
139133 } ) ;
140134 }
@@ -264,9 +258,6 @@ class P2PPeerConnectionChannel extends EventDispatcher {
264258 case SignalingType . CLOSED :
265259 this . _chatClosedHandler ( message . data ) ;
266260 break ;
267- case SignalingType . NEGOTIATION_NEEDED :
268- this . _doNegotiate ( ) ;
269- break ;
270261 default :
271262 Logger . error ( 'Invalid signaling message received. Type: ' +
272263 message . type ) ;
@@ -538,12 +529,14 @@ class P2PPeerConnectionChannel extends EventDispatcher {
538529 }
539530
540531 _onNegotiationneeded ( ) {
532+ // This is intented to be executed when onnegotiationneeded event is fired.
533+ // However, onnegotiationneeded may fire mutiple times when more than one
534+ // track is added/removed. So we manually execute this function after
535+ // adding/removing track and creating data channel.
541536 Logger . debug ( 'On negotiation needed.' ) ;
542537
543- if ( this . _pc . signalingState === 'stable' && this . _negotiating === false ) {
544- this . _negotiating = true ;
538+ if ( this . _pc . signalingState === 'stable' ) {
545539 this . _doNegotiate ( ) ;
546- this . _isNegotiationNeeded = false ;
547540 } else {
548541 this . _isNegotiationNeeded = true ;
549542 }
@@ -667,9 +660,6 @@ class P2PPeerConnectionChannel extends EventDispatcher {
667660 this . _onRemoteTrackAdded . apply ( this , [ event ] ) ;
668661 } ;
669662 }
670- this . _pc . onnegotiationneeded = ( event ) => {
671- this . _onNegotiationneeded . apply ( this , [ event ] ) ;
672- } ;
673663 this . _pc . onicecandidate = ( event ) => {
674664 this . _onLocalIceCandidate . apply ( this , [ event ] ) ;
675665 } ;
@@ -709,6 +699,7 @@ class P2PPeerConnectionChannel extends EventDispatcher {
709699 }
710700
711701 _drainPendingStreams ( ) {
702+ let negotiationNeeded = false ;
712703 Logger . debug ( 'Draining pending streams.' ) ;
713704 if ( this . _pc && this . _pc . signalingState === 'stable' ) {
714705 Logger . debug ( 'Peer connection is ready for draining pending streams.' ) ;
@@ -721,7 +712,10 @@ class P2PPeerConnectionChannel extends EventDispatcher {
721712 if ( ! stream . mediaStream ) {
722713 continue ;
723714 }
724- this . _pc . addStream ( stream . mediaStream ) ;
715+ for ( const track of stream . mediaStream . getTracks ( ) ) {
716+ this . _pc . addTrack ( track , stream . mediaStream ) ;
717+ negotiationNeeded = true ;
718+ }
725719 Logger . debug ( 'Added stream to peer connection.' ) ;
726720 this . _publishingStreams . push ( stream ) ;
727721 }
@@ -731,13 +725,17 @@ class P2PPeerConnectionChannel extends EventDispatcher {
731725 continue ;
732726 }
733727 this . _pc . removeStream ( this . _pendingUnpublishStreams [ j ] . mediaStream ) ;
728+ negotiationNeeded = true ;
734729 this . _unpublishPromises . get (
735730 this . _pendingUnpublishStreams [ j ] . mediaStream . id ) . resolve ( ) ;
736731 this . _publishedStreams . delete ( this . _pendingUnpublishStreams [ j ] ) ;
737732 Logger . debug ( 'Remove stream.' ) ;
738733 }
739734 this . _pendingUnpublishStreams . length = 0 ;
740735 }
736+ if ( negotiationNeeded ) {
737+ this . _onNegotiationneeded ( ) ;
738+ }
741739 }
742740
743741 _drainPendingRemoteIceCandidates ( ) {
@@ -762,7 +760,7 @@ class P2PPeerConnectionChannel extends EventDispatcher {
762760 dc . send ( JSON . stringify ( this . _pendingMessages [ i ] ) ) ;
763761 }
764762 this . _pendingMessages . length = 0 ;
765- } else if ( this . _pc && ! dc ) {
763+ } else if ( this . _pc && ! dc ) {
766764 this . _createDataChannel ( DataChannelLabel . MESSAGE ) ;
767765 }
768766 }
@@ -822,11 +820,7 @@ class P2PPeerConnectionChannel extends EventDispatcher {
822820 }
823821
824822 _doNegotiate ( ) {
825- if ( this . _isCaller ) {
826- this . _createAndSendOffer ( ) ;
827- } else {
828- this . _sendSignalingMessage ( SignalingType . NEGOTIATION_NEEDED ) ;
829- }
823+ this . _createAndSendOffer ( ) ;
830824 }
831825
832826 _setCodecOrder ( sdp ) {
@@ -871,12 +865,14 @@ class P2PPeerConnectionChannel extends EventDispatcher {
871865 this . _isNegotiationNeeded = false ;
872866 this . _isCaller = true ;
873867 let localDesc ;
874- this . _pc . createOffer ( offerOptions ) . then ( ( desc ) => {
868+ this . _pc . createOffer ( ) . then ( ( desc ) => {
875869 desc . sdp = this . _setRtpReceiverOptions ( desc . sdp ) ;
876870 localDesc = desc ;
877- return this . _pc . setLocalDescription ( desc ) ;
878- } ) . then ( ( ) => {
879- return this . _sendSdp ( localDesc ) ;
871+ if ( this . _pc . signalingState === 'stable' ) {
872+ return this . _pc . setLocalDescription ( desc ) . then ( ( ) => {
873+ return this . _sendSdp ( localDesc ) ;
874+ } ) ;
875+ }
880876 } ) . catch ( ( e ) => {
881877 Logger . error ( e . message + ' Please check your codec settings.' ) ;
882878 const error = new ErrorModule . P2PError ( ErrorModule . errors . P2P_WEBRTC_SDP ,
@@ -893,6 +889,7 @@ class P2PPeerConnectionChannel extends EventDispatcher {
893889 this . _pc . createAnswer ( ) . then ( ( desc ) => {
894890 desc . sdp = this . _setRtpReceiverOptions ( desc . sdp ) ;
895891 localDesc = desc ;
892+ this . _logCurrentAndPendingLocalDescription ( ) ;
896893 return this . _pc . setLocalDescription ( desc ) ;
897894 } ) . then ( ( ) => {
898895 return this . _sendSdp ( localDesc ) ;
@@ -904,6 +901,10 @@ class P2PPeerConnectionChannel extends EventDispatcher {
904901 } ) ;
905902 }
906903
904+ _logCurrentAndPendingLocalDescription ( ) {
905+ Logger . info ( 'Current description: ' + this . _pc . currentLocalDescription ) ;
906+ Logger . info ( 'Pending description: ' + this . _pc . pendingLocalDescription ) ;
907+ }
907908
908909 _getAndDeleteTrackSourceInfo ( tracks ) {
909910 if ( tracks . length > 0 ) {
@@ -955,6 +956,7 @@ class P2PPeerConnectionChannel extends EventDispatcher {
955956 const dc = this . _pc . createDataChannel ( label ) ;
956957 this . _bindEventsToDataChannel ( dc ) ;
957958 this . _dataChannels . set ( DataChannelLabel . MESSAGE , dc ) ;
959+ this . _onNegotiationneeded ( ) ;
958960 }
959961
960962 _bindEventsToDataChannel ( dc ) {
0 commit comments