Skip to content
This repository was archived by the owner on Oct 25, 2024. It is now read-only.

Commit e0ae8da

Browse files
committed
Listen to negotiation needed event.
Negotiation needed works good now, the SDK doesn't need to handle negotiation needed flag manually.
1 parent 9b8139c commit e0ae8da

File tree

1 file changed

+4
-37
lines changed

1 file changed

+4
-37
lines changed

src/sdk/p2p/peerconnection-channel.js

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ class P2PPeerConnectionChannel extends EventDispatcher {
126126
for (const track of stream.mediaStream.getTracks()) {
127127
this._pc.addTrack(track, stream.mediaStream);
128128
}
129-
this._onNegotiationneeded();
130129
this._publishingStreams.push(stream);
131130
const trackIds = Array.from(stream.mediaStream.getTracks(),
132131
(track) => track.id);
@@ -420,6 +419,7 @@ class P2PPeerConnectionChannel extends EventDispatcher {
420419
if (this._pc.signalingState !== 'stable') {
421420
if (this._isPolitePeer) {
422421
// Rollback.
422+
Logger.debug('Rollback.');
423423
this._pc.setLocalDescription();
424424
} else {
425425
// Ignore this offer.
@@ -549,12 +549,6 @@ class P2PPeerConnectionChannel extends EventDispatcher {
549549
}
550550

551551
_onNegotiationneeded() {
552-
// This is intented to be executed when onnegotiationneeded event is fired.
553-
// However, onnegotiationneeded may fire mutiple times when more than one
554-
// track is added/removed. So we manually execute this function after
555-
// adding/removing track and creating data channel.
556-
Logger.debug('On negotiation needed.');
557-
558552
if (this._pc.signalingState === 'stable') {
559553
this._doNegotiate();
560554
} else {
@@ -699,43 +693,23 @@ class P2PPeerConnectionChannel extends EventDispatcher {
699693
this._pc.oniceconnectionstatechange = (event) => {
700694
this._onIceConnectionStateChange.apply(this, [event]);
701695
};
702-
/*
703-
this._pc.oniceChannelStatechange = function(event) {
704-
_onIceChannelStateChange(peer, event);
705-
};
706-
= function() {
707-
onNegotiationneeded(peers[peer.id]);
696+
this._pc.onnegotiationneeded = () => {
697+
this._onNegotiationneeded();
708698
};
709-
710-
//DataChannel
711-
this._pc.ondatachannel = function(event) {
712-
Logger.debug(myId + ': On data channel');
713-
// Save remote created data channel.
714-
if (!peer.dataChannels[event.channel.label]) {
715-
peer.dataChannels[event.channel.label] = event.channel;
716-
Logger.debug('Save remote created data channel.');
717-
}
718-
bindEventsToDataChannel(event.channel, peer);
719-
};*/
720699
}
721700

722701
_drainPendingStreams() {
723-
let negotiationNeeded = false;
724702
Logger.debug('Draining pending streams.');
725703
if (this._pc && this._pc.signalingState === 'stable') {
726704
Logger.debug('Peer connection is ready for draining pending streams.');
727705
for (let i = 0; i < this._pendingStreams.length; i++) {
728706
const stream = this._pendingStreams[i];
729-
// OnNegotiationNeeded event will be triggered immediately after adding stream to PeerConnection in Firefox.
730-
// And OnNegotiationNeeded handler will execute drainPendingStreams. To avoid add the same stream multiple times,
731-
// shift it from pending stream list before adding it to PeerConnection.
732707
this._pendingStreams.shift();
733708
if (!stream.mediaStream) {
734709
continue;
735710
}
736711
for (const track of stream.mediaStream.getTracks()) {
737712
this._pc.addTrack(track, stream.mediaStream);
738-
negotiationNeeded = true;
739713
}
740714
Logger.debug('Added stream to peer connection.');
741715
this._publishingStreams.push(stream);
@@ -746,17 +720,13 @@ class P2PPeerConnectionChannel extends EventDispatcher {
746720
continue;
747721
}
748722
this._pc.removeStream(this._pendingUnpublishStreams[j].mediaStream);
749-
negotiationNeeded = true;
750723
this._unpublishPromises.get(
751724
this._pendingUnpublishStreams[j].mediaStream.id).resolve();
752725
this._publishedStreams.delete(this._pendingUnpublishStreams[j]);
753726
Logger.debug('Remove stream.');
754727
}
755728
this._pendingUnpublishStreams.length = 0;
756729
}
757-
if (negotiationNeeded) {
758-
this._onNegotiationneeded();
759-
}
760730
}
761731

762732
_drainPendingRemoteIceCandidates() {
@@ -877,7 +847,6 @@ class P2PPeerConnectionChannel extends EventDispatcher {
877847
return;
878848
}
879849
this._isNegotiationNeeded = false;
880-
this._isCaller = true;
881850
let localDesc;
882851
this._pc.createOffer().then((desc) => {
883852
desc.sdp = this._setRtpReceiverOptions(desc.sdp);
@@ -888,7 +857,7 @@ class P2PPeerConnectionChannel extends EventDispatcher {
888857
});
889858
}
890859
}).catch((e) => {
891-
Logger.error(e.message + ' Please check your codec settings.');
860+
Logger.error(e.message);
892861
const error = new ErrorModule.P2PError(ErrorModule.errors.P2P_WEBRTC_SDP,
893862
e.message);
894863
this._stop(error, true);
@@ -898,7 +867,6 @@ class P2PPeerConnectionChannel extends EventDispatcher {
898867
_createAndSendAnswer() {
899868
this._drainPendingStreams();
900869
this._isNegotiationNeeded = false;
901-
this._isCaller = false;
902870
let localDesc;
903871
this._pc.createAnswer().then((desc) => {
904872
desc.sdp = this._setRtpReceiverOptions(desc.sdp);
@@ -973,7 +941,6 @@ class P2PPeerConnectionChannel extends EventDispatcher {
973941
const dc = this._pc.createDataChannel(label);
974942
this._bindEventsToDataChannel(dc);
975943
this._dataChannels.set(DataChannelLabel.MESSAGE, dc);
976-
this._onNegotiationneeded();
977944
}
978945

979946
_bindEventsToDataChannel(dc) {

0 commit comments

Comments
 (0)