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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. if (um.indexOf('video') >= 0) {
  112. setResolutionConstraints(constraints, resolution, isAndroid);
  113. }
  114. if (bandwidth) { // doesn't work currently, see webrtc issue 1846
  115. if (!constraints.video) constraints.video = {mandatory: {}, optional: []};//same behaviour as true
  116. constraints.video.optional.push({bandwidth: bandwidth});
  117. }
  118. if (fps) { // for some cameras it might be necessary to request 30fps
  119. // so they choose 30fps mjpg over 10fps yuy2
  120. if (!constraints.video) constraints.video = {mandatory: {}, optional: []};// same behaviour as true;
  121. constraints.video.mandatory.minFrameRate = fps;
  122. }
  123. return constraints;
  124. }
  125. function RTCUtils(RTCService)
  126. {
  127. this.service = RTCService;
  128. if (navigator.mozGetUserMedia) {
  129. console.log('This appears to be Firefox');
  130. var version = parseInt(navigator.userAgent.match(/Firefox\/([0-9]+)\./)[1], 10);
  131. if (version >= 22) {
  132. this.peerconnection = mozRTCPeerConnection;
  133. this.browser = RTCBrowserType.RTC_BROWSER_FIREFOX;
  134. this.getUserMedia = navigator.mozGetUserMedia.bind(navigator);
  135. this.pc_constraints = {};
  136. this.attachMediaStream = function (element, stream) {
  137. element[0].mozSrcObject = stream;
  138. element[0].play();
  139. };
  140. this.getStreamID = function (stream) {
  141. var tracks = stream.getVideoTracks();
  142. if(!tracks || tracks.length == 0)
  143. {
  144. tracks = stream.getAudioTracks();
  145. }
  146. return tracks[0].id.replace(/[\{,\}]/g,"");
  147. };
  148. this.getVideoSrc = function (element) {
  149. return element.mozSrcObject;
  150. };
  151. this.setVideoSrc = function (element, src) {
  152. element.mozSrcObject = src;
  153. };
  154. RTCSessionDescription = mozRTCSessionDescription;
  155. RTCIceCandidate = mozRTCIceCandidate;
  156. }
  157. } else if (navigator.webkitGetUserMedia) {
  158. console.log('This appears to be Chrome');
  159. this.peerconnection = webkitRTCPeerConnection;
  160. this.browser = RTCBrowserType.RTC_BROWSER_CHROME;
  161. this.getUserMedia = navigator.webkitGetUserMedia.bind(navigator);
  162. this.attachMediaStream = function (element, stream) {
  163. element.attr('src', webkitURL.createObjectURL(stream));
  164. };
  165. this.getStreamID = function (stream) {
  166. // streams from FF endpoints have the characters '{' and '}'
  167. // that make jQuery choke.
  168. return stream.id.replace(/[\{,\}]/g,"");
  169. };
  170. this.getVideoSrc = function (element) {
  171. return element.getAttribute("src");
  172. };
  173. this.setVideoSrc = function (element, src) {
  174. element.setAttribute("src", src);
  175. };
  176. // DTLS should now be enabled by default but..
  177. this.pc_constraints = {'optional': [{'DtlsSrtpKeyAgreement': 'true'}]};
  178. if (navigator.userAgent.indexOf('Android') != -1) {
  179. this.pc_constraints = {}; // disable DTLS on Android
  180. }
  181. if (!webkitMediaStream.prototype.getVideoTracks) {
  182. webkitMediaStream.prototype.getVideoTracks = function () {
  183. return this.videoTracks;
  184. };
  185. }
  186. if (!webkitMediaStream.prototype.getAudioTracks) {
  187. webkitMediaStream.prototype.getAudioTracks = function () {
  188. return this.audioTracks;
  189. };
  190. }
  191. }
  192. else
  193. {
  194. try { console.log('Browser does not appear to be WebRTC-capable'); } catch (e) { }
  195. window.location.href = 'webrtcrequired.html';
  196. return;
  197. }
  198. if (this.browser !== RTCBrowserType.RTC_BROWSER_CHROME &&
  199. config.enableFirefoxSupport !== true) {
  200. window.location.href = 'chromeonly.html';
  201. return;
  202. }
  203. }
  204. RTCUtils.prototype.getUserMediaWithConstraints = function(
  205. um, success_callback, failure_callback, resolution,bandwidth, fps,
  206. desktopStream)
  207. {
  208. // Check if we are running on Android device
  209. var isAndroid = navigator.userAgent.indexOf('Android') != -1;
  210. var constraints = getConstraints(
  211. um, resolution, bandwidth, fps, desktopStream, isAndroid);
  212. var isFF = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
  213. try {
  214. if (config.enableSimulcast
  215. && constraints.video
  216. && constraints.video.chromeMediaSource !== 'screen'
  217. && constraints.video.chromeMediaSource !== 'desktop'
  218. && !isAndroid
  219. // We currently do not support FF, as it doesn't have multistream support.
  220. && !isFF) {
  221. APP.simulcast.getUserMedia(constraints, function (stream) {
  222. console.log('onUserMediaSuccess');
  223. success_callback(stream);
  224. },
  225. function (error) {
  226. console.warn('Failed to get access to local media. Error ', error);
  227. if (failure_callback) {
  228. failure_callback(error);
  229. }
  230. });
  231. } else {
  232. this.getUserMedia(constraints,
  233. function (stream) {
  234. console.log('onUserMediaSuccess');
  235. success_callback(stream);
  236. },
  237. function (error) {
  238. console.warn('Failed to get access to local media. Error ',
  239. error, constraints);
  240. if (failure_callback) {
  241. failure_callback(error);
  242. }
  243. });
  244. }
  245. } catch (e) {
  246. console.error('GUM failed: ', e);
  247. if(failure_callback) {
  248. failure_callback(e);
  249. }
  250. }
  251. };
  252. /**
  253. * We ask for audio and video combined stream in order to get permissions and
  254. * not to ask twice.
  255. */
  256. RTCUtils.prototype.obtainAudioAndVideoPermissions = function() {
  257. var self = this;
  258. // Get AV
  259. var cb = function (stream) {
  260. console.log('got', stream, stream.getAudioTracks().length, stream.getVideoTracks().length);
  261. self.handleLocalStream(stream);
  262. };
  263. var self = this;
  264. this.getUserMediaWithConstraints(
  265. ['audio', 'video'],
  266. cb,
  267. function (error) {
  268. console.error('failed to obtain audio/video stream - trying audio only', error);
  269. self.getUserMediaWithConstraints(
  270. ['audio'],
  271. cb,
  272. function (error) {
  273. console.error('failed to obtain audio/video stream - stop', error);
  274. APP.UI.messageHandler.showError("Error",
  275. "Failed to obtain permissions to use the local microphone" +
  276. "and/or camera.");
  277. }
  278. );
  279. },
  280. config.resolution || '360');
  281. }
  282. RTCUtils.prototype.handleLocalStream = function(stream)
  283. {
  284. if(window.webkitMediaStream)
  285. {
  286. var audioStream = new webkitMediaStream();
  287. var videoStream = new webkitMediaStream();
  288. var audioTracks = stream.getAudioTracks();
  289. var videoTracks = stream.getVideoTracks();
  290. for (var i = 0; i < audioTracks.length; i++) {
  291. audioStream.addTrack(audioTracks[i]);
  292. }
  293. this.service.createLocalStream(audioStream, "audio");
  294. for (i = 0; i < videoTracks.length; i++) {
  295. videoStream.addTrack(videoTracks[i]);
  296. }
  297. this.service.createLocalStream(videoStream, "video");
  298. }
  299. else
  300. {//firefox
  301. this.service.createLocalStream(stream, "stream");
  302. }
  303. };
  304. module.exports = RTCUtils;