forked from muaz-khan/DetectRTC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DetectRTC.js
406 lines (328 loc) · 13.9 KB
/
DetectRTC.js
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// Last time updated at May 05, 2015, 08:32:23
// Latest file can be found here: https://cdn.webrtc-experiment.com/DetectRTC.js
// Muaz Khan - www.MuazKhan.com
// MIT License - www.WebRTC-Experiment.com/licence
// Documentation - github.com/muaz-khan/DetectRTC
// ____________
// DetectRTC.js
// DetectRTC.hasWebcam (has webcam device!)
// DetectRTC.hasMicrophone (has microphone device!)
// DetectRTC.hasSpeakers (has speakers!)
// DetectRTC.isScreenCapturingSupported
// DetectRTC.isSctpDataChannelsSupported
// DetectRTC.isRtpDataChannelsSupported
// DetectRTC.isAudioContextSupported
// DetectRTC.isWebRTCSupported
// DetectRTC.isDesktopCapturingSupported
// DetectRTC.isMobileDevice
// DetectRTC.isWebSocketsSupported
// DetectRTC.DetectLocalIPAddress(callback)
// ----------todo: add
// DetectRTC.videoResolutions
// DetectRTC.screenResolutions
(function() {
'use strict';
function warn(log) {
if (window.console && typeof window.console.warn !== 'undefined') {
console.warn(log);
}
}
// detect node-webkit
var browser = getBrowserInfo();
// is this a chromium browser (opera or chrome)
var isOpera = !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
var isFirefox = typeof InstallTrigger !== 'undefined';
var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0;
var isChrome = !!window.chrome && !isOpera;
var isIE = !!document.documentMode;
var isMobileDevice = !!navigator.userAgent.match(/Android|iPhone|iPad|iPod|BlackBerry|IEMobile/i);
// detect node-webkit
var isNodeWebkit = !!(window.process && (typeof window.process === 'object') && window.process.versions && window.process.versions['node-webkit']);
var isHTTPs = location.protocol === 'https:';
if (navigator.mediaDevices && navigator.mediaDevices.enumerateDevices) {
// Firefox 38+ seems having support of enumerateDevices
// Thanks @xdumaine/enumerateDevices
navigator.enumerateDevices = function(callback) {
navigator.mediaDevices.enumerateDevices().then(callback);
};
}
window.DetectRTC = {
browser: browser,
hasMicrophone: navigator.getMediaDevices || navigator.enumerateDevices ? false : 'unable to detect',
hasSpeakers: navigator.getMediaDevices || navigator.enumerateDevices ? false : 'unable to detect',
hasWebcam: navigator.getMediaDevices || navigator.enumerateDevices ? false : 'unable to detect',
isWebRTCSupported: !!window.webkitRTCPeerConnection || !!window.mozRTCPeerConnection,
isAudioContextSupported: (!!window.AudioContext && !!window.AudioContext.prototype.createMediaStreamSource) || (!!window.webkitAudioContext && !!window.webkitAudioContext.prototype.createMediaStreamSource),
isScreenCapturingSupported: (isFirefox && browser.version >= 33) ||
(isChrome && browser.version >= 26 && (isNodeWebkit ? true : location.protocol === 'https:')),
isDesktopCapturingSupported: isHTTPs && ((isFirefox && browser.version >= 33) || (isChrome && browser.version >= 34) || isNodeWebkit || false),
isSctpDataChannelsSupported: isFirefox || (isChrome && browser.version >= 25),
isRtpDataChannelsSupported: isChrome && browser.version >= 31,
isMobileDevice: !!navigator.userAgent.match(/Android|iPhone|iPad|iPod|BlackBerry|IEMobile/i),
isWebSocketsSupported: 'WebSocket' in window && 2 === window.WebSocket.CLOSING
};
if (!isHTTPs) {
window.DetectRTC.isScreenCapturingSupported =
window.DetectRTC.isDesktopCapturingSupported = 'Requires HTTPs.';
}
DetectRTC.browser = {
isFirefox: isFirefox,
isChrome: isChrome,
isMobileDevice: isMobileDevice,
isNodeWebkit: isNodeWebkit,
isSafari: isSafari,
isIE: isIE,
isOpera: isOpera,
name: browser.name,
version: browser.version
};
var osName = 'Unknown OS';
if (navigator.appVersion.indexOf('Win') !== -1) {
osName = 'Windows';
}
if (navigator.appVersion.indexOf('Mac') !== -1) {
osName = 'MacOS';
}
if (navigator.appVersion.indexOf('X11') !== -1) {
osName = 'UNIX';
}
if (navigator.appVersion.indexOf('Linux') !== -1) {
osName = 'Linux';
}
DetectRTC.osName = osName;
DetectRTC.MediaDevices = [];
if (!navigator.getMediaDevices) {
warn('navigator.getMediaDevices API are not available.');
}
if (!navigator.enumerateDevices) {
warn('navigator.enumerateDevices API are not available.');
}
if (!window.MediaStreamTrack || !window.MediaStreamTrack.getSources) {
warn('MediaStreamTrack.getSources are not available.');
}
// http://dev.w3.org/2011/webrtc/editor/getusermedia.html#mediadevices
// todo: switch to enumerateDevices when landed in canary.
function CheckDeviceSupport(callback) {
// This method is useful only for Chrome!
if (!navigator.getMediaDevices && window.MediaStreamTrack && window.MediaStreamTrack.getSources) {
navigator.getMediaDevices = window.MediaStreamTrack.getSources.bind(window.MediaStreamTrack);
}
if (!navigator.getMediaDevices && navigator.enumerateDevices) {
navigator.getMediaDevices = navigator.enumerateDevices.bind(navigator);
}
// if still no 'getMediaDevices'; it MUST be Firefox!
if (!navigator.getMediaDevices) {
warn('navigator.getMediaDevices is undefined.');
// assuming that it is older chrome or chromium implementation
if (isChrome) {
DetectRTC.hasMicrophone = true;
DetectRTC.hasSpeakers = true;
DetectRTC.hasWebcam = true;
}
if (callback) {
callback();
}
return;
}
DetectRTC.MediaDevices = [];
navigator.getMediaDevices(function(devices) {
devices.forEach(function(_device) {
var device = {};
for (var d in _device) {
device[d] = _device[d];
}
var skip;
DetectRTC.MediaDevices.forEach(function(d) {
if (d.id === device.id) {
skip = true;
}
});
if (skip) {
return;
}
// if it is MediaStreamTrack.getSources
if (device.kind === 'audio') {
device.kind = 'audioinput';
}
if (device.kind === 'video') {
device.kind = 'videoinput';
}
if (!device.deviceId) {
device.deviceId = device.id;
}
if (!device.id) {
device.id = device.deviceId;
}
if (!device.label) {
device.label = 'Please invoke getUserMedia once.';
}
if (device.kind === 'audioinput' || device.kind === 'audio') {
DetectRTC.hasMicrophone = true;
}
if (device.kind === 'audiooutput') {
DetectRTC.hasSpeakers = true;
}
if (device.kind === 'videoinput' || device.kind === 'video') {
DetectRTC.hasWebcam = true;
}
// there is no 'videoouput' in the spec.
DetectRTC.MediaDevices.push(device);
});
if (callback) {
callback();
}
});
}
// check for microphone/camera support!
new CheckDeviceSupport();
DetectRTC.load = CheckDeviceSupport;
function getBrowserInfo() {
var nVer = navigator.appVersion;
var nAgt = navigator.userAgent;
var browserName = navigator.appName;
var fullVersion = '' + parseFloat(navigator.appVersion);
var majorVersion = parseInt(navigator.appVersion, 10);
var nameOffset, verOffset, ix;
// In Opera, the true version is after 'Opera' or after 'Version'
if ((verOffset = nAgt.indexOf('Opera')) !== -1) {
browserName = 'Opera';
fullVersion = nAgt.substring(verOffset + 6);
if ((verOffset = nAgt.indexOf('Version')) !== -1) {
fullVersion = nAgt.substring(verOffset + 8);
}
}
// In MSIE, the true version is after 'MSIE' in userAgent
else if ((verOffset = nAgt.indexOf('MSIE')) !== -1) {
browserName = 'IE';
fullVersion = nAgt.substring(verOffset + 5);
}
// In Chrome, the true version is after 'Chrome'
else if ((verOffset = nAgt.indexOf('Chrome')) !== -1) {
browserName = 'Chrome';
fullVersion = nAgt.substring(verOffset + 7);
}
// In Safari, the true version is after 'Safari' or after 'Version'
else if ((verOffset = nAgt.indexOf('Safari')) !== -1) {
browserName = 'Safari';
fullVersion = nAgt.substring(verOffset + 7);
if ((verOffset = nAgt.indexOf('Version')) !== -1) {
fullVersion = nAgt.substring(verOffset + 8);
}
}
// In Firefox, the true version is after 'Firefox'
else if ((verOffset = nAgt.indexOf('Firefox')) !== -1) {
browserName = 'Firefox';
fullVersion = nAgt.substring(verOffset + 8);
}
// In most other browsers, 'name/version' is at the end of userAgent
else if ((nameOffset = nAgt.lastIndexOf(' ') + 1) < (verOffset = nAgt.lastIndexOf('/'))) {
browserName = nAgt.substring(nameOffset, verOffset);
fullVersion = nAgt.substring(verOffset + 1);
if (browserName.toLowerCase() === browserName.toUpperCase()) {
browserName = navigator.appName;
}
}
// trim the fullVersion string at semicolon/space if present
if ((ix = fullVersion.indexOf(';')) !== -1) {
fullVersion = fullVersion.substring(0, ix);
}
if ((ix = fullVersion.indexOf(' ')) !== -1) {
fullVersion = fullVersion.substring(0, ix);
}
majorVersion = parseInt('' + fullVersion, 10);
if (isNaN(majorVersion)) {
fullVersion = '' + parseFloat(navigator.appVersion);
majorVersion = parseInt(navigator.appVersion, 10);
}
return {
fullVersion: fullVersion,
version: majorVersion,
name: browserName
};
}
// via: https://github.com/diafygi/webrtc-ips
DetectRTC.DetectLocalIPAddress = function(callback) {
getIPs(function(ip) {
//local IPs
if (ip.match(/^(192\.168\.|169\.254\.|10\.|172\.(1[6-9]|2\d|3[01]))/)) {
callback('Local: ' + ip);
}
//assume the rest are public IPs
else {
callback('Public: ' + ip);
}
});
};
//get the IP addresses associated with an account
function getIPs(callback) {
var ipDuplicates = {};
//compatibility for firefox and chrome
var RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
var useWebKit = !!window.webkitRTCPeerConnection;
// bypass naive webrtc blocking using an iframe
if (!RTCPeerConnection) {
var iframe = document.getElementById('iframe');
if (!iframe) {
//<iframe id="iframe" sandbox="allow-same-origin" style="display: none"></iframe>
throw 'NOTE: you need to have an iframe in the page right above the script tag.';
}
var win = iframe.contentWindow;
RTCPeerConnection = win.RTCPeerConnection || win.mozRTCPeerConnection || win.webkitRTCPeerConnection;
useWebKit = !!win.webkitRTCPeerConnection;
}
//minimal requirements for data connection
var mediaConstraints = {
optional: [{
RtpDataChannels: true
}]
};
//firefox already has a default stun server in about:config
// media.peerconnection.default_iceservers =
// [{"url": "stun:stun.services.mozilla.com"}]
var servers;
//add same stun server for chrome
if (useWebKit) {
servers = {
iceServers: [{
urls: 'stun:stun.services.mozilla.com'
}]
};
}
//construct a new RTCPeerConnection
var pc = new RTCPeerConnection(servers, mediaConstraints);
function handleCandidate(candidate) {
//match just the IP address
var ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3})/;
var ipAddress = ipRegex.exec(candidate)[1];
//remove duplicates
if (ipDuplicates[ipAddress] === undefined) {
callback(ipAddress);
}
ipDuplicates[ipAddress] = true;
}
//listen for candidate events
pc.onicecandidate = function(ice) {
//skip non-candidate events
if (ice.candidate) {
handleCandidate(ice.candidate.candidate);
}
};
//create a bogus data channel
pc.createDataChannel('');
//create an offer sdp
pc.createOffer(function(result) {
//trigger the stun server request
pc.setLocalDescription(result, function() {}, function() {});
}, function() {});
//wait for a while to let everything done
setTimeout(function() {
//read candidate info from local description
var lines = pc.localDescription.sdp.split('\n');
lines.forEach(function(line) {
if (line.indexOf('a=candidate:') === 0) {
handleCandidate(line);
}
});
}, 1000);
}
})();