forked from butorenjin/easyrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapter.js
184 lines (178 loc) · 7.09 KB
/
adapter.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
//
// the below code is a copy of the standard polyfill adapter.js
//
var getUserMedia = null;
var attachMediaStream = null;
var reattachMediaStream = null;
var webrtcDetectedBrowser = null;
var webrtcDetectedVersion = null;
if (navigator.mozGetUserMedia) {
// console.log("This appears to be Firefox");
webrtcDetectedBrowser = "firefox";
//
// better version detection for gecko based browsers provided by
// Kévin Poulet.
//
var matches = navigator.userAgent.match(/\srv:([0-9]+)\./);
if (matches !== null && matches.length > 1) {
webrtcDetectedVersion = parseInt(matches[1]);
}
// The RTCPeerConnection object.
window.RTCPeerConnection = mozRTCPeerConnection;
// The RTCSessionDescription object.
window.RTCSessionDescription = mozRTCSessionDescription;
// The RTCIceCandidate object.
window.RTCIceCandidate = mozRTCIceCandidate;
// Get UserMedia (only difference is the prefix).
// Code from Adam Barth.
window.getUserMedia = navigator.mozGetUserMedia.bind(navigator);
// Creates iceServer from the url for FF.
window.createIceServer = function(url, username, password) {
var iceServer = null;
var url_parts = url.split(':');
var turn_url_parts;
if (url_parts[0].indexOf('stun') === 0) {
// Create iceServer with stun url.
iceServer = {'url': url};
} else if (url_parts[0].indexOf('turn') === 0 &&
(url.indexOf('transport=udp') !== -1 ||
url.indexOf('?transport') === -1)) {
// Create iceServer with turn url.
// Ignore the transport parameter from TURN url.
turn_url_parts = url.split("?");
iceServer = {'url': turn_url_parts[0],
'credential': password,
'username': username};
}
return iceServer;
};
// Attach a media stream to an element.
attachMediaStream = function(element, stream) {
// console.log("Attaching media stream");
element.mozSrcObject = stream;
element.play();
};
reattachMediaStream = function(to, from) {
// console.log("Reattaching media stream");
to.mozSrcObject = from.mozSrcObject;
to.play();
};
if (webrtcDetectedVersion < 23) {
// Fake get{Video,Audio}Tracks
MediaStream.prototype.getVideoTracks = function() {
return [];
};
MediaStream.prototype.getAudioTracks = function() {
return [];
};
}
} else if (navigator.webkitGetUserMedia) {
// console.log("This appears to be Chrome");
webrtcDetectedBrowser = "chrome";
webrtcDetectedVersion =
parseInt(navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)[2]);
// Creates iceServer from the url for Chrome.
window.createIceServer = function(url, username, password) {
var iceServer = null;
var url_turn_parts;
var url_parts = url.split(':');
if (url_parts[0].indexOf('stun') === 0) {
// Create iceServer with stun url.
iceServer = {'url': url};
} else if (url_parts[0].indexOf('turn') === 0) {
if (webrtcDetectedVersion < 28) {
// For pre-M28 chrome versions use old TURN format.
url_turn_parts = url.split("turn:");
iceServer = {'url': 'turn:' + username + '@' + url_turn_parts[1],
'credential': password};
} else {
// For Chrome M28 & above use new TURN format.
iceServer = {'url': url,
'credential': password,
'username': username};
}
}
return iceServer;
};
// The RTCPeerConnection object.
window.RTCPeerConnection = webkitRTCPeerConnection;
// Get UserMedia (only difference is the prefix).
// Code from Adam Barth.
window.getUserMedia = navigator.webkitGetUserMedia.bind(navigator);
// Attach a media stream to an element.
attachMediaStream = function(element, stream) {
if (typeof element.srcObject !== 'undefined') {
element.srcObject = stream;
} else if (typeof element.mozSrcObject !== 'undefined') {
element.mozSrcObject = stream;
} else if (typeof element.src !== 'undefined') {
element.src = URL.createObjectURL(stream);
} else {
console.log('Error attaching stream to element.');
}
};
reattachMediaStream = function(to, from) {
to.src = from.src;
};
// The representation of tracks in a stream is changed in M26.
// Unify them for earlier Chrome versions in the coexisting period.
if (!webkitMediaStream.prototype.getVideoTracks) {
webkitMediaStream.prototype.getVideoTracks = function() {
return this.videoTracks;
};
webkitMediaStream.prototype.getAudioTracks = function() {
return this.audioTracks;
};
}
// New syntax of getXXXStreams method in M26.
if (!webkitRTCPeerConnection.prototype.getLocalStreams) {
webkitRTCPeerConnection.prototype.getLocalStreams = function() {
return this.localStreams;
};
webkitRTCPeerConnection.prototype.getRemoteStreams = function() {
return this.remoteStreams;
};
}
//} else if( window.ActiveXObject ){ // appears to IE so check for the wrapper.
// var head = document.getElementsByTagName('head')[0];
// var i;
// var adapterAddress;
// var wrapperPresent = false;
//
// //
// // we look for the adapter as well as the wrapper because if we don't find the
// // wrapper, we'll look for it in the same directory as the adapter was found.
// //
// for( i = 0; i < head.childNodes.length; i++) {
// var child = head.childNodes[i];
// if( /\/adapter.js$/.test(child.src)) {
// adapterAddress = child.src;
// }
// else if( /\/rtcplugin.js$/.test(child.src)) {
// wrapperPresent = true;
// }
// }
//
//
// if( wrapperPresent) {
// addIEDeclarations();
// }
// else if( adapterAddress) {
// var script = document.createElement('script');
// script.type = 'text/javascript';
// script.src = adapterAddress.replace(/\/adapter.js$/, "/rtcplugin.js");
// src.onload = addIEDeclarations;
// src.onerror = function () {
// alert("Developer error: this page requires the Priologic IE Webrtc plugin wrapper (rtcplugin.js) to run when using Internet Explorer, which the developer has not supplied.");
// throw new Error("No rtcplugin.js found. It should be in the same folder as your adapter.js or you can include it yourself before the adapter.js");
// }
// head.appendChild(script);
// }
} else {
console.log("Browser does not appear to be WebRTC-capable");
}
if (!window.createIceServer) {
window.createIceServer = function(url, username, credential) {
return {'url': url, 'credential': credential, 'username': username};
};
}