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 36KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  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. this.stream.onended = function()
  216. {
  217. self.streamEnded();
  218. };
  219. }
  220. LocalStream.prototype.streamEnded = function () {
  221. this.eventEmitter.emit(StreamEventTypes.EVENT_TYPE_LOCAL_ENDED, this);
  222. }
  223. LocalStream.prototype.getOriginalStream = function()
  224. {
  225. return this.stream;
  226. }
  227. LocalStream.prototype.isAudioStream = function () {
  228. return (this.stream.getAudioTracks() && this.stream.getAudioTracks().length > 0);
  229. }
  230. LocalStream.prototype.mute = function()
  231. {
  232. var ismuted = false;
  233. var tracks = [];
  234. if(this.type = "audio")
  235. {
  236. tracks = this.stream.getAudioTracks();
  237. }
  238. else
  239. {
  240. tracks = this.stream.getVideoTracks();
  241. }
  242. for (var idx = 0; idx < tracks.length; idx++) {
  243. ismuted = !tracks[idx].enabled;
  244. tracks[idx].enabled = !tracks[idx].enabled;
  245. }
  246. return ismuted;
  247. }
  248. LocalStream.prototype.isMuted = function () {
  249. var tracks = [];
  250. if(this.type = "audio")
  251. {
  252. tracks = this.stream.getAudioTracks();
  253. }
  254. else
  255. {
  256. tracks = this.stream.getVideoTracks();
  257. }
  258. for (var idx = 0; idx < tracks.length; idx++) {
  259. if(tracks[idx].enabled)
  260. return false;
  261. }
  262. return true;
  263. }
  264. module.exports = LocalStream;
  265. },{}],3:[function(require,module,exports){
  266. ////These lines should be uncommented when require works in app.js
  267. //var RTCBrowserType = require("../../service/RTC/RTCBrowserType.js");
  268. //var StreamEventTypes = require("../../service/RTC/StreamEventTypes.js");
  269. //var MediaStreamType = require("../../service/RTC/MediaStreamTypes");
  270. /**
  271. * Creates a MediaStream object for the given data, session id and ssrc.
  272. * It is a wrapper class for the MediaStream.
  273. *
  274. * @param data the data object from which we obtain the stream,
  275. * the peerjid, etc.
  276. * @param sid the session id
  277. * @param ssrc the ssrc corresponding to this MediaStream
  278. *
  279. * @constructor
  280. */
  281. function MediaStream(data, sid, ssrc, eventEmmiter, browser) {
  282. // XXX(gp) to minimize headaches in the future, we should build our
  283. // abstractions around tracks and not streams. ORTC is track based API.
  284. // Mozilla expects m-lines to represent media tracks.
  285. //
  286. // Practically, what I'm saying is that we should have a MediaTrack class
  287. // and not a MediaStream class.
  288. //
  289. // Also, we should be able to associate multiple SSRCs with a MediaTrack as
  290. // a track might have an associated RTX and FEC sources.
  291. this.sid = sid;
  292. this.stream = data.stream;
  293. this.peerjid = data.peerjid;
  294. this.ssrc = ssrc;
  295. this.type = (this.stream.getVideoTracks().length > 0)?
  296. MediaStreamType.VIDEO_TYPE : MediaStreamType.AUDIO_TYPE;
  297. this.muted = false;
  298. eventEmmiter.emit(StreamEventTypes.EVENT_TYPE_REMOTE_CREATED, this);
  299. if(browser == RTCBrowserType.RTC_BROWSER_FIREFOX)
  300. {
  301. if (!this.getVideoTracks)
  302. this.getVideoTracks = function () { return []; };
  303. if (!this.getAudioTracks)
  304. this.getAudioTracks = function () { return []; };
  305. }
  306. }
  307. MediaStream.prototype.getOriginalStream = function()
  308. {
  309. return this.stream;
  310. }
  311. MediaStream.prototype.setMute = function (value)
  312. {
  313. this.stream.muted = value;
  314. this.muted = value;
  315. }
  316. module.exports = MediaStream;
  317. },{}],4:[function(require,module,exports){
  318. var EventEmitter = require("events");
  319. var RTCUtils = require("./RTCUtils.js");
  320. var LocalStream = require("./LocalStream.js");
  321. var DataChannels = require("./DataChannels");
  322. var MediaStream = require("./MediaStream.js");
  323. //These lines should be uncommented when require works in app.js
  324. //var StreamEventTypes = require("../../service/RTC/StreamEventTypes.js");
  325. //var XMPPEvents = require("../service/xmpp/XMPPEvents");
  326. var eventEmitter = new EventEmitter();
  327. var RTC = {
  328. rtcUtils: null,
  329. localStreams: [],
  330. remoteStreams: {},
  331. localAudio: null,
  332. localVideo: null,
  333. addStreamListener: function (listener, eventType) {
  334. eventEmitter.on(eventType, listener);
  335. },
  336. removeStreamListener: function (listener, eventType) {
  337. if(!(eventType instanceof StreamEventTypes))
  338. throw "Illegal argument";
  339. eventEmitter.removeListener(eventType, listener);
  340. },
  341. createLocalStream: function (stream, type) {
  342. var localStream = new LocalStream(stream, type, eventEmitter);
  343. this.localStreams.push(localStream);
  344. if(type == "audio")
  345. {
  346. this.localAudio = localStream;
  347. }
  348. else
  349. {
  350. this.localVideo = localStream;
  351. }
  352. eventEmitter.emit(StreamEventTypes.EVENT_TYPE_LOCAL_CREATED,
  353. localStream);
  354. return localStream;
  355. },
  356. removeLocalStream: function (stream) {
  357. for(var i = 0; i < this.localStreams.length; i++)
  358. {
  359. if(this.localStreams[i].getOriginalStream() === stream) {
  360. delete this.localStreams[i];
  361. return;
  362. }
  363. }
  364. },
  365. createRemoteStream: function (data, sid, thessrc) {
  366. var remoteStream = new MediaStream(data, sid, thessrc, eventEmitter,
  367. this.getBrowserType());
  368. var jid = data.peerjid || connection.emuc.myroomjid;
  369. if(!this.remoteStreams[jid]) {
  370. this.remoteStreams[jid] = {};
  371. }
  372. this.remoteStreams[jid][remoteStream.type]= remoteStream;
  373. return remoteStream;
  374. },
  375. getBrowserType: function () {
  376. return this.rtcUtils.browser;
  377. },
  378. getPCConstraints: function () {
  379. return this.rtcUtils.pc_constraints;
  380. },
  381. getUserMediaWithConstraints:function(um, success_callback,
  382. failure_callback, resolution,
  383. bandwidth, fps, desktopStream)
  384. {
  385. return this.rtcUtils.getUserMediaWithConstraints(um, success_callback,
  386. failure_callback, resolution, bandwidth, fps, desktopStream);
  387. },
  388. attachMediaStream: function (element, stream) {
  389. this.rtcUtils.attachMediaStream(element, stream);
  390. },
  391. getStreamID: function (stream) {
  392. return this.rtcUtils.getStreamID(stream);
  393. },
  394. getVideoSrc: function (element) {
  395. return this.rtcUtils.getVideoSrc(element);
  396. },
  397. setVideoSrc: function (element, src) {
  398. this.rtcUtils.setVideoSrc(element, src);
  399. },
  400. dispose: function() {
  401. if (this.rtcUtils) {
  402. this.rtcUtils = null;
  403. }
  404. },
  405. stop: function () {
  406. this.dispose();
  407. },
  408. start: function () {
  409. this.rtcUtils = new RTCUtils(this);
  410. this.rtcUtils.obtainAudioAndVideoPermissions();
  411. },
  412. onConferenceCreated: function(event) {
  413. DataChannels.bindDataChannelListener(event.peerconnection);
  414. },
  415. muteRemoteVideoStream: function (jid, value) {
  416. var stream;
  417. if(this.remoteStreams[jid] &&
  418. this.remoteStreams[jid][MediaStreamType.VIDEO_TYPE])
  419. {
  420. stream = this.remoteStreams[jid][MediaStreamType.VIDEO_TYPE];
  421. }
  422. if(!stream)
  423. return false;
  424. var isMuted = (value === "true");
  425. if (isMuted != stream.muted) {
  426. stream.setMute(isMuted);
  427. return true;
  428. }
  429. return false;
  430. }
  431. };
  432. module.exports = RTC;
  433. },{"./DataChannels":1,"./LocalStream.js":2,"./MediaStream.js":3,"./RTCUtils.js":5,"events":6}],5:[function(require,module,exports){
  434. //This should be uncommented when app.js supports require
  435. //var RTCBrowserType = require("../../service/RTC/RTCBrowserType.js");
  436. function setResolutionConstraints(constraints, resolution, isAndroid)
  437. {
  438. if (resolution && !constraints.video || isAndroid) {
  439. constraints.video = { mandatory: {}, optional: [] };// same behaviour as true
  440. }
  441. // see https://code.google.com/p/chromium/issues/detail?id=143631#c9 for list of supported resolutions
  442. switch (resolution) {
  443. // 16:9 first
  444. case '1080':
  445. case 'fullhd':
  446. constraints.video.mandatory.minWidth = 1920;
  447. constraints.video.mandatory.minHeight = 1080;
  448. break;
  449. case '720':
  450. case 'hd':
  451. constraints.video.mandatory.minWidth = 1280;
  452. constraints.video.mandatory.minHeight = 720;
  453. break;
  454. case '360':
  455. constraints.video.mandatory.minWidth = 640;
  456. constraints.video.mandatory.minHeight = 360;
  457. break;
  458. case '180':
  459. constraints.video.mandatory.minWidth = 320;
  460. constraints.video.mandatory.minHeight = 180;
  461. break;
  462. // 4:3
  463. case '960':
  464. constraints.video.mandatory.minWidth = 960;
  465. constraints.video.mandatory.minHeight = 720;
  466. break;
  467. case '640':
  468. case 'vga':
  469. constraints.video.mandatory.minWidth = 640;
  470. constraints.video.mandatory.minHeight = 480;
  471. break;
  472. case '320':
  473. constraints.video.mandatory.minWidth = 320;
  474. constraints.video.mandatory.minHeight = 240;
  475. break;
  476. default:
  477. if (isAndroid) {
  478. constraints.video.mandatory.minWidth = 320;
  479. constraints.video.mandatory.minHeight = 240;
  480. constraints.video.mandatory.maxFrameRate = 15;
  481. }
  482. break;
  483. }
  484. if (constraints.video.mandatory.minWidth)
  485. constraints.video.mandatory.maxWidth = constraints.video.mandatory.minWidth;
  486. if (constraints.video.mandatory.minHeight)
  487. constraints.video.mandatory.maxHeight = constraints.video.mandatory.minHeight;
  488. }
  489. function getConstraints(um, resolution, bandwidth, fps, desktopStream, isAndroid)
  490. {
  491. var constraints = {audio: false, video: false};
  492. if (um.indexOf('video') >= 0) {
  493. constraints.video = { mandatory: {}, optional: [] };// same behaviour as true
  494. }
  495. if (um.indexOf('audio') >= 0) {
  496. constraints.audio = { mandatory: {}, optional: []};// same behaviour as true
  497. }
  498. if (um.indexOf('screen') >= 0) {
  499. constraints.video = {
  500. mandatory: {
  501. chromeMediaSource: "screen",
  502. googLeakyBucket: true,
  503. maxWidth: window.screen.width,
  504. maxHeight: window.screen.height,
  505. maxFrameRate: 3
  506. },
  507. optional: []
  508. };
  509. }
  510. if (um.indexOf('desktop') >= 0) {
  511. constraints.video = {
  512. mandatory: {
  513. chromeMediaSource: "desktop",
  514. chromeMediaSourceId: desktopStream,
  515. googLeakyBucket: true,
  516. maxWidth: window.screen.width,
  517. maxHeight: window.screen.height,
  518. maxFrameRate: 3
  519. },
  520. optional: []
  521. };
  522. }
  523. if (constraints.audio) {
  524. // if it is good enough for hangouts...
  525. constraints.audio.optional.push(
  526. {googEchoCancellation: true},
  527. {googAutoGainControl: true},
  528. {googNoiseSupression: true},
  529. {googHighpassFilter: true},
  530. {googNoisesuppression2: true},
  531. {googEchoCancellation2: true},
  532. {googAutoGainControl2: true}
  533. );
  534. }
  535. if (constraints.video) {
  536. constraints.video.optional.push(
  537. {googNoiseReduction: false} // chrome 37 workaround for issue 3807, reenable in M38
  538. );
  539. if (um.indexOf('video') >= 0) {
  540. constraints.video.optional.push(
  541. {googLeakyBucket: true}
  542. );
  543. }
  544. }
  545. setResolutionConstraints(constraints, resolution, isAndroid);
  546. if (bandwidth) { // doesn't work currently, see webrtc issue 1846
  547. if (!constraints.video) constraints.video = {mandatory: {}, optional: []};//same behaviour as true
  548. constraints.video.optional.push({bandwidth: bandwidth});
  549. }
  550. if (fps) { // for some cameras it might be necessary to request 30fps
  551. // so they choose 30fps mjpg over 10fps yuy2
  552. if (!constraints.video) constraints.video = {mandatory: {}, optional: []};// same behaviour as true;
  553. constraints.video.mandatory.minFrameRate = fps;
  554. }
  555. return constraints;
  556. }
  557. function RTCUtils(RTCService)
  558. {
  559. this.service = RTCService;
  560. if (navigator.mozGetUserMedia) {
  561. console.log('This appears to be Firefox');
  562. var version = parseInt(navigator.userAgent.match(/Firefox\/([0-9]+)\./)[1], 10);
  563. if (version >= 22) {
  564. this.peerconnection = mozRTCPeerConnection;
  565. this.browser = RTCBrowserType.RTC_BROWSER_FIREFOX;
  566. this.getUserMedia = navigator.mozGetUserMedia.bind(navigator);
  567. this.pc_constraints = {};
  568. this.attachMediaStream = function (element, stream) {
  569. element[0].mozSrcObject = stream;
  570. element[0].play();
  571. };
  572. this.getStreamID = function (stream) {
  573. var tracks = stream.getVideoTracks();
  574. if(!tracks || tracks.length == 0)
  575. {
  576. tracks = stream.getAudioTracks();
  577. }
  578. return tracks[0].id.replace(/[\{,\}]/g,"");
  579. };
  580. this.getVideoSrc = function (element) {
  581. return element.mozSrcObject;
  582. };
  583. this.setVideoSrc = function (element, src) {
  584. element.mozSrcObject = src;
  585. };
  586. RTCSessionDescription = mozRTCSessionDescription;
  587. RTCIceCandidate = mozRTCIceCandidate;
  588. }
  589. } else if (navigator.webkitGetUserMedia) {
  590. console.log('This appears to be Chrome');
  591. this.peerconnection = webkitRTCPeerConnection;
  592. this.browser = RTCBrowserType.RTC_BROWSER_CHROME;
  593. this.getUserMedia = navigator.webkitGetUserMedia.bind(navigator);
  594. this.attachMediaStream = function (element, stream) {
  595. element.attr('src', webkitURL.createObjectURL(stream));
  596. };
  597. this.getStreamID = function (stream) {
  598. // streams from FF endpoints have the characters '{' and '}'
  599. // that make jQuery choke.
  600. return stream.id.replace(/[\{,\}]/g,"");
  601. };
  602. this.getVideoSrc = function (element) {
  603. return element.getAttribute("src");
  604. };
  605. this.setVideoSrc = function (element, src) {
  606. element.setAttribute("src", src);
  607. };
  608. // DTLS should now be enabled by default but..
  609. this.pc_constraints = {'optional': [{'DtlsSrtpKeyAgreement': 'true'}]};
  610. if (navigator.userAgent.indexOf('Android') != -1) {
  611. this.pc_constraints = {}; // disable DTLS on Android
  612. }
  613. if (!webkitMediaStream.prototype.getVideoTracks) {
  614. webkitMediaStream.prototype.getVideoTracks = function () {
  615. return this.videoTracks;
  616. };
  617. }
  618. if (!webkitMediaStream.prototype.getAudioTracks) {
  619. webkitMediaStream.prototype.getAudioTracks = function () {
  620. return this.audioTracks;
  621. };
  622. }
  623. }
  624. else
  625. {
  626. try { console.log('Browser does not appear to be WebRTC-capable'); } catch (e) { }
  627. window.location.href = 'webrtcrequired.html';
  628. return;
  629. }
  630. if (this.browser !== RTCBrowserType.RTC_BROWSER_CHROME &&
  631. config.enableFirefoxSupport !== true) {
  632. window.location.href = 'chromeonly.html';
  633. return;
  634. }
  635. }
  636. RTCUtils.prototype.getUserMediaWithConstraints = function(
  637. um, success_callback, failure_callback, resolution,bandwidth, fps,
  638. desktopStream)
  639. {
  640. // Check if we are running on Android device
  641. var isAndroid = navigator.userAgent.indexOf('Android') != -1;
  642. var constraints = getConstraints(
  643. um, resolution, bandwidth, fps, desktopStream, isAndroid);
  644. var isFF = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
  645. try {
  646. if (config.enableSimulcast
  647. && constraints.video
  648. && constraints.video.chromeMediaSource !== 'screen'
  649. && constraints.video.chromeMediaSource !== 'desktop'
  650. && !isAndroid
  651. // We currently do not support FF, as it doesn't have multistream support.
  652. && !isFF) {
  653. simulcast.getUserMedia(constraints, function (stream) {
  654. console.log('onUserMediaSuccess');
  655. success_callback(stream);
  656. },
  657. function (error) {
  658. console.warn('Failed to get access to local media. Error ', error);
  659. if (failure_callback) {
  660. failure_callback(error);
  661. }
  662. });
  663. } else {
  664. this.getUserMedia(constraints,
  665. function (stream) {
  666. console.log('onUserMediaSuccess');
  667. success_callback(stream);
  668. },
  669. function (error) {
  670. console.warn('Failed to get access to local media. Error ',
  671. error, constraints);
  672. if (failure_callback) {
  673. failure_callback(error);
  674. }
  675. });
  676. }
  677. } catch (e) {
  678. console.error('GUM failed: ', e);
  679. if(failure_callback) {
  680. failure_callback(e);
  681. }
  682. }
  683. };
  684. /**
  685. * We ask for audio and video combined stream in order to get permissions and
  686. * not to ask twice.
  687. */
  688. RTCUtils.prototype.obtainAudioAndVideoPermissions = function() {
  689. var self = this;
  690. // Get AV
  691. var cb = function (stream) {
  692. console.log('got', stream, stream.getAudioTracks().length, stream.getVideoTracks().length);
  693. self.handleLocalStream(stream);
  694. };
  695. var self = this;
  696. this.getUserMediaWithConstraints(
  697. ['audio', 'video'],
  698. cb,
  699. function (error) {
  700. console.error('failed to obtain audio/video stream - trying audio only', error);
  701. self.getUserMediaWithConstraints(
  702. ['audio'],
  703. cb,
  704. function (error) {
  705. console.error('failed to obtain audio/video stream - stop', error);
  706. UI.messageHandler.showError("Error",
  707. "Failed to obtain permissions to use the local microphone" +
  708. "and/or camera.");
  709. }
  710. );
  711. },
  712. config.resolution || '360');
  713. }
  714. RTCUtils.prototype.handleLocalStream = function(stream)
  715. {
  716. if(window.webkitMediaStream)
  717. {
  718. var audioStream = new webkitMediaStream();
  719. var videoStream = new webkitMediaStream();
  720. var audioTracks = stream.getAudioTracks();
  721. var videoTracks = stream.getVideoTracks();
  722. for (var i = 0; i < audioTracks.length; i++) {
  723. audioStream.addTrack(audioTracks[i]);
  724. }
  725. this.service.createLocalStream(audioStream, "audio");
  726. for (i = 0; i < videoTracks.length; i++) {
  727. videoStream.addTrack(videoTracks[i]);
  728. }
  729. this.service.createLocalStream(videoStream, "video");
  730. }
  731. else
  732. {//firefox
  733. this.service.createLocalStream(stream, "stream");
  734. }
  735. };
  736. module.exports = RTCUtils;
  737. },{}],6:[function(require,module,exports){
  738. // Copyright Joyent, Inc. and other Node contributors.
  739. //
  740. // Permission is hereby granted, free of charge, to any person obtaining a
  741. // copy of this software and associated documentation files (the
  742. // "Software"), to deal in the Software without restriction, including
  743. // without limitation the rights to use, copy, modify, merge, publish,
  744. // distribute, sublicense, and/or sell copies of the Software, and to permit
  745. // persons to whom the Software is furnished to do so, subject to the
  746. // following conditions:
  747. //
  748. // The above copyright notice and this permission notice shall be included
  749. // in all copies or substantial portions of the Software.
  750. //
  751. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  752. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  753. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  754. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  755. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  756. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  757. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  758. function EventEmitter() {
  759. this._events = this._events || {};
  760. this._maxListeners = this._maxListeners || undefined;
  761. }
  762. module.exports = EventEmitter;
  763. // Backwards-compat with node 0.10.x
  764. EventEmitter.EventEmitter = EventEmitter;
  765. EventEmitter.prototype._events = undefined;
  766. EventEmitter.prototype._maxListeners = undefined;
  767. // By default EventEmitters will print a warning if more than 10 listeners are
  768. // added to it. This is a useful default which helps finding memory leaks.
  769. EventEmitter.defaultMaxListeners = 10;
  770. // Obviously not all Emitters should be limited to 10. This function allows
  771. // that to be increased. Set to zero for unlimited.
  772. EventEmitter.prototype.setMaxListeners = function(n) {
  773. if (!isNumber(n) || n < 0 || isNaN(n))
  774. throw TypeError('n must be a positive number');
  775. this._maxListeners = n;
  776. return this;
  777. };
  778. EventEmitter.prototype.emit = function(type) {
  779. var er, handler, len, args, i, listeners;
  780. if (!this._events)
  781. this._events = {};
  782. // If there is no 'error' event listener then throw.
  783. if (type === 'error') {
  784. if (!this._events.error ||
  785. (isObject(this._events.error) && !this._events.error.length)) {
  786. er = arguments[1];
  787. if (er instanceof Error) {
  788. throw er; // Unhandled 'error' event
  789. } else {
  790. throw TypeError('Uncaught, unspecified "error" event.');
  791. }
  792. return false;
  793. }
  794. }
  795. handler = this._events[type];
  796. if (isUndefined(handler))
  797. return false;
  798. if (isFunction(handler)) {
  799. switch (arguments.length) {
  800. // fast cases
  801. case 1:
  802. handler.call(this);
  803. break;
  804. case 2:
  805. handler.call(this, arguments[1]);
  806. break;
  807. case 3:
  808. handler.call(this, arguments[1], arguments[2]);
  809. break;
  810. // slower
  811. default:
  812. len = arguments.length;
  813. args = new Array(len - 1);
  814. for (i = 1; i < len; i++)
  815. args[i - 1] = arguments[i];
  816. handler.apply(this, args);
  817. }
  818. } else if (isObject(handler)) {
  819. len = arguments.length;
  820. args = new Array(len - 1);
  821. for (i = 1; i < len; i++)
  822. args[i - 1] = arguments[i];
  823. listeners = handler.slice();
  824. len = listeners.length;
  825. for (i = 0; i < len; i++)
  826. listeners[i].apply(this, args);
  827. }
  828. return true;
  829. };
  830. EventEmitter.prototype.addListener = function(type, listener) {
  831. var m;
  832. if (!isFunction(listener))
  833. throw TypeError('listener must be a function');
  834. if (!this._events)
  835. this._events = {};
  836. // To avoid recursion in the case that type === "newListener"! Before
  837. // adding it to the listeners, first emit "newListener".
  838. if (this._events.newListener)
  839. this.emit('newListener', type,
  840. isFunction(listener.listener) ?
  841. listener.listener : listener);
  842. if (!this._events[type])
  843. // Optimize the case of one listener. Don't need the extra array object.
  844. this._events[type] = listener;
  845. else if (isObject(this._events[type]))
  846. // If we've already got an array, just append.
  847. this._events[type].push(listener);
  848. else
  849. // Adding the second element, need to change to array.
  850. this._events[type] = [this._events[type], listener];
  851. // Check for listener leak
  852. if (isObject(this._events[type]) && !this._events[type].warned) {
  853. var m;
  854. if (!isUndefined(this._maxListeners)) {
  855. m = this._maxListeners;
  856. } else {
  857. m = EventEmitter.defaultMaxListeners;
  858. }
  859. if (m && m > 0 && this._events[type].length > m) {
  860. this._events[type].warned = true;
  861. console.error('(node) warning: possible EventEmitter memory ' +
  862. 'leak detected. %d listeners added. ' +
  863. 'Use emitter.setMaxListeners() to increase limit.',
  864. this._events[type].length);
  865. if (typeof console.trace === 'function') {
  866. // not supported in IE 10
  867. console.trace();
  868. }
  869. }
  870. }
  871. return this;
  872. };
  873. EventEmitter.prototype.on = EventEmitter.prototype.addListener;
  874. EventEmitter.prototype.once = function(type, listener) {
  875. if (!isFunction(listener))
  876. throw TypeError('listener must be a function');
  877. var fired = false;
  878. function g() {
  879. this.removeListener(type, g);
  880. if (!fired) {
  881. fired = true;
  882. listener.apply(this, arguments);
  883. }
  884. }
  885. g.listener = listener;
  886. this.on(type, g);
  887. return this;
  888. };
  889. // emits a 'removeListener' event iff the listener was removed
  890. EventEmitter.prototype.removeListener = function(type, listener) {
  891. var list, position, length, i;
  892. if (!isFunction(listener))
  893. throw TypeError('listener must be a function');
  894. if (!this._events || !this._events[type])
  895. return this;
  896. list = this._events[type];
  897. length = list.length;
  898. position = -1;
  899. if (list === listener ||
  900. (isFunction(list.listener) && list.listener === listener)) {
  901. delete this._events[type];
  902. if (this._events.removeListener)
  903. this.emit('removeListener', type, listener);
  904. } else if (isObject(list)) {
  905. for (i = length; i-- > 0;) {
  906. if (list[i] === listener ||
  907. (list[i].listener && list[i].listener === listener)) {
  908. position = i;
  909. break;
  910. }
  911. }
  912. if (position < 0)
  913. return this;
  914. if (list.length === 1) {
  915. list.length = 0;
  916. delete this._events[type];
  917. } else {
  918. list.splice(position, 1);
  919. }
  920. if (this._events.removeListener)
  921. this.emit('removeListener', type, listener);
  922. }
  923. return this;
  924. };
  925. EventEmitter.prototype.removeAllListeners = function(type) {
  926. var key, listeners;
  927. if (!this._events)
  928. return this;
  929. // not listening for removeListener, no need to emit
  930. if (!this._events.removeListener) {
  931. if (arguments.length === 0)
  932. this._events = {};
  933. else if (this._events[type])
  934. delete this._events[type];
  935. return this;
  936. }
  937. // emit removeListener for all listeners on all events
  938. if (arguments.length === 0) {
  939. for (key in this._events) {
  940. if (key === 'removeListener') continue;
  941. this.removeAllListeners(key);
  942. }
  943. this.removeAllListeners('removeListener');
  944. this._events = {};
  945. return this;
  946. }
  947. listeners = this._events[type];
  948. if (isFunction(listeners)) {
  949. this.removeListener(type, listeners);
  950. } else {
  951. // LIFO order
  952. while (listeners.length)
  953. this.removeListener(type, listeners[listeners.length - 1]);
  954. }
  955. delete this._events[type];
  956. return this;
  957. };
  958. EventEmitter.prototype.listeners = function(type) {
  959. var ret;
  960. if (!this._events || !this._events[type])
  961. ret = [];
  962. else if (isFunction(this._events[type]))
  963. ret = [this._events[type]];
  964. else
  965. ret = this._events[type].slice();
  966. return ret;
  967. };
  968. EventEmitter.listenerCount = function(emitter, type) {
  969. var ret;
  970. if (!emitter._events || !emitter._events[type])
  971. ret = 0;
  972. else if (isFunction(emitter._events[type]))
  973. ret = 1;
  974. else
  975. ret = emitter._events[type].length;
  976. return ret;
  977. };
  978. function isFunction(arg) {
  979. return typeof arg === 'function';
  980. }
  981. function isNumber(arg) {
  982. return typeof arg === 'number';
  983. }
  984. function isObject(arg) {
  985. return typeof arg === 'object' && arg !== null;
  986. }
  987. function isUndefined(arg) {
  988. return arg === void 0;
  989. }
  990. },{}]},{},[4])(4)
  991. });