12. new PeerConnection()
function prepareNewConnection() {
var pc_config = {"iceServers":[]}; // info of STUN/TURN
var peer = null;
peer = new webkitRTCPeerConnection(pc_config);
peer.onicecandidate = function (evt) { … }
peer.addStream(localStream);
peer.addEventListener("addstream",
onRemoteStreamAdded, false);
peer.addEventListener("removestream",
onRemoteStreamRemoved, false);
return peer;
}
14. sendOffer()
function sendOffer() {
peerConnection = prepareNewConnection();
peerConnection.createOffer(
function (sessionDescription) { // in case of success
peerConnection.setLocalDescription(sessionDescription);
sendSDP(sessionDescription); // show in textarea
}, function () { // in case of error
}, mediaConstraints);
}
27. peer.onIceCandidate()
function prepareNewConnection() {
var pc_config = {"iceServers":[]};
var peer = null;
peer = new webkitRTCPeerConnection(pc_config);
peer.onicecandidate = function (evt) {
if (evt.candidate) {
sendCandidate({ // show in text area
type: "candidate",
sdpMLineIndex: evt.candidate.sdpMLineIndex,
sdpMid: evt.candidate.sdpMid,
candidate: evt.candidate.candidate
});
} else {
// End of candidates.
}
}
…
return peer;
}
33. peer.onIceCandidate() ※同じ
function prepareNewConnection() {
var pc_config = {"iceServers":[]};
var peer = null;
peer = new webkitRTCPeerConnection(pc_config);
peer.onicecandidate = function (evt) {
if (evt.candidate) {
sendCandidate({ // show in text area
type: "candidate",
sdpMLineIndex: evt.candidate.sdpMLineIndex,
sdpMid: evt.candidate.sdpMid,
candidate: evt.candidate.candidate
});
} else {
// End of candidates.
}
}
…
return peer;
}
34. addIceCandidate() ※同じ
function onCandidate(evt) {
var candidate = new RTCIceCandidate({
sdpMLineIndex:evt.sdpMLineIndex,
sdpMid:evt.sdpMid,
candidate:evt.candidate
});
peerConnection.addIceCandidate(candidate);
}
51. sendSDP()
function sendOffer() {
peerConnection = prepareNewConnection();
peerConnection.createOffer(
function (sessionDescription) { // in case of success
peerConnection.setLocalDescription(sessionDescription);
sendSDP(sessionDescription);
},
function () { // in case of error
},
mediaConstraints);
}
function sendSDP(sdp) {
// send via socket
socket.json.send(sdp);
}
52. onMessage()
function onMessage(evt) {
if (evt.type === 'offer') {
onOffer(evt);
} else if (evt.type === 'answer' && peerStarted) {
onAnswer(evt);
} else if (evt.type === 'candidate' && peerStarted) {
onCandidate(evt);
} else if (evt.type === 'user dissconnected' && peerStarted) {
stop();
}
}
53. onOffer(), setOffer()
function onOffer(evt) {
setOffer(evt);
sendAnswer(evt);
}
function setOffer(evt) {
peerConnection = prepareNewConnection();
peerConnection.setRemoteDescription(
new RTCSessionDescription(evt)
);
}
55. sendSDP()
function sendAnswer(evt) {
peerConnection.createAnswer(
function (sessionDescription) { // in case of success
peerConnection.setLocalDescription(sessionDescription);
sendSDP(sessionDescription);
},
function () { // in case of error
},
mediaConstraints);
}
function sendSDP(sdp) {
// send via socket
socket.json.send(sdp);
}
56. onMessage()
function onMessage(evt) {
if (evt.type === 'offer') {
onOffer(evt);
} else if (evt.type === 'answer' && peerStarted) {
onAnswer(evt);
} else if (evt.type === 'candidate' && peerStarted) {
onCandidate(evt);
} else if (evt.type === 'user dissconnected' && peerStarted) {
stop();
}
}
57. onAnswer(), setAnswer ()
function onAnswer(evt) {
setAnswer(evt);
}
function setAnswer(evt) {
peerConnection.setRemoteDescription(
new RTCSessionDescription(evt)
);
}