Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

RTC.js 12KB

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