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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. var RTCBrowserType = require("./RTCBrowserType");
  2. var Resolutions = require("../../service/RTC/Resolutions");
  3. var AdapterJS = require("./adapter.screenshare");
  4. var SDPUtil = require("../xmpp/SDPUtil");
  5. var currentResolution = null;
  6. function getPreviousResolution(resolution) {
  7. if(!Resolutions[resolution])
  8. return null;
  9. var order = Resolutions[resolution].order;
  10. var res = null;
  11. var resName = null;
  12. for(var i in Resolutions)
  13. {
  14. var tmp = Resolutions[i];
  15. if(res == null || (res.order < tmp.order && tmp.order < order))
  16. {
  17. resName = i;
  18. res = tmp;
  19. }
  20. }
  21. return resName;
  22. }
  23. function setResolutionConstraints(constraints, resolution, isAndroid)
  24. {
  25. if (resolution && !constraints.video || isAndroid) {
  26. constraints.video = { mandatory: {}, optional: [] };// same behaviour as true
  27. }
  28. if(Resolutions[resolution])
  29. {
  30. constraints.video.mandatory.minWidth = Resolutions[resolution].width;
  31. constraints.video.mandatory.minHeight = Resolutions[resolution].height;
  32. }
  33. else
  34. {
  35. if (isAndroid) {
  36. constraints.video.mandatory.minWidth = 320;
  37. constraints.video.mandatory.minHeight = 240;
  38. constraints.video.mandatory.maxFrameRate = 15;
  39. }
  40. }
  41. if (constraints.video.mandatory.minWidth)
  42. constraints.video.mandatory.maxWidth = constraints.video.mandatory.minWidth;
  43. if (constraints.video.mandatory.minHeight)
  44. constraints.video.mandatory.maxHeight = constraints.video.mandatory.minHeight;
  45. }
  46. function getConstraints(um, resolution, bandwidth, fps, desktopStream, isAndroid)
  47. {
  48. var constraints = {audio: false, video: false};
  49. if (um.indexOf('video') >= 0) {
  50. constraints.video = { mandatory: {}, optional: [] };// same behaviour as true
  51. }
  52. if (um.indexOf('audio') >= 0) {
  53. constraints.audio = { mandatory: {}, optional: []};// same behaviour as true
  54. }
  55. if (um.indexOf('screen') >= 0) {
  56. if (RTCBrowserType.isChrome()) {
  57. constraints.video = {
  58. mandatory: {
  59. chromeMediaSource: "screen",
  60. googLeakyBucket: true,
  61. maxWidth: window.screen.width,
  62. maxHeight: window.screen.height,
  63. maxFrameRate: 3
  64. },
  65. optional: []
  66. };
  67. } else if (RTCBrowserType.isTemasysPluginUsed()) {
  68. constraints.video = {
  69. optional: [
  70. {
  71. sourceId: AdapterJS.WebRTCPlugin.plugin.screensharingKey
  72. }
  73. ]
  74. };
  75. } else {
  76. console.error(
  77. "'screen' WebRTC media source is supported only in Chrome" +
  78. " and with Temasys plugin");
  79. }
  80. }
  81. if (um.indexOf('desktop') >= 0) {
  82. constraints.video = {
  83. mandatory: {
  84. chromeMediaSource: "desktop",
  85. chromeMediaSourceId: desktopStream,
  86. googLeakyBucket: true,
  87. maxWidth: window.screen.width,
  88. maxHeight: window.screen.height,
  89. maxFrameRate: 3
  90. },
  91. optional: []
  92. };
  93. }
  94. if (constraints.audio) {
  95. // if it is good enough for hangouts...
  96. constraints.audio.optional.push(
  97. {googEchoCancellation: true},
  98. {googAutoGainControl: true},
  99. {googNoiseSupression: true},
  100. {googHighpassFilter: true},
  101. {googNoisesuppression2: true},
  102. {googEchoCancellation2: true},
  103. {googAutoGainControl2: true}
  104. );
  105. }
  106. if (constraints.video) {
  107. constraints.video.optional.push(
  108. {googNoiseReduction: false} // chrome 37 workaround for issue 3807, reenable in M38
  109. );
  110. if (um.indexOf('video') >= 0) {
  111. constraints.video.optional.push(
  112. {googLeakyBucket: true}
  113. );
  114. }
  115. }
  116. if (um.indexOf('video') >= 0) {
  117. setResolutionConstraints(constraints, resolution, isAndroid);
  118. }
  119. if (bandwidth) { // doesn't work currently, see webrtc issue 1846
  120. if (!constraints.video) constraints.video = {mandatory: {}, optional: []};//same behaviour as true
  121. constraints.video.optional.push({bandwidth: bandwidth});
  122. }
  123. if (fps) { // for some cameras it might be necessary to request 30fps
  124. // so they choose 30fps mjpg over 10fps yuy2
  125. if (!constraints.video) constraints.video = {mandatory: {}, optional: []};// same behaviour as true;
  126. constraints.video.mandatory.minFrameRate = fps;
  127. }
  128. return constraints;
  129. }
  130. function RTCUtils(RTCService, onTemasysPluginReady)
  131. {
  132. var self = this;
  133. this.service = RTCService;
  134. if (RTCBrowserType.isFirefox()) {
  135. var FFversion = RTCBrowserType.getFirefoxVersion();
  136. if (FFversion >= 40 && config.useBundle && config.useRtcpMux) {
  137. this.peerconnection = mozRTCPeerConnection;
  138. this.getUserMedia = navigator.mozGetUserMedia.bind(navigator);
  139. this.pc_constraints = {};
  140. this.attachMediaStream = function (element, stream) {
  141. // srcObject is being standardized and FF will eventually
  142. // support that unprefixed. FF also supports the
  143. // "element.src = URL.createObjectURL(...)" combo, but that
  144. // will be deprecated in favour of srcObject.
  145. //
  146. // https://groups.google.com/forum/#!topic/mozilla.dev.media/pKOiioXonJg
  147. // https://github.com/webrtc/samples/issues/302
  148. if(!element[0])
  149. return;
  150. element[0].mozSrcObject = stream;
  151. element[0].play();
  152. };
  153. this.getStreamID = function (stream) {
  154. var id = stream.id;
  155. if (!id) {
  156. var tracks = stream.getVideoTracks();
  157. if (!tracks || tracks.length === 0)
  158. {
  159. tracks = stream.getAudioTracks();
  160. }
  161. id = tracks[0].id;
  162. }
  163. return SDPUtil.filter_special_chars(id);
  164. };
  165. this.getVideoSrc = function (element) {
  166. if(!element)
  167. return null;
  168. return element.mozSrcObject;
  169. };
  170. this.setVideoSrc = function (element, src) {
  171. if(element)
  172. element.mozSrcObject = src;
  173. };
  174. RTCSessionDescription = mozRTCSessionDescription;
  175. RTCIceCandidate = mozRTCIceCandidate;
  176. } else {
  177. console.error(
  178. "Firefox requirements not met, ver: " + FFversion +
  179. ", bundle: " + config.useBundle +
  180. ", rtcp-mux: " + config.useRtcpMux);
  181. window.location.href = 'unsupported_browser.html';
  182. return;
  183. }
  184. } else if (RTCBrowserType.isChrome() || RTCBrowserType.isOpera()) {
  185. this.peerconnection = webkitRTCPeerConnection;
  186. this.getUserMedia = navigator.webkitGetUserMedia.bind(navigator);
  187. this.attachMediaStream = function (element, stream) {
  188. element.attr('src', webkitURL.createObjectURL(stream));
  189. };
  190. this.getStreamID = function (stream) {
  191. // streams from FF endpoints have the characters '{' and '}'
  192. // that make jQuery choke.
  193. return SDPUtil.filter_special_chars(stream.id);
  194. };
  195. this.getVideoSrc = function (element) {
  196. if(!element)
  197. return null;
  198. return element.getAttribute("src");
  199. };
  200. this.setVideoSrc = function (element, src) {
  201. if(element)
  202. element.setAttribute("src", src);
  203. };
  204. // DTLS should now be enabled by default but..
  205. this.pc_constraints = {'optional': [{'DtlsSrtpKeyAgreement': 'true'}]};
  206. if (navigator.userAgent.indexOf('Android') != -1) {
  207. this.pc_constraints = {}; // disable DTLS on Android
  208. }
  209. if (!webkitMediaStream.prototype.getVideoTracks) {
  210. webkitMediaStream.prototype.getVideoTracks = function () {
  211. return this.videoTracks;
  212. };
  213. }
  214. if (!webkitMediaStream.prototype.getAudioTracks) {
  215. webkitMediaStream.prototype.getAudioTracks = function () {
  216. return this.audioTracks;
  217. };
  218. }
  219. }
  220. // Detect IE/Safari
  221. else if (RTCBrowserType.isTemasysPluginUsed()) {
  222. //AdapterJS.WebRTCPlugin.setLogLevel(
  223. // AdapterJS.WebRTCPlugin.PLUGIN_LOG_LEVELS.VERBOSE);
  224. AdapterJS.webRTCReady(function (isPlugin) {
  225. self.peerconnection = RTCPeerConnection;
  226. self.getUserMedia = getUserMedia;
  227. self.attachMediaStream = function (elSel, stream) {
  228. if (stream.id === "dummyAudio" || stream.id === "dummyVideo") {
  229. return;
  230. }
  231. attachMediaStream(elSel[0], stream);
  232. };
  233. self.getStreamID = function (stream) {
  234. var id = SDPUtil.filter_special_chars(stream.label);
  235. return id;
  236. };
  237. self.getVideoSrc = function (element) {
  238. if (!element) {
  239. console.warn("Attempt to get video SRC of null element");
  240. return null;
  241. }
  242. var src = null;
  243. var children = element.children;
  244. for (var i = 0; i !== children.length; ++i) {
  245. if (children[i].name === 'streamId') {
  246. return children[i].value;
  247. }
  248. }
  249. //console.info(element.id + " SRC: " + src);
  250. return null;
  251. };
  252. self.setVideoSrc = function (element, src) {
  253. //console.info("Set video src: ", element, src);
  254. if (!src) {
  255. console.warn("Not attaching video stream, 'src' is null");
  256. return;
  257. }
  258. AdapterJS.WebRTCPlugin.WaitForPluginReady();
  259. var stream = AdapterJS.WebRTCPlugin.plugin
  260. .getStreamWithId(AdapterJS.WebRTCPlugin.pageId, src);
  261. attachMediaStream(element, stream);
  262. };
  263. onTemasysPluginReady(isPlugin);
  264. });
  265. } else {
  266. try {
  267. console.log('Browser does not appear to be WebRTC-capable');
  268. } catch (e) { }
  269. window.location.href = 'unsupported_browser.html';
  270. }
  271. }
  272. RTCUtils.prototype.getUserMediaWithConstraints = function(
  273. um, success_callback, failure_callback, resolution,bandwidth, fps,
  274. desktopStream)
  275. {
  276. currentResolution = resolution;
  277. // Check if we are running on Android device
  278. var isAndroid = navigator.userAgent.indexOf('Android') != -1;
  279. var constraints = getConstraints(
  280. um, resolution, bandwidth, fps, desktopStream, isAndroid);
  281. console.info("Get media constraints", constraints);
  282. var self = this;
  283. try {
  284. this.getUserMedia(constraints,
  285. function (stream) {
  286. console.log('onUserMediaSuccess');
  287. self.setAvailableDevices(um, true);
  288. success_callback(stream);
  289. },
  290. function (error) {
  291. self.setAvailableDevices(um, false);
  292. console.warn('Failed to get access to local media. Error ',
  293. error, constraints);
  294. if (failure_callback) {
  295. failure_callback(error);
  296. }
  297. });
  298. } catch (e) {
  299. console.error('GUM failed: ', e);
  300. if(failure_callback) {
  301. failure_callback(e);
  302. }
  303. }
  304. };
  305. RTCUtils.prototype.setAvailableDevices = function (um, available) {
  306. var devices = {};
  307. if(um.indexOf("video") != -1)
  308. {
  309. devices.video = available;
  310. }
  311. if(um.indexOf("audio") != -1)
  312. {
  313. devices.audio = available;
  314. }
  315. this.service.setDeviceAvailability(devices);
  316. }
  317. /**
  318. * We ask for audio and video combined stream in order to get permissions and
  319. * not to ask twice.
  320. */
  321. RTCUtils.prototype.obtainAudioAndVideoPermissions =
  322. function(devices, callback, usageOptions)
  323. {
  324. var self = this;
  325. // Get AV
  326. var successCallback = function (stream) {
  327. if(callback)
  328. callback(stream, usageOptions);
  329. else
  330. self.successCallback(stream, usageOptions);
  331. };
  332. if(!devices)
  333. devices = ['audio', 'video'];
  334. var newDevices = [];
  335. if(usageOptions)
  336. for(var i = 0; i < devices.length; i++)
  337. {
  338. var device = devices[i];
  339. if(usageOptions[device] === true)
  340. newDevices.push(device);
  341. }
  342. else
  343. newDevices = devices;
  344. if(newDevices.length === 0)
  345. {
  346. successCallback();
  347. return;
  348. }
  349. if (RTCBrowserType.isFirefox() || RTCBrowserType.isTemasysPluginUsed()) {
  350. // With FF/IE we can't split the stream into audio and video because FF
  351. // doesn't support media stream constructors. So, we need to get the
  352. // audio stream separately from the video stream using two distinct GUM
  353. // calls. Not very user friendly :-( but we don't have many other
  354. // options neither.
  355. //
  356. // Note that we pack those 2 streams in a single object and pass it to
  357. // the successCallback method.
  358. var obtainVideo = function (audioStream) {
  359. self.getUserMediaWithConstraints(
  360. ['video'],
  361. function (videoStream) {
  362. return successCallback({
  363. audioStream: audioStream,
  364. videoStream: videoStream
  365. });
  366. },
  367. function (error) {
  368. console.error(
  369. 'failed to obtain video stream - stop', error);
  370. self.errorCallback(error);
  371. },
  372. config.resolution || '360');
  373. };
  374. var obtainAudio = function () {
  375. self.getUserMediaWithConstraints(
  376. ['audio'],
  377. function (audioStream) {
  378. if (newDevices.indexOf('video') !== -1)
  379. obtainVideo(audioStream);
  380. },
  381. function (error) {
  382. console.error(
  383. 'failed to obtain audio stream - stop', error);
  384. self.errorCallback(error);
  385. }
  386. );
  387. };
  388. if (newDevices.indexOf('audio') !== -1) {
  389. obtainAudio();
  390. } else {
  391. obtainVideo(null);
  392. }
  393. } else {
  394. this.getUserMediaWithConstraints(
  395. newDevices,
  396. function (stream) {
  397. successCallback(stream);
  398. },
  399. function (error) {
  400. self.errorCallback(error);
  401. },
  402. config.resolution || '360');
  403. }
  404. };
  405. RTCUtils.prototype.successCallback = function (stream, usageOptions) {
  406. // If this is FF or IE, the stream parameter is *not* a MediaStream object,
  407. // it's an object with two properties: audioStream, videoStream.
  408. if (stream && stream.getAudioTracks && stream.getVideoTracks)
  409. console.log('got', stream, stream.getAudioTracks().length,
  410. stream.getVideoTracks().length);
  411. this.handleLocalStream(stream, usageOptions);
  412. };
  413. RTCUtils.prototype.errorCallback = function (error) {
  414. var self = this;
  415. console.error('failed to obtain audio/video stream - trying audio only', error);
  416. var resolution = getPreviousResolution(currentResolution);
  417. if(typeof error == "object" && error.constraintName && error.name
  418. && (error.name == "ConstraintNotSatisfiedError" ||
  419. error.name == "OverconstrainedError") &&
  420. (error.constraintName == "minWidth" || error.constraintName == "maxWidth" ||
  421. error.constraintName == "minHeight" || error.constraintName == "maxHeight")
  422. && resolution != null)
  423. {
  424. self.getUserMediaWithConstraints(['audio', 'video'],
  425. function (stream) {
  426. return self.successCallback(stream);
  427. }, function (error) {
  428. return self.errorCallback(error);
  429. }, resolution);
  430. }
  431. else
  432. {
  433. self.getUserMediaWithConstraints(
  434. ['audio'],
  435. function (stream) {
  436. return self.successCallback(stream);
  437. },
  438. function (error) {
  439. console.error('failed to obtain audio/video stream - stop',
  440. error);
  441. return self.successCallback(null);
  442. }
  443. );
  444. }
  445. }
  446. RTCUtils.prototype.handleLocalStream = function(stream, usageOptions)
  447. {
  448. // If this is FF, the stream parameter is *not* a MediaStream object, it's
  449. // an object with two properties: audioStream, videoStream.
  450. var audioStream, videoStream;
  451. if(window.webkitMediaStream)
  452. {
  453. audioStream = new webkitMediaStream();
  454. videoStream = new webkitMediaStream();
  455. if(stream) {
  456. var audioTracks = stream.getAudioTracks();
  457. for (var i = 0; i < audioTracks.length; i++) {
  458. audioStream.addTrack(audioTracks[i]);
  459. }
  460. var videoTracks = stream.getVideoTracks();
  461. for (i = 0; i < videoTracks.length; i++) {
  462. videoStream.addTrack(videoTracks[i]);
  463. }
  464. }
  465. }
  466. else if (RTCBrowserType.isFirefox() || RTCBrowserType.isTemasysPluginUsed())
  467. { // Firefox and Temasys plugin
  468. if (stream && stream.audioStream)
  469. audioStream = stream.audioStream;
  470. else
  471. audioStream = new DummyMediaStream("dummyAudio");
  472. if (stream && stream.videoStream)
  473. videoStream = stream.videoStream;
  474. else
  475. videoStream = new DummyMediaStream("dummyVideo");
  476. }
  477. var audioMuted = (usageOptions && usageOptions.audio === false),
  478. videoMuted = (usageOptions && usageOptions.video === false);
  479. var audioGUM = (!usageOptions || usageOptions.audio !== false),
  480. videoGUM = (!usageOptions || usageOptions.video !== false);
  481. this.service.createLocalStream(audioStream, "audio", null, null,
  482. audioMuted, audioGUM);
  483. this.service.createLocalStream(videoStream, "video", null, null,
  484. videoMuted, videoGUM);
  485. };
  486. function DummyMediaStream(id) {
  487. this.id = id;
  488. this.label = id;
  489. this.stop = function() { };
  490. this.getAudioTracks = function() { return []; }
  491. this.getVideoTracks = function() { return []; }
  492. }
  493. RTCUtils.prototype.createStream = function(stream, isVideo) {
  494. var newStream = null;
  495. if (window.webkitMediaStream) {
  496. newStream = new webkitMediaStream();
  497. if (newStream) {
  498. var tracks = (isVideo ? stream.getVideoTracks() : stream.getAudioTracks());
  499. for (i = 0; i < tracks.length; i++) {
  500. newStream.addTrack(tracks[i]);
  501. }
  502. }
  503. }
  504. else {
  505. // FIXME: this is duplicated with 'handleLocalStream' !!!
  506. if (stream) {
  507. newStream = stream;
  508. } else {
  509. newStream = new DummyMediaStream(isVideo ? "dummyVideo" : "dummyAudio");
  510. }
  511. }
  512. return newStream;
  513. };
  514. module.exports = RTCUtils;