blob: 803ef150cb4b0803a32aa535027e2369deb4bbfc [file] [log] [blame]
vikasmarwaha@webrtc.org98fce152013-02-27 23:22:10 +00001var RTCPeerConnection = null;
2var getUserMedia = null;
3var attachMediaStream = null;
4var reattachMediaStream = null;
5var webrtcDetectedBrowser = null;
vikasmarwaha@webrtc.orgfddf6be2013-05-29 22:13:19 +00006var webrtcDetectedVersion = null;
vikasmarwaha@webrtc.org98fce152013-02-27 23:22:10 +00007
vikasmarwaha@webrtc.orga856db22013-03-05 03:35:26 +00008function trace(text) {
9 // This function is used for logging.
10 if (text[text.length - 1] == '\n') {
11 text = text.substring(0, text.length - 1);
12 }
13 console.log((performance.now() / 1000).toFixed(3) + ": " + text);
14}
15
vikasmarwaha@webrtc.org98fce152013-02-27 23:22:10 +000016if (navigator.mozGetUserMedia) {
17 console.log("This appears to be Firefox");
18
19 webrtcDetectedBrowser = "firefox";
20
vikasmarwaha@webrtc.orgbb252562013-06-25 14:52:51 +000021 webrtcDetectedVersion =
vikasmarwaha@webrtc.org90d87192013-10-22 18:02:41 +000022 parseInt(navigator.userAgent.match(/Firefox\/([0-9]+)\./)[1], 10);
vikasmarwaha@webrtc.orgbb252562013-06-25 14:52:51 +000023
vikasmarwaha@webrtc.org98fce152013-02-27 23:22:10 +000024 // The RTCPeerConnection object.
25 RTCPeerConnection = mozRTCPeerConnection;
26
27 // The RTCSessionDescription object.
28 RTCSessionDescription = mozRTCSessionDescription;
29
30 // The RTCIceCandidate object.
31 RTCIceCandidate = mozRTCIceCandidate;
32
33 // Get UserMedia (only difference is the prefix).
34 // Code from Adam Barth.
35 getUserMedia = navigator.mozGetUserMedia.bind(navigator);
36
vikasmarwaha@webrtc.orgbb252562013-06-25 14:52:51 +000037 // Creates iceServer from the url for FF.
38 createIceServer = function(url, username, password) {
39 var iceServer = null;
40 var url_parts = url.split(':');
41 if (url_parts[0].indexOf('stun') === 0) {
42 // Create iceServer with stun url.
43 iceServer = { 'url': url };
vikasmarwaha@webrtc.org442c5e42013-10-24 20:31:57 +000044 } else if (url_parts[0].indexOf('turn') === 0) {
45 if (webrtcDetectedVersion < 27) {
46 // Create iceServer with turn url.
47 // Ignore the transport parameter from TURN url for FF version <=27.
48 var turn_url_parts = url.split("?");
49 // Return null for createIceServer if transport=tcp.
50 if (turn_url_parts[1].indexOf('transport=udp') === 0) {
51 iceServer = { 'url': turn_url_parts[0],
52 'credential': password,
53 'username': username };
54 }
55 } else {
56 // FF 27 and above supports transport parameters in TURN url,
57 // So passing in the full url to create iceServer.
58 iceServer = { 'url': url,
59 'credential': password,
60 'username': username };
61 }
vikasmarwaha@webrtc.orgbb252562013-06-25 14:52:51 +000062 }
vikasmarwaha@webrtc.orgfddf6be2013-05-29 22:13:19 +000063 return iceServer;
64 };
65
vikasmarwaha@webrtc.org98fce152013-02-27 23:22:10 +000066 // Attach a media stream to an element.
67 attachMediaStream = function(element, stream) {
68 console.log("Attaching media stream");
69 element.mozSrcObject = stream;
70 element.play();
71 };
72
73 reattachMediaStream = function(to, from) {
74 console.log("Reattaching media stream");
75 to.mozSrcObject = from.mozSrcObject;
76 to.play();
77 };
78
79 // Fake get{Video,Audio}Tracks
80 MediaStream.prototype.getVideoTracks = function() {
81 return [];
82 };
83
84 MediaStream.prototype.getAudioTracks = function() {
85 return [];
86 };
87} else if (navigator.webkitGetUserMedia) {
88 console.log("This appears to be Chrome");
89
90 webrtcDetectedBrowser = "chrome";
vikasmarwaha@webrtc.orgfddf6be2013-05-29 22:13:19 +000091 webrtcDetectedVersion =
vikasmarwaha@webrtc.org90d87192013-10-22 18:02:41 +000092 parseInt(navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)[2], 10);
vikasmarwaha@webrtc.orgfddf6be2013-05-29 22:13:19 +000093
vikasmarwaha@webrtc.orgbb252562013-06-25 14:52:51 +000094 // Creates iceServer from the url for Chrome.
95 createIceServer = function(url, username, password) {
96 var iceServer = null;
97 var url_parts = url.split(':');
98 if (url_parts[0].indexOf('stun') === 0) {
99 // Create iceServer with stun url.
100 iceServer = { 'url': url };
101 } else if (url_parts[0].indexOf('turn') === 0) {
vikasmarwaha@webrtc.org442c5e42013-10-24 20:31:57 +0000102 // Chrome M28 & above uses below TURN format.
103 iceServer = { 'url': url,
104 'credential': password,
105 'username': username };
vikasmarwaha@webrtc.orgbb252562013-06-25 14:52:51 +0000106 }
107 return iceServer;
108 };
vikasmarwaha@webrtc.org98fce152013-02-27 23:22:10 +0000109
110 // The RTCPeerConnection object.
111 RTCPeerConnection = webkitRTCPeerConnection;
braveyao@webrtc.org07db4a62013-03-06 03:35:03 +0000112
vikasmarwaha@webrtc.org98fce152013-02-27 23:22:10 +0000113 // Get UserMedia (only difference is the prefix).
114 // Code from Adam Barth.
115 getUserMedia = navigator.webkitGetUserMedia.bind(navigator);
116
117 // Attach a media stream to an element.
118 attachMediaStream = function(element, stream) {
vikasmarwaha@webrtc.org77ac8482013-04-25 23:22:03 +0000119 if (typeof element.srcObject !== 'undefined') {
120 element.srcObject = stream;
121 } else if (typeof element.mozSrcObject !== 'undefined') {
122 element.mozSrcObject = stream;
123 } else if (typeof element.src !== 'undefined') {
124 element.src = URL.createObjectURL(stream);
125 } else {
126 console.log('Error attaching stream to element.');
127 }
vikasmarwaha@webrtc.org98fce152013-02-27 23:22:10 +0000128 };
129
130 reattachMediaStream = function(to, from) {
131 to.src = from.src;
132 };
vikasmarwaha@webrtc.org98fce152013-02-27 23:22:10 +0000133} else {
134 console.log("Browser does not appear to be WebRTC-capable");
135}