const SOCK_STREAM = 1; /* stream socket */ /* eslint-enable no-unused-vars */ // XXX At the time of this writing extending RTCPeerConnection using ES6 'class' // and 'extends' causes a runtime error related to the attempt to define the // onaddstream property setter. The error mentions that babelHelpers.set is // undefined which appears to be a thing inside React Native's packager. As a // workaround, extend using the pre-ES6 way. /** * The RTCPeerConnection provided by react-native-webrtc fires onaddstream * before it remembers remotedescription (and thus makes it available to API * clients). Because that appears to be a problem for lib-jitsi-meet which has * been successfully running on Chrome, Firefox, etc. for a very long * time, attempt to meet its expectations (by extending RTCPPeerConnection). * * @class */ export default function _RTCPeerConnection(...args: any[]) { /* eslint-disable indent, no-invalid-this */ RTCPeerConnection.apply(this, args); this.onaddstream = (...args) => // eslint-disable-line no-shadow (this._onaddstreamQueue ? this._queueOnaddstream : this._invokeOnaddstream) .apply(this, args); // Shadow RTCPeerConnection's onaddstream but after _RTCPeerConnection has // assigned to the property in question. Defining the property on // _RTCPeerConnection's prototype may (or may not, I don't know) work but I // don't want to try because the following approach appears to work and I // understand it. // $FlowFixMe Object.defineProperty(this, 'onaddstream', { configurable: true, enumerable: true, get() { return this._onaddstream; }, set(value) { this._onaddstream = value; } }); /* eslint-enable indent, no-invalid-this */ }
// XXX The RTCPeerConnection provided by react-native-webrtc fires // onaddstream before it remembers remotedescription (and thus makes it // available to API clients). Because that appears to be a problem for // lib-jitsi-meet which has been successfully running on Chrome, Firefox, // Temasys, etc. for a very long time, attempt to meets its expectations (by // extending RTCPPeerConnection). // XXX At the time of this writing extending RTCPeerConnection using ES6 // 'class' and 'extends' causes a runtime error related to the attemp to // define the onaddstream property setter. The error mentions that // babelHelpers.set is undefined which appears to be a thing inside React // Native's packager. As a workaround, extend using the pre-ES6 way. function _RTCPeerConnection() { RTCPeerConnection.apply(this, arguments); this.onaddstream = function () { return ( (this._onaddstreamQueue ? this._queueOnaddstream : this._invokeOnaddstream) .apply(this, arguments)); }; // Shadow RTCPeerConnection's onaddstream but after _RTCPeerConnection has // assigned to the property in question. Defining the property on // _RTCPeerConnection's prototype may (or may not, I don't know) work but // I don't want to try because the following approach appears to work and // I understand it. Object.defineProperty(this, 'onaddstream', { configurable: true, enumerable: true, get: function () { return this._onaddstream; }, set: function (value) { this._onaddstream = value; } }); };
function createPC(socketId, isOffer) { const pc = new RTCPeerConnection(configuration); pcPeers[socketId] = pc; pc.onicecandidate = function (event) { console.log('onicecandidate', event.candidate); if (event.candidate) { socket.emit('exchange', {'to': socketId, 'candidate': event.candidate }); } }; function createOffer() { pc.createOffer(function(desc) { console.log('createOffer', desc); pc.setLocalDescription(desc, function () { console.log('setLocalDescription', pc.localDescription); socket.emit('exchange', {'to': socketId, 'sdp': pc.localDescription }); }, logError); }, logError); } pc.onnegotiationneeded = function () { console.log('onnegotiationneeded'); if (isOffer) { createOffer(); } } pc.oniceconnectionstatechange = function(event) { console.log('oniceconnectionstatechange', event.target.iceConnectionState); if (event.target.iceConnectionState === 'connected') { createDataChannel(); } }; pc.onsignalingstatechange = function(event) { console.log('onsignalingstatechange', event.target.signalingState); }; pc.onaddstream = function (event) { console.log('onaddstream', event.stream); container.setState({info: 'One peer join!'}); const remoteList = container.state.remoteList; remoteList[socketId] = event.stream.toURL(); streams.push(event.stream); container.setState({ remoteList: remoteList }); }; pc.onremovestream = function (event) { console.log('onremovestream', event.stream); }; pc.addStream(localStream); function createDataChannel() { if (pc.textDataChannel) { return; } const dataChannel = pc.createDataChannel("text"); dataChannel.onerror = function (error) { console.log("dataChannel.onerror", error); }; dataChannel.onmessage = function (event) { console.log("dataChannel.onmessage:", event.data); container.receiveTextData({user: socketId, message: event.data}); }; dataChannel.onopen = function () { console.log('dataChannel.onopen'); container.setState({textRoomConnected: true}); }; dataChannel.onclose = function () { console.log("dataChannel.onclose"); }; pc.textDataChannel = dataChannel; } return pc; }