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 12KB

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