Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / test / data / webrtc / adapter.js
blob09352b8f58cd128a11b7f67ed62883b65faef985
1 var RTCPeerConnection = null;
2 var getUserMedia = null;
3 var attachMediaStream = null;
4 var reattachMediaStream = null;
5 var webrtcDetectedBrowser = null;
6 var webrtcDetectedVersion = null;
8 function trace(text) {
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) {
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 };
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 };
54 return iceServer;
57 // Attach a media stream to an element.
58 attachMediaStream = function(element, stream) {
59 console.log("Attaching media stream");
60 element.mozSrcObject = stream;
61 element.play();
64 reattachMediaStream = function(to, from) {
65 console.log("Reattaching media stream");
66 to.mozSrcObject = from.mozSrcObject;
67 to.play();
70 // Fake get{Video,Audio}Tracks
71 MediaStream.prototype.getVideoTracks = function() {
72 return [];
75 MediaStream.prototype.getAudioTracks = function() {
76 return [];
78 } else if (navigator.webkitGetUserMedia) {
79 console.log("This appears to be Chrome");
81 webrtcDetectedBrowser = "chrome";
82 webrtcDetectedVersion =
83 parseInt(navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)[2]);
85 // Creates iceServer from the url for Chrome.
86 createIceServer = function(url, username, password) {
87 var iceServer = null;
88 var url_parts = url.split(':');
89 if (url_parts[0].indexOf('stun') === 0) {
90 // Create iceServer with stun url.
91 iceServer = { 'url': url };
92 } else if (url_parts[0].indexOf('turn') === 0) {
93 if (webrtcDetectedVersion < 28) {
94 // For pre-M28 chrome versions use old TURN format.
95 var url_turn_parts = url.split("turn:");
96 iceServer = { 'url': 'turn:' + username + '@' + url_turn_parts[1],
97 'credential': password };
98 } else {
99 // For Chrome M28 & above use new TURN format.
100 iceServer = { 'url': url,
101 'credential': password,
102 'username': username };
105 return iceServer;
108 // The RTCPeerConnection object.
109 RTCPeerConnection = webkitRTCPeerConnection;
111 // Get UserMedia (only difference is the prefix).
112 // Code from Adam Barth.
113 getUserMedia = navigator.webkitGetUserMedia.bind(navigator);
115 // Attach a media stream to an element.
116 attachMediaStream = function(element, stream) {
117 if (typeof element.srcObject !== 'undefined') {
118 element.srcObject = stream;
119 } else if (typeof element.mozSrcObject !== 'undefined') {
120 element.mozSrcObject = stream;
121 } else if (typeof element.src !== 'undefined') {
122 element.src = URL.createObjectURL(stream);
123 } else {
124 console.log('Error attaching stream to element.');
128 reattachMediaStream = function(to, from) {
129 to.src = from.src;
132 // The representation of tracks in a stream is changed in M26.
133 // Unify them for earlier Chrome versions in the coexisting period.
134 if (!webkitMediaStream.prototype.getVideoTracks) {
135 webkitMediaStream.prototype.getVideoTracks = function() {
136 return this.videoTracks;
138 webkitMediaStream.prototype.getAudioTracks = function() {
139 return this.audioTracks;
143 // New syntax of getXXXStreams method in M26.
144 if (!webkitRTCPeerConnection.prototype.getLocalStreams) {
145 webkitRTCPeerConnection.prototype.getLocalStreams = function() {
146 return this.localStreams;
148 webkitRTCPeerConnection.prototype.getRemoteStreams = function() {
149 return this.remoteStreams;
152 } else {
153 console.log("Browser does not appear to be WebRTC-capable");