38
5分わかる WebRTC仕組み HTML5minutes!! vol.01 西畑一馬

5分でわかるWebRTCの仕組み - html5minutes vol.01

  • Upload
    -

  • View
    21

  • Download
    0

Embed Size (px)

DESCRIPTION

html5minutes vol.01で発表したスライドです。

Citation preview

Page 1: 5分でわかるWebRTCの仕組み - html5minutes vol.01

5分でわかる WebRTCの仕組み

HTML5minutes!! vol.01

西畑一馬

Page 2: 5分でわかるWebRTCの仕組み - html5minutes vol.01

最初に

Page 3: 5分でわかるWebRTCの仕組み - html5minutes vol.01

5分でわかるほど WebRTCは簡単じゃない

Page 4: 5分でわかるWebRTCの仕組み - html5minutes vol.01

WebRTCを取り巻くHTML5技術

WebSocket

Web Audio

RTCPeerConnection

video/audio API

getUserMedia API

Media Stream

Page 5: 5分でわかるWebRTCの仕組み - html5minutes vol.01

WebRTCを取り巻くHTML5技術

WebSocket

Web Audio

RTCPeerConnection

video/audio API

getUserMedia API

Media Streamとわ本日あたえられた時間は5分しかない

Page 6: 5分でわかるWebRTCの仕組み - html5minutes vol.01

WebRTCを取り巻くHTML5技術

WebSocket

Web Audiovideo/audio API

getUserMedia API

Media Streamとわ

RTCPeerConnection

重要なコレに絞って解説

Page 7: 5分でわかるWebRTCの仕組み - html5minutes vol.01

そもそもWebRTCって??

Page 8: 5分でわかるWebRTCの仕組み - html5minutes vol.01

WebRTC Web Real-Time Communication

Page 9: 5分でわかるWebRTCの仕組み - html5minutes vol.01

離れた2台以上のPCで通信する技術

Page 10: 5分でわかるWebRTCの仕組み - html5minutes vol.01

ウェブカムの映像や

Page 11: 5分でわかるWebRTCの仕組み - html5minutes vol.01

音声やデータなど

Page 12: 5分でわかるWebRTCの仕組み - html5minutes vol.01

これまでのビデオチャットは仲介サーバー必要

Page 13: 5分でわかるWebRTCの仕組み - html5minutes vol.01

WebRTCはP2PでPC同士が通信を行う

Page 14: 5分でわかるWebRTCの仕組み - html5minutes vol.01

WebRTC通信に必要な4つのステップ

Page 15: 5分でわかるWebRTCの仕組み - html5minutes vol.01

Step 1

自分のストリームデータを取得

Page 16: 5分でわかるWebRTCの仕組み - html5minutes vol.01

getUserMediagetUserMedia( { video:true, audio:true }, function(stream) { localStream = stream; }, function(error) { alert("メディアを取得できませんでした"); });

Page 17: 5分でわかるWebRTCの仕組み - html5minutes vol.01

Step 2

お互いのPCのSDPを交換する

Page 18: 5分でわかるWebRTCの仕組み - html5minutes vol.01

SDP Session Description Protocol

Page 19: 5分でわかるWebRTCの仕組み - html5minutes vol.01

PeerConnectionオブジェクトを生成

peer = new RTCPeerConnection({ "iceServers": [ { "url": "stun:stun.l.google.com:19302" } ]});peer.addStream(localStream);peer.createOffer(function(sdp) { peer.setLocalDescription(sdp, function() { socket.emit('sendOffer',{ "sdp": sdp }); });});

Page 20: 5分でわかるWebRTCの仕組み - html5minutes vol.01

先ほど取得したストリームデータをセット

peer = new RTCPeerConnection({ "iceServers": [ { "url": "stun:stun.l.google.com:19302" } ]});peer.addStream(localStream);peer.createOffer(function(sdp) { peer.setLocalDescription(sdp, function() { socket.emit('sendOffer',{ "sdp": sdp }); });});

Page 21: 5分でわかるWebRTCの仕組み - html5minutes vol.01

接続元から接続先にオファーを送る

Page 22: 5分でわかるWebRTCの仕組み - html5minutes vol.01

接続元から接続先にオファーを送る

Page 23: 5分でわかるWebRTCの仕組み - html5minutes vol.01

シグナリングサーバー

Page 24: 5分でわかるWebRTCの仕組み - html5minutes vol.01

SDPをWebSocketで送信

peer = new RTCPeerConnection({ "iceServers": [ { "url": "stun:stun.l.google.com:19302" } ]});peer.addStream(localStream);peer.createOffer(function(sdp) { peer.setLocalDescription(sdp, function() { socket.emit('sendOffer',{ "sdp": sdp }); });});

Page 25: 5分でわかるWebRTCの仕組み - html5minutes vol.01

接続先はアンサーを返信

Page 26: 5分でわかるWebRTCの仕組み - html5minutes vol.01

受け取ったオファーからSDPを作成して返信

socket.on('reciveOffer',function(data){ var sdp = new RTCSessionDescription(data.sdp); peer.setRemoteDescription(sdp, function() { peer.createAnswer(function(sdp) { peer.setLocalDescription(sdp, function() { socket.emit('sendAnswer',{ "sdp": sdp }); }); }); });});

Page 27: 5分でわかるWebRTCの仕組み - html5minutes vol.01

接続先から返信を受け取ったらそれをセット

socket.on('reciveAnswer',function(data){ var sdp = new RTCSessionDescription(data.sdp); peer.setRemoteDescription(sdp);});

Page 28: 5分でわかるWebRTCの仕組み - html5minutes vol.01

Step 3

お互いのネットワークのICE情報を交換する

Page 29: 5分でわかるWebRTCの仕組み - html5minutes vol.01

ICE Interactive Connectivity Establishment

Page 30: 5分でわかるWebRTCの仕組み - html5minutes vol.01

IPアドレスやポート番号を 交換しないと通信できない

Page 31: 5分でわかるWebRTCの仕組み - html5minutes vol.01

STUNサーバー

Page 32: 5分でわかるWebRTCの仕組み - html5minutes vol.01

STUNサーバーは RTCPeerConnectionオブジェクト生成時に指定済み

peer = new RTCPeerConnection({ "iceServers": [ { "url": "stun:stun.l.google.com:19302" } ]});peer.addStream(localStream);peer.createOffer(function(sdp) { peer.setLocalDescription(sdp, function() { socket.emit('sendOffer',{ "sdp": sdp }); });});

Page 33: 5分でわかるWebRTCの仕組み - html5minutes vol.01

icecandidateイベントで取得できるのでWebSocketで相手に送信

peer.onicecandidate = function (evt) { if ( evt.candidate ) { socket.emit('sendCdi',{ "ice": evt.candidate }); }}

Page 34: 5分でわかるWebRTCの仕組み - html5minutes vol.01

socket.on('reciveCdi',function(data){ var ice = new RTCIceCandidate(data.ice); peer.addIceCandidate(ice);});

ice情報を受け取ったらセット

Page 35: 5分でわかるWebRTCの仕組み - html5minutes vol.01

Step 4

video要素にremoteStreamを設置

Page 36: 5分でわかるWebRTCの仕組み - html5minutes vol.01

peer.addEventListener("addstream", function(event){ var targetVideo = document.getElementById("remoteVideo"); targetVideo.src = URL.createObjectURL(event.stream); targetVideo.play();}, false);

あとは受け取ったストリームデータを video要素に突っ込むだけ

Page 37: 5分でわかるWebRTCの仕組み - html5minutes vol.01

WebRTCの課題• 対応ブラウザがChrome/OperaとFirefoxのみ • ルーターやFirewallで通信できないことも • CPU使用率が半端ない • ドライバレベルの不具合を引き起こすことも • 不安定、とにかく不安定

Page 38: 5分でわかるWebRTCの仕組み - html5minutes vol.01

HTML5minutes!! vol.01

西畑一馬

Thank you!!