You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SignalingLayerImpl.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /* global __filename */
  2. import { getLogger } from 'jitsi-meet-logger';
  3. import { Strophe } from 'strophe.js';
  4. import * as MediaType from '../../service/RTC/MediaType';
  5. import * as SignalingEvents from '../../service/RTC/SignalingEvents';
  6. import SignalingLayer, { getMediaTypeFromSourceName } from '../../service/RTC/SignalingLayer';
  7. import VideoType from '../../service/RTC/VideoType';
  8. import XMPPEvents from '../../service/xmpp/XMPPEvents';
  9. import FeatureFlags from '../flags/FeatureFlags';
  10. import { filterNodeFromPresenceJSON } from './ChatRoom';
  11. const logger = getLogger(__filename);
  12. export const SOURCE_INFO_PRESENCE_ELEMENT = 'SourceInfo';
  13. /**
  14. * Default XMPP implementation of the {@link SignalingLayer} interface. Obtains
  15. * the data from the MUC presence.
  16. */
  17. export default class SignalingLayerImpl extends SignalingLayer {
  18. /**
  19. * Creates new instance.
  20. */
  21. constructor() {
  22. super();
  23. /**
  24. * A map that stores SSRCs of remote streams. And is used only locally
  25. * We store the mapping when jingle is received, and later is used
  26. * onaddstream webrtc event where we have only the ssrc
  27. * FIXME: This map got filled and never cleaned and can grow during long
  28. * conference
  29. * @type {Map<number, string>} maps SSRC number to jid
  30. */
  31. this.ssrcOwners = new Map();
  32. /**
  33. *
  34. * @type {ChatRoom|null}
  35. */
  36. this.chatRoom = null;
  37. /**
  38. * @type {Map<SourceName, SourceInfo>}
  39. * @private
  40. */
  41. this._localSourceState = { };
  42. /**
  43. * @type {Map<EndpointId, Map<SourceName, SourceInfo>>}
  44. * @private
  45. */
  46. this._remoteSourceState = { };
  47. }
  48. /**
  49. * Adds <SourceInfo> element to the local presence.
  50. *
  51. * @returns {void}
  52. * @private
  53. */
  54. _addLocalSourceInfoToPresence() {
  55. if (this.chatRoom) {
  56. this.chatRoom.addOrReplaceInPresence(
  57. SOURCE_INFO_PRESENCE_ELEMENT,
  58. { value: JSON.stringify(this._localSourceState) });
  59. }
  60. }
  61. /**
  62. * Check is given endpoint has advertised <SourceInfo/> in it's presence which means that the source name signaling
  63. * is used by this endpoint.
  64. *
  65. * @param {EndpointId} endpointId
  66. * @returns {boolean}
  67. */
  68. _doesEndpointSendNewSourceInfo(endpointId) {
  69. const presence = this.chatRoom?.getLastPresence(endpointId);
  70. return Boolean(presence && presence.find(node => node.tagName === SOURCE_INFO_PRESENCE_ELEMENT));
  71. }
  72. /**
  73. * Sets the <tt>ChatRoom</tt> instance used and binds presence listeners.
  74. * @param {ChatRoom} room
  75. */
  76. setChatRoom(room) {
  77. const oldChatRoom = this.chatRoom;
  78. this.chatRoom = room;
  79. if (oldChatRoom) {
  80. oldChatRoom.removePresenceListener(
  81. 'audiomuted', this._audioMuteHandler);
  82. oldChatRoom.removePresenceListener(
  83. 'videomuted', this._videoMuteHandler);
  84. oldChatRoom.removePresenceListener(
  85. 'videoType', this._videoTypeHandler);
  86. if (FeatureFlags.isSourceNameSignalingEnabled()) {
  87. this._sourceInfoHandler
  88. && oldChatRoom.removePresenceListener(
  89. SOURCE_INFO_PRESENCE_ELEMENT, this._sourceInfoHandler);
  90. this._memberLeftHandler
  91. && oldChatRoom.removeEventListener(
  92. XMPPEvents.MUC_MEMBER_LEFT, this._memberLeftHandler);
  93. }
  94. }
  95. if (room) {
  96. if (FeatureFlags.isSourceNameSignalingEnabled()) {
  97. this._bindChatRoomEventHandlers(room);
  98. this._addLocalSourceInfoToPresence();
  99. } else {
  100. // TODO the logic below has been duplicated in _bindChatRoomEventHandlers, clean this up once
  101. // the new impl has been tested well enough
  102. // SignalingEvents
  103. this._audioMuteHandler = (node, from) => {
  104. this.eventEmitter.emit(
  105. SignalingEvents.PEER_MUTED_CHANGED,
  106. from, MediaType.AUDIO, node.value === 'true');
  107. };
  108. room.addPresenceListener('audiomuted', this._audioMuteHandler);
  109. this._videoMuteHandler = (node, from) => {
  110. this.eventEmitter.emit(
  111. SignalingEvents.PEER_MUTED_CHANGED,
  112. from, MediaType.VIDEO, node.value === 'true');
  113. };
  114. room.addPresenceListener('videomuted', this._videoMuteHandler);
  115. this._videoTypeHandler = (node, from) => {
  116. this.eventEmitter.emit(
  117. SignalingEvents.PEER_VIDEO_TYPE_CHANGED,
  118. from, node.value);
  119. };
  120. room.addPresenceListener('videoType', this._videoTypeHandler);
  121. }
  122. }
  123. }
  124. /**
  125. * Binds event listeners to the chat room instance.
  126. * @param {ChatRoom} room
  127. * @private
  128. * @returns {void}
  129. */
  130. _bindChatRoomEventHandlers(room) {
  131. const emitAudioMutedEvent = (endpointId, muted) => {
  132. this.eventEmitter.emit(
  133. SignalingEvents.PEER_MUTED_CHANGED,
  134. endpointId,
  135. MediaType.AUDIO,
  136. muted);
  137. };
  138. const emitVideoMutedEvent = (endpointId, muted) => {
  139. this.eventEmitter.emit(
  140. SignalingEvents.PEER_MUTED_CHANGED,
  141. endpointId,
  142. MediaType.VIDEO,
  143. muted);
  144. };
  145. // SignalingEvents
  146. this._audioMuteHandler = (node, from) => {
  147. if (!this._doesEndpointSendNewSourceInfo(from)) {
  148. emitAudioMutedEvent(from, node.value === 'true');
  149. }
  150. };
  151. room.addPresenceListener('audiomuted', this._audioMuteHandler);
  152. this._videoMuteHandler = (node, from) => {
  153. if (!this._doesEndpointSendNewSourceInfo(from)) {
  154. emitVideoMutedEvent(from, node.value === 'true');
  155. }
  156. };
  157. room.addPresenceListener('videomuted', this._videoMuteHandler);
  158. const emitVideoTypeEvent = (endpointId, videoType) => {
  159. this.eventEmitter.emit(
  160. SignalingEvents.PEER_VIDEO_TYPE_CHANGED,
  161. endpointId, videoType);
  162. };
  163. this._videoTypeHandler = (node, from) => {
  164. if (!this._doesEndpointSendNewSourceInfo(from)) {
  165. emitVideoTypeEvent(from, node.value);
  166. }
  167. };
  168. room.addPresenceListener('videoType', this._videoTypeHandler);
  169. this._sourceInfoHandler = (node, mucNick) => {
  170. const endpointId = mucNick;
  171. const { value } = node;
  172. const sourceInfoJSON = JSON.parse(value);
  173. const emitEventsFromHere = this._doesEndpointSendNewSourceInfo(endpointId);
  174. const endpointSourceState
  175. = this._remoteSourceState[endpointId] || (this._remoteSourceState[endpointId] = {});
  176. for (const sourceName of Object.keys(sourceInfoJSON)) {
  177. const mediaType = getMediaTypeFromSourceName(sourceName);
  178. const newMutedState = Boolean(sourceInfoJSON[sourceName].muted);
  179. const oldSourceState = endpointSourceState[sourceName]
  180. || (endpointSourceState[sourceName] = { sourceName });
  181. if (oldSourceState.muted !== newMutedState) {
  182. oldSourceState.muted = newMutedState;
  183. if (emitEventsFromHere && mediaType === MediaType.AUDIO) {
  184. emitAudioMutedEvent(endpointId, newMutedState);
  185. } else {
  186. emitVideoMutedEvent(endpointId, newMutedState);
  187. }
  188. }
  189. const newVideoType = sourceInfoJSON[sourceName].videoType;
  190. if (oldSourceState.videoType !== newVideoType) {
  191. oldSourceState.videoType = newVideoType;
  192. emitEventsFromHere && emitVideoTypeEvent(endpointId, newVideoType);
  193. }
  194. }
  195. // Cleanup removed source names
  196. const newSourceNames = Object.keys(sourceInfoJSON);
  197. for (const sourceName of Object.keys(endpointSourceState)) {
  198. if (newSourceNames.indexOf(sourceName) === -1) {
  199. delete endpointSourceState[sourceName];
  200. }
  201. }
  202. };
  203. room.addPresenceListener('SourceInfo', this._sourceInfoHandler);
  204. // Cleanup when participant leaves
  205. this._memberLeftHandler = jid => {
  206. const endpointId = Strophe.getResourceFromJid(jid);
  207. delete this._remoteSourceState[endpointId];
  208. };
  209. room.addEventListener(XMPPEvents.MUC_MEMBER_LEFT, this._memberLeftHandler);
  210. }
  211. /**
  212. * Finds the first source of given media type for the given endpoint.
  213. * @param endpointId
  214. * @param mediaType
  215. * @returns {SourceInfo|null}
  216. * @private
  217. */
  218. _findEndpointSourceInfoForMediaType(endpointId, mediaType) {
  219. const remoteSourceState = this._remoteSourceState[endpointId];
  220. if (!remoteSourceState) {
  221. return null;
  222. }
  223. for (const sourceInfo of Object.values(remoteSourceState)) {
  224. const _mediaType = getMediaTypeFromSourceName(sourceInfo.sourceName);
  225. if (_mediaType === mediaType) {
  226. return sourceInfo;
  227. }
  228. }
  229. return null;
  230. }
  231. /**
  232. * @inheritDoc
  233. */
  234. getPeerMediaInfo(owner, mediaType) {
  235. const legacyGetPeerMediaInfo = () => {
  236. if (this.chatRoom) {
  237. return this.chatRoom.getMediaPresenceInfo(owner, mediaType);
  238. }
  239. logger.error('Requested peer media info, before room was set');
  240. };
  241. if (FeatureFlags.isSourceNameSignalingEnabled()) {
  242. const lastPresence = this.chatRoom.getLastPresence(owner);
  243. if (!lastPresence) {
  244. throw new Error(`getPeerMediaInfo - no presence stored for: ${owner}`);
  245. }
  246. if (!this._doesEndpointSendNewSourceInfo(owner)) {
  247. return legacyGetPeerMediaInfo();
  248. }
  249. /**
  250. * @type {PeerMediaInfo}
  251. */
  252. const mediaInfo = {};
  253. const endpointMediaSource = this._findEndpointSourceInfoForMediaType(owner, mediaType);
  254. // The defaults are provided only, because getPeerMediaInfo is a legacy method. This will be eventually
  255. // changed into a getSourceInfo method which returns undefined if there's no source. Also there will be
  256. // no mediaType argument there.
  257. if (mediaType === MediaType.AUDIO) {
  258. mediaInfo.muted = endpointMediaSource ? endpointMediaSource.muted : true;
  259. } else if (mediaType === MediaType.VIDEO) {
  260. mediaInfo.muted = endpointMediaSource ? endpointMediaSource.muted : true;
  261. mediaInfo.videoType = endpointMediaSource ? endpointMediaSource.videoType : undefined;
  262. const codecTypeNode = filterNodeFromPresenceJSON(lastPresence, 'jitsi_participant_codecType');
  263. if (codecTypeNode.length > 0) {
  264. mediaInfo.codecType = codecTypeNode[0].value;
  265. }
  266. } else {
  267. throw new Error(`Unsupported media type: ${mediaType}`);
  268. }
  269. return mediaInfo;
  270. }
  271. return legacyGetPeerMediaInfo();
  272. }
  273. /**
  274. * @inheritDoc
  275. */
  276. getPeerSourceInfo(owner, sourceName) {
  277. return this._remoteSourceState[owner] ? this._remoteSourceState[owner][sourceName] : undefined;
  278. }
  279. /**
  280. * @inheritDoc
  281. */
  282. getSSRCOwner(ssrc) {
  283. return this.ssrcOwners.get(ssrc);
  284. }
  285. /**
  286. * Set an SSRC owner.
  287. * @param {number} ssrc an SSRC to be owned
  288. * @param {string} endpointId owner's ID (MUC nickname)
  289. * @throws TypeError if <tt>ssrc</tt> is not a number
  290. */
  291. setSSRCOwner(ssrc, endpointId) {
  292. if (typeof ssrc !== 'number') {
  293. throw new TypeError(`SSRC(${ssrc}) must be a number`);
  294. }
  295. // Now signaling layer instance is shared between different JingleSessionPC instances, so although very unlikely
  296. // an SSRC conflict could potentially occur. Log a message to make debugging easier.
  297. if (this.ssrcOwners.has(ssrc)) {
  298. logger.error(`SSRC owner re-assigned from ${this.ssrcOwners.get(ssrc)} to ${endpointId}`);
  299. }
  300. this.ssrcOwners.set(ssrc, endpointId);
  301. }
  302. /**
  303. * Adjusts muted status of given track.
  304. *
  305. * @param {SourceName} sourceName - the name of the track's source.
  306. * @param {boolean} muted - the new muted status.
  307. * @returns {boolean}
  308. */
  309. setTrackMuteStatus(sourceName, muted) {
  310. if (!this._localSourceState[sourceName]) {
  311. this._localSourceState[sourceName] = {};
  312. }
  313. this._localSourceState[sourceName].muted = muted;
  314. if (this.chatRoom) {
  315. // FIXME This only adjusts the presence, but doesn't actually send it. Here we temporarily rely on
  316. // the legacy signaling part to send the presence. Remember to add "send presence" here when the legacy
  317. // signaling is removed.
  318. this._addLocalSourceInfoToPresence();
  319. }
  320. }
  321. /**
  322. * Sets track's video type.
  323. * @param {SourceName} sourceName - the track's source name.
  324. * @param {VideoType} videoType - the new video type.
  325. */
  326. setTrackVideoType(sourceName, videoType) {
  327. if (!this._localSourceState[sourceName]) {
  328. this._localSourceState[sourceName] = {};
  329. }
  330. if (this._localSourceState[sourceName].videoType !== videoType) {
  331. // Include only if not a camera (default)
  332. this._localSourceState[sourceName].videoType = videoType === VideoType.CAMERA ? undefined : videoType;
  333. // NOTE this doesn't send the actual presence, because is called from the same place where the legacy video
  334. // type is emitted which does the actual sending. A send presence statement needs to be added when
  335. // the legacy part is removed.
  336. this._addLocalSourceInfoToPresence();
  337. }
  338. }
  339. }