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.

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