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 14KB

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