1 var RTCPeerConnection = null;
2 var getUserMedia = null;
3 var attachMediaStream = null;
4 var reattachMediaStream = null;
5 var webrtcDetectedBrowser = null;
6 var webrtcDetectedVersion = null;
9 // This function is used for logging.
10 if (text[text.length - 1] == '\n') {
11 text = text.substring(0, text.length - 1);
13 console.log((performance.now() / 1000).toFixed(3) + ": " + text);
16 if (navigator.mozGetUserMedia) {
17 console.log("This appears to be Firefox");
19 webrtcDetectedBrowser = "firefox";
21 webrtcDetectedVersion =
22 parseInt(navigator.userAgent.match(/Firefox\/([0-9]+)\./)[1]);
24 // The RTCPeerConnection object.
25 RTCPeerConnection = mozRTCPeerConnection;
27 // The RTCSessionDescription object.
28 RTCSessionDescription = mozRTCSessionDescription;
30 // The RTCIceCandidate object.
31 RTCIceCandidate = mozRTCIceCandidate;
33 // Get UserMedia (only difference is the prefix).
34 // Code from Adam Barth.
35 getUserMedia = navigator.mozGetUserMedia.bind(navigator);
37 // Creates iceServer from the url for FF.
38 createIceServer = function(url, username, password) {
40 var url_parts = url.split(':');
41 if (url_parts[0].indexOf('stun') === 0) {
42 // Create iceServer with stun url.
43 iceServer = { 'url': url };
44 } else if (url_parts[0].indexOf('turn') === 0 &&
45 (url.indexOf('transport=udp') !== -1 ||
46 url.indexOf('?transport') === -1)) {
47 // Create iceServer with turn url.
48 // Ignore the transport parameter from TURN url.
49 var turn_url_parts = url.split("?");
50 iceServer = { 'url': turn_url_parts[0],
51 'credential': password,
52 'username': username };
57 // Attach a media stream to an element.
58 attachMediaStream = function(element, stream) {
59 console.log("Attaching media stream");
60 element.mozSrcObject = stream;
64 reattachMediaStream = function(to, from) {
65 console.log("Reattaching media stream");
66 to.mozSrcObject = from.mozSrcObject;
70 } else if (navigator.webkitGetUserMedia) {
71 console.log("This appears to be Chrome");
73 webrtcDetectedBrowser = "chrome";
74 webrtcDetectedVersion =
75 parseInt(navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)[2]);
77 // Creates iceServer from the url for Chrome.
78 createIceServer = function(url, username, password) {
80 var url_parts = url.split(':');
81 if (url_parts[0].indexOf('stun') === 0) {
82 // Create iceServer with stun url.
83 iceServer = { 'url': url };
84 } else if (url_parts[0].indexOf('turn') === 0) {
85 if (webrtcDetectedVersion < 28) {
86 // For pre-M28 chrome versions use old TURN format.
87 var url_turn_parts = url.split("turn:");
88 iceServer = { 'url': 'turn:' + username + '@' + url_turn_parts[1],
89 'credential': password };
91 // For Chrome M28 & above use new TURN format.
92 iceServer = { 'url': url,
93 'credential': password,
94 'username': username };
100 // The RTCPeerConnection object.
101 RTCPeerConnection = webkitRTCPeerConnection;
103 // Get UserMedia (only difference is the prefix).
104 // Code from Adam Barth.
105 getUserMedia = navigator.webkitGetUserMedia.bind(navigator);
107 // Attach a media stream to an element.
108 attachMediaStream = function(element, stream) {
109 if (typeof element.srcObject !== 'undefined') {
110 element.srcObject = stream;
111 } else if (typeof element.mozSrcObject !== 'undefined') {
112 element.mozSrcObject = stream;
113 } else if (typeof element.src !== 'undefined') {
114 element.src = URL.createObjectURL(stream);
116 console.log('Error attaching stream to element.');
120 reattachMediaStream = function(to, from) {
124 // The representation of tracks in a stream is changed in M26.
125 // Unify them for earlier Chrome versions in the coexisting period.
126 if (!webkitMediaStream.prototype.getVideoTracks) {
127 webkitMediaStream.prototype.getVideoTracks = function() {
128 return this.videoTracks;
130 webkitMediaStream.prototype.getAudioTracks = function() {
131 return this.audioTracks;
135 // New syntax of getXXXStreams method in M26.
136 if (!webkitRTCPeerConnection.prototype.getLocalStreams) {
137 webkitRTCPeerConnection.prototype.getLocalStreams = function() {
138 return this.localStreams;
140 webkitRTCPeerConnection.prototype.getRemoteStreams = function() {
141 return this.remoteStreams;
145 console.log("Browser does not appear to be WebRTC-capable");