Calls: create data channel only on the caller's side.
Removes redundancy and a memory leak (and thus an error upon closing).
This commit is contained in:
+21
-15
@@ -121,7 +121,8 @@ class CallPanel extends React.PureComponent {
|
|||||||
|
|
||||||
handleVideoCallAccepted(info) {
|
handleVideoCallAccepted(info) {
|
||||||
RING_SOUND.pause();
|
RING_SOUND.pause();
|
||||||
const pc = this.createPeerConnection();
|
// Create peer connection with a data channel.
|
||||||
|
const pc = this.createPeerConnection(true);
|
||||||
const stream = this.state.localStream;
|
const stream = this.state.localStream;
|
||||||
stream.getTracks().forEach(track => {
|
stream.getTracks().forEach(track => {
|
||||||
pc.addTrack(track, stream);
|
pc.addTrack(track, stream);
|
||||||
@@ -230,7 +231,7 @@ class CallPanel extends React.PureComponent {
|
|||||||
this.state.pc.onicecandidateerror = null;
|
this.state.pc.onicecandidateerror = null;
|
||||||
this.state.pc.ondatachannel = null;
|
this.state.pc.ondatachannel = null;
|
||||||
|
|
||||||
if (this.state.dataChannel) {
|
if (this.state.dataChannel && (this.state.dataChannel.readyState == 'open' || this.state.dataChannel.readyState == 'connecting')) {
|
||||||
this.state.dataChannel.close();
|
this.state.dataChannel.close();
|
||||||
}
|
}
|
||||||
this.state.pc.close();
|
this.state.pc.close();
|
||||||
@@ -284,11 +285,12 @@ class CallPanel extends React.PureComponent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
handleDataChannelClose() {
|
handleDataChannelClose(event) {
|
||||||
/* no action */
|
console.log('close data channel:', event);
|
||||||
}
|
}
|
||||||
|
|
||||||
handleDataChannelEvent(event) {
|
handleDataChannelEvent(event) {
|
||||||
|
console.log('data channel event:', event);
|
||||||
const channel = event.channel;
|
const channel = event.channel;
|
||||||
channel.onerror = this.handleDataChannelError;
|
channel.onerror = this.handleDataChannelError;
|
||||||
channel.onmessage = this.handleDataChannelMessage;
|
channel.onmessage = this.handleDataChannelMessage;
|
||||||
@@ -297,7 +299,7 @@ class CallPanel extends React.PureComponent {
|
|||||||
this.setState({dataChannel: channel});
|
this.setState({dataChannel: channel});
|
||||||
}
|
}
|
||||||
|
|
||||||
createPeerConnection() {
|
createPeerConnection(withDataChannel) {
|
||||||
const iceServers = this.props.tinode.getServerParam('iceServers', null);
|
const iceServers = this.props.tinode.getServerParam('iceServers', null);
|
||||||
const pc = iceServers ? new RTCPeerConnection({iceServers: iceServers}) : new RTCPeerConnection();
|
const pc = iceServers ? new RTCPeerConnection({iceServers: iceServers}) : new RTCPeerConnection();
|
||||||
|
|
||||||
@@ -310,15 +312,17 @@ class CallPanel extends React.PureComponent {
|
|||||||
pc.ontrack = this.handleTrackEvent;
|
pc.ontrack = this.handleTrackEvent;
|
||||||
pc.ondatachannel = this.handleDataChannelEvent;
|
pc.ondatachannel = this.handleDataChannelEvent;
|
||||||
|
|
||||||
// creating data channel events
|
let stateUpdate = {pc: pc, waitingForPeer: false};
|
||||||
const channel = pc.createDataChannel("events", {ordered: true});
|
if (withDataChannel) {
|
||||||
channel.onerror = this.handleDataChannelError;
|
// Create data channel for exchanging events.
|
||||||
channel.onmessage = this.handleDataChannelMessage;
|
const channel = pc.createDataChannel("events", {ordered: true});
|
||||||
channel.onopen = this.handleDataChannelOpen;
|
channel.onerror = this.handleDataChannelError;
|
||||||
channel.onclose = this.handleDataChannelClose;
|
channel.onmessage = this.handleDataChannelMessage;
|
||||||
|
channel.onopen = this.handleDataChannelOpen;
|
||||||
this.setState({pc: pc, dataChannel: channel, waitingForPeer: false});
|
channel.onclose = this.handleDataChannelClose;
|
||||||
|
stateUpdate.dataChannel = channel;
|
||||||
|
}
|
||||||
|
this.setState(stateUpdate);
|
||||||
return pc;
|
return pc;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -441,7 +445,9 @@ class CallPanel extends React.PureComponent {
|
|||||||
|
|
||||||
handleVideoOfferMsg(info) {
|
handleVideoOfferMsg(info) {
|
||||||
let localStream = null;
|
let localStream = null;
|
||||||
const pc = this.state.pc ? this.state.pc : this.createPeerConnection();
|
// If needed, create peer connection.
|
||||||
|
// Not creating data channel since it should be created by the peer.
|
||||||
|
const pc = this.state.pc ? this.state.pc : this.createPeerConnection(false);
|
||||||
const desc = new RTCSessionDescription(info.payload);
|
const desc = new RTCSessionDescription(info.payload);
|
||||||
|
|
||||||
pc.setRemoteDescription(desc).then(_ => {
|
pc.setRemoteDescription(desc).then(_ => {
|
||||||
|
|||||||
+21
-15
@@ -10372,7 +10372,7 @@ class CallPanel extends (react__WEBPACK_IMPORTED_MODULE_0___default().PureCompon
|
|||||||
}
|
}
|
||||||
handleVideoCallAccepted(info) {
|
handleVideoCallAccepted(info) {
|
||||||
RING_SOUND.pause();
|
RING_SOUND.pause();
|
||||||
const pc = this.createPeerConnection();
|
const pc = this.createPeerConnection(true);
|
||||||
const stream = this.state.localStream;
|
const stream = this.state.localStream;
|
||||||
stream.getTracks().forEach(track => {
|
stream.getTracks().forEach(track => {
|
||||||
pc.addTrack(track, stream);
|
pc.addTrack(track, stream);
|
||||||
@@ -10466,7 +10466,7 @@ class CallPanel extends (react__WEBPACK_IMPORTED_MODULE_0___default().PureCompon
|
|||||||
this.state.pc.onnegotiationneeded = null;
|
this.state.pc.onnegotiationneeded = null;
|
||||||
this.state.pc.onicecandidateerror = null;
|
this.state.pc.onicecandidateerror = null;
|
||||||
this.state.pc.ondatachannel = null;
|
this.state.pc.ondatachannel = null;
|
||||||
if (this.state.dataChannel) {
|
if (this.state.dataChannel && (this.state.dataChannel.readyState == 'open' || this.state.dataChannel.readyState == 'connecting')) {
|
||||||
this.state.dataChannel.close();
|
this.state.dataChannel.close();
|
||||||
}
|
}
|
||||||
this.state.pc.close();
|
this.state.pc.close();
|
||||||
@@ -10523,8 +10523,11 @@ class CallPanel extends (react__WEBPACK_IMPORTED_MODULE_0___default().PureCompon
|
|||||||
event.target.send(VIDEO_UNMUTED_EVENT);
|
event.target.send(VIDEO_UNMUTED_EVENT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
handleDataChannelClose() {}
|
handleDataChannelClose(event) {
|
||||||
|
console.log('close data channel:', event);
|
||||||
|
}
|
||||||
handleDataChannelEvent(event) {
|
handleDataChannelEvent(event) {
|
||||||
|
console.log('data channel event:', event);
|
||||||
const channel = event.channel;
|
const channel = event.channel;
|
||||||
channel.onerror = this.handleDataChannelError;
|
channel.onerror = this.handleDataChannelError;
|
||||||
channel.onmessage = this.handleDataChannelMessage;
|
channel.onmessage = this.handleDataChannelMessage;
|
||||||
@@ -10534,7 +10537,7 @@ class CallPanel extends (react__WEBPACK_IMPORTED_MODULE_0___default().PureCompon
|
|||||||
dataChannel: channel
|
dataChannel: channel
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
createPeerConnection() {
|
createPeerConnection(withDataChannel) {
|
||||||
const iceServers = this.props.tinode.getServerParam('iceServers', null);
|
const iceServers = this.props.tinode.getServerParam('iceServers', null);
|
||||||
const pc = iceServers ? new RTCPeerConnection({
|
const pc = iceServers ? new RTCPeerConnection({
|
||||||
iceServers: iceServers
|
iceServers: iceServers
|
||||||
@@ -10547,18 +10550,21 @@ class CallPanel extends (react__WEBPACK_IMPORTED_MODULE_0___default().PureCompon
|
|||||||
pc.onicecandidateerror = this.handleIceCandidateErrorEvent;
|
pc.onicecandidateerror = this.handleIceCandidateErrorEvent;
|
||||||
pc.ontrack = this.handleTrackEvent;
|
pc.ontrack = this.handleTrackEvent;
|
||||||
pc.ondatachannel = this.handleDataChannelEvent;
|
pc.ondatachannel = this.handleDataChannelEvent;
|
||||||
const channel = pc.createDataChannel("events", {
|
let stateUpdate = {
|
||||||
ordered: true
|
|
||||||
});
|
|
||||||
channel.onerror = this.handleDataChannelError;
|
|
||||||
channel.onmessage = this.handleDataChannelMessage;
|
|
||||||
channel.onopen = this.handleDataChannelOpen;
|
|
||||||
channel.onclose = this.handleDataChannelClose;
|
|
||||||
this.setState({
|
|
||||||
pc: pc,
|
pc: pc,
|
||||||
dataChannel: channel,
|
|
||||||
waitingForPeer: false
|
waitingForPeer: false
|
||||||
});
|
};
|
||||||
|
if (withDataChannel) {
|
||||||
|
const channel = pc.createDataChannel("events", {
|
||||||
|
ordered: true
|
||||||
|
});
|
||||||
|
channel.onerror = this.handleDataChannelError;
|
||||||
|
channel.onmessage = this.handleDataChannelMessage;
|
||||||
|
channel.onopen = this.handleDataChannelOpen;
|
||||||
|
channel.onclose = this.handleDataChannelClose;
|
||||||
|
stateUpdate.dataChannel = channel;
|
||||||
|
}
|
||||||
|
this.setState(stateUpdate);
|
||||||
return pc;
|
return pc;
|
||||||
}
|
}
|
||||||
handleVideoAnswerMsg(info) {
|
handleVideoAnswerMsg(info) {
|
||||||
@@ -10655,7 +10661,7 @@ class CallPanel extends (react__WEBPACK_IMPORTED_MODULE_0___default().PureCompon
|
|||||||
}
|
}
|
||||||
handleVideoOfferMsg(info) {
|
handleVideoOfferMsg(info) {
|
||||||
let localStream = null;
|
let localStream = null;
|
||||||
const pc = this.state.pc ? this.state.pc : this.createPeerConnection();
|
const pc = this.state.pc ? this.state.pc : this.createPeerConnection(false);
|
||||||
const desc = new RTCSessionDescription(info.payload);
|
const desc = new RTCSessionDescription(info.payload);
|
||||||
pc.setRemoteDescription(desc).then(_ => {
|
pc.setRemoteDescription(desc).then(_ => {
|
||||||
return navigator.mediaDevices.getUserMedia(this.localStreamConstraints);
|
return navigator.mediaDevices.getUserMedia(this.localStreamConstraints);
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
+1
-1
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user