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.js 13KB

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