Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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