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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 >= 38
  121. && !config.enableSimulcast && config.useBundle && config.useRtcpMux) {
  122. this.peerconnection = mozRTCPeerConnection;
  123. this.browser = RTCBrowserType.RTC_BROWSER_FIREFOX;
  124. this.getUserMedia = navigator.mozGetUserMedia.bind(navigator);
  125. this.pc_constraints = {};
  126. this.attachMediaStream = function (element, stream) {
  127. // srcObject is being standardized and FF will eventually
  128. // support that unprefixed. FF also supports the
  129. // "element.src = URL.createObjectURL(...)" combo, but that
  130. // will be deprecated in favour of srcObject.
  131. //
  132. // https://groups.google.com/forum/#!topic/mozilla.dev.media/pKOiioXonJg
  133. // https://github.com/webrtc/samples/issues/302
  134. element[0].mozSrcObject = stream;
  135. element[0].play();
  136. };
  137. this.getStreamID = function (stream) {
  138. var tracks = stream.getVideoTracks();
  139. if(!tracks || tracks.length == 0)
  140. {
  141. tracks = stream.getAudioTracks();
  142. }
  143. return tracks[0].id.replace(/[\{,\}]/g,"");
  144. };
  145. this.getVideoSrc = function (element) {
  146. return element.mozSrcObject;
  147. };
  148. this.setVideoSrc = function (element, src) {
  149. element.mozSrcObject = src;
  150. };
  151. RTCSessionDescription = mozRTCSessionDescription;
  152. RTCIceCandidate = mozRTCIceCandidate;
  153. } else {
  154. window.location.href = 'unsupported_browser.html';
  155. return;
  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 = 'unsupported_browser.html';
  196. return;
  197. }
  198. }
  199. RTCUtils.prototype.getUserMediaWithConstraints = function(
  200. um, success_callback, failure_callback, resolution,bandwidth, fps,
  201. desktopStream)
  202. {
  203. currentResolution = resolution;
  204. // Check if we are running on Android device
  205. var isAndroid = navigator.userAgent.indexOf('Android') != -1;
  206. var constraints = getConstraints(
  207. um, resolution, bandwidth, fps, desktopStream, isAndroid);
  208. var isFF = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
  209. var self = this;
  210. try {
  211. if (config.enableSimulcast
  212. && constraints.video
  213. && constraints.video.chromeMediaSource !== 'screen'
  214. && constraints.video.chromeMediaSource !== 'desktop'
  215. && !isAndroid
  216. // We currently do not support FF, as it doesn't have multistream support.
  217. && !isFF) {
  218. APP.simulcast.getUserMedia(constraints, function (stream) {
  219. console.log('onUserMediaSuccess');
  220. self.setAvailableDevices(um, true);
  221. success_callback(stream);
  222. },
  223. function (error) {
  224. console.warn('Failed to get access to local media. Error ', error);
  225. self.setAvailableDevices(um, false);
  226. if (failure_callback) {
  227. failure_callback(error);
  228. }
  229. });
  230. } else {
  231. this.getUserMedia(constraints,
  232. function (stream) {
  233. console.log('onUserMediaSuccess');
  234. self.setAvailableDevices(um, true);
  235. success_callback(stream);
  236. },
  237. function (error) {
  238. self.setAvailableDevices(um, false);
  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. RTCUtils.prototype.setAvailableDevices = function (um, available) {
  254. var devices = {};
  255. if(um.indexOf("video") != -1)
  256. {
  257. devices.video = available;
  258. }
  259. if(um.indexOf("audio") != -1)
  260. {
  261. devices.audio = available;
  262. }
  263. this.service.setDeviceAvailability(devices);
  264. }
  265. /**
  266. * We ask for audio and video combined stream in order to get permissions and
  267. * not to ask twice.
  268. */
  269. RTCUtils.prototype.obtainAudioAndVideoPermissions = function(devices, callback) {
  270. var self = this;
  271. // Get AV
  272. if(!devices)
  273. devices = ['audio', 'video'];
  274. this.getUserMediaWithConstraints(
  275. devices,
  276. function (stream) {
  277. if(callback)
  278. callback(stream);
  279. else
  280. self.successCallback(stream);
  281. },
  282. function (error) {
  283. self.errorCallback(error);
  284. },
  285. config.resolution || '360');
  286. }
  287. RTCUtils.prototype.successCallback = function (stream) {
  288. if(stream)
  289. console.log('got', stream, stream.getAudioTracks().length,
  290. stream.getVideoTracks().length);
  291. this.handleLocalStream(stream);
  292. };
  293. RTCUtils.prototype.errorCallback = function (error) {
  294. var self = this;
  295. console.error('failed to obtain audio/video stream - trying audio only', error);
  296. var resolution = getPreviousResolution(currentResolution);
  297. if(typeof error == "object" && error.constraintName && error.name
  298. && (error.name == "ConstraintNotSatisfiedError" ||
  299. error.name == "OverconstrainedError") &&
  300. (error.constraintName == "minWidth" || error.constraintName == "maxWidth" ||
  301. error.constraintName == "minHeight" || error.constraintName == "maxHeight")
  302. && resolution != null)
  303. {
  304. self.getUserMediaWithConstraints(['audio', 'video'],
  305. function (stream) {
  306. return self.successCallback(stream);
  307. }, function (error) {
  308. return self.errorCallback(error);
  309. }, resolution);
  310. }
  311. else
  312. {
  313. self.getUserMediaWithConstraints(
  314. ['audio'],
  315. function (stream) {
  316. return self.successCallback(stream);
  317. },
  318. function (error) {
  319. console.error('failed to obtain audio/video stream - stop',
  320. error);
  321. return self.successCallback(null);
  322. }
  323. );
  324. }
  325. }
  326. RTCUtils.prototype.handleLocalStream = function(stream)
  327. {
  328. if(window.webkitMediaStream)
  329. {
  330. var audioStream = new webkitMediaStream();
  331. var videoStream = new webkitMediaStream();
  332. if(stream) {
  333. var audioTracks = stream.getAudioTracks();
  334. for (var i = 0; i < audioTracks.length; i++) {
  335. audioStream.addTrack(audioTracks[i]);
  336. }
  337. var videoTracks = stream.getVideoTracks();
  338. for (i = 0; i < videoTracks.length; i++) {
  339. videoStream.addTrack(videoTracks[i]);
  340. }
  341. }
  342. this.service.createLocalStream(audioStream, "audio");
  343. this.service.createLocalStream(videoStream, "video");
  344. }
  345. else
  346. {//firefox
  347. this.service.createLocalStream(stream, "stream");
  348. }
  349. };
  350. RTCUtils.prototype.createVideoStream = function(stream)
  351. {
  352. var videoStream = null;
  353. if(window.webkitMediaStream)
  354. {
  355. videoStream = new webkitMediaStream();
  356. if(stream)
  357. {
  358. var videoTracks = stream.getVideoTracks();
  359. for (i = 0; i < videoTracks.length; i++) {
  360. videoStream.addTrack(videoTracks[i]);
  361. }
  362. }
  363. }
  364. else
  365. videoStream = stream;
  366. return videoStream;
  367. };
  368. module.exports = RTCUtils;