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

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