您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

RTCUtils.js 23KB

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