@@ -121,10 +121,7 @@ class P2PPeerConnectionChannel extends EventDispatcher {
121
121
}
122
122
return this . _sendStreamInfo ( stream ) . then ( ( ) => {
123
123
return new Promise ( ( resolve , reject ) => {
124
- // Replace |addStream| with PeerConnection.addTrack when all browsers are ready.
125
- for ( const track of stream . mediaStream . getTracks ( ) ) {
126
- this . _pc . addTrack ( track , stream . mediaStream ) ;
127
- }
124
+ this . _addStream ( stream . mediaStream ) ;
128
125
this . _publishingStreams . push ( stream ) ;
129
126
const trackIds = Array . from ( stream . mediaStream . getTracks ( ) ,
130
127
( track ) => track . id ) ;
@@ -214,6 +211,18 @@ class P2PPeerConnectionChannel extends EventDispatcher {
214
211
} ) ;
215
212
}
216
213
214
+ /**
215
+ * @function _addStream
216
+ * @desc Create RTCRtpSenders for all tracks in the stream.
217
+ * @private
218
+ */
219
+ _addStream ( stream ) {
220
+ for ( const track of stream . getTracks ( ) ) {
221
+ this . _pc . addTransceiver (
222
+ track , { direction : 'sendonly' , streams : [ stream ] } ) ;
223
+ }
224
+ }
225
+
217
226
/**
218
227
* @function onMessage
219
228
* @desc This method is called by P2PClient when there is new signaling message arrived.
@@ -688,9 +697,7 @@ class P2PPeerConnectionChannel extends EventDispatcher {
688
697
if ( ! stream . mediaStream ) {
689
698
continue ;
690
699
}
691
- for ( const track of stream . mediaStream . getTracks ( ) ) {
692
- this . _pc . addTrack ( track , stream . mediaStream ) ;
693
- }
700
+ this . _addStream ( stream . mediaStream ) ;
694
701
Logger . debug ( 'Added stream to peer connection.' ) ;
695
702
this . _publishingStreams . push ( stream ) ;
696
703
}
@@ -699,12 +706,16 @@ class P2PPeerConnectionChannel extends EventDispatcher {
699
706
if ( ! stream . stream ) {
700
707
continue ;
701
708
}
702
- if ( typeof this . _pc . getSenders === 'function' &&
709
+ if ( typeof this . _pc . getTransceivers === 'function' &&
703
710
typeof this . _pc . removeTrack === 'function' ) {
704
- for ( const sender of this . _pc . getSenders ( ) ) {
711
+ for ( const transceiver of this . _pc . getTransceivers ( ) ) {
705
712
for ( const track of stream . stream . getTracks ( ) ) {
706
- if ( sender . track == track ) {
707
- this . _pc . removeTrack ( sender ) ;
713
+ if ( transceiver . sender . track == track ) {
714
+ if ( transceiver . direction === 'sendonly' ) {
715
+ transceiver . stop ( ) ;
716
+ } else {
717
+ this . _pc . removeTrack ( track ) ;
718
+ }
708
719
}
709
720
}
710
721
}
0 commit comments