You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
6.7 KiB

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