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.

RTC.bundle.js 38KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  1. !function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.RTC=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  2. /* global connection, Strophe, updateLargeVideo, focusedVideoSrc*/
  3. // cache datachannels to avoid garbage collection
  4. // https://code.google.com/p/chromium/issues/detail?id=405545
  5. var _dataChannels = [];
  6. var DataChannels =
  7. {
  8. /**
  9. * Callback triggered by PeerConnection when new data channel is opened
  10. * on the bridge.
  11. * @param event the event info object.
  12. */
  13. onDataChannel: function (event)
  14. {
  15. var dataChannel = event.channel;
  16. dataChannel.onopen = function () {
  17. console.info("Data channel opened by the Videobridge!", dataChannel);
  18. // Code sample for sending string and/or binary data
  19. // Sends String message to the bridge
  20. //dataChannel.send("Hello bridge!");
  21. // Sends 12 bytes binary message to the bridge
  22. //dataChannel.send(new ArrayBuffer(12));
  23. // when the data channel becomes available, tell the bridge about video
  24. // selections so that it can do adaptive simulcast,
  25. // we want the notification to trigger even if userJid is undefined,
  26. // or null.
  27. var userJid = UI.getLargeVideoState().userJid;
  28. // we want the notification to trigger even if userJid is undefined,
  29. // or null.
  30. onSelectedEndpointChanged(userJid);
  31. };
  32. dataChannel.onerror = function (error) {
  33. console.error("Data Channel Error:", error, dataChannel);
  34. };
  35. dataChannel.onmessage = function (event) {
  36. var data = event.data;
  37. // JSON
  38. var obj;
  39. try {
  40. obj = JSON.parse(data);
  41. }
  42. catch (e) {
  43. console.error(
  44. "Failed to parse data channel message as JSON: ",
  45. data,
  46. dataChannel);
  47. }
  48. if (('undefined' !== typeof(obj)) && (null !== obj)) {
  49. var colibriClass = obj.colibriClass;
  50. if ("DominantSpeakerEndpointChangeEvent" === colibriClass) {
  51. // Endpoint ID from the Videobridge.
  52. var dominantSpeakerEndpoint = obj.dominantSpeakerEndpoint;
  53. console.info(
  54. "Data channel new dominant speaker event: ",
  55. dominantSpeakerEndpoint);
  56. $(document).trigger(
  57. 'dominantspeakerchanged',
  58. [dominantSpeakerEndpoint]);
  59. }
  60. else if ("InLastNChangeEvent" === colibriClass)
  61. {
  62. var oldValue = obj.oldValue;
  63. var newValue = obj.newValue;
  64. // Make sure that oldValue and newValue are of type boolean.
  65. var type;
  66. if ((type = typeof oldValue) !== 'boolean') {
  67. if (type === 'string') {
  68. oldValue = (oldValue == "true");
  69. } else {
  70. oldValue = new Boolean(oldValue).valueOf();
  71. }
  72. }
  73. if ((type = typeof newValue) !== 'boolean') {
  74. if (type === 'string') {
  75. newValue = (newValue == "true");
  76. } else {
  77. newValue = new Boolean(newValue).valueOf();
  78. }
  79. }
  80. $(document).trigger('inlastnchanged', [oldValue, newValue]);
  81. }
  82. else if ("LastNEndpointsChangeEvent" === colibriClass)
  83. {
  84. // The new/latest list of last-n endpoint IDs.
  85. var lastNEndpoints = obj.lastNEndpoints;
  86. // The list of endpoint IDs which are entering the list of
  87. // last-n at this time i.e. were not in the old list of last-n
  88. // endpoint IDs.
  89. var endpointsEnteringLastN = obj.endpointsEnteringLastN;
  90. var stream = obj.stream;
  91. console.log(
  92. "Data channel new last-n event: ",
  93. lastNEndpoints, endpointsEnteringLastN, obj);
  94. $(document).trigger(
  95. 'lastnchanged',
  96. [lastNEndpoints, endpointsEnteringLastN, stream]);
  97. }
  98. else if ("SimulcastLayersChangedEvent" === colibriClass)
  99. {
  100. $(document).trigger(
  101. 'simulcastlayerschanged',
  102. [obj.endpointSimulcastLayers]);
  103. }
  104. else if ("SimulcastLayersChangingEvent" === colibriClass)
  105. {
  106. $(document).trigger(
  107. 'simulcastlayerschanging',
  108. [obj.endpointSimulcastLayers]);
  109. }
  110. else if ("StartSimulcastLayerEvent" === colibriClass)
  111. {
  112. $(document).trigger('startsimulcastlayer', obj.simulcastLayer);
  113. }
  114. else if ("StopSimulcastLayerEvent" === colibriClass)
  115. {
  116. $(document).trigger('stopsimulcastlayer', obj.simulcastLayer);
  117. }
  118. else
  119. {
  120. console.debug("Data channel JSON-formatted message: ", obj);
  121. }
  122. }
  123. };
  124. dataChannel.onclose = function ()
  125. {
  126. console.info("The Data Channel closed", dataChannel);
  127. var idx = _dataChannels.indexOf(dataChannel);
  128. if (idx > -1)
  129. _dataChannels = _dataChannels.splice(idx, 1);
  130. };
  131. _dataChannels.push(dataChannel);
  132. },
  133. /**
  134. * Binds "ondatachannel" event listener to given PeerConnection instance.
  135. * @param peerConnection WebRTC peer connection instance.
  136. */
  137. bindDataChannelListener: function (peerConnection) {
  138. if(!config.openSctp)
  139. retrun;
  140. peerConnection.ondatachannel = this.onDataChannel;
  141. // Sample code for opening new data channel from Jitsi Meet to the bridge.
  142. // Although it's not a requirement to open separate channels from both bridge
  143. // and peer as single channel can be used for sending and receiving data.
  144. // So either channel opened by the bridge or the one opened here is enough
  145. // for communication with the bridge.
  146. /*var dataChannelOptions =
  147. {
  148. reliable: true
  149. };
  150. var dataChannel
  151. = peerConnection.createDataChannel("myChannel", dataChannelOptions);
  152. // Can be used only when is in open state
  153. dataChannel.onopen = function ()
  154. {
  155. dataChannel.send("My channel !!!");
  156. };
  157. dataChannel.onmessage = function (event)
  158. {
  159. var msgData = event.data;
  160. console.info("Got My Data Channel Message:", msgData, dataChannel);
  161. };*/
  162. }
  163. }
  164. function onSelectedEndpointChanged(userJid)
  165. {
  166. console.log('selected endpoint changed: ', userJid);
  167. if (_dataChannels && _dataChannels.length != 0)
  168. {
  169. _dataChannels.some(function (dataChannel) {
  170. if (dataChannel.readyState == 'open')
  171. {
  172. dataChannel.send(JSON.stringify({
  173. 'colibriClass': 'SelectedEndpointChangedEvent',
  174. 'selectedEndpoint': (!userJid || userJid == null)
  175. ? null : userJid
  176. }));
  177. return true;
  178. }
  179. });
  180. }
  181. }
  182. $(document).bind("selectedendpointchanged", function(event, userJid) {
  183. onSelectedEndpointChanged(userJid);
  184. });
  185. function onPinnedEndpointChanged(userJid)
  186. {
  187. console.log('pinned endpoint changed: ', userJid);
  188. if (_dataChannels && _dataChannels.length != 0)
  189. {
  190. _dataChannels.some(function (dataChannel) {
  191. if (dataChannel.readyState == 'open')
  192. {
  193. dataChannel.send(JSON.stringify({
  194. 'colibriClass': 'PinnedEndpointChangedEvent',
  195. 'pinnedEndpoint': (!userJid || userJid == null)
  196. ? null : Strophe.getResourceFromJid(userJid)
  197. }));
  198. return true;
  199. }
  200. });
  201. }
  202. }
  203. $(document).bind("pinnedendpointchanged", function(event, userJid) {
  204. onPinnedEndpointChanged(userJid);
  205. });
  206. module.exports = DataChannels;
  207. },{}],2:[function(require,module,exports){
  208. //var StreamEventTypes = require("../../service/RTC/StreamEventTypes.js");
  209. function LocalStream(stream, type, eventEmitter)
  210. {
  211. this.stream = stream;
  212. this.eventEmitter = eventEmitter;
  213. this.type = type;
  214. var self = this;
  215. if(type == "audio")
  216. {
  217. this.getTracks = function () {
  218. return self.stream.getAudioTracks();
  219. };
  220. }
  221. else
  222. {
  223. this.getTracks = function () {
  224. return self.stream.getVideoTracks();
  225. };
  226. }
  227. this.stream.onended = function()
  228. {
  229. self.streamEnded();
  230. };
  231. }
  232. LocalStream.prototype.streamEnded = function () {
  233. this.eventEmitter.emit(StreamEventTypes.EVENT_TYPE_LOCAL_ENDED, this);
  234. }
  235. LocalStream.prototype.getOriginalStream = function()
  236. {
  237. return this.stream;
  238. }
  239. LocalStream.prototype.isAudioStream = function () {
  240. return (this.stream.getAudioTracks() && this.stream.getAudioTracks().length > 0);
  241. };
  242. LocalStream.prototype.mute = function()
  243. {
  244. var ismuted = false;
  245. var tracks = this.getTracks();
  246. for (var idx = 0; idx < tracks.length; idx++) {
  247. ismuted = !tracks[idx].enabled;
  248. tracks[idx].enabled = ismuted;
  249. }
  250. return ismuted;
  251. };
  252. LocalStream.prototype.setMute = function(mute)
  253. {
  254. var tracks = this.getTracks();
  255. for (var idx = 0; idx < tracks.length; idx++) {
  256. tracks[idx].enabled = mute;
  257. }
  258. };
  259. LocalStream.prototype.isMuted = function () {
  260. var tracks = [];
  261. if(this.type == "audio")
  262. {
  263. tracks = this.stream.getAudioTracks();
  264. }
  265. else
  266. {
  267. tracks = this.stream.getVideoTracks();
  268. }
  269. for (var idx = 0; idx < tracks.length; idx++) {
  270. if(tracks[idx].enabled)
  271. return false;
  272. }
  273. return true;
  274. }
  275. LocalStream.prototype.getId = function () {
  276. return this.stream.getTracks()[0].id;
  277. }
  278. module.exports = LocalStream;
  279. },{}],3:[function(require,module,exports){
  280. ////These lines should be uncommented when require works in app.js
  281. //var RTCBrowserType = require("../../service/RTC/RTCBrowserType.js");
  282. //var StreamEventTypes = require("../../service/RTC/StreamEventTypes.js");
  283. //var MediaStreamType = require("../../service/RTC/MediaStreamTypes");
  284. /**
  285. * Creates a MediaStream object for the given data, session id and ssrc.
  286. * It is a wrapper class for the MediaStream.
  287. *
  288. * @param data the data object from which we obtain the stream,
  289. * the peerjid, etc.
  290. * @param sid the session id
  291. * @param ssrc the ssrc corresponding to this MediaStream
  292. *
  293. * @constructor
  294. */
  295. function MediaStream(data, sid, ssrc, eventEmmiter, browser) {
  296. // XXX(gp) to minimize headaches in the future, we should build our
  297. // abstractions around tracks and not streams. ORTC is track based API.
  298. // Mozilla expects m-lines to represent media tracks.
  299. //
  300. // Practically, what I'm saying is that we should have a MediaTrack class
  301. // and not a MediaStream class.
  302. //
  303. // Also, we should be able to associate multiple SSRCs with a MediaTrack as
  304. // a track might have an associated RTX and FEC sources.
  305. this.sid = sid;
  306. this.stream = data.stream;
  307. this.peerjid = data.peerjid;
  308. this.ssrc = ssrc;
  309. this.type = (this.stream.getVideoTracks().length > 0)?
  310. MediaStreamType.VIDEO_TYPE : MediaStreamType.AUDIO_TYPE;
  311. this.muted = false;
  312. eventEmmiter.emit(StreamEventTypes.EVENT_TYPE_REMOTE_CREATED, this);
  313. if(browser == RTCBrowserType.RTC_BROWSER_FIREFOX)
  314. {
  315. if (!this.getVideoTracks)
  316. this.getVideoTracks = function () { return []; };
  317. if (!this.getAudioTracks)
  318. this.getAudioTracks = function () { return []; };
  319. }
  320. }
  321. MediaStream.prototype.getOriginalStream = function()
  322. {
  323. return this.stream;
  324. }
  325. MediaStream.prototype.setMute = function (value)
  326. {
  327. this.stream.muted = value;
  328. this.muted = value;
  329. }
  330. module.exports = MediaStream;
  331. },{}],4:[function(require,module,exports){
  332. var EventEmitter = require("events");
  333. var RTCUtils = require("./RTCUtils.js");
  334. var LocalStream = require("./LocalStream.js");
  335. var DataChannels = require("./DataChannels");
  336. var MediaStream = require("./MediaStream.js");
  337. //These lines should be uncommented when require works in app.js
  338. //var StreamEventTypes = require("../../service/RTC/StreamEventTypes.js");
  339. //var XMPPEvents = require("../service/xmpp/XMPPEvents");
  340. var eventEmitter = new EventEmitter();
  341. var RTC = {
  342. rtcUtils: null,
  343. localStreams: [],
  344. remoteStreams: {},
  345. localAudio: null,
  346. localVideo: null,
  347. addStreamListener: function (listener, eventType) {
  348. eventEmitter.on(eventType, listener);
  349. },
  350. removeStreamListener: function (listener, eventType) {
  351. if(!(eventType instanceof StreamEventTypes))
  352. throw "Illegal argument";
  353. eventEmitter.removeListener(eventType, listener);
  354. },
  355. createLocalStream: function (stream, type, change) {
  356. var localStream = new LocalStream(stream, type, eventEmitter);
  357. //in firefox we have only one stream object
  358. if(this.localStreams.length == 0 ||
  359. this.localStreams[0].getOriginalStream() != stream)
  360. this.localStreams.push(localStream);
  361. if(type == "audio")
  362. {
  363. this.localAudio = localStream;
  364. }
  365. else
  366. {
  367. this.localVideo = localStream;
  368. }
  369. var eventType = StreamEventTypes.EVENT_TYPE_LOCAL_CREATED;
  370. if(change)
  371. eventType = StreamEventTypes.EVENT_TYPE_LOCAL_CHANGED;
  372. eventEmitter.emit(eventType, localStream);
  373. return localStream;
  374. },
  375. removeLocalStream: function (stream) {
  376. for(var i = 0; i < this.localStreams.length; i++)
  377. {
  378. if(this.localStreams[i].getOriginalStream() === stream) {
  379. delete this.localStreams[i];
  380. return;
  381. }
  382. }
  383. },
  384. createRemoteStream: function (data, sid, thessrc) {
  385. var remoteStream = new MediaStream(data, sid, thessrc, eventEmitter,
  386. this.getBrowserType());
  387. var jid = data.peerjid || connection.emuc.myroomjid;
  388. if(!this.remoteStreams[jid]) {
  389. this.remoteStreams[jid] = {};
  390. }
  391. this.remoteStreams[jid][remoteStream.type]= remoteStream;
  392. return remoteStream;
  393. },
  394. getBrowserType: function () {
  395. return this.rtcUtils.browser;
  396. },
  397. getPCConstraints: function () {
  398. return this.rtcUtils.pc_constraints;
  399. },
  400. getUserMediaWithConstraints:function(um, success_callback,
  401. failure_callback, resolution,
  402. bandwidth, fps, desktopStream)
  403. {
  404. return this.rtcUtils.getUserMediaWithConstraints(um, success_callback,
  405. failure_callback, resolution, bandwidth, fps, desktopStream);
  406. },
  407. attachMediaStream: function (element, stream) {
  408. this.rtcUtils.attachMediaStream(element, stream);
  409. },
  410. getStreamID: function (stream) {
  411. return this.rtcUtils.getStreamID(stream);
  412. },
  413. getVideoSrc: function (element) {
  414. return this.rtcUtils.getVideoSrc(element);
  415. },
  416. setVideoSrc: function (element, src) {
  417. this.rtcUtils.setVideoSrc(element, src);
  418. },
  419. dispose: function() {
  420. if (this.rtcUtils) {
  421. this.rtcUtils = null;
  422. }
  423. },
  424. stop: function () {
  425. this.dispose();
  426. },
  427. start: function () {
  428. desktopsharing.addListener(
  429. function (stream, isUsingScreenStream, callback) {
  430. RTC.changeLocalVideo(stream, isUsingScreenStream, callback);
  431. }, DesktopSharingEventTypes.NEW_STREAM_CREATED);
  432. this.rtcUtils = new RTCUtils(this);
  433. this.rtcUtils.obtainAudioAndVideoPermissions();
  434. },
  435. onConferenceCreated: function(event) {
  436. DataChannels.bindDataChannelListener(event.peerconnection);
  437. },
  438. muteRemoteVideoStream: function (jid, value) {
  439. var stream;
  440. if(this.remoteStreams[jid] &&
  441. this.remoteStreams[jid][MediaStreamType.VIDEO_TYPE])
  442. {
  443. stream = this.remoteStreams[jid][MediaStreamType.VIDEO_TYPE];
  444. }
  445. if(!stream)
  446. return false;
  447. if (value != stream.muted) {
  448. stream.setMute(value);
  449. return true;
  450. }
  451. return false;
  452. },
  453. switchVideoStreams: function (new_stream) {
  454. this.localVideo.stream = new_stream;
  455. this.localStreams = [];
  456. //in firefox we have only one stream object
  457. if (this.localAudio.getOriginalStream() != new_stream)
  458. this.localStreams.push(this.localAudio);
  459. this.localStreams.push(this.localVideo);
  460. },
  461. changeLocalVideo: function (stream, isUsingScreenStream, callback) {
  462. var oldStream = this.localVideo.getOriginalStream();
  463. var type = (isUsingScreenStream? "desktop" : "video");
  464. RTC.localVideo = this.createLocalStream(stream, type, true);
  465. // Stop the stream to trigger onended event for old stream
  466. oldStream.stop();
  467. if (activecall) {
  468. // FIXME: will block switchInProgress on true value in case of exception
  469. activecall.switchStreams(stream, oldStream, callback);
  470. } else {
  471. // We are done immediately
  472. console.error("No conference handler");
  473. UI.messageHandler.showError('Error',
  474. 'Unable to switch video stream.');
  475. callback();
  476. }
  477. }
  478. };
  479. module.exports = RTC;
  480. },{"./DataChannels":1,"./LocalStream.js":2,"./MediaStream.js":3,"./RTCUtils.js":5,"events":6}],5:[function(require,module,exports){
  481. //This should be uncommented when app.js supports require
  482. //var RTCBrowserType = require("../../service/RTC/RTCBrowserType.js");
  483. function setResolutionConstraints(constraints, resolution, isAndroid)
  484. {
  485. if (resolution && !constraints.video || isAndroid) {
  486. constraints.video = { mandatory: {}, optional: [] };// same behaviour as true
  487. }
  488. // see https://code.google.com/p/chromium/issues/detail?id=143631#c9 for list of supported resolutions
  489. switch (resolution) {
  490. // 16:9 first
  491. case '1080':
  492. case 'fullhd':
  493. constraints.video.mandatory.minWidth = 1920;
  494. constraints.video.mandatory.minHeight = 1080;
  495. break;
  496. case '720':
  497. case 'hd':
  498. constraints.video.mandatory.minWidth = 1280;
  499. constraints.video.mandatory.minHeight = 720;
  500. break;
  501. case '360':
  502. constraints.video.mandatory.minWidth = 640;
  503. constraints.video.mandatory.minHeight = 360;
  504. break;
  505. case '180':
  506. constraints.video.mandatory.minWidth = 320;
  507. constraints.video.mandatory.minHeight = 180;
  508. break;
  509. // 4:3
  510. case '960':
  511. constraints.video.mandatory.minWidth = 960;
  512. constraints.video.mandatory.minHeight = 720;
  513. break;
  514. case '640':
  515. case 'vga':
  516. constraints.video.mandatory.minWidth = 640;
  517. constraints.video.mandatory.minHeight = 480;
  518. break;
  519. case '320':
  520. constraints.video.mandatory.minWidth = 320;
  521. constraints.video.mandatory.minHeight = 240;
  522. break;
  523. default:
  524. if (isAndroid) {
  525. constraints.video.mandatory.minWidth = 320;
  526. constraints.video.mandatory.minHeight = 240;
  527. constraints.video.mandatory.maxFrameRate = 15;
  528. }
  529. break;
  530. }
  531. if (constraints.video.mandatory.minWidth)
  532. constraints.video.mandatory.maxWidth = constraints.video.mandatory.minWidth;
  533. if (constraints.video.mandatory.minHeight)
  534. constraints.video.mandatory.maxHeight = constraints.video.mandatory.minHeight;
  535. }
  536. function getConstraints(um, resolution, bandwidth, fps, desktopStream, isAndroid)
  537. {
  538. var constraints = {audio: false, video: false};
  539. if (um.indexOf('video') >= 0) {
  540. constraints.video = { mandatory: {}, optional: [] };// same behaviour as true
  541. }
  542. if (um.indexOf('audio') >= 0) {
  543. constraints.audio = { mandatory: {}, optional: []};// same behaviour as true
  544. }
  545. if (um.indexOf('screen') >= 0) {
  546. constraints.video = {
  547. mandatory: {
  548. chromeMediaSource: "screen",
  549. googLeakyBucket: true,
  550. maxWidth: window.screen.width,
  551. maxHeight: window.screen.height,
  552. maxFrameRate: 3
  553. },
  554. optional: []
  555. };
  556. }
  557. if (um.indexOf('desktop') >= 0) {
  558. constraints.video = {
  559. mandatory: {
  560. chromeMediaSource: "desktop",
  561. chromeMediaSourceId: desktopStream,
  562. googLeakyBucket: true,
  563. maxWidth: window.screen.width,
  564. maxHeight: window.screen.height,
  565. maxFrameRate: 3
  566. },
  567. optional: []
  568. };
  569. }
  570. if (constraints.audio) {
  571. // if it is good enough for hangouts...
  572. constraints.audio.optional.push(
  573. {googEchoCancellation: true},
  574. {googAutoGainControl: true},
  575. {googNoiseSupression: true},
  576. {googHighpassFilter: true},
  577. {googNoisesuppression2: true},
  578. {googEchoCancellation2: true},
  579. {googAutoGainControl2: true}
  580. );
  581. }
  582. if (constraints.video) {
  583. constraints.video.optional.push(
  584. {googNoiseReduction: false} // chrome 37 workaround for issue 3807, reenable in M38
  585. );
  586. if (um.indexOf('video') >= 0) {
  587. constraints.video.optional.push(
  588. {googLeakyBucket: true}
  589. );
  590. }
  591. }
  592. setResolutionConstraints(constraints, resolution, isAndroid);
  593. if (bandwidth) { // doesn't work currently, see webrtc issue 1846
  594. if (!constraints.video) constraints.video = {mandatory: {}, optional: []};//same behaviour as true
  595. constraints.video.optional.push({bandwidth: bandwidth});
  596. }
  597. if (fps) { // for some cameras it might be necessary to request 30fps
  598. // so they choose 30fps mjpg over 10fps yuy2
  599. if (!constraints.video) constraints.video = {mandatory: {}, optional: []};// same behaviour as true;
  600. constraints.video.mandatory.minFrameRate = fps;
  601. }
  602. return constraints;
  603. }
  604. function RTCUtils(RTCService)
  605. {
  606. this.service = RTCService;
  607. if (navigator.mozGetUserMedia) {
  608. console.log('This appears to be Firefox');
  609. var version = parseInt(navigator.userAgent.match(/Firefox\/([0-9]+)\./)[1], 10);
  610. if (version >= 22) {
  611. this.peerconnection = mozRTCPeerConnection;
  612. this.browser = RTCBrowserType.RTC_BROWSER_FIREFOX;
  613. this.getUserMedia = navigator.mozGetUserMedia.bind(navigator);
  614. this.pc_constraints = {};
  615. this.attachMediaStream = function (element, stream) {
  616. element[0].mozSrcObject = stream;
  617. element[0].play();
  618. };
  619. this.getStreamID = function (stream) {
  620. var tracks = stream.getVideoTracks();
  621. if(!tracks || tracks.length == 0)
  622. {
  623. tracks = stream.getAudioTracks();
  624. }
  625. return tracks[0].id.replace(/[\{,\}]/g,"");
  626. };
  627. this.getVideoSrc = function (element) {
  628. return element.mozSrcObject;
  629. };
  630. this.setVideoSrc = function (element, src) {
  631. element.mozSrcObject = src;
  632. };
  633. RTCSessionDescription = mozRTCSessionDescription;
  634. RTCIceCandidate = mozRTCIceCandidate;
  635. }
  636. } else if (navigator.webkitGetUserMedia) {
  637. console.log('This appears to be Chrome');
  638. this.peerconnection = webkitRTCPeerConnection;
  639. this.browser = RTCBrowserType.RTC_BROWSER_CHROME;
  640. this.getUserMedia = navigator.webkitGetUserMedia.bind(navigator);
  641. this.attachMediaStream = function (element, stream) {
  642. element.attr('src', webkitURL.createObjectURL(stream));
  643. };
  644. this.getStreamID = function (stream) {
  645. // streams from FF endpoints have the characters '{' and '}'
  646. // that make jQuery choke.
  647. return stream.id.replace(/[\{,\}]/g,"");
  648. };
  649. this.getVideoSrc = function (element) {
  650. return element.getAttribute("src");
  651. };
  652. this.setVideoSrc = function (element, src) {
  653. element.setAttribute("src", src);
  654. };
  655. // DTLS should now be enabled by default but..
  656. this.pc_constraints = {'optional': [{'DtlsSrtpKeyAgreement': 'true'}]};
  657. if (navigator.userAgent.indexOf('Android') != -1) {
  658. this.pc_constraints = {}; // disable DTLS on Android
  659. }
  660. if (!webkitMediaStream.prototype.getVideoTracks) {
  661. webkitMediaStream.prototype.getVideoTracks = function () {
  662. return this.videoTracks;
  663. };
  664. }
  665. if (!webkitMediaStream.prototype.getAudioTracks) {
  666. webkitMediaStream.prototype.getAudioTracks = function () {
  667. return this.audioTracks;
  668. };
  669. }
  670. }
  671. else
  672. {
  673. try { console.log('Browser does not appear to be WebRTC-capable'); } catch (e) { }
  674. window.location.href = 'webrtcrequired.html';
  675. return;
  676. }
  677. if (this.browser !== RTCBrowserType.RTC_BROWSER_CHROME &&
  678. config.enableFirefoxSupport !== true) {
  679. window.location.href = 'chromeonly.html';
  680. return;
  681. }
  682. }
  683. RTCUtils.prototype.getUserMediaWithConstraints = function(
  684. um, success_callback, failure_callback, resolution,bandwidth, fps,
  685. desktopStream)
  686. {
  687. // Check if we are running on Android device
  688. var isAndroid = navigator.userAgent.indexOf('Android') != -1;
  689. var constraints = getConstraints(
  690. um, resolution, bandwidth, fps, desktopStream, isAndroid);
  691. var isFF = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
  692. try {
  693. if (config.enableSimulcast
  694. && constraints.video
  695. && constraints.video.chromeMediaSource !== 'screen'
  696. && constraints.video.chromeMediaSource !== 'desktop'
  697. && !isAndroid
  698. // We currently do not support FF, as it doesn't have multistream support.
  699. && !isFF) {
  700. simulcast.getUserMedia(constraints, function (stream) {
  701. console.log('onUserMediaSuccess');
  702. success_callback(stream);
  703. },
  704. function (error) {
  705. console.warn('Failed to get access to local media. Error ', error);
  706. if (failure_callback) {
  707. failure_callback(error);
  708. }
  709. });
  710. } else {
  711. this.getUserMedia(constraints,
  712. function (stream) {
  713. console.log('onUserMediaSuccess');
  714. success_callback(stream);
  715. },
  716. function (error) {
  717. console.warn('Failed to get access to local media. Error ',
  718. error, constraints);
  719. if (failure_callback) {
  720. failure_callback(error);
  721. }
  722. });
  723. }
  724. } catch (e) {
  725. console.error('GUM failed: ', e);
  726. if(failure_callback) {
  727. failure_callback(e);
  728. }
  729. }
  730. };
  731. /**
  732. * We ask for audio and video combined stream in order to get permissions and
  733. * not to ask twice.
  734. */
  735. RTCUtils.prototype.obtainAudioAndVideoPermissions = function() {
  736. var self = this;
  737. // Get AV
  738. var cb = function (stream) {
  739. console.log('got', stream, stream.getAudioTracks().length, stream.getVideoTracks().length);
  740. self.handleLocalStream(stream);
  741. };
  742. var self = this;
  743. this.getUserMediaWithConstraints(
  744. ['audio', 'video'],
  745. cb,
  746. function (error) {
  747. console.error('failed to obtain audio/video stream - trying audio only', error);
  748. self.getUserMediaWithConstraints(
  749. ['audio'],
  750. cb,
  751. function (error) {
  752. console.error('failed to obtain audio/video stream - stop', error);
  753. UI.messageHandler.showError("Error",
  754. "Failed to obtain permissions to use the local microphone" +
  755. "and/or camera.");
  756. }
  757. );
  758. },
  759. config.resolution || '360');
  760. }
  761. RTCUtils.prototype.handleLocalStream = function(stream)
  762. {
  763. if(window.webkitMediaStream)
  764. {
  765. var audioStream = new webkitMediaStream();
  766. var videoStream = new webkitMediaStream();
  767. var audioTracks = stream.getAudioTracks();
  768. var videoTracks = stream.getVideoTracks();
  769. for (var i = 0; i < audioTracks.length; i++) {
  770. audioStream.addTrack(audioTracks[i]);
  771. }
  772. this.service.createLocalStream(audioStream, "audio");
  773. for (i = 0; i < videoTracks.length; i++) {
  774. videoStream.addTrack(videoTracks[i]);
  775. }
  776. this.service.createLocalStream(videoStream, "video");
  777. }
  778. else
  779. {//firefox
  780. this.service.createLocalStream(stream, "stream");
  781. }
  782. };
  783. module.exports = RTCUtils;
  784. },{}],6:[function(require,module,exports){
  785. // Copyright Joyent, Inc. and other Node contributors.
  786. //
  787. // Permission is hereby granted, free of charge, to any person obtaining a
  788. // copy of this software and associated documentation files (the
  789. // "Software"), to deal in the Software without restriction, including
  790. // without limitation the rights to use, copy, modify, merge, publish,
  791. // distribute, sublicense, and/or sell copies of the Software, and to permit
  792. // persons to whom the Software is furnished to do so, subject to the
  793. // following conditions:
  794. //
  795. // The above copyright notice and this permission notice shall be included
  796. // in all copies or substantial portions of the Software.
  797. //
  798. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  799. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  800. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  801. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  802. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  803. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  804. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  805. function EventEmitter() {
  806. this._events = this._events || {};
  807. this._maxListeners = this._maxListeners || undefined;
  808. }
  809. module.exports = EventEmitter;
  810. // Backwards-compat with node 0.10.x
  811. EventEmitter.EventEmitter = EventEmitter;
  812. EventEmitter.prototype._events = undefined;
  813. EventEmitter.prototype._maxListeners = undefined;
  814. // By default EventEmitters will print a warning if more than 10 listeners are
  815. // added to it. This is a useful default which helps finding memory leaks.
  816. EventEmitter.defaultMaxListeners = 10;
  817. // Obviously not all Emitters should be limited to 10. This function allows
  818. // that to be increased. Set to zero for unlimited.
  819. EventEmitter.prototype.setMaxListeners = function(n) {
  820. if (!isNumber(n) || n < 0 || isNaN(n))
  821. throw TypeError('n must be a positive number');
  822. this._maxListeners = n;
  823. return this;
  824. };
  825. EventEmitter.prototype.emit = function(type) {
  826. var er, handler, len, args, i, listeners;
  827. if (!this._events)
  828. this._events = {};
  829. // If there is no 'error' event listener then throw.
  830. if (type === 'error') {
  831. if (!this._events.error ||
  832. (isObject(this._events.error) && !this._events.error.length)) {
  833. er = arguments[1];
  834. if (er instanceof Error) {
  835. throw er; // Unhandled 'error' event
  836. } else {
  837. throw TypeError('Uncaught, unspecified "error" event.');
  838. }
  839. return false;
  840. }
  841. }
  842. handler = this._events[type];
  843. if (isUndefined(handler))
  844. return false;
  845. if (isFunction(handler)) {
  846. switch (arguments.length) {
  847. // fast cases
  848. case 1:
  849. handler.call(this);
  850. break;
  851. case 2:
  852. handler.call(this, arguments[1]);
  853. break;
  854. case 3:
  855. handler.call(this, arguments[1], arguments[2]);
  856. break;
  857. // slower
  858. default:
  859. len = arguments.length;
  860. args = new Array(len - 1);
  861. for (i = 1; i < len; i++)
  862. args[i - 1] = arguments[i];
  863. handler.apply(this, args);
  864. }
  865. } else if (isObject(handler)) {
  866. len = arguments.length;
  867. args = new Array(len - 1);
  868. for (i = 1; i < len; i++)
  869. args[i - 1] = arguments[i];
  870. listeners = handler.slice();
  871. len = listeners.length;
  872. for (i = 0; i < len; i++)
  873. listeners[i].apply(this, args);
  874. }
  875. return true;
  876. };
  877. EventEmitter.prototype.addListener = function(type, listener) {
  878. var m;
  879. if (!isFunction(listener))
  880. throw TypeError('listener must be a function');
  881. if (!this._events)
  882. this._events = {};
  883. // To avoid recursion in the case that type === "newListener"! Before
  884. // adding it to the listeners, first emit "newListener".
  885. if (this._events.newListener)
  886. this.emit('newListener', type,
  887. isFunction(listener.listener) ?
  888. listener.listener : listener);
  889. if (!this._events[type])
  890. // Optimize the case of one listener. Don't need the extra array object.
  891. this._events[type] = listener;
  892. else if (isObject(this._events[type]))
  893. // If we've already got an array, just append.
  894. this._events[type].push(listener);
  895. else
  896. // Adding the second element, need to change to array.
  897. this._events[type] = [this._events[type], listener];
  898. // Check for listener leak
  899. if (isObject(this._events[type]) && !this._events[type].warned) {
  900. var m;
  901. if (!isUndefined(this._maxListeners)) {
  902. m = this._maxListeners;
  903. } else {
  904. m = EventEmitter.defaultMaxListeners;
  905. }
  906. if (m && m > 0 && this._events[type].length > m) {
  907. this._events[type].warned = true;
  908. console.error('(node) warning: possible EventEmitter memory ' +
  909. 'leak detected. %d listeners added. ' +
  910. 'Use emitter.setMaxListeners() to increase limit.',
  911. this._events[type].length);
  912. if (typeof console.trace === 'function') {
  913. // not supported in IE 10
  914. console.trace();
  915. }
  916. }
  917. }
  918. return this;
  919. };
  920. EventEmitter.prototype.on = EventEmitter.prototype.addListener;
  921. EventEmitter.prototype.once = function(type, listener) {
  922. if (!isFunction(listener))
  923. throw TypeError('listener must be a function');
  924. var fired = false;
  925. function g() {
  926. this.removeListener(type, g);
  927. if (!fired) {
  928. fired = true;
  929. listener.apply(this, arguments);
  930. }
  931. }
  932. g.listener = listener;
  933. this.on(type, g);
  934. return this;
  935. };
  936. // emits a 'removeListener' event iff the listener was removed
  937. EventEmitter.prototype.removeListener = function(type, listener) {
  938. var list, position, length, i;
  939. if (!isFunction(listener))
  940. throw TypeError('listener must be a function');
  941. if (!this._events || !this._events[type])
  942. return this;
  943. list = this._events[type];
  944. length = list.length;
  945. position = -1;
  946. if (list === listener ||
  947. (isFunction(list.listener) && list.listener === listener)) {
  948. delete this._events[type];
  949. if (this._events.removeListener)
  950. this.emit('removeListener', type, listener);
  951. } else if (isObject(list)) {
  952. for (i = length; i-- > 0;) {
  953. if (list[i] === listener ||
  954. (list[i].listener && list[i].listener === listener)) {
  955. position = i;
  956. break;
  957. }
  958. }
  959. if (position < 0)
  960. return this;
  961. if (list.length === 1) {
  962. list.length = 0;
  963. delete this._events[type];
  964. } else {
  965. list.splice(position, 1);
  966. }
  967. if (this._events.removeListener)
  968. this.emit('removeListener', type, listener);
  969. }
  970. return this;
  971. };
  972. EventEmitter.prototype.removeAllListeners = function(type) {
  973. var key, listeners;
  974. if (!this._events)
  975. return this;
  976. // not listening for removeListener, no need to emit
  977. if (!this._events.removeListener) {
  978. if (arguments.length === 0)
  979. this._events = {};
  980. else if (this._events[type])
  981. delete this._events[type];
  982. return this;
  983. }
  984. // emit removeListener for all listeners on all events
  985. if (arguments.length === 0) {
  986. for (key in this._events) {
  987. if (key === 'removeListener') continue;
  988. this.removeAllListeners(key);
  989. }
  990. this.removeAllListeners('removeListener');
  991. this._events = {};
  992. return this;
  993. }
  994. listeners = this._events[type];
  995. if (isFunction(listeners)) {
  996. this.removeListener(type, listeners);
  997. } else {
  998. // LIFO order
  999. while (listeners.length)
  1000. this.removeListener(type, listeners[listeners.length - 1]);
  1001. }
  1002. delete this._events[type];
  1003. return this;
  1004. };
  1005. EventEmitter.prototype.listeners = function(type) {
  1006. var ret;
  1007. if (!this._events || !this._events[type])
  1008. ret = [];
  1009. else if (isFunction(this._events[type]))
  1010. ret = [this._events[type]];
  1011. else
  1012. ret = this._events[type].slice();
  1013. return ret;
  1014. };
  1015. EventEmitter.listenerCount = function(emitter, type) {
  1016. var ret;
  1017. if (!emitter._events || !emitter._events[type])
  1018. ret = 0;
  1019. else if (isFunction(emitter._events[type]))
  1020. ret = 1;
  1021. else
  1022. ret = emitter._events[type].length;
  1023. return ret;
  1024. };
  1025. function isFunction(arg) {
  1026. return typeof arg === 'function';
  1027. }
  1028. function isNumber(arg) {
  1029. return typeof arg === 'number';
  1030. }
  1031. function isObject(arg) {
  1032. return typeof arg === 'object' && arg !== null;
  1033. }
  1034. function isUndefined(arg) {
  1035. return arg === void 0;
  1036. }
  1037. },{}]},{},[4])(4)
  1038. });