40. 1 to 1 ブラウザ側 (1) JavaScript
socket.io 関連
<script src="http://localhost:9001/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:9001/');
socket.on('connect', onChannelOpened)
.on('message', onMessage);
function onChannelOpened(evt) {
}
function onMessage(evt) {
Signaling
if (evt.type === 'offer') {
(1)
sendAnswer(evt); // receive offer, send answer
} else if (evt.type === 'answer') {
peerConn.setRemoteDescription(new RTCSessionDescription(evt)); // receive answer
} else if (evt.type === 'candidate') {
var candidate = new RTCIceCandidate({sdpMLineIndex:evt.sdpMLineIndex,
sdpMid:evt.sdpMid, candidate:evt.candidate});
peerConn.addIceCandidate(candidate); // set and send candidate
} else if (evt.type === 'bye') {
stop();
}
}
42
41. 1 to 1 ブラウザ側 (2) JavaScript
connection handling
// 通信開始
function connect() {
sendOffer();
}
// 通信終了
function hangUp() {
socket.json.send({type: "bye"});
stop();
}
function stop() {
peerConn.close();
peerConn = null;
}
43
42. 1 to 1 ブラウザ側 (3) JavaScript
SDP offer / answer
var peerConn = null; // RTCPeerConnection
function sendOffer() {
peerConn = prepareNewConnection();
peerConn.createOffer(function (sessionDescription) { // in case of success
peerConn.setLocalDescription(sessionDescription);
socket.json.send(sessionDescription);
}, function () { // in case of error
console.log("Create Offer failed");
}, mediaConstraints);
}
function sendAnswer(evt) {
peerConn = prepareNewConnection();
peerConn.setRemoteDescription(new RTCSessionDescription(evt));
peerConn.createAnswer(function (sessionDescription) { // in case of success
peerConn.setLocalDescription(sessionDescription);
socket.json.send(sessionDescription);
}, function () { // in case of error
console.log("Create Answer failed");
}, mediaConstraints);
}
44
43. 1 to 1 ブラウザ側 (4) JavaScript
RTCPeerConnection
function prepareNewConnection() {
var pc_config = {"iceServers":[]};
var peer = new webkitRTCPeerConnection(pc_config);
peer.onicecandidate = function (evt) {
if (evt.candidate) {
socket.json.send({type: "candidate", sdpMLineIndex: evt.candidate.sdpMLineIndex,
sdpMid: evt.candidate.sdpMid, candidate: evt.candidate.candidate});
} else { }
};
Signaling
(2)
peer.addStream(localStream);
peer.addEventListener("addstream", onRemoteStreamAdded, false);
peer.addEventListener("removestream", onRemoteStreamRemoved, false)
function onRemoteStreamAdded(event) {
remoteVideo.src = window.webkitURL.createObjectURL(event.stream); // set stream to video element
}
function onRemoteStreamRemoved(event) {
remoteVideo.src = ""; // clear stream of video element
}
return peer;
}
45
44. 1 to 1 ブラウザ側 (5) JavaScript
Video handling
var localVideo = document.getElementById('local-video');
var remoteVideo = document.getElementById('remote-video');
var localStream = null;
function startVideo() {
navigator.webkitGetUserMedia({video: true, audio: true}, successCallback, errorCallback);
function successCallback(stream) {
localStream = stream;
localVideo.src = window.webkitURL.createObjectURL(stream);
localVideo.play();
}
function errorCallback(error) {
console.error('An error occurred: [CODE ' + error.code + ']');
return;
}
}
function stopVideo() {
localVideo.src = "";
localStream.stop();
}
46
51. ICEのやり取り(理想ケース)
User A
createNewPeer
User B
send offer sdp
createNewPeer
setRemoteDescription(sdp)
send answer sdp
setRemoteDescription(sdp)
send ice
addIceCandidate(ice)
send ice
addIceCandidate(ice)
Iceを
交互に送信
send ice
send ice
addIceCandidate(ice)
addIceCandidate(ice)
end of candidate
end of candidate
P2P stream transfer
53
52. ICEのやり取り(想定していたケー
ス)
User A
User B
createNewPeer
send offer sdp
createNewPeer
setRemoteDescription(sdp)
send answer sdp
setRemoteDescription(sdp)
send ice
send ice
end of candidate
addIceCandidate(ice)
addIceCandidate(ice)
addIceCandidate(ice)
send ice
Iceを片方が
連続して送信
send ice
addIceCandidate(ice)
end of candidate
P2P stream transfer
54
53. ICEのやり取り(想定外のケース)
User A
createNewPeer
User B
send offer sdp
createNewPeer
setRemoteDescription(sdp)
send ice
addIceCandidate(ice)
send ice
end of candidate
addIceCandidate(ice)
send answer sdp
※エラーとして
弾いていたら
嵌った…
setRemoteDescription(sdp)
send ice
addIceCandidate(ice)
Answer SDP
を生成前に、
Ice を受け取る
ことも可能
約1ヶ月
send ice
addIceCandidate(ice)
end of candidate
P2P stream transfer
55
54. WebRTCで可能な(通信)形態
• 双方向通信
– 1 to 1 の音声通信
– 1 to 1 の映像通信
– 1 to 1 のスクリーンキャプチャ通信(と
音声通信)
– n to n の音声通信
– n to n の映像通信
– n to n のスクリーンキャプチャ
通信(と音声通信)
1会議室に複数人、
かつ複数会議室
56
55. では、見てみましょう
• WebRTC 4 people Video Chat (Chrome用)
– http://localhost/rtc/rtc4.html
57
57. 1対1の流れ
User A
createNewPeer
User B
send offer sdp (broadcast)
createNewPeer
setRemoteDescription(sdp)
send answer sdp (broadcast)
setRemoteDescription(sdp)
send ice
send ice
addIceCandidate(ice)
addIceCandidate(ice)
send ice
send ice
addIceCandidate(ice)
addIceCandidate(ice)
end of candidate
end of candidate
P2P stream transfer
59
58. n対nの流れ:SDP部分で衝突
User B
User A
createNewPeer
send offer sdp (broadcast)
send answer sdp
OK
setRemoteDescription(sdp)
User C
send offer sdp (broadcast)
createNewPeer
setRemoteDescription(sdp)
OK
createNewPeer
setRemoteDescription(sdp)
OK
send answer sdp
no peer!
→ createNewPeer
setRemoteDescription(sdp)
send offer sdp
※この流れを理解するのに、苦労
約2ヶ月
corrupt!
60
59. n対nの流れ:Broadcastを分離
User B
User A
send “call” (broadcast)
createNewPeer
send “call” (broadcast)
send “response”
send offer sdp
send answer sdp
setRemoteDescription(sdp)
OK
createNewPeer
setRemoteDescription(sdp)
OK
send “response”
createNewPeer
User C
setRemoteDescription(sdp)
send offer sdp
createNewPeer
send answer sdp
setRemoteDescription(sdp)
OK
setRemoteDescription(sdp)
OK
61
60. 4 people ブラウザ側 (1) JavaScript
function call() {
if (! isLocalStreamStarted()) return;
socket.json.send({type: "call"});
}
function onMessage(evt) {
var id = evt.from;
var target = evt.sendto;
var conn = getConnection(id);
if (evt.type === 'call') {
if (! isLocalStreamStarted()) return;
if (conn) return; // already connected
if (isConnectPossible()) {
socket.json.send({type: "response", sendto: id });
}
else { console.warn('max connections. so ignore call');
}
}
else if (evt.type === 'response') {
sendOffer(id);
return;
}
}
62
61. 4 people ブラウザ側 (3) JavaScript
function sendOffer(id) {
var conn = getConnection(id);
if (!conn) {
conn = prepareNewConnection(id);
}
conn.peerconnection.createOffer(function (sessionDescription) { // in case of success
conn.iceReady = true;
conn.peerconnection.setLocalDescription(sessionDescription);
sessionDescription.sendto = conn.id;
socket.json.send(sessionDescription);
}, function () { // in case of error
console.log("Create Offer failed");
}, mediaConstraints);
conn.iceReady = true;
}
63
62. 4 people ブラウザ側 (3) JavaScript
function sendAnswer(id, evt) {
var conn = getConnection(id);
if (! conn) {
conn = prepareNewConnection(id);
}
conn.peerconnection.setRemoteDescription(new RTCSessionDescription(evt));
conn.peerconnection.createAnswer(function (sessionDescription) {
// in case of success
conn.iceReady = true;
conn.peerconnection.setLocalDescription(sessionDescription);
sessionDescription.sendto = id;
socket.json.send(sessionDescription);
}, function () { // in case of error
console.log("Create Answer failed");
}, mediaConstraints);
conn.iceReady = true;
}
64
63. 4 people ブラウザ側 (4) JavaScript
function prepareNewConnection(id) {
var pc_config = {"iceServers":[]};
var peer = null;
peer = new webkitRTCPeerConnection(pc_config);
var conn = new Connection();
conn.id = id;
conn.peerconnection = peer;
peer.id = id;
addConnection(id, conn);
// send any ice candidates to the other peer
peer.onicecandidate = function (evt) {
if (evt.candidate) {
socket.json.send({type: "candidate", sendto: conn.id,
sdpMLineIndex: evt.candidate.sdpMLineIndex, sdpMid: evt.candidate.sdpMid,
candidate: evt.candidate.candidate});
} else {
conn.established = true; // end of candidates
}
};
…
65
64. 4 people ブラウザ側 (5) JavaScript
var MAX_CONNECTION_COUNT = 3;
var connections = {}; // Connection hash
function Connection() { // Connection Class
var self = this;
var id = ""; // socket.id of partner
var peerconnection = null; // RTCPeerConnection instance
var established = false; // is Already Established
var iceReady = false;
}
function getConnection(id) {
var con = null;
con = connections[id];
return con;
}
function addConnection(id, connection) {
connections[id] = connection;
}
getConnectionCount(), isConnectPossible(), deleteConnection(id), …, etc.
66
65. 4 peopleサーバー (node.js)
socket.on('message', function(message) {
// set message sender
message.from = socket.id;
// send to specific target
var target = message.sendto;
if (target) {
io.sockets.socket(target).emit('message', message);
return;
}
// broadcast in room
emitMessage('message', message);
});
67