-
Notifications
You must be signed in to change notification settings - Fork 104
/
WebRTC.htm
executable file
·332 lines (293 loc) · 12.9 KB
/
WebRTC.htm
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<HTML>
<HEAD>
<TITLE>ATL 8.0 test page for object WebRTC</TITLE>
<meta http-equiv="X-UA-Compatible" content="IE=9,chrome=1" />
<style type="text/css">
.overlay {
position: absolute;
margin-top: -0px;
}
</style>
</HEAD>
<script type="text/javascript">
var mediaStream = null;
var peerConnection = null;
var localStreams = null;
var remoteStreams = null;
var video = null;
function get_plugin() {
return document.getElementById('WebrtcEverywhere');
//return new ActiveXObject("webrtceverywhere.WebRTC");
}
function _log_info(msg) {
if (window.console && window.console.info) {
window.console.info(msg);
}
}
function _stop() {
_attachStream(video, null);
if (mediaStream) {
mediaStream.stop();
}
localStreams = null;
remoteStreams = null;
mediaStream = null;
if (peerConnection) {
peerConnection.close();
}
peerConnection = null;
}
var logs = {};
function _peerConnection() {
if (mediaStream) {
peerConnection = get_plugin().createPeerConnection(
{ "iceServers": [{ "url": "stun:stun.l.google.com:19302" }, { "url": "stun:stun.l.google.com:19302", "credential": "mypassword", "username": "myusername" }] }, // RTCConfiguration configuration
{} // optional MediaConstraints constraints
);
peerConnection.onicecandidate = function onicecandidate(_candidate) {
if (_candidate.candidate) {
_log_info("onicecandidate(candidate=" + _candidate.candidate + ", sdpMid=" + _candidate.sdpMid + ", sdpMLineIndex=" + _candidate.sdpMLineIndex + ")");
peerConnection.addIceCandidate(_candidate,
function () { _log_info("addIceCandidate ok"); },
function (err) { _log_info("addIceCandidate nok:" + err); }
);
}
else {
_log_info("Ice gathering returned null. *iceGatheringState= " + peerConnection.iceGatheringState + " *localDescription= " + peerConnection.localDescription.sdp);
}
}
var onnegotiationneeded = function () {
_log_info("onnegotiationneeded");
};
peerConnection.onnegotiationneeded = function () {
_log_info("onnegotiationneeded");
}
peerConnection.onsignalingstatechange = function () {
_log_info("onsignalingstatechange: " + peerConnection.signalingState);
}
peerConnection.onaddstream = function (e) {
_log_info("onaddstream");
}
peerConnection.onremovestream = function (e) {
_log_info("onremovestream");
}
peerConnection.oniceconnectionstatechange = function () {
_log_info("oniceconnectionstatechange: " + peerConnection.iceConnectionState);
}
peerConnection.addStream(mediaStream, {});
localStreams = peerConnection.getLocalStreams();
remoteStreams = peerConnection.getLocalStreams();
if (localStreams.length > 0) {
var strm = peerConnection.getStreamById(localStreams[0].id);
if (strm) {
strm.clone(); // for fun
strm.onended = function () {
_log_info("MediaStream::onended()");
}
strm.onaddtrack = function () {
_log_info("MediaStream::onaddtrack()");
}
strm.onremovetrack = function () {
_log_info("MediaStream::onremovetrack()");
}
_log_info("found stream with id = " + strm.id);
var audioTracks = strm.getAudioTracks();
var videoTracks = strm.getVideoTracks();
if (audioTracks) {
_log_info("Audio Tracks count: " + audioTracks.length);
for (var i = 0; i < audioTracks.length; ++i) {
var track = strm.getTrackById(audioTracks[i].id);
if (track) {
_log_info("Track: id=" + track.id + ", kind=" + track.kind + ", label=" + track.label);
strm.removeTrack(track);
strm.addTrack(track);
}
}
}
if (videoTracks) {
_log_info("Video Tracks count: " + videoTracks.length);
}
}
}
peerConnection.createOffer(
function (sdp) {
_log_info("PeerConnection Success: type=" + sdp.type + "\n and sdp=" + sdp.sdp);
/*peerConnection.setLocalDescription(sdp,
function () { _log_info("setLocalDescription ok"); },
function (err) { _log_info("setLocalDescription nok:" + err); }
);*/
},
function (error) { alert("PeerConnection Error: " + error); },
{} // optional MediaConstraints constraints
);
}
}
function _attachStream(element, stream) {
if (!element) return;
if (element.isWebRtcPlugin) {
element.src = stream;
return element;
}
else if (element.nodeName.toLowerCase() === 'video') {
if (!element.pluginObj && stream) {
// FIXME
var _pluginObj = document.createElement('object');
_pluginObj.innerHTML = '<param name=\"windowless\" value=\"false\">';
var _isIE = (Object.getOwnPropertyDescriptor && Object.getOwnPropertyDescriptor(window, "ActiveXObject")) || ("ActiveXObject" in window);
if (_isIE) {
_pluginObj.setAttribute('classid', 'CLSID:7FD49E23-C8D7-4C4F-93A1-F7EACFA1EC53');
} else {
_pluginObj.setAttribute('type', 'application/webrtc-everywhere');
}
element.pluginObj = _pluginObj;
if (_isIE || 1) {
_pluginObj.style = element.style;
_pluginObj.setAttribute('className', element.className);
_pluginObj.setAttribute('innerHTML', element.innerHTML);
var width = element.getAttribute("width");
var height = element.getAttribute("height");
var bounds = element.getBoundingClientRect();
if (!width) {
width = bounds.right - bounds.left;;
}
if (!height) {
height = bounds.bottom - bounds.top;
}
if ("getComputedStyle" in window) {
var computedStyle = window.getComputedStyle(element, null);
if (!width && computedStyle.width != 'auto' && computedStyle.width != '0px') {
width = computedStyle.width;
}
if (!height && computedStyle.height != 'auto' && computedStyle.height != '0px') {
height = computedStyle.height;
}
}
if (width) _pluginObj.setAttribute('width', width);
if (height) _pluginObj.setAttribute('height', height);
document.body.appendChild(_pluginObj);
if (element.parentNode) {
element.parentNode.replaceChild(_pluginObj, element);
}
}
else {
//_pluginObj.setAttribute('height', '100%');
//_pluginObj.setAttribute('width', '100%');
// FIXME
document.body.appendChild(_pluginObj);
//element.appendChild(_pluginObj);
}
}
if (element.pluginObj) {
element.pluginObj.src = stream;
}
return element.pluginObj;
}
else if (element.nodeName.toLowerCase() === 'audio') {
return element;
}
}
function _getUserMedia() {
alert(get_plugin().versionName);
//get_plugin().setVideo(myVideo);
//var sdp = get_plugin().createSessionDescription({ type:"offer", sdp:"..my sdp" });
//if (sdp) {
// alert(sdp.sdp + "//" + sdp.type);
//}
//var peer = get_plugin().createPeerConnection({"iceServers": [{"url": "stun:stun.l.google.com:19302"}]});
//if (peer) {
// peer.createOffer(function(e) { alert("Success="+e); });
// alert("peer=ok");
//}
// var candidate = get_plugin().createIceCandidate({ candidate: "candidateStr", sdpMid: "sdpMidStr", sdpMLineIndex: 75 });
get_plugin().getUserMedia(
{
audio: true,
video: {
mandatory: {
minFrameRate: 15,
maxFrameRate: 25.5,
minWidth: 1280,
minHeight: 720,
maxWidth: 1280,
maxHeight: 720
},
optional: []
}
}, // constraints
function (_mediaStream) {
//alert("successCallback:" + _mediaStream.id);
mediaStream = _mediaStream;
video = _attachStream(video || document.getElementById("myvideo_local"), _mediaStream);
//video = video_local;
//video_local.src = _mediaStream;
video.bindEventListener('play', function () {
_log_info("Actual video dimensions: " + video.videoWidth + "x" + video.videoHeight + 'px.');
});
},
function (error) { alert("errorCallback:" + error); }
);
}
function _screenshot() {
if (video) {
if (video.videoWidth > 0 && video.videoHeight > 0) {
var elCanvas = document.getElementById("mycanvas");
var ctx2d = elCanvas.getContext("2d");
if (typeof video.getScreenShot !== "undefined") {
var bmpBase64 = video.getScreenShot();
if (bmpBase64) {
var image = new Image();
image.onload = function () {
ctx2d.drawImage(image, 0, 0, elCanvas.width, elCanvas.height);
};
image.src = "data:image/png;base64," + bmpBase64;
}
}
else {
var imageData = ctx2d.createImageData(video.videoWidth, video.videoHeight);
if (imageData) {
video.fillImageData(imageData);
ctx2d.putImageData(imageData, 0, 0); // at (0,0)
}
}
}
}
}
window.onload = function () {
var completeStyle = window.getComputedStyle(myvideo_local, null);
// IE11: "window.ActiveXObject" not defined -> http://msdn.microsoft.com/en-us/library/ie/dn423948%28v=vs.85%29.aspx
var pluginObj = document.createElement('object');
pluginObj.innerHTML = '<param name=\"windowless\" value=\"false\">';
pluginObj.setAttribute('id', 'WebrtcEverywhere');
if ((Object.getOwnPropertyDescriptor && Object.getOwnPropertyDescriptor(window, "ActiveXObject")) || ("ActiveXObject" in window)) {
pluginObj.setAttribute('classid', 'CLSID:7FD49E23-C8D7-4C4F-93A1-F7EACFA1EC53');
} else {
pluginObj.setAttribute('type', 'application/webrtc-everywhere');
}
document.body.appendChild(pluginObj);
pluginObj.setAttribute('width', '0');
pluginObj.setAttribute('height', '0');
}
</script>
<body>
<div>
<div style="border:0px solid #000; margin-top: 42px; z-index: 10">
<video id="myvideo_local" style="background-color: #000000; margin-top: 42px; z-index: 10">
<!--object id="video_local" width="500px" height="500px" type="application/webrtc-everywhere"></object-->
</video>
</div>
<div>
<iframe class="overlay" style="border:0px solid #009; z-index: 1000"> </iframe>
<div id="div2" class="overlay" style='border:0px solid #009; z-index: 10000'>
<input type="button" value="getUserMedia" onclick='_getUserMedia();' />
<input type="button" value="screenshot" onclick='_screenshot();' />
<input type="button" value="peerConnection" onclick='_peerConnection();' />
<input type="button" value="stop" onclick='_stop();' />
</div>
</div>
</div>
<canvas id="mycanvas" width="250px" height="250px" style="margin-top: 72px; border:1px solid #000; z-index: 10"></canvas>
<!--object id="video_local" width="50px" height="50px" classid="clsid:7fd49e23-c8d7-4c4f-93a1-f7eacfa1ec53" style="background-color: #000000;">
<param name="windowless" value=true>
</object-->
</body>
</HTML>