modified lib-jitsi-meet dev repo
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /* global APP */
  2. var EventEmitter = require("events");
  3. var RTCBrowserType = require("./RTCBrowserType");
  4. var RTCUtils = require("./RTCUtils.js");
  5. var JitsiTrack = require("./JitsiTrack");
  6. var JitsiLocalTrack = require("./JitsiLocalTrack.js");
  7. var DataChannels = require("./DataChannels");
  8. var JitsiRemoteTrack = require("./JitsiRemoteTrack.js");
  9. var DesktopSharingEventTypes
  10. = require("../../service/desktopsharing/DesktopSharingEventTypes");
  11. var MediaStreamType = require("../../service/RTC/MediaStreamTypes");
  12. var StreamEventTypes = require("../../service/RTC/StreamEventTypes.js");
  13. var RTCEvents = require("../../service/RTC/RTCEvents.js");
  14. var desktopsharing = require("../desktopsharing/desktopsharing");
  15. function getMediaStreamUsage()
  16. {
  17. var result = {
  18. audio: true,
  19. video: true
  20. };
  21. /** There are some issues with the desktop sharing
  22. * when this property is enabled.
  23. * WARNING: We must change the implementation to start video/audio if we
  24. * receive from the focus that the peer is not muted.
  25. var isSecureConnection = window.location.protocol == "https:";
  26. if(config.disableEarlyMediaPermissionRequests || !isSecureConnection)
  27. {
  28. result = {
  29. audio: false,
  30. video: false
  31. };
  32. }
  33. **/
  34. return result;
  35. }
  36. var rtcReady = false;
  37. function RTC(room, options) {
  38. this.devices = {
  39. audio: true,
  40. video: true
  41. };
  42. this.room = room;
  43. this.localStreams = [];
  44. this.remoteStreams = {};
  45. this.localAudio = null;
  46. this.localVideo = null;
  47. this.eventEmitter = new EventEmitter();
  48. var self = this;
  49. this.options = options || {};
  50. desktopsharing.addListener(
  51. function (stream, isUsingScreenStream, callback) {
  52. self.changeLocalVideo(stream, isUsingScreenStream, callback);
  53. }, DesktopSharingEventTypes.NEW_STREAM_CREATED);
  54. room.addPresenceListener("videomuted", function (values, from) {
  55. if(self.remoteStreams[from])
  56. self.remoteStreams[from][JitsiTrack.VIDEO].setMute(values.value == "true");
  57. });
  58. room.addPresenceListener("audiomuted", function (values, from) {
  59. if(self.remoteStreams[from])
  60. self.remoteStreams[from][JitsiTrack.AUDIO].setMute(values.value == "true");
  61. });
  62. }
  63. /**
  64. * Creates the local MediaStreams.
  65. * @param options object for options (NOTE: currently only list of devices and resolution are supported)
  66. * @param dontCreateJitsiTrack if <tt>true</tt> objects with the following structure {stream: the Media Stream,
  67. * type: "audio" or "video", isMuted: true/false, videoType: "camera" or "desktop"}
  68. * will be returned trough the Promise, otherwise JitsiTrack objects will be returned.
  69. * @returns {*} Promise object that will receive the new JitsiTracks
  70. */
  71. RTC.prototype.obtainAudioAndVideoPermissions = function (options, dontCreateJitsiTrack) {
  72. return RTCUtils.obtainAudioAndVideoPermissions(this,
  73. options.devices, getMediaStreamUsage(), options.resolution, dontCreateJitsiTrack);
  74. }
  75. RTC.prototype.onIncommingCall = function(event) {
  76. if(this.options.config.openSctp)
  77. this.dataChannels = new DataChannels(event.peerconnection, this.eventEmitter);
  78. for(var i = 0; i < this.localStreams.length; i++)
  79. if(this.localStreams[i].isStarted)
  80. {
  81. this.localStreams[i].start();
  82. }
  83. }
  84. RTC.prototype.selectedEndpoint = function (id) {
  85. if(this.dataChannels)
  86. this.dataChannels.handleSelectedEndpointEvent(id);
  87. }
  88. RTC.prototype.pinEndpoint = function (id) {
  89. if(this.dataChannels)
  90. this.dataChannels.handlePinnedEndpointEvent(id);
  91. }
  92. RTC.prototype.addListener = function (type, listener) {
  93. this.eventEmitter.on(type, listener);
  94. };
  95. RTC.prototype.removeListener = function (eventType, listener) {
  96. this.eventEmitter.removeListener(eventType, listener);
  97. };
  98. RTC.addRTCReadyListener = function (listener) {
  99. RTCUtils.eventEmitter.on(RTCEvents.RTC_READY, listener);
  100. }
  101. RTC.removeRTCReadyListener = function (listener) {
  102. RTCUtils.eventEmitter.removeListener(RTCEvents.RTC_READY, listener);
  103. }
  104. RTC.isRTCReady = function () {
  105. return rtcReady;
  106. }
  107. RTC.init = function (options) {
  108. // In case of IE we continue from 'onReady' callback
  109. // passed to RTCUtils constructor. It will be invoked by Temasys plugin
  110. // once it is initialized.
  111. var onReady = function () {
  112. rtcReady = true;
  113. RTCUtils.eventEmitter.emit(RTCEvents.RTC_READY, true);
  114. };
  115. RTCUtils.init(onReady, options || {});
  116. // Call onReady() if Temasys plugin is not used
  117. if (!RTCBrowserType.isTemasysPluginUsed()) {
  118. onReady();
  119. }
  120. }
  121. RTC.prototype.createLocalStreams = function (streams, change) {
  122. for (var i = 0; i < streams.length; i++) {
  123. var localStream = new JitsiLocalTrack(this, streams[i].stream,
  124. this.eventEmitter, streams[i].videoType,
  125. streams[i].isGUMStream);
  126. this.localStreams.push(localStream);
  127. if (streams[i].isMuted === true)
  128. localStream.setMute(true);
  129. if (streams[i].type == "audio") {
  130. this.localAudio = localStream;
  131. } else {
  132. this.localVideo = localStream;
  133. }
  134. var eventType = StreamEventTypes.EVENT_TYPE_LOCAL_CREATED;
  135. if (change)
  136. eventType = StreamEventTypes.EVENT_TYPE_LOCAL_CHANGED;
  137. this.eventEmitter.emit(eventType, localStream, streams[i].isMuted);
  138. }
  139. return this.localStreams;
  140. };
  141. RTC.prototype.removeLocalStream = function (stream) {
  142. for(var i = 0; i < this.localStreams.length; i++) {
  143. if(this.localStreams[i].getOriginalStream() === stream) {
  144. delete this.localStreams[i];
  145. return;
  146. }
  147. }
  148. };
  149. RTC.prototype.createRemoteStream = function (data, sid, thessrc) {
  150. var remoteStream = new JitsiRemoteTrack(this, data, sid, thessrc,
  151. this.eventEmitter);
  152. if(!data.peerjid)
  153. return;
  154. var jid = data.peerjid;
  155. if(!this.remoteStreams[jid]) {
  156. this.remoteStreams[jid] = {};
  157. }
  158. this.remoteStreams[jid][remoteStream.type]= remoteStream;
  159. this.eventEmitter.emit(StreamEventTypes.EVENT_TYPE_REMOTE_CREATED, remoteStream);
  160. return remoteStream;
  161. };
  162. RTC.getPCConstraints = function () {
  163. return RTCUtils.pc_constraints;
  164. };
  165. RTC.prototype.getUserMediaWithConstraints = function(um, success_callback,
  166. failure_callback, resolution,
  167. bandwidth, fps, desktopStream)
  168. {
  169. return RTCUtils.getUserMediaWithConstraints(this, um, success_callback,
  170. failure_callback, resolution, bandwidth, fps, desktopStream);
  171. };
  172. RTC.attachMediaStream = function (elSelector, stream) {
  173. RTCUtils.attachMediaStream(elSelector, stream);
  174. };
  175. RTC.getStreamID = function (stream) {
  176. return RTCUtils.getStreamID(stream);
  177. };
  178. RTC.getVideoSrc = function (element) {
  179. return RTCUtils.getVideoSrc(element);
  180. };
  181. RTC.setVideoSrc = function (element, src) {
  182. RTCUtils.setVideoSrc(element, src);
  183. };
  184. RTC.prototype.getVideoElementName = function () {
  185. return RTCBrowserType.isTemasysPluginUsed() ? 'object' : 'video';
  186. };
  187. RTC.prototype.dispose = function() {
  188. };
  189. RTC.prototype.muteRemoteVideoStream = function (jid, value) {
  190. var stream;
  191. if(this.remoteStreams[jid] &&
  192. this.remoteStreams[jid][MediaStreamType.VIDEO_TYPE]) {
  193. stream = this.remoteStreams[jid][MediaStreamType.VIDEO_TYPE];
  194. }
  195. if(!stream)
  196. return true;
  197. if (value != stream.muted) {
  198. stream.setMute(value);
  199. return true;
  200. }
  201. return false;
  202. };
  203. RTC.prototype.switchVideoStreams = function (new_stream) {
  204. this.localVideo.stream = new_stream;
  205. this.localStreams = [];
  206. //in firefox we have only one stream object
  207. if (this.localAudio.getOriginalStream() != new_stream)
  208. this.localStreams.push(this.localAudio);
  209. this.localStreams.push(this.localVideo);
  210. };
  211. /**
  212. * Creates <tt>JitsiTrack</tt> instance and replaces it with the local video.
  213. * The method also handles the sdp changes.
  214. * @param stream the new MediaStream received by the browser.
  215. * @param isUsingScreenStream <tt>true</tt> if the stream is for desktop stream.
  216. * @param callback - function that will be called after the operation is completed.
  217. */
  218. RTC.prototype.changeLocalVideo = function (stream, isUsingScreenStream, callback) {
  219. var oldStream = this.localVideo.getOriginalStream();
  220. var type = (isUsingScreenStream ? "screen" : "camera");
  221. var localCallback = callback;
  222. if(this.localVideo.isMuted() && this.localVideo.videoType !== type) {
  223. localCallback = function() {
  224. this.room.setVideoMute(false, function(mute) {
  225. this.eventEmitter.emit(RTCEvents.VIDEO_MUTE, mute);
  226. }.bind(this));
  227. callback();
  228. };
  229. }
  230. // FIXME: Workaround for FF/IE/Safari
  231. if (stream && stream.videoStream) {
  232. stream = stream.videoStream;
  233. }
  234. var videoStream = RTCUtils.createStream(stream, true);
  235. this.localVideo = this.createLocalStream(videoStream, "video", true, type);
  236. // Stop the stream to trigger onended event for old stream
  237. oldStream.stop();
  238. this.switchVideoStreams(videoStream, oldStream);
  239. this.room.switchStreams(videoStream, oldStream,localCallback);
  240. };
  241. /**
  242. * Creates <tt>JitsiTrack</tt> instance and replaces it with the local audio.
  243. * The method also handles the sdp changes.
  244. * @param stream the new MediaStream received by the browser.
  245. * @param callback - function that will be called after the operation is completed.
  246. */
  247. RTC.prototype.changeLocalAudio = function (stream, callback) {
  248. var oldStream = this.localAudio.getOriginalStream();
  249. var newStream = RTCUtils.createStream(stream);
  250. this.localAudio = this.createLocalStream(newStream, "audio", true);
  251. // Stop the stream to trigger onended event for old stream
  252. oldStream.stop();
  253. this.room.switchStreams(newStream, oldStream, callback, true);
  254. };
  255. RTC.prototype.isVideoMuted = function (jid) {
  256. if (jid === APP.xmpp.myJid()) {
  257. var localVideo = APP.RTC.localVideo;
  258. return (!localVideo || localVideo.isMuted());
  259. } else {
  260. if (!this.remoteStreams[jid] ||
  261. !this.remoteStreams[jid][MediaStreamType.VIDEO_TYPE]) {
  262. return null;
  263. }
  264. return this.remoteStreams[jid][MediaStreamType.VIDEO_TYPE].muted;
  265. }
  266. };
  267. RTC.prototype.setVideoMute = function (mute, callback, options) {
  268. if (!this.localVideo)
  269. return;
  270. if (mute == this.localVideo.isMuted())
  271. {
  272. APP.xmpp.sendVideoInfoPresence(mute);
  273. if (callback)
  274. callback(mute);
  275. }
  276. else
  277. {
  278. this.localVideo.setMute(mute);
  279. this.room.setVideoMute(
  280. mute,
  281. callback,
  282. options);
  283. }
  284. };
  285. RTC.prototype.setDeviceAvailability = function (devices) {
  286. if(!devices)
  287. return;
  288. if(devices.audio === true || devices.audio === false)
  289. this.devices.audio = devices.audio;
  290. if(devices.video === true || devices.video === false)
  291. this.devices.video = devices.video;
  292. this.eventEmitter.emit(RTCEvents.AVAILABLE_DEVICES_CHANGED, this.devices);
  293. };
  294. module.exports = RTC;