Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /* global __filename, APP, module */
  2. var logger = require("jitsi-meet-logger").getLogger(__filename);
  3. var EventEmitter = require("events");
  4. var RTCBrowserType = require("./RTCBrowserType");
  5. var RTCEvents = require("../../service/RTC/RTCEvents.js");
  6. var RTCUtils = require("./RTCUtils.js");
  7. var JitsiTrack = require("./JitsiTrack");
  8. var JitsiLocalTrack = require("./JitsiLocalTrack.js");
  9. var DataChannels = require("./DataChannels");
  10. var JitsiRemoteTrack = require("./JitsiRemoteTrack.js");
  11. var MediaType = require("../../service/RTC/MediaType");
  12. var VideoType = require("../../service/RTC/VideoType");
  13. function createLocalTracks(tracksInfo, options) {
  14. var newTracks = [];
  15. var deviceId = null;
  16. tracksInfo.forEach(function(trackInfo){
  17. if (trackInfo.type === MediaType.AUDIO) {
  18. deviceId = options.micDeviceId;
  19. } else if (trackInfo.videoType === VideoType.CAMERA){
  20. deviceId = options.cameraDeviceId;
  21. }
  22. var localTrack = new JitsiLocalTrack(trackInfo.stream,
  23. trackInfo.videoType, trackInfo.resolution, deviceId);
  24. newTracks.push(localTrack);
  25. });
  26. return newTracks;
  27. }
  28. function RTC(room, options) {
  29. this.room = room;
  30. this.localTracks = [];
  31. //FIXME: We should support multiple streams per jid.
  32. this.remoteTracks = {};
  33. this.localAudio = null;
  34. this.localVideo = null;
  35. this.eventEmitter = new EventEmitter();
  36. var self = this;
  37. this.options = options || {};
  38. room.addPresenceListener("videomuted", function (values, from) {
  39. var videoTrack = self.getRemoteVideoTrack(from);
  40. // If there is no video track, but we receive it is muted,
  41. // we need to create a dummy track which we will mute, so we can
  42. // notify interested about the muting
  43. if (!videoTrack) {
  44. videoTrack = self.createRemoteTrack(
  45. {
  46. peerjid: room.roomjid + "/" + from,
  47. videoType: VideoType.CAMERA,
  48. jitsiTrackType: MediaType.VIDEO
  49. },
  50. null, null);
  51. self.eventEmitter
  52. .emit(RTCEvents.FAKE_VIDEO_TRACK_CREATED, videoTrack);
  53. }
  54. videoTrack.setMute(values.value == "true");
  55. });
  56. room.addPresenceListener("audiomuted", function (values, from) {
  57. var audioTrack = self.getRemoteAudioTrack(from);
  58. if (audioTrack) {
  59. audioTrack.setMute(values.value == "true");
  60. }
  61. });
  62. room.addPresenceListener("videoType", function(data, from) {
  63. var videoTrack = self.getRemoteVideoTrack(from);
  64. if (videoTrack) {
  65. videoTrack._setVideoType(data.value);
  66. }
  67. });
  68. }
  69. /**
  70. * Creates the local MediaStreams.
  71. * @param {Object} [options] optional parameters
  72. * @param {Array} options.devices the devices that will be requested
  73. * @param {string} options.resolution resolution constraints
  74. * @param {bool} options.dontCreateJitsiTrack if <tt>true</tt> objects with the
  75. * following structure {stream: the Media Stream,
  76. * type: "audio" or "video", videoType: "camera" or "desktop"}
  77. * will be returned trough the Promise, otherwise JitsiTrack objects will be
  78. * returned.
  79. * @param {string} options.cameraDeviceId
  80. * @param {string} options.micDeviceId
  81. * @returns {*} Promise object that will receive the new JitsiTracks
  82. */
  83. RTC.obtainAudioAndVideoPermissions = function (options) {
  84. return RTCUtils.obtainAudioAndVideoPermissions(options).then(
  85. function (tracksInfo) {
  86. return createLocalTracks(tracksInfo, options);
  87. });
  88. };
  89. RTC.prototype.onIncommingCall = function(event) {
  90. if(this.options.config.openSctp)
  91. this.dataChannels = new DataChannels(event.peerconnection,
  92. this.eventEmitter);
  93. for(var i = 0; i < this.localTracks.length; i++)
  94. if(this.localTracks[i])
  95. {
  96. var ssrcInfo = null;
  97. if(this.localTracks[i].isMuted() &&
  98. this.localTracks[i].getType() === MediaType.VIDEO) {
  99. /**
  100. * Handles issues when the stream is added before the peerconnection is created.
  101. * The peerconnection is created when second participant enters the call. In
  102. * that use case the track doesn't have information about it's ssrcs and no
  103. * jingle packets are sent. That can cause inconsistant behavior later.
  104. *
  105. * For example:
  106. * If we mute the stream and than second participant enter it's remote SDP won't
  107. * include that track. On unmute we are not sending any jingle packets which
  108. * will brake the unmute.
  109. *
  110. * In order to solve issues like the above one here we have to generate the ssrc
  111. * information for the track .
  112. */
  113. this.localTracks[i]._setSSRC(
  114. this.room.generateNewStreamSSRCInfo());
  115. ssrcInfo = {
  116. mtype: this.localTracks[i].getType(),
  117. type: "addMuted",
  118. ssrc: this.localTracks[i].ssrc,
  119. msid: this.localTracks[i].initialMSID
  120. }
  121. }
  122. this.room.addStream(this.localTracks[i].getOriginalStream(),
  123. function () {}, ssrcInfo, true);
  124. }
  125. };
  126. RTC.prototype.selectedEndpoint = function (id) {
  127. if(this.dataChannels)
  128. this.dataChannels.handleSelectedEndpointEvent(id);
  129. };
  130. RTC.prototype.pinEndpoint = function (id) {
  131. if(this.dataChannels)
  132. this.dataChannels.handlePinnedEndpointEvent(id);
  133. };
  134. RTC.prototype.addListener = function (type, listener) {
  135. this.eventEmitter.on(type, listener);
  136. };
  137. RTC.prototype.removeListener = function (eventType, listener) {
  138. this.eventEmitter.removeListener(eventType, listener);
  139. };
  140. RTC.addListener = function (eventType, listener) {
  141. RTCUtils.addListener(eventType, listener);
  142. };
  143. RTC.removeListener = function (eventType, listener) {
  144. RTCUtils.removeListener(eventType, listener)
  145. };
  146. RTC.isRTCReady = function () {
  147. return RTCUtils.isRTCReady();
  148. };
  149. RTC.init = function (options) {
  150. this.options = options || {};
  151. return RTCUtils.init(this.options);
  152. };
  153. RTC.getDeviceAvailability = function () {
  154. return RTCUtils.getDeviceAvailability();
  155. };
  156. RTC.prototype.addLocalTrack = function (track) {
  157. this.localTracks.push(track);
  158. track._setRTC(this);
  159. if (track.isAudioTrack()) {
  160. this.localAudio = track;
  161. } else {
  162. this.localVideo = track;
  163. }
  164. };
  165. /**
  166. * Get local video track.
  167. * @returns {JitsiLocalTrack}
  168. */
  169. RTC.prototype.getLocalVideoTrack = function () {
  170. return this.localVideo;
  171. };
  172. /**
  173. * Gets JitsiRemoteTrack for AUDIO MediaType associated with given MUC nickname
  174. * (resource part of the JID).
  175. * @param resource the resource part of the MUC JID
  176. * @returns {JitsiRemoteTrack|null}
  177. */
  178. RTC.prototype.getRemoteAudioTrack = function (resource) {
  179. if (this.remoteTracks[resource])
  180. return this.remoteTracks[resource][MediaType.AUDIO];
  181. else
  182. return null;
  183. };
  184. /**
  185. * Gets JitsiRemoteTrack for VIDEO MediaType associated with given MUC nickname
  186. * (resource part of the JID).
  187. * @param resource the resource part of the MUC JID
  188. * @returns {JitsiRemoteTrack|null}
  189. */
  190. RTC.prototype.getRemoteVideoTrack = function (resource) {
  191. if (this.remoteTracks[resource])
  192. return this.remoteTracks[resource][MediaType.VIDEO];
  193. else
  194. return null;
  195. };
  196. /**
  197. * Set mute for all local audio streams attached to the conference.
  198. * @param value the mute value
  199. * @returns {Promise}
  200. */
  201. RTC.prototype.setAudioMute = function (value) {
  202. var mutePromises = [];
  203. for(var i = 0; i < this.localTracks.length; i++) {
  204. var track = this.localTracks[i];
  205. if(track.getType() !== MediaType.AUDIO) {
  206. continue;
  207. }
  208. // this is a Promise
  209. mutePromises.push(value ? track.mute() : track.unmute());
  210. }
  211. // we return a Promise from all Promises so we can wait for their execution
  212. return Promise.all(mutePromises);
  213. };
  214. RTC.prototype.removeLocalTrack = function (track) {
  215. var pos = this.localTracks.indexOf(track);
  216. if (pos === -1) {
  217. return;
  218. }
  219. this.localTracks.splice(pos, 1);
  220. if (track.isAudioTrack()) {
  221. this.localAudio = null;
  222. } else {
  223. this.localVideo = null;
  224. }
  225. };
  226. RTC.prototype.createRemoteTrack = function (data, sid, thessrc) {
  227. var remoteTrack = new JitsiRemoteTrack(this, data, sid, thessrc);
  228. if(!data.peerjid)
  229. return;
  230. var resource = Strophe.getResourceFromJid(data.peerjid);
  231. if(!this.remoteTracks[resource]) {
  232. this.remoteTracks[resource] = {};
  233. }
  234. var mediaType = remoteTrack.getType();
  235. if (this.remoteTracks[resource][mediaType]) {
  236. logger.warn(
  237. "Overwriting remote track !", resource, mediaType);
  238. }
  239. this.remoteTracks[resource][mediaType] = remoteTrack;
  240. return remoteTrack;
  241. };
  242. /**
  243. * Removes all JitsiRemoteTracks associated with given MUC nickname (resource
  244. * part of the JID).
  245. * @param resource the resource part of the MUC JID
  246. * @returns {JitsiRemoteTrack|null}
  247. */
  248. RTC.prototype.removeRemoteTracks = function (resource) {
  249. if(this.remoteTracks[resource]) {
  250. delete this.remoteTracks[resource];
  251. }
  252. };
  253. RTC.getPCConstraints = function () {
  254. return RTCUtils.pc_constraints;
  255. };
  256. RTC.attachMediaStream = function (elSelector, stream) {
  257. return RTCUtils.attachMediaStream(elSelector, stream);
  258. };
  259. RTC.getStreamID = function (stream) {
  260. return RTCUtils.getStreamID(stream);
  261. };
  262. RTC.getVideoSrc = function (element) {
  263. return RTCUtils.getVideoSrc(element);
  264. };
  265. /**
  266. * Returns true if retrieving the the list of input devices is supported and
  267. * false if not.
  268. */
  269. RTC.isDeviceListAvailable = function () {
  270. return RTCUtils.isDeviceListAvailable();
  271. };
  272. /**
  273. * Returns true if changing the camera / microphone device is supported and
  274. * false if not.
  275. */
  276. RTC.isDeviceChangeAvailable = function () {
  277. return RTCUtils.isDeviceChangeAvailable();
  278. };
  279. /**
  280. * Allows to receive list of available cameras/microphones.
  281. * @param {function} callback would receive array of devices as an argument
  282. */
  283. RTC.enumerateDevices = function (callback) {
  284. RTCUtils.enumerateDevices(callback);
  285. };
  286. RTC.setVideoSrc = function (element, src) {
  287. RTCUtils.setVideoSrc(element, src);
  288. };
  289. /**
  290. * A method to handle stopping of the stream.
  291. * One point to handle the differences in various implementations.
  292. * @param mediaStream MediaStream object to stop.
  293. */
  294. RTC.stopMediaStream = function (mediaStream) {
  295. RTCUtils.stopMediaStream(mediaStream);
  296. };
  297. /**
  298. * Returns whether the desktop sharing is enabled or not.
  299. * @returns {boolean}
  300. */
  301. RTC.isDesktopSharingEnabled = function () {
  302. return RTCUtils.isDesktopSharingEnabled();
  303. };
  304. RTC.prototype.dispose = function() {
  305. };
  306. RTC.prototype.switchVideoTracks = function (newStream) {
  307. this.localVideo.stream = newStream;
  308. this.localTracks = [];
  309. //in firefox we have only one stream object
  310. if (this.localAudio.getOriginalStream() != newStream)
  311. this.localTracks.push(this.localAudio);
  312. this.localTracks.push(this.localVideo);
  313. };
  314. RTC.prototype.setAudioLevel = function (resource, audioLevel) {
  315. if(!resource)
  316. return;
  317. var audioTrack = this.getRemoteAudioTrack(resource);
  318. if(audioTrack) {
  319. audioTrack.setAudioLevel(audioLevel);
  320. }
  321. };
  322. /**
  323. * Searches in localTracks(session stores ssrc for audio and video) and
  324. * remoteTracks for the ssrc and returns the corresponding resource.
  325. * @param ssrc the ssrc to check.
  326. */
  327. RTC.prototype.getResourceBySSRC = function (ssrc) {
  328. if((this.localVideo && ssrc == this.localVideo.getSSRC())
  329. || (this.localAudio && ssrc == this.localAudio.getSSRC())) {
  330. return Strophe.getResourceFromJid(this.room.myroomjid);
  331. }
  332. var self = this;
  333. var resultResource = null;
  334. Object.keys(this.remoteTracks).forEach(function (resource) {
  335. var audioTrack = self.getRemoteAudioTrack(resource);
  336. var videoTrack = self.getRemoteVideoTrack(resource);
  337. if((audioTrack && audioTrack.getSSRC() == ssrc) ||
  338. (videoTrack && videoTrack.getSSRC() == ssrc)) {
  339. resultResource = resource;
  340. }
  341. });
  342. return resultResource;
  343. };
  344. module.exports = RTC;