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.

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