modified lib-jitsi-meet dev repo
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

SignalingLayerImpl.js 14KB

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