@@ -121,10 +121,7 @@ class P2PPeerConnectionChannel extends EventDispatcher {
121121 }
122122 return this . _sendStreamInfo ( stream ) . then ( ( ) => {
123123 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 ) ;
128125 this . _publishingStreams . push ( stream ) ;
129126 const trackIds = Array . from ( stream . mediaStream . getTracks ( ) ,
130127 ( track ) => track . id ) ;
@@ -214,6 +211,18 @@ class P2PPeerConnectionChannel extends EventDispatcher {
214211 } ) ;
215212 }
216213
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+
217226 /**
218227 * @function onMessage
219228 * @desc This method is called by P2PClient when there is new signaling message arrived.
@@ -688,9 +697,7 @@ class P2PPeerConnectionChannel extends EventDispatcher {
688697 if ( ! stream . mediaStream ) {
689698 continue ;
690699 }
691- for ( const track of stream . mediaStream . getTracks ( ) ) {
692- this . _pc . addTrack ( track , stream . mediaStream ) ;
693- }
700+ this . _addStream ( stream . mediaStream ) ;
694701 Logger . debug ( 'Added stream to peer connection.' ) ;
695702 this . _publishingStreams . push ( stream ) ;
696703 }
@@ -699,12 +706,16 @@ class P2PPeerConnectionChannel extends EventDispatcher {
699706 if ( ! stream . stream ) {
700707 continue ;
701708 }
702- if ( typeof this . _pc . getSenders === 'function' &&
709+ if ( typeof this . _pc . getTransceivers === 'function' &&
703710 typeof this . _pc . removeTrack === 'function' ) {
704- for ( const sender of this . _pc . getSenders ( ) ) {
711+ for ( const transceiver of this . _pc . getTransceivers ( ) ) {
705712 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+ }
708719 }
709720 }
710721 }
0 commit comments