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.spec.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. import { MediaType } from '../../service/RTC/MediaType';
  2. import * as SignalingEvents from '../../service/RTC/SignalingEvents';
  3. import { getSourceNameForJitsiTrack } from '../../service/RTC/SignalingLayer';
  4. import { VideoType } from '../../service/RTC/VideoType';
  5. import { XMPPEvents } from '../../service/xmpp/XMPPEvents';
  6. import FeatureFlags from '../flags/FeatureFlags';
  7. import Listenable from '../util/Listenable';
  8. import SignalingLayerImpl, { SOURCE_INFO_PRESENCE_ELEMENT } from './SignalingLayerImpl';
  9. const INITIAL_SOURCE_INFO = { value: JSON.stringify({}) };
  10. // eslint-disable-next-line require-jsdoc
  11. function createMockChatRoom() {
  12. const chatRoom = {
  13. ...new Listenable(),
  14. ...jasmine.createSpyObj('', [
  15. 'addOrReplaceInPresence',
  16. 'setAudioMute',
  17. 'setVideoMute'
  18. ])
  19. };
  20. const listeners = {};
  21. // Stores presence listeners
  22. chatRoom.addPresenceListener = (tagName, l) => {
  23. listeners[tagName] || (listeners[tagName] = []);
  24. listeners[tagName].push(l);
  25. };
  26. // Notify presence listeners
  27. chatRoom.emitPresenceListener = (node, mucNick) => {
  28. const nodeListeners = listeners[node.tagName];
  29. if (nodeListeners) {
  30. for (const l of nodeListeners) {
  31. l(node, mucNick);
  32. }
  33. }
  34. };
  35. // Fakes 'SourceInfo' in the presence by adjusting getLastPresence return value and emitting a presence event.
  36. chatRoom.mockSourceInfoPresence = (endpointId, sourceInfo) => {
  37. chatRoom.getLastPresence = () => [ {
  38. tagName: SOURCE_INFO_PRESENCE_ELEMENT,
  39. value: JSON.stringify(sourceInfo)
  40. } ];
  41. chatRoom.emitPresenceListener({
  42. tagName: SOURCE_INFO_PRESENCE_ELEMENT,
  43. value: JSON.stringify(sourceInfo)
  44. }, endpointId);
  45. };
  46. chatRoom.emitParticipantLeft = endpointId => {
  47. // Only the resource part (MUC nick) is relevant
  48. chatRoom.eventEmitter.emit(XMPPEvents.MUC_MEMBER_LEFT, `room@server.com/${endpointId}`);
  49. };
  50. return chatRoom;
  51. }
  52. describe('SignalingLayerImpl', () => {
  53. describe('setTrackMuteStatus advertises the track muted status in the chat room', () => {
  54. describe('with source name signaling enabled', () => {
  55. const endpointId = 'abcdef12';
  56. let signalingLayer;
  57. let chatRoom;
  58. beforeEach(() => {
  59. FeatureFlags.init({ sourceNameSignaling: true });
  60. signalingLayer = new SignalingLayerImpl();
  61. chatRoom = createMockChatRoom();
  62. signalingLayer.setChatRoom(chatRoom);
  63. // No tracks yet
  64. expect(chatRoom.addOrReplaceInPresence)
  65. .toHaveBeenCalledWith(
  66. SOURCE_INFO_PRESENCE_ELEMENT,
  67. INITIAL_SOURCE_INFO);
  68. });
  69. it('for audio track', () => {
  70. const audioSourceName = getSourceNameForJitsiTrack(endpointId, MediaType.AUDIO, 0);
  71. // Audio track: muted
  72. signalingLayer.setTrackMuteStatus(audioSourceName, true);
  73. expect(chatRoom.addOrReplaceInPresence)
  74. .toHaveBeenCalledWith(
  75. SOURCE_INFO_PRESENCE_ELEMENT,
  76. { value: `{"${audioSourceName}":{"muted":true}}` });
  77. // Audio track: unmuted
  78. signalingLayer.setTrackMuteStatus(audioSourceName, false);
  79. expect(chatRoom.addOrReplaceInPresence)
  80. .toHaveBeenCalledWith(
  81. SOURCE_INFO_PRESENCE_ELEMENT,
  82. { value: `{"${audioSourceName}":{"muted":false}}` });
  83. });
  84. it('for video track', () => {
  85. const videoSourceName = getSourceNameForJitsiTrack(endpointId, MediaType.VIDEO, 0);
  86. // Video track: muted
  87. signalingLayer.setTrackMuteStatus(videoSourceName, true);
  88. expect(chatRoom.addOrReplaceInPresence)
  89. .toHaveBeenCalledWith(
  90. SOURCE_INFO_PRESENCE_ELEMENT,
  91. { value: `{"${videoSourceName}":{"muted":true}}` });
  92. // Video track: unmuted
  93. signalingLayer.setTrackMuteStatus(videoSourceName, false);
  94. expect(chatRoom.addOrReplaceInPresence)
  95. .toHaveBeenCalledWith(
  96. SOURCE_INFO_PRESENCE_ELEMENT,
  97. { value: `{"${videoSourceName}":{"muted":false}}` });
  98. });
  99. });
  100. });
  101. describe('setTrackVideoType', () => {
  102. const endpointId = 'abcdef12';
  103. let signalingLayer;
  104. let chatRoom = createMockChatRoom();
  105. beforeEach(() => {
  106. FeatureFlags.init({ sourceNameSignaling: true });
  107. signalingLayer = new SignalingLayerImpl();
  108. chatRoom = createMockChatRoom();
  109. signalingLayer.setChatRoom(chatRoom);
  110. // Initial value is set in signalingLayer.setChatRoom
  111. expect(chatRoom.addOrReplaceInPresence)
  112. .toHaveBeenCalledWith(
  113. SOURCE_INFO_PRESENCE_ELEMENT,
  114. INITIAL_SOURCE_INFO);
  115. });
  116. it('sends video type in chat room presence', () => {
  117. const videoSourceName = getSourceNameForJitsiTrack(endpointId, MediaType.VIDEO, 0);
  118. signalingLayer.setTrackVideoType(videoSourceName, VideoType.CAMERA);
  119. expect(chatRoom.addOrReplaceInPresence)
  120. .toHaveBeenCalledWith(
  121. SOURCE_INFO_PRESENCE_ELEMENT,
  122. { value: '{"abcdef12-v0":{}}' });
  123. signalingLayer.setTrackVideoType(videoSourceName, VideoType.DESKTOP);
  124. expect(chatRoom.addOrReplaceInPresence)
  125. .toHaveBeenCalledWith(
  126. SOURCE_INFO_PRESENCE_ELEMENT,
  127. { value: '{"abcdef12-v0":{"videoType":"desktop"}}' });
  128. signalingLayer.setTrackVideoType(videoSourceName, VideoType.CAMERA);
  129. expect(chatRoom.addOrReplaceInPresence)
  130. .toHaveBeenCalledWith(
  131. SOURCE_INFO_PRESENCE_ELEMENT,
  132. { value: '{"abcdef12-v0":{}}' });
  133. });
  134. });
  135. describe('should emit muted/video type events based on presence', () => {
  136. describe('with: sourceNameSignaling: true', () => {
  137. let signalingLayer;
  138. let chatRoom = createMockChatRoom();
  139. beforeEach(() => {
  140. FeatureFlags.init({ sourceNameSignaling: true });
  141. signalingLayer = new SignalingLayerImpl();
  142. chatRoom = createMockChatRoom();
  143. signalingLayer.setChatRoom(chatRoom);
  144. });
  145. it('from a legacy user (no SourceInfo)', () => {
  146. const emitterSpy = spyOn(signalingLayer.eventEmitter, 'emit');
  147. chatRoom.getLastPresence = () => [];
  148. chatRoom.emitPresenceListener({
  149. tagName: 'audiomuted',
  150. value: 'true'
  151. }, 'endpoint1');
  152. expect(emitterSpy).toHaveBeenCalledWith(
  153. SignalingEvents.PEER_MUTED_CHANGED,
  154. 'endpoint1',
  155. 'audio',
  156. true
  157. );
  158. });
  159. it('from a user with SourceInfo', () => {
  160. const emitterSpy = spyOn(signalingLayer.eventEmitter, 'emit');
  161. const sourceInfo = {
  162. '12345678-a0': {
  163. muted: true
  164. }
  165. };
  166. chatRoom.mockSourceInfoPresence('endpoint1', sourceInfo);
  167. // <audiomuted/> still included for backwards compat and ChatRoom will emit the presence event
  168. chatRoom.emitPresenceListener({
  169. tagName: 'audiomuted',
  170. value: 'true'
  171. }, 'endpoint1');
  172. // Just once event though the legacy presence is there as well
  173. expect(emitterSpy).toHaveBeenCalledTimes(1);
  174. expect(emitterSpy).toHaveBeenCalledWith(
  175. SignalingEvents.SOURCE_MUTED_CHANGED,
  176. '12345678-a0',
  177. true
  178. );
  179. });
  180. });
  181. describe('with: sourceNameSignaling: false', () => {
  182. let signalingLayer;
  183. let chatRoom;
  184. beforeEach(() => {
  185. FeatureFlags.init({ sourceNameSignaling: false });
  186. signalingLayer = new SignalingLayerImpl();
  187. chatRoom = createMockChatRoom();
  188. signalingLayer.setChatRoom(chatRoom);
  189. });
  190. it('does not react to SourceInfo', () => {
  191. const emitterSpy = spyOn(signalingLayer.eventEmitter, 'emit');
  192. const sourceInfo = {
  193. '12345678-a0': {
  194. muted: true
  195. }
  196. };
  197. chatRoom.mockSourceInfoPresence('endpoint1', sourceInfo);
  198. expect(emitterSpy).not.toHaveBeenCalled();
  199. });
  200. });
  201. });
  202. describe('getPeerMediaInfo', () => {
  203. describe('with: sourceNameSignaling: true', () => {
  204. let signalingLayer;
  205. let chatRoom;
  206. beforeEach(() => {
  207. FeatureFlags.init({ sourceNameSignaling: true });
  208. signalingLayer = new SignalingLayerImpl();
  209. chatRoom = createMockChatRoom();
  210. signalingLayer.setChatRoom(chatRoom);
  211. });
  212. it('will provide default value if only empty source info was sent so far', () => {
  213. const endpointId = '12345678';
  214. chatRoom.mockSourceInfoPresence(endpointId, { });
  215. const audioPeerMediaInfo = signalingLayer.getPeerMediaInfo(endpointId, MediaType.AUDIO);
  216. expect(audioPeerMediaInfo).toEqual({ muted: true });
  217. const videoPeerMediaInfo = signalingLayer.getPeerMediaInfo(endpointId, MediaType.VIDEO);
  218. expect(videoPeerMediaInfo).toEqual({
  219. muted: true,
  220. videoType: undefined
  221. });
  222. });
  223. describe('will read from SourceInfo if available', () => {
  224. it('for audio', () => {
  225. const endpointId = '12345678';
  226. const sourceInfo = {
  227. '12345678-a0': {
  228. muted: true
  229. }
  230. };
  231. chatRoom.mockSourceInfoPresence(endpointId, sourceInfo);
  232. const peerMediaInfo = signalingLayer.getPeerMediaInfo(endpointId, MediaType.AUDIO);
  233. expect(peerMediaInfo).toEqual({ muted: true });
  234. });
  235. it('for video', () => {
  236. const endointId = '12345678';
  237. const sourceInfo = {
  238. '12345678-v0': {
  239. muted: true,
  240. videoType: 'desktop'
  241. }
  242. };
  243. chatRoom.mockSourceInfoPresence(endointId, sourceInfo);
  244. const peerMediaInfo = signalingLayer.getPeerMediaInfo(endointId, MediaType.VIDEO);
  245. expect(peerMediaInfo).toEqual({
  246. muted: true,
  247. videoType: 'desktop'
  248. });
  249. });
  250. });
  251. describe('if there\'s no SourceInfo then will read from the legacy element', () => {
  252. const endointId = '12345678';
  253. it('for audio', () => {
  254. // There's no 'SourceInfo' in the presence
  255. chatRoom.getLastPresence = () => [ { } ];
  256. // This test is very implementation specific and relies on the fact that the backwards compat logic
  257. // is supposed to call into 'chatRoom.getMediaPresenceInfo' and return whatever it returns.
  258. // To be removed once legacy signaling is deprecated.
  259. chatRoom.getMediaPresenceInfo = () => {
  260. return {
  261. muted: true
  262. };
  263. };
  264. const peerMediaInfo = signalingLayer.getPeerMediaInfo(endointId, MediaType.AUDIO);
  265. expect(peerMediaInfo).toEqual({ muted: true });
  266. });
  267. it('for video', () => {
  268. // There's no 'SourceInfo' in the presence
  269. chatRoom.getLastPresence = () => [ { } ];
  270. // This test is very implementation specific and relies on the fact that the backwards compat logic
  271. // is supposed to call into 'chatRoom.getMediaPresenceInfo' and return whatever it returns.
  272. // To be removed once legacy signaling is deprecated.
  273. chatRoom.getMediaPresenceInfo = () => {
  274. return {
  275. muted: true,
  276. videoType: 'desktop'
  277. };
  278. };
  279. const peerMediaInfo = signalingLayer.getPeerMediaInfo(endointId, MediaType.VIDEO);
  280. expect(peerMediaInfo).toEqual({
  281. muted: true,
  282. videoType: 'desktop'
  283. });
  284. });
  285. });
  286. });
  287. describe('with: sourceNameSignaling: false', () => {
  288. beforeEach(() => {
  289. FeatureFlags.init({ sourceNameSignaling: false });
  290. });
  291. it('should not read from SourceInfo element', () => {
  292. const signalingLayer = new SignalingLayerImpl();
  293. const chatRoom = createMockChatRoom();
  294. signalingLayer.setChatRoom(chatRoom);
  295. const endointId = '12345678';
  296. const sourceInfo = {
  297. '12345678-v0': {
  298. muted: true,
  299. videoType: 'desktop'
  300. }
  301. };
  302. chatRoom.mockSourceInfoPresence(endointId, sourceInfo);
  303. // This is the value the legacy flow will use (the values are different that the SourceInfo one).
  304. const legacyMediaInfoValue = {
  305. muted: false,
  306. videoType: 'camera'
  307. };
  308. chatRoom.getMediaPresenceInfo = () => legacyMediaInfoValue;
  309. const peerMediaInfo = signalingLayer.getPeerMediaInfo(endointId, MediaType.VIDEO);
  310. expect(peerMediaInfo).toEqual(legacyMediaInfoValue);
  311. });
  312. });
  313. });
  314. describe('will remove source info(cleanup corner cases)', () => {
  315. let signalingLayer;
  316. let chatRoom;
  317. const endpointId = '12345678';
  318. beforeEach(() => {
  319. FeatureFlags.init({ sourceNameSignaling: true });
  320. signalingLayer = new SignalingLayerImpl();
  321. chatRoom = createMockChatRoom();
  322. signalingLayer.setChatRoom(chatRoom);
  323. });
  324. it('when participant leaves', () => {
  325. const sourceInfo = {
  326. '12345678-v0': {
  327. muted: false,
  328. videoType: 'desktop'
  329. }
  330. };
  331. chatRoom.mockSourceInfoPresence(endpointId, sourceInfo);
  332. expect(signalingLayer.getPeerSourceInfo(endpointId, '12345678-v0')).toBeDefined();
  333. chatRoom.emitParticipantLeft(endpointId);
  334. expect(signalingLayer.getPeerSourceInfo(endpointId, '12345678-v0')).toBeUndefined();
  335. });
  336. });
  337. });