1/**
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS.  All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11// This file is cloned from samples/js/base/adapter.js
12// Modify the original and do new copy instead of doing changes here.
13
14var RTCPeerConnection = null;
15var getUserMedia = null;
16var attachMediaStream = null;
17var reattachMediaStream = null;
18var webrtcDetectedBrowser = null;
19var webrtcDetectedVersion = null;
20
21function trace(text) {
22  // This function is used for logging.
23  if (text[text.length - 1] == '\n') {
24    text = text.substring(0, text.length - 1);
25  }
26  console.log((performance.now() / 1000).toFixed(3) + ": " + text);
27}
28function maybeFixConfiguration(pcConfig) {
29  if (pcConfig == null) {
30    return;
31  }
32  for (var i = 0; i < pcConfig.iceServers.length; i++) {
33    if (pcConfig.iceServers[i].hasOwnProperty('urls')){
34      pcConfig.iceServers[i]['url'] = pcConfig.iceServers[i]['urls'];
35      delete pcConfig.iceServers[i]['urls'];
36    }
37  }
38}
39
40if (navigator.mozGetUserMedia) {
41  console.log("This appears to be Firefox");
42
43  webrtcDetectedBrowser = "firefox";
44
45  webrtcDetectedVersion =
46           parseInt(navigator.userAgent.match(/Firefox\/([0-9]+)\./)[1], 10);
47
48  // The RTCPeerConnection object.
49  var RTCPeerConnection = function(pcConfig, pcConstraints) {
50    // .urls is not supported in FF yet.
51    maybeFixConfiguration(pcConfig);
52    return new mozRTCPeerConnection(pcConfig, pcConstraints);
53  }
54
55  // The RTCSessionDescription object.
56  RTCSessionDescription = mozRTCSessionDescription;
57
58  // The RTCIceCandidate object.
59  RTCIceCandidate = mozRTCIceCandidate;
60
61  // Get UserMedia (only difference is the prefix).
62  // Code from Adam Barth.
63  getUserMedia = navigator.mozGetUserMedia.bind(navigator);
64  navigator.getUserMedia = getUserMedia;
65
66  // Creates iceServer from the url for FF.
67  createIceServer = function(url, username, password) {
68    var iceServer = null;
69    var url_parts = url.split(':');
70    if (url_parts[0].indexOf('stun') === 0) {
71      // Create iceServer with stun url.
72      iceServer = { 'url': url };
73    } else if (url_parts[0].indexOf('turn') === 0) {
74      if (webrtcDetectedVersion < 27) {
75        // Create iceServer with turn url.
76        // Ignore the transport parameter from TURN url for FF version <=27.
77        var turn_url_parts = url.split("?");
78        // Return null for createIceServer if transport=tcp.
79        if (turn_url_parts.length === 1 ||
80            turn_url_parts[1].indexOf('transport=udp') === 0) {
81          iceServer = {'url': turn_url_parts[0],
82                       'credential': password,
83                       'username': username};
84        }
85      } else {
86        // FF 27 and above supports transport parameters in TURN url,
87        // So passing in the full url to create iceServer.
88        iceServer = {'url': url,
89                     'credential': password,
90                     'username': username};
91      }
92    }
93    return iceServer;
94  };
95
96  createIceServers = function(urls, username, password) {
97    var iceServers = [];
98    // Use .url for FireFox.
99    for (i = 0; i < urls.length; i++) {
100      var iceServer = createIceServer(urls[i],
101                                      username,
102                                      password);
103      if (iceServer !== null) {
104        iceServers.push(iceServer);
105      }
106    }
107    return iceServers;
108  }
109
110  // Attach a media stream to an element.
111  attachMediaStream = function(element, stream) {
112    console.log("Attaching media stream");
113    element.mozSrcObject = stream;
114    element.play();
115  };
116
117  reattachMediaStream = function(to, from) {
118    console.log("Reattaching media stream");
119    to.mozSrcObject = from.mozSrcObject;
120    to.play();
121  };
122
123  // Fake get{Video,Audio}Tracks
124  if (!MediaStream.prototype.getVideoTracks) {
125    MediaStream.prototype.getVideoTracks = function() {
126      return [];
127    };
128  }
129
130  if (!MediaStream.prototype.getAudioTracks) {
131    MediaStream.prototype.getAudioTracks = function() {
132      return [];
133    };
134  }
135} else if (navigator.webkitGetUserMedia) {
136  console.log("This appears to be Chrome");
137
138  webrtcDetectedBrowser = "chrome";
139  webrtcDetectedVersion =
140         parseInt(navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)[2], 10);
141
142  // Creates iceServer from the url for Chrome M33 and earlier.
143  createIceServer = function(url, username, password) {
144    var iceServer = null;
145    var url_parts = url.split(':');
146    if (url_parts[0].indexOf('stun') === 0) {
147      // Create iceServer with stun url.
148      iceServer = { 'url': url };
149    } else if (url_parts[0].indexOf('turn') === 0) {
150      // Chrome M28 & above uses below TURN format.
151      iceServer = {'url': url,
152                   'credential': password,
153                   'username': username};
154    }
155    return iceServer;
156  };
157
158  // Creates iceServers from the urls for Chrome M34 and above.
159  createIceServers = function(urls, username, password) {
160    var iceServers = [];
161    if (webrtcDetectedVersion >= 34) {
162      // .urls is supported since Chrome M34.
163      iceServers = {'urls': urls,
164                    'credential': password,
165                    'username': username };
166    } else {
167      for (i = 0; i < urls.length; i++) {
168        var iceServer = createIceServer(urls[i],
169                                        username,
170                                        password);
171        if (iceServer !== null) {
172          iceServers.push(iceServer);
173        }
174      }
175    }
176    return iceServers;
177  };
178
179  // The RTCPeerConnection object.
180  var RTCPeerConnection = function(pcConfig, pcConstraints) {
181    // .urls is supported since Chrome M34.
182    if (webrtcDetectedVersion < 34) {
183      maybeFixConfiguration(pcConfig);
184    }
185    return new webkitRTCPeerConnection(pcConfig, pcConstraints);
186  }
187
188  // Get UserMedia (only difference is the prefix).
189  // Code from Adam Barth.
190  getUserMedia = navigator.webkitGetUserMedia.bind(navigator);
191  navigator.getUserMedia = getUserMedia;
192
193  // Attach a media stream to an element.
194  attachMediaStream = function(element, stream) {
195    if (typeof element.srcObject !== 'undefined') {
196      element.srcObject = stream;
197    } else if (typeof element.mozSrcObject !== 'undefined') {
198      element.mozSrcObject = stream;
199    } else if (typeof element.src !== 'undefined') {
200      element.src = URL.createObjectURL(stream);
201    } else {
202      console.log('Error attaching stream to element.');
203    }
204  };
205
206  reattachMediaStream = function(to, from) {
207    to.src = from.src;
208  };
209} else {
210  console.log("Browser does not appear to be WebRTC-capable");
211}
212