Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

RTC.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. var remoteTracks = this.remoteTracks[resource];
  255. if(remoteTracks) {
  256. remoteTracks['audio'] && remoteTracks['audio'].dispose();
  257. remoteTracks['video'] && remoteTracks['video'].dispose();
  258. delete this.remoteTracks[resource];
  259. }
  260. };
  261. RTC.getPCConstraints = function () {
  262. return RTCUtils.pc_constraints;
  263. };
  264. RTC.attachMediaStream = function (elSelector, stream) {
  265. return RTCUtils.attachMediaStream(elSelector, stream);
  266. };
  267. RTC.getStreamID = function (stream) {
  268. return RTCUtils.getStreamID(stream);
  269. };
  270. RTC.getVideoSrc = function (element) {
  271. return RTCUtils.getVideoSrc(element);
  272. };
  273. /**
  274. * Returns true if retrieving the the list of input devices is supported and
  275. * false if not.
  276. */
  277. RTC.isDeviceListAvailable = function () {
  278. return RTCUtils.isDeviceListAvailable();
  279. };
  280. /**
  281. * Returns true if changing the input (camera / microphone) or output
  282. * (audio) device is supported and false if not.
  283. * @params {string} [deviceType] - type of device to change. Default is
  284. * undefined or 'input', 'output' - for audio output device change.
  285. * @returns {boolean} true if available, false otherwise.
  286. */
  287. RTC.isDeviceChangeAvailable = function (deviceType) {
  288. return RTCUtils.isDeviceChangeAvailable(deviceType);
  289. };
  290. /**
  291. * Returns currently used audio output device id, '' stands for default
  292. * device
  293. * @returns {string}
  294. */
  295. RTC.getAudioOutputDevice = function () {
  296. return RTCUtils.getAudioOutputDevice();
  297. };
  298. /**
  299. * Sets current audio output device.
  300. * @param {string} deviceId - id of 'audiooutput' device from
  301. * navigator.mediaDevices.enumerateDevices()
  302. * @returns {Promise} - resolves when audio output is changed, is rejected
  303. * otherwise
  304. */
  305. RTC.setAudioOutputDevice = function (deviceId) {
  306. return RTCUtils.setAudioOutputDevice(deviceId);
  307. };
  308. /**
  309. * Returns <tt>true<tt/> if given WebRTC MediaStream is considered a valid
  310. * "user" stream which means that it's not a "receive only" stream nor a "mixed"
  311. * JVB stream.
  312. *
  313. * Clients that implement Unified Plan, such as Firefox use recvonly
  314. * "streams/channels/tracks" for receiving remote stream/tracks, as opposed to
  315. * Plan B where there are only 3 channels: audio, video and data.
  316. *
  317. * @param stream WebRTC MediaStream instance
  318. * @returns {boolean}
  319. */
  320. RTC.isUserStream = function (stream) {
  321. var streamId = RTCUtils.getStreamID(stream);
  322. return streamId && streamId !== "mixedmslabel" && streamId !== "default";
  323. };
  324. /**
  325. * Allows to receive list of available cameras/microphones.
  326. * @param {function} callback would receive array of devices as an argument
  327. */
  328. RTC.enumerateDevices = function (callback) {
  329. RTCUtils.enumerateDevices(callback);
  330. };
  331. RTC.setVideoSrc = function (element, src) {
  332. RTCUtils.setVideoSrc(element, src);
  333. };
  334. /**
  335. * A method to handle stopping of the stream.
  336. * One point to handle the differences in various implementations.
  337. * @param mediaStream MediaStream object to stop.
  338. */
  339. RTC.stopMediaStream = function (mediaStream) {
  340. RTCUtils.stopMediaStream(mediaStream);
  341. };
  342. /**
  343. * Returns whether the desktop sharing is enabled or not.
  344. * @returns {boolean}
  345. */
  346. RTC.isDesktopSharingEnabled = function () {
  347. return RTCUtils.isDesktopSharingEnabled();
  348. };
  349. /**
  350. * Closes all currently opened data channels.
  351. */
  352. RTC.prototype.closeAllDataChannels = function () {
  353. this.dataChannels.closeAllChannels();
  354. };
  355. RTC.prototype.dispose = function() {
  356. };
  357. /*
  358. //FIXME Never used, but probably *should* be used for switching
  359. // between camera and screen, but has to be adjusted to work with tracks.
  360. // Current when switching to desktop we can see recv-only being advertised
  361. // because we do remove and add.
  362. //
  363. // Leaving it commented out, in order to not forget about FF specific
  364. // thing
  365. RTC.prototype.switchVideoTracks = function (newStream) {
  366. this.localVideo.stream = newStream;
  367. this.localTracks = [];
  368. //in firefox we have only one stream object
  369. if (this.localAudio.getOriginalStream() != newStream)
  370. this.localTracks.push(this.localAudio);
  371. this.localTracks.push(this.localVideo);
  372. };*/
  373. RTC.prototype.setAudioLevel = function (resource, audioLevel) {
  374. if(!resource)
  375. return;
  376. var audioTrack = this.getRemoteAudioTrack(resource);
  377. if(audioTrack) {
  378. audioTrack.setAudioLevel(audioLevel);
  379. }
  380. };
  381. /**
  382. * Searches in localTracks(session stores ssrc for audio and video) and
  383. * remoteTracks for the ssrc and returns the corresponding resource.
  384. * @param ssrc the ssrc to check.
  385. */
  386. RTC.prototype.getResourceBySSRC = function (ssrc) {
  387. if((this.localVideo && ssrc == this.localVideo.getSSRC())
  388. || (this.localAudio && ssrc == this.localAudio.getSSRC())) {
  389. return Strophe.getResourceFromJid(this.room.myroomjid);
  390. }
  391. var self = this;
  392. var resultResource = null;
  393. Object.keys(this.remoteTracks).some(function (resource) {
  394. var audioTrack = self.getRemoteAudioTrack(resource);
  395. var videoTrack = self.getRemoteVideoTrack(resource);
  396. if((audioTrack && audioTrack.getSSRC() == ssrc) ||
  397. (videoTrack && videoTrack.getSSRC() == ssrc)) {
  398. resultResource = resource;
  399. return true;
  400. }
  401. });
  402. return resultResource;
  403. };
  404. module.exports = RTC;