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.

SignalingLayerImpl.js 14KB

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