Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

RTC.bundle.js 40KB

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