forked from mpromonet/webrtc-streamer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
220 lines (196 loc) · 6.43 KB
/
index.html
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<html>
<head>
<title>WebRTC Streamer</title>
<script>
var pc;
var pcConfig = {'iceServers': [] };
var pcOptions = { 'optional': [{'DtlsSrtpKeyAgreement': true} ] };
var mediaConstraints = {
'mandatory': {'OfferToReceiveVideo': true },
'offerToReceiveVideo': true
};
RTCPeerConnection = window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
RTCSessionDescription = window.mozRTCSessionDescription || window.RTCSessionDescription;
RTCIceCandidate = window.mozRTCIceCandidate || window.RTCIceCandidate;
URL = window.webkitURL || window.URL;
function trace(txt) {
console.log(txt);
}
// ------------------------------------------
// AJAX helper
// ------------------------------------------
function send(method,peerid,data,onSuccess,onFailure) {
trace("HTTP call "+ method);
try {
var r = new XMLHttpRequest();
r.open("POST",method, true);
r.setRequestHeader("Content-Type", "text/plain");
r.setRequestHeader("peerid", peerid);
r.onreadystatechange = function() {
if (this.readyState == 4) {
if ( (this.status == 200) && onSuccess ) {
onSuccess(this);
}
else if (onFailure) {
onFailure(this);
}
}
}
r.send(data);
r = null;
} catch (e) {
trace("send to peer:" + peerid + " error: " + e.description);
}
}
// ------------------------------------------
// RTCPeerConnection and its callbacks
// ------------------------------------------
function createPeerConnection() {
try {
trace("createPeerConnection config: " + JSON.stringify(pcConfig) + " option:"+ JSON.stringify(pcOptions));
pc = new RTCPeerConnection(pcConfig, pcOptions);
pc.onicecandidate = onIceCandidate;
pc.ontrack = onTrack;
pc.onaddstream = onAddStream;
trace("Created RTCPeerConnnection with config: " + JSON.stringify(pcConfig) + "option:"+ JSON.stringify(pcOptions) );
}
catch (e) {
trace("Failed to create PeerConnection with " + connectionId + ", exception: " + e.message);
}
return pc;
}
function onIceCandidate(event) {
if (event.candidate) {
send("/addicecandidate",pc.peerid,JSON.stringify(event.candidate));
} else {
trace("End of candidates.");
}
}
function onAddStream(event) {
trace("Remote stream added: " + JSON.stringify(event));
var remoteVideoElement = document.getElementById('remote-video');
remoteVideoElement.src = URL.createObjectURL(event.stream);
remoteVideoElement.play();
}
function onTrack(event) {
trace("Remote stream added:" + JSON.stringify(event));
var remoteVideoElement = document.getElementById('remote-video');
remoteVideoElement.src = URL.createObjectURL(event.streams[0]);
remoteVideoElement.play();
}
// ------------------------------------------
// AJAX callbacks
// ------------------------------------------
function onReceiveCall(request) {
trace("peerid: " + request.getResponseHeader("peerid") + " offer: " + request.responseText);
pc.peerid = request.getResponseHeader("peerid");
var dataJson = JSON.parse(request.responseText);
pc.setRemoteDescription(new RTCSessionDescription(dataJson)).then(function() {
send("/getIceCandidate", pc.peerid, null, onReceiveCandidate);
}).catch(function(error) {
trace ("setRemoteDescription error:" + JSON.stringify(error));
});
}
function onReceiveCandidate(request) {
trace("candidate: " + request.responseText);
var dataJson = JSON.parse(request.responseText);
if (dataJson)
{
for (var i=0; i<dataJson.length; i++)
{
var candidate = new RTCIceCandidate(dataJson[i]);
trace("Adding ICE candidate :" + JSON.stringify(candidate) );
pc.addIceCandidate(candidate)
.then(function() { trace ("addIceCandidate OK"); })
.catch(function(error) { trace ("addIceCandidate error:" + JSON.stringify(error)); });
}
}
}
// ------------------------------------------
// Connect button callback
// ------------------------------------------
function connect() {
var url = document.getElementById("url").value;
document.getElementById("connect").disabled = true;
document.getElementById("disconnect").disabled = false;
try {
pc = createPeerConnection();
// create Offer
pc.createOffer(mediaConstraints).then(function(sessionDescription) {
trace("Create offer:" + JSON.stringify(sessionDescription));
pc.setLocalDescription(sessionDescription).then(function() {
send("/call",url ,JSON.stringify(sessionDescription), onReceiveCall);
});
}).catch(function(error) {
alert("Create offer error:" + JSON.stringify(error));
});
} catch (e) {
disconnect();
alert("connect error: " + e);
}
}
// ------------------------------------------
// Disconnect button callback
// ------------------------------------------
function disconnect() {
if (pc) {
send("/hangup", pc.peerid);
try {
pc.close();
}
catch (e) {
trace ("Failure close peer connection:" + e);
}
pc = null;
var remoteVideoElement = document.getElementById('remote-video');
remoteVideoElement.src = 'data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
}
document.getElementById("connect").disabled = false;
document.getElementById("disconnect").disabled = true;
}
// ------------------------------------------
// init device list and callback
// ------------------------------------------
function onGetDeviceList(request) {
var deviceList = eval( "{" + request.responseText + "}" );
var urllist = document.getElementById("urlList");
for (var dev in deviceList)
{
var option = document.createElement("option");
option.text = deviceList[dev];
trace("device: " + option.text);
urllist.add(option);
}
var url = document.getElementById("url");
if (urllist.length > 0)
{
url.value=urllist[0].value;
}
else
{
url.value="";
}
}
function init() {
send("/getDeviceList", null, null, onGetDeviceList);
}
window.onload = init;
window.onbeforeunload = disconnect;
</script>
</head>
<body>
<div>
<select id="urlList" onchange="this.nextElementSibling.value=this.value">
</select>
<input type="text" id="url"/>
<button id="connect" onclick="connect();">Connect</button>
<button id="disconnect" onclick="disconnect();" disabled="true">Disconnect</button>
</div>
<br>
<div id="container">
<div id="remote">
<video id="remote-video" width="50%" height="50%"></video>
</div>
</div>
</body>
</html>