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

RTCUtils.js 20KB

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