Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

RTC.js 14KB

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