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

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