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

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