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.

RTCUtils.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. var RTCBrowserType = require("../../service/RTC/RTCBrowserType.js");
  2. function setResolutionConstraints(constraints, resolution, isAndroid)
  3. {
  4. if (resolution && !constraints.video || isAndroid) {
  5. constraints.video = { mandatory: {}, optional: [] };// same behaviour as true
  6. }
  7. // see https://code.google.com/p/chromium/issues/detail?id=143631#c9 for list of supported resolutions
  8. switch (resolution) {
  9. // 16:9 first
  10. case '1080':
  11. case 'fullhd':
  12. constraints.video.mandatory.minWidth = 1920;
  13. constraints.video.mandatory.minHeight = 1080;
  14. break;
  15. case '720':
  16. case 'hd':
  17. constraints.video.mandatory.minWidth = 1280;
  18. constraints.video.mandatory.minHeight = 720;
  19. break;
  20. case '360':
  21. constraints.video.mandatory.minWidth = 640;
  22. constraints.video.mandatory.minHeight = 360;
  23. break;
  24. case '180':
  25. constraints.video.mandatory.minWidth = 320;
  26. constraints.video.mandatory.minHeight = 180;
  27. break;
  28. // 4:3
  29. case '960':
  30. constraints.video.mandatory.minWidth = 960;
  31. constraints.video.mandatory.minHeight = 720;
  32. break;
  33. case '640':
  34. case 'vga':
  35. constraints.video.mandatory.minWidth = 640;
  36. constraints.video.mandatory.minHeight = 480;
  37. break;
  38. case '320':
  39. constraints.video.mandatory.minWidth = 320;
  40. constraints.video.mandatory.minHeight = 240;
  41. break;
  42. default:
  43. if (isAndroid) {
  44. constraints.video.mandatory.minWidth = 320;
  45. constraints.video.mandatory.minHeight = 240;
  46. constraints.video.mandatory.maxFrameRate = 15;
  47. }
  48. break;
  49. }
  50. if (constraints.video.mandatory.minWidth)
  51. constraints.video.mandatory.maxWidth = constraints.video.mandatory.minWidth;
  52. if (constraints.video.mandatory.minHeight)
  53. constraints.video.mandatory.maxHeight = constraints.video.mandatory.minHeight;
  54. }
  55. function getConstraints(um, resolution, bandwidth, fps, desktopStream, isAndroid)
  56. {
  57. var constraints = {audio: false, video: false};
  58. if (um.indexOf('video') >= 0) {
  59. constraints.video = { mandatory: {}, optional: [] };// same behaviour as true
  60. }
  61. if (um.indexOf('audio') >= 0) {
  62. constraints.audio = { mandatory: {}, optional: []};// same behaviour as true
  63. }
  64. if (um.indexOf('screen') >= 0) {
  65. constraints.video = {
  66. mandatory: {
  67. chromeMediaSource: "screen",
  68. googLeakyBucket: true,
  69. maxWidth: window.screen.width,
  70. maxHeight: window.screen.height,
  71. maxFrameRate: 3
  72. },
  73. optional: []
  74. };
  75. }
  76. if (um.indexOf('desktop') >= 0) {
  77. constraints.video = {
  78. mandatory: {
  79. chromeMediaSource: "desktop",
  80. chromeMediaSourceId: desktopStream,
  81. googLeakyBucket: true,
  82. maxWidth: window.screen.width,
  83. maxHeight: window.screen.height,
  84. maxFrameRate: 3
  85. },
  86. optional: []
  87. };
  88. }
  89. if (constraints.audio) {
  90. // if it is good enough for hangouts...
  91. constraints.audio.optional.push(
  92. {googEchoCancellation: true},
  93. {googAutoGainControl: true},
  94. {googNoiseSupression: true},
  95. {googHighpassFilter: true},
  96. {googNoisesuppression2: true},
  97. {googEchoCancellation2: true},
  98. {googAutoGainControl2: true}
  99. );
  100. }
  101. if (constraints.video) {
  102. constraints.video.optional.push(
  103. {googNoiseReduction: false} // chrome 37 workaround for issue 3807, reenable in M38
  104. );
  105. if (um.indexOf('video') >= 0) {
  106. constraints.video.optional.push(
  107. {googLeakyBucket: true}
  108. );
  109. }
  110. }
  111. setResolutionConstraints(constraints, resolution, isAndroid);
  112. if (bandwidth) { // doesn't work currently, see webrtc issue 1846
  113. if (!constraints.video) constraints.video = {mandatory: {}, optional: []};//same behaviour as true
  114. constraints.video.optional.push({bandwidth: bandwidth});
  115. }
  116. if (fps) { // for some cameras it might be necessary to request 30fps
  117. // so they choose 30fps mjpg over 10fps yuy2
  118. if (!constraints.video) constraints.video = {mandatory: {}, optional: []};// same behaviour as true;
  119. constraints.video.mandatory.minFrameRate = fps;
  120. }
  121. return constraints;
  122. }
  123. function RTCUtils(RTCService)
  124. {
  125. this.service = RTCService;
  126. if (navigator.mozGetUserMedia) {
  127. console.log('This appears to be Firefox');
  128. var version = parseInt(navigator.userAgent.match(/Firefox\/([0-9]+)\./)[1], 10);
  129. if (version >= 22) {
  130. this.peerconnection = mozRTCPeerConnection;
  131. this.browser = RTCBrowserType.RTC_BROWSER_FIREFOX;
  132. this.getUserMedia = navigator.mozGetUserMedia.bind(navigator);
  133. this.pc_constraints = {};
  134. this.attachMediaStream = function (element, stream) {
  135. element[0].mozSrcObject = stream;
  136. element[0].play();
  137. };
  138. this.getStreamID = function (stream) {
  139. var tracks = stream.getVideoTracks();
  140. if(!tracks || tracks.length == 0)
  141. {
  142. tracks = stream.getAudioTracks();
  143. }
  144. return tracks[0].id.replace(/[\{,\}]/g,"");
  145. };
  146. this.getVideoSrc = function (element) {
  147. return element.mozSrcObject;
  148. };
  149. this.setVideoSrc = function (element, src) {
  150. element.mozSrcObject = src;
  151. };
  152. RTCSessionDescription = mozRTCSessionDescription;
  153. RTCIceCandidate = mozRTCIceCandidate;
  154. }
  155. } else if (navigator.webkitGetUserMedia) {
  156. console.log('This appears to be Chrome');
  157. this.peerconnection = webkitRTCPeerConnection;
  158. this.browser = RTCBrowserType.RTC_BROWSER_CHROME;
  159. this.getUserMedia = navigator.webkitGetUserMedia.bind(navigator);
  160. this.attachMediaStream = function (element, stream) {
  161. element.attr('src', webkitURL.createObjectURL(stream));
  162. };
  163. this.getStreamID = function (stream) {
  164. // streams from FF endpoints have the characters '{' and '}'
  165. // that make jQuery choke.
  166. return stream.id.replace(/[\{,\}]/g,"");
  167. };
  168. this.getVideoSrc = function (element) {
  169. return element.getAttribute("src");
  170. };
  171. this.setVideoSrc = function (element, src) {
  172. element.setAttribute("src", src);
  173. };
  174. // DTLS should now be enabled by default but..
  175. this.pc_constraints = {'optional': [{'DtlsSrtpKeyAgreement': 'true'}]};
  176. if (navigator.userAgent.indexOf('Android') != -1) {
  177. this.pc_constraints = {}; // disable DTLS on Android
  178. }
  179. if (!webkitMediaStream.prototype.getVideoTracks) {
  180. webkitMediaStream.prototype.getVideoTracks = function () {
  181. return this.videoTracks;
  182. };
  183. }
  184. if (!webkitMediaStream.prototype.getAudioTracks) {
  185. webkitMediaStream.prototype.getAudioTracks = function () {
  186. return this.audioTracks;
  187. };
  188. }
  189. }
  190. else
  191. {
  192. try { console.log('Browser does not appear to be WebRTC-capable'); } catch (e) { }
  193. window.location.href = 'webrtcrequired.html';
  194. return;
  195. }
  196. if (this.browser !== RTCBrowserType.RTC_BROWSER_CHROME &&
  197. config.enableFirefoxSupport !== true) {
  198. window.location.href = 'chromeonly.html';
  199. return;
  200. }
  201. }
  202. RTCUtils.prototype.getUserMediaWithConstraints = function(
  203. um, success_callback, failure_callback, resolution,bandwidth, fps,
  204. desktopStream)
  205. {
  206. // Check if we are running on Android device
  207. var isAndroid = navigator.userAgent.indexOf('Android') != -1;
  208. var constraints = getConstraints(
  209. um, resolution, bandwidth, fps, desktopStream, isAndroid);
  210. var isFF = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
  211. try {
  212. if (config.enableSimulcast
  213. && constraints.video
  214. && constraints.video.chromeMediaSource !== 'screen'
  215. && constraints.video.chromeMediaSource !== 'desktop'
  216. && !isAndroid
  217. // We currently do not support FF, as it doesn't have multistream support.
  218. && !isFF) {
  219. APP.simulcast.getUserMedia(constraints, function (stream) {
  220. console.log('onUserMediaSuccess');
  221. success_callback(stream);
  222. },
  223. function (error) {
  224. console.warn('Failed to get access to local media. Error ', error);
  225. if (failure_callback) {
  226. failure_callback(error);
  227. }
  228. });
  229. } else {
  230. this.getUserMedia(constraints,
  231. function (stream) {
  232. console.log('onUserMediaSuccess');
  233. success_callback(stream);
  234. },
  235. function (error) {
  236. console.warn('Failed to get access to local media. Error ',
  237. error, constraints);
  238. if (failure_callback) {
  239. failure_callback(error);
  240. }
  241. });
  242. }
  243. } catch (e) {
  244. console.error('GUM failed: ', e);
  245. if(failure_callback) {
  246. failure_callback(e);
  247. }
  248. }
  249. };
  250. /**
  251. * We ask for audio and video combined stream in order to get permissions and
  252. * not to ask twice.
  253. */
  254. RTCUtils.prototype.obtainAudioAndVideoPermissions = function() {
  255. var self = this;
  256. // Get AV
  257. var cb = function (stream) {
  258. console.log('got', stream, stream.getAudioTracks().length, stream.getVideoTracks().length);
  259. self.handleLocalStream(stream);
  260. };
  261. var self = this;
  262. this.getUserMediaWithConstraints(
  263. ['audio', 'video'],
  264. cb,
  265. function (error) {
  266. console.error('failed to obtain audio/video stream - trying audio only', error);
  267. self.getUserMediaWithConstraints(
  268. ['audio'],
  269. cb,
  270. function (error) {
  271. console.error('failed to obtain audio/video stream - stop', error);
  272. APP.UI.messageHandler.showError("Error",
  273. "Failed to obtain permissions to use the local microphone" +
  274. "and/or camera.");
  275. }
  276. );
  277. },
  278. config.resolution || '360');
  279. }
  280. RTCUtils.prototype.handleLocalStream = function(stream)
  281. {
  282. if(window.webkitMediaStream)
  283. {
  284. var audioStream = new webkitMediaStream();
  285. var videoStream = new webkitMediaStream();
  286. var audioTracks = stream.getAudioTracks();
  287. var videoTracks = stream.getVideoTracks();
  288. for (var i = 0; i < audioTracks.length; i++) {
  289. audioStream.addTrack(audioTracks[i]);
  290. }
  291. this.service.createLocalStream(audioStream, "audio");
  292. for (i = 0; i < videoTracks.length; i++) {
  293. videoStream.addTrack(videoTracks[i]);
  294. }
  295. this.service.createLocalStream(videoStream, "video");
  296. }
  297. else
  298. {//firefox
  299. this.service.createLocalStream(stream, "stream");
  300. }
  301. };
  302. module.exports = RTCUtils;