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 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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,
  29. trackInfo.resolution,
  30. deviceId,
  31. options.facingMode);
  32. newTracks.push(localTrack);
  33. });
  34. return newTracks;
  35. }
  36. function RTC(conference, options) {
  37. this.conference = conference;
  38. this.localTracks = [];
  39. //FIXME: We should support multiple streams per jid.
  40. this.remoteTracks = {};
  41. this.localAudio = null;
  42. this.localVideo = null;
  43. this.eventEmitter = new EventEmitter();
  44. var self = this;
  45. this.options = options || {};
  46. // Switch audio output device on all remote audio tracks. Local audio tracks
  47. // handle this event by themselves.
  48. if (RTCUtils.isDeviceChangeAvailable('output')) {
  49. RTCUtils.addListener(RTCEvents.AUDIO_OUTPUT_DEVICE_CHANGED,
  50. function (deviceId) {
  51. for (var key in self.remoteTracks) {
  52. if (self.remoteTracks.hasOwnProperty(key)
  53. && self.remoteTracks[key].audio) {
  54. self.remoteTracks[key].audio.setAudioOutput(deviceId);
  55. }
  56. }
  57. });
  58. }
  59. }
  60. /**
  61. * Creates the local MediaStreams.
  62. * @param {Object} [options] optional parameters
  63. * @param {Array} options.devices the devices that will be requested
  64. * @param {string} options.resolution resolution constraints
  65. * @param {bool} options.dontCreateJitsiTrack if <tt>true</tt> objects with the
  66. * following structure {stream: the Media Stream,
  67. * type: "audio" or "video", videoType: "camera" or "desktop"}
  68. * will be returned trough the Promise, otherwise JitsiTrack objects will be
  69. * returned.
  70. * @param {string} options.cameraDeviceId
  71. * @param {string} options.micDeviceId
  72. * @returns {*} Promise object that will receive the new JitsiTracks
  73. */
  74. RTC.obtainAudioAndVideoPermissions = function (options) {
  75. return RTCUtils.obtainAudioAndVideoPermissions(options).then(
  76. function (tracksInfo) {
  77. return createLocalTracks(tracksInfo, options);
  78. });
  79. };
  80. RTC.prototype.onIncommingCall = function(event) {
  81. if(this.options.config.openSctp)
  82. this.dataChannels = new DataChannels(event.peerconnection,
  83. this.eventEmitter);
  84. };
  85. RTC.prototype.selectEndpoint = function (id) {
  86. if(this.dataChannels)
  87. this.dataChannels.sendSelectedEndpointMessage(id);
  88. };
  89. RTC.prototype.pinEndpoint = function (id) {
  90. if(this.dataChannels)
  91. this.dataChannels.sendPinnedEndpointMessage(id);
  92. };
  93. RTC.prototype.addListener = function (type, listener) {
  94. this.eventEmitter.on(type, listener);
  95. };
  96. RTC.prototype.removeListener = function (eventType, listener) {
  97. this.eventEmitter.removeListener(eventType, listener);
  98. };
  99. RTC.addListener = function (eventType, listener) {
  100. RTCUtils.addListener(eventType, listener);
  101. };
  102. RTC.removeListener = function (eventType, listener) {
  103. RTCUtils.removeListener(eventType, listener)
  104. };
  105. RTC.isRTCReady = function () {
  106. return RTCUtils.isRTCReady();
  107. };
  108. RTC.init = function (options) {
  109. this.options = options || {};
  110. return RTCUtils.init(this.options);
  111. };
  112. RTC.getDeviceAvailability = function () {
  113. return RTCUtils.getDeviceAvailability();
  114. };
  115. RTC.prototype.addLocalTrack = function (track) {
  116. if (!track)
  117. throw new Error('track must not be null nor undefined');
  118. this.localTracks.push(track);
  119. track.conference = this.conference;
  120. if (track.isAudioTrack()) {
  121. this.localAudio = track;
  122. } else {
  123. this.localVideo = track;
  124. }
  125. };
  126. /**
  127. * Get local video track.
  128. * @returns {JitsiLocalTrack}
  129. */
  130. RTC.prototype.getLocalVideoTrack = function () {
  131. return this.localVideo;
  132. };
  133. /**
  134. * Gets JitsiRemoteTrack for the passed MediaType associated with given MUC
  135. * nickname (resource part of the JID).
  136. * @param type audio or video.
  137. * @param resource the resource part of the MUC JID
  138. * @returns {JitsiRemoteTrack|null}
  139. */
  140. RTC.prototype.getRemoteTrackByType = function (type, resource) {
  141. if (this.remoteTracks[resource])
  142. return this.remoteTracks[resource][type];
  143. else
  144. return null;
  145. };
  146. /**
  147. * Gets JitsiRemoteTrack for AUDIO MediaType associated with given MUC nickname
  148. * (resource part of the JID).
  149. * @param resource the resource part of the MUC JID
  150. * @returns {JitsiRemoteTrack|null}
  151. */
  152. RTC.prototype.getRemoteAudioTrack = function (resource) {
  153. return this.getRemoteTrackByType(MediaType.AUDIO, resource);
  154. };
  155. /**
  156. * Gets JitsiRemoteTrack for VIDEO MediaType associated with given MUC nickname
  157. * (resource part of the JID).
  158. * @param resource the resource part of the MUC JID
  159. * @returns {JitsiRemoteTrack|null}
  160. */
  161. RTC.prototype.getRemoteVideoTrack = function (resource) {
  162. return this.getRemoteTrackByType(MediaType.VIDEO, resource);
  163. };
  164. /**
  165. * Set mute for all local audio streams attached to the conference.
  166. * @param value the mute value
  167. * @returns {Promise}
  168. */
  169. RTC.prototype.setAudioMute = function (value) {
  170. var mutePromises = [];
  171. for(var i = 0; i < this.localTracks.length; i++) {
  172. var track = this.localTracks[i];
  173. if(track.getType() !== MediaType.AUDIO) {
  174. continue;
  175. }
  176. // this is a Promise
  177. mutePromises.push(value ? track.mute() : track.unmute());
  178. }
  179. // we return a Promise from all Promises so we can wait for their execution
  180. return Promise.all(mutePromises);
  181. };
  182. RTC.prototype.removeLocalTrack = function (track) {
  183. var pos = this.localTracks.indexOf(track);
  184. if (pos === -1) {
  185. return;
  186. }
  187. this.localTracks.splice(pos, 1);
  188. if (track.isAudioTrack()) {
  189. this.localAudio = null;
  190. } else {
  191. this.localVideo = null;
  192. }
  193. };
  194. /**
  195. * Initializes a new JitsiRemoteTrack instance with the data provided by (a)
  196. * ChatRoom to XMPPEvents.REMOTE_TRACK_ADDED.
  197. *
  198. * @param {Object} event the data provided by (a) ChatRoom to
  199. * XMPPEvents.REMOTE_TRACK_ADDED to (a)
  200. */
  201. RTC.prototype.createRemoteTrack = function (event) {
  202. var ownerJid = event.owner;
  203. var remoteTrack = new JitsiRemoteTrack(
  204. this.conference, ownerJid, event.stream, event.track,
  205. event.mediaType, event.videoType, event.ssrc, event.muted);
  206. var resource = Strophe.getResourceFromJid(ownerJid);
  207. var remoteTracks
  208. = this.remoteTracks[resource] || (this.remoteTracks[resource] = {});
  209. var mediaType = remoteTrack.getType();
  210. if (remoteTracks[mediaType]) {
  211. logger.warn("Overwriting remote track!", resource, mediaType);
  212. }
  213. remoteTracks[mediaType] = remoteTrack;
  214. return remoteTrack;
  215. };
  216. /**
  217. * Removes all JitsiRemoteTracks associated with given MUC nickname (resource
  218. * part of the JID). Returns array of removed tracks.
  219. *
  220. * @param {string} resource - The resource part of the MUC JID.
  221. * @returns {JitsiRemoteTrack[]}
  222. */
  223. RTC.prototype.removeRemoteTracks = function (resource) {
  224. var removedTracks = [];
  225. var removedAudioTrack = this.removeRemoteTrack(resource, MediaType.AUDIO);
  226. var removedVideoTrack = this.removeRemoteTrack(resource, MediaType.VIDEO);
  227. removedAudioTrack && removedTracks.push(removedAudioTrack);
  228. removedVideoTrack && removedTracks.push(removedVideoTrack);
  229. delete this.remoteTracks[resource];
  230. return removedTracks;
  231. };
  232. /**
  233. * Removes specified track type associated with given MUC nickname
  234. * (resource part of the JID). Returns removed track if any.
  235. *
  236. * @param {string} resource - The resource part of the MUC JID.
  237. * @param {string} mediaType - Type of track to remove.
  238. * @returns {JitsiRemoteTrack|undefined}
  239. */
  240. RTC.prototype.removeRemoteTrack = function (resource, mediaType) {
  241. var remoteTracksForResource = this.remoteTracks[resource];
  242. if (remoteTracksForResource && remoteTracksForResource[mediaType]) {
  243. var track = remoteTracksForResource[mediaType];
  244. track.dispose();
  245. delete remoteTracksForResource[mediaType];
  246. return track;
  247. }
  248. };
  249. RTC.getPCConstraints = function () {
  250. return RTCUtils.pc_constraints;
  251. };
  252. RTC.attachMediaStream = function (elSelector, stream) {
  253. return RTCUtils.attachMediaStream(elSelector, stream);
  254. };
  255. RTC.getStreamID = function (stream) {
  256. return RTCUtils.getStreamID(stream);
  257. };
  258. /**
  259. * Returns true if retrieving the the list of input devices is supported and
  260. * false if not.
  261. */
  262. RTC.isDeviceListAvailable = function () {
  263. return RTCUtils.isDeviceListAvailable();
  264. };
  265. /**
  266. * Returns true if changing the input (camera / microphone) or output
  267. * (audio) device is supported and false if not.
  268. * @params {string} [deviceType] - type of device to change. Default is
  269. * undefined or 'input', 'output' - for audio output device change.
  270. * @returns {boolean} true if available, false otherwise.
  271. */
  272. RTC.isDeviceChangeAvailable = function (deviceType) {
  273. return RTCUtils.isDeviceChangeAvailable(deviceType);
  274. };
  275. /**
  276. * Returns currently used audio output device id, '' stands for default
  277. * device
  278. * @returns {string}
  279. */
  280. RTC.getAudioOutputDevice = function () {
  281. return RTCUtils.getAudioOutputDevice();
  282. };
  283. /**
  284. * Returns list of available media devices if its obtained, otherwise an
  285. * empty array is returned/
  286. * @returns {Array} list of available media devices.
  287. */
  288. RTC.getCurrentlyAvailableMediaDevices = function () {
  289. return RTCUtils.getCurrentlyAvailableMediaDevices();
  290. };
  291. /**
  292. * Returns event data for device to be reported to stats.
  293. * @returns {MediaDeviceInfo} device.
  294. */
  295. RTC.getEventDataForActiveDevice = function (device) {
  296. return RTCUtils.getEventDataForActiveDevice(device);
  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. /**
  332. * A method to handle stopping of the stream.
  333. * One point to handle the differences in various implementations.
  334. * @param mediaStream MediaStream object to stop.
  335. */
  336. RTC.stopMediaStream = function (mediaStream) {
  337. RTCUtils.stopMediaStream(mediaStream);
  338. };
  339. /**
  340. * Returns whether the desktop sharing is enabled or not.
  341. * @returns {boolean}
  342. */
  343. RTC.isDesktopSharingEnabled = function () {
  344. return RTCUtils.isDesktopSharingEnabled();
  345. };
  346. /**
  347. * Closes all currently opened data channels.
  348. */
  349. RTC.prototype.closeAllDataChannels = function () {
  350. if(this.dataChannels)
  351. this.dataChannels.closeAllChannels();
  352. };
  353. RTC.prototype.dispose = function() {
  354. };
  355. /*
  356. //FIXME Never used, but probably *should* be used for switching
  357. // between camera and screen, but has to be adjusted to work with tracks.
  358. // Current when switching to desktop we can see recv-only being advertised
  359. // because we do remove and add.
  360. //
  361. // Leaving it commented out, in order to not forget about FF specific
  362. // thing
  363. RTC.prototype.switchVideoTracks = function (newStream) {
  364. this.localVideo.stream = newStream;
  365. this.localTracks = [];
  366. //in firefox we have only one stream object
  367. if (this.localAudio.getOriginalStream() != newStream)
  368. this.localTracks.push(this.localAudio);
  369. this.localTracks.push(this.localVideo);
  370. };*/
  371. RTC.prototype.setAudioLevel = function (resource, audioLevel) {
  372. if(!resource)
  373. return;
  374. var audioTrack = this.getRemoteAudioTrack(resource);
  375. if(audioTrack) {
  376. audioTrack.setAudioLevel(audioLevel);
  377. }
  378. };
  379. /**
  380. * Searches in localTracks(session stores ssrc for audio and video) and
  381. * remoteTracks for the ssrc and returns the corresponding resource.
  382. * @param ssrc the ssrc to check.
  383. */
  384. RTC.prototype.getResourceBySSRC = function (ssrc) {
  385. if((this.localVideo && ssrc == this.localVideo.getSSRC())
  386. || (this.localAudio && ssrc == this.localAudio.getSSRC())) {
  387. return this.conference.myUserId();
  388. }
  389. var track = this.getRemoteTrackBySSRC(ssrc);
  390. return track? track.getParticipantId() : null;
  391. };
  392. /**
  393. * Searches in remoteTracks for the ssrc and returns the corresponding track.
  394. * @param ssrc the ssrc to check.
  395. */
  396. RTC.prototype.getRemoteTrackBySSRC = function (ssrc) {
  397. for (var resource in this.remoteTracks) {
  398. var track = this.getRemoteAudioTrack(resource);
  399. if(track && track.getSSRC() == ssrc) {
  400. return track;
  401. }
  402. track = this.getRemoteVideoTrack(resource);
  403. if(track && track.getSSRC() == ssrc) {
  404. return track;
  405. }
  406. }
  407. return null;
  408. };
  409. /**
  410. * Handles remote track mute / unmute events.
  411. * @param type {string} "audio" or "video"
  412. * @param isMuted {boolean} the new mute state
  413. * @param from {string} user id
  414. */
  415. RTC.prototype.handleRemoteTrackMute = function (type, isMuted, from) {
  416. var track = this.getRemoteTrackByType(type, from);
  417. if (track) {
  418. track.setMute(isMuted);
  419. }
  420. }
  421. /**
  422. * Handles remote track video type events
  423. * @param value {string} the new video type
  424. * @param from {string} user id
  425. */
  426. RTC.prototype.handleRemoteTrackVideoTypeChanged = function (value, from) {
  427. var videoTrack = this.getRemoteVideoTrack(from);
  428. if (videoTrack) {
  429. videoTrack._setVideoType(value);
  430. }
  431. }
  432. /**
  433. * Sends message via the datachannels.
  434. * @param to {string} the id of the endpoint that should receive the message.
  435. * If "" the message will be sent to all participants.
  436. * @param payload {object} the payload of the message.
  437. */
  438. RTC.prototype.sendDataChannelMessage = function (to, payload) {
  439. if(this.dataChannels) {
  440. this.dataChannels.sendDataChannelMessage(to, payload);
  441. }
  442. }
  443. module.exports = RTC;