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

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