Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// TODO
}
// Until we implement the proxying of open requests within the Janus core, we open a channel ourselves whatever the case
config.dataChannel = config.pc.createDataChannel("JanusDataChannel", {ordered:false}); // FIXME Add options (ordered, maxRetransmits, etc.)
config.dataChannel.onmessage = onDataChannelMessage;
config.dataChannel.onopen = onDataChannelStateChange;
config.dataChannel.onclose = onDataChannelStateChange;
config.dataChannel.onerror = onDataChannelError;
}
// Create offer/answer now
if(jsep === null || jsep === undefined) {
console.log("createOffer")
createOffer(handleId, media, callbacks);
} else {
config.pc.setRemoteDescription(
new RTCSessionDescription(jsep),
function() {
Janus.log("Remote description accepted!");
createAnswer(handleId, media, callbacks);
}, callbacks.error);
}
}
callbacks.error("Invalid handle");
return;
}
var config = pluginHandle.webrtcStuff;
if (jsep !== undefined && jsep !== null) {
if (config.pc === null) {
Janus.warn("Wait, no PeerConnection?? if this is an answer, use createAnswer and not handleRemoteJsep");
callbacks.error("No PeerConnection: if this is an answer, use createAnswer and not handleRemoteJsep");
return;
}
if (adapter.browserDetails.browser === "edge") {
// This is Edge, add an a=end-of-candidates at the end
jsep.sdp += "a=end-of-candidates\r\n";
}
config.pc.setRemoteDescription(
new RTCSessionDescription(jsep),
function () {
Janus.log("Remote description accepted!");
callbacks.success();
}, callbacks.error);
} else {
callbacks.error("Invalid JSEP");
}
}
config.dataChannel = config.pc.createDataChannel("JanusDataChannel", { ordered: false }); // FIXME Add options (ordered, maxRetransmits, etc.)
config.dataChannel.onmessage = onDataChannelMessage;
config.dataChannel.onopen = onDataChannelStateChange;
config.dataChannel.onclose = onDataChannelStateChange;
config.dataChannel.onerror = onDataChannelError;
}
// Create offer/answer now
if (jsep === null || jsep === undefined) {
createOffer(handleId, media, callbacks);
} else {
if (adapter.browserDetails.browser === "edge") {
// This is Edge, add an a=end-of-candidates at the end
jsep.sdp += "a=end-of-candidates\r\n";
}
config.pc.setRemoteDescription(
new RTCSessionDescription(jsep),
function () {
Janus.log("Remote description accepted!");
createAnswer(handleId, media, callbacks);
}, callbacks.error);
}
}
let fromId = data.from;
if (data.sdp) {
log('Exchange', data);
}
let peer;
if (fromId in pcPeers) {
peer = pcPeers[fromId];
} else {
peer = createPC(fromId, false);
}
if (data.sdp) {
//console.log('exchange sdp', data);
let sdp = new RTCSessionDescription(data.sdp);
let callback = () => peer.remoteDescription.type === 'offer' ? peer.createAnswer(callback2, logError) : null;
let callback2 = desc => peer.setLocalDescription(desc, callback3, logError);
let callback3 = () => socket.emit('exchange', { to: fromId, sdp: peer.localDescription });
peer.setRemoteDescription(sdp, callback, logError);
} else {
peer.addIceCandidate(new RTCIceCandidate(data.candidate));
}
};
function exchange(store, data) {
if(socketId === null) {
socketId = data.from;
}
if (data.sdp) {
console.log('exchange sdp', data);
peerconn.setRemoteDescription(new RTCSessionDescription(data.sdp))
.then(() => {
if (peerconn.remoteDescription.type === "offer") {
peerconn.createAnswer(offerOpts)
.then(desc => {
peerconn.setLocalDescription(desc)
.then(() => {
store.dispatch({ type: EXCHANGE, payload: {'to': data.from, 'sdp': peerconn.localDescription } });
})
.catch(err => console.error("exchange sdp error : ", err))
})
}
})
} else {
console.log('exchange candidate');
peerconn.addIceCandidate(new RTCIceCandidate(data.candidate));
}
const pcPeers = PeerConnectionUtils.getPeers();
const fromId = data.from;
let pc;
if (
fromId === Utils.getStreamerSocketId() ||
Utils.getStreamerSocketId() === null
) {
if (fromId in pcPeers) {
pc = pcPeers[fromId];
} else {
pc = createPC(fromId, false);
}
if (data.sdp) {
console.log('exchange sdp', data);
pc.setRemoteDescription(
new RTCSessionDescription(data.sdp),
() => {
if (pc.remoteDescription.type == 'offer')
pc.createAnswer(
desc => {
console.log('createAnswer', desc);
pc.setLocalDescription(
desc,
() => {
console.log('setLocalDescription', pc.localDescription);
SocketUtils.emitExchangeServerSdp(
fromId,
pc.localDescription
);
},
error => console.log('error : ' + error)
);
var pluginHandle = pluginHandles[handleId];
if(pluginHandle === null || pluginHandle === undefined ||
pluginHandle.webrtcStuff === null || pluginHandle.webrtcStuff === undefined) {
Janus.warn("Invalid handle");
callbacks.error("Invalid handle");
return;
}
var config = pluginHandle.webrtcStuff;
if(jsep !== undefined && jsep !== null) {
if(config.pc === null) {
Janus.warn("Wait, no PeerConnection?? if this is an answer, use createAnswer and not handleRemoteJsep");
callbacks.error("No PeerConnection: if this is an answer, use createAnswer and not handleRemoteJsep");
return;
}
config.pc.setRemoteDescription(
new RTCSessionDescription(jsep),
function() {
Janus.log("Remote description accepted!");
callbacks.success();
}, callbacks.error);
} else {
callbacks.error("Invalid JSEP");
}
}
ip6 && (candidate[4] = ip6);
for (let i = 8; i < candidate.length; ++i) {
if (candidate[i] === 'raddr') {
ip4 = candidate[++i];
(ip6 = ips.get(ip4)) && (candidate[i] = ip6);
break;
}
}
lines[l] = candidate.join(' ');
}
}
return new RTCSessionDescription({
sdp: lines.join('\r\n'),
type: sessionDescription.type
});
}
async on_Answer_Received(data) {
const { payload } = data
await this.peer.setRemoteDescription(new WebRTCLib.RTCSessionDescription(payload.description))
payload.candidates.forEach(c => this.peer.addIceCandidate(new RTCIceCandidate(c)))
this.setState({
answer_recevied: true
})
}
async handleAnswer() {
const { payload } = this.state.offer
await this.setupWebRTC()
const { peer } = this
await peer.setRemoteDescription(new WebRTCLib.RTCSessionDescription(payload.description))
if (Array.isArray(payload.candidates)) {
payload.candidates.forEach((c) => peer.addIceCandidate(new RTCIceCandidate(c)))
}
const answer = await peer.createAnswer()
await peer.setLocalDescription(answer)
this.setState({
offer_answered: true
})
}