forked from gtk2k/gtk2k.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebrtc_ff.html
More file actions
90 lines (80 loc) · 3.64 KB
/
webrtc_ff.html
File metadata and controls
90 lines (80 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<!DOCTYPE html>
<html>
<head>
<meta charset="utf8">
</head>
<body>
<video id="localView"></video>
<video id="remoteView"></video>
<button id="btnStart">start</button>
<script src="https://localhost:4001/socket.io/socket.io.js"></script>
<script>
var signalingChannel = io.connect('https://localhost:4001');
var configuration = { "iceServers": [{ "urls": "stun:stun.l.google.com:19302" }] };
var pc;
// call start() to initiate
function start() {
pc = new RTCPeerConnection(configuration);
// send any ice candidates to the other peer
pc.onicecandidate = function (evt) {
if (evt.candidate)
signalingChannel.send(JSON.stringify({ "candidate": evt.candidate }));
};
// let the "negotiationneeded" event trigger offer generation
pc.onnegotiationneeded = function () {
pc.createOffer().then(function (offer) {
return pc.setLocalDescription(offer);
})
.then(function () {
// send the offer to the other peer
signalingChannel.send(JSON.stringify({ "desc": pc.localDescription }));
})
.catch(logError);
};
// once remote video track arrives, show it in the remote video element
pc.ontrack = function (evt) {
if (evt.track.kind === "video")
remoteView.srcObject = evt.streams[0];
};
// get a local stream, show it in a self-view and add it to be sent
navigator.mediaDevices.getUserMedia({ "audio": false, "video": true }, function (stream) {
selfView.srcObject = stream;
if (stream.getAudioTracks().length > 0)
pc.addTrack(stream.getAudioTracks()[0], stream);
if (stream.getVideoTracks().length > 0)
pc.addTrack(stream.getVideoTracks()[0], stream);
}, logError);
}
signalingChannel.onmessage = function (evt) {
if (!pc)
start();
var message = JSON.parse(evt.data);
if (message.desc) {
var desc = message.desc;
// if we get an offer, we need to reply with an answer
if (desc.type == "offer") {
pc.setRemoteDescription(desc).then(function () {
return pc.createAnswer();
})
.then(function (answer) {
return pc.setLocalDescription(answer);
})
.then(function () {
signalingChannel.send(JSON.stringify({ "desc": pc.localDescription }));
})
.catch(logError);
} else if (desc.type == "answer") {
pc.setRemoteDescription(desc).catch(logError);
} else {
log("Unsupported SDP type. Your code may differ here.");
}
} else
pc.addIceCandidate(message.candidate).catch(logError);
};
function logError(error) {
log(error.name + ": " + error.message);
}
btnStart.onclick = start;
</script>
</body>
</html>