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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. import * as 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.PEER_MUTED_CHANGED,
  176. 'endpoint1',
  177. 'audio',
  178. true
  179. );
  180. });
  181. });
  182. describe('with: sourceNameSignaling: false', () => {
  183. let signalingLayer;
  184. let chatRoom;
  185. beforeEach(() => {
  186. FeatureFlags.init({ sourceNameSignaling: false });
  187. signalingLayer = new SignalingLayerImpl();
  188. chatRoom = createMockChatRoom();
  189. signalingLayer.setChatRoom(chatRoom);
  190. });
  191. it('does not react to SourceInfo', () => {
  192. const emitterSpy = spyOn(signalingLayer.eventEmitter, 'emit');
  193. const sourceInfo = {
  194. '12345678-a0': {
  195. muted: true
  196. }
  197. };
  198. chatRoom.mockSourceInfoPresence('endpoint1', sourceInfo);
  199. expect(emitterSpy).not.toHaveBeenCalled();
  200. });
  201. });
  202. });
  203. describe('getPeerMediaInfo', () => {
  204. describe('with: sourceNameSignaling: true', () => {
  205. let signalingLayer;
  206. let chatRoom;
  207. beforeEach(() => {
  208. FeatureFlags.init({ sourceNameSignaling: true });
  209. signalingLayer = new SignalingLayerImpl();
  210. chatRoom = createMockChatRoom();
  211. signalingLayer.setChatRoom(chatRoom);
  212. });
  213. it('will provide default value if only empty source info was sent so far', () => {
  214. const endpointId = '12345678';
  215. chatRoom.mockSourceInfoPresence(endpointId, { });
  216. const audioPeerMediaInfo = signalingLayer.getPeerMediaInfo(endpointId, MediaType.AUDIO);
  217. expect(audioPeerMediaInfo).toEqual({ muted: true });
  218. const videoPeerMediaInfo = signalingLayer.getPeerMediaInfo(endpointId, MediaType.VIDEO);
  219. expect(videoPeerMediaInfo).toEqual({
  220. muted: true,
  221. videoType: undefined
  222. });
  223. });
  224. describe('will read from SourceInfo if available', () => {
  225. it('for audio', () => {
  226. const endpointId = '12345678';
  227. const sourceInfo = {
  228. '12345678-a0': {
  229. muted: true
  230. }
  231. };
  232. chatRoom.mockSourceInfoPresence(endpointId, sourceInfo);
  233. const peerMediaInfo = signalingLayer.getPeerMediaInfo(endpointId, MediaType.AUDIO);
  234. expect(peerMediaInfo).toEqual({ muted: true });
  235. });
  236. it('for video', () => {
  237. const endointId = '12345678';
  238. const sourceInfo = {
  239. '12345678-v0': {
  240. muted: true,
  241. videoType: 'desktop'
  242. }
  243. };
  244. chatRoom.mockSourceInfoPresence(endointId, sourceInfo);
  245. const peerMediaInfo = signalingLayer.getPeerMediaInfo(endointId, MediaType.VIDEO);
  246. expect(peerMediaInfo).toEqual({
  247. muted: true,
  248. videoType: 'desktop'
  249. });
  250. });
  251. });
  252. describe('if there\'s no SourceInfo then will read from the legacy element', () => {
  253. const endointId = '12345678';
  254. it('for audio', () => {
  255. // There's no 'SourceInfo' in the presence
  256. chatRoom.getLastPresence = () => [ { } ];
  257. // This test is very implementation specific and relies on the fact that the backwards compat logic
  258. // is supposed to call into 'chatRoom.getMediaPresenceInfo' and return whatever it returns.
  259. // To be removed once legacy signaling is deprecated.
  260. chatRoom.getMediaPresenceInfo = () => {
  261. return {
  262. muted: true
  263. };
  264. };
  265. const peerMediaInfo = signalingLayer.getPeerMediaInfo(endointId, MediaType.AUDIO);
  266. expect(peerMediaInfo).toEqual({ muted: true });
  267. });
  268. it('for video', () => {
  269. // There's no 'SourceInfo' in the presence
  270. chatRoom.getLastPresence = () => [ { } ];
  271. // This test is very implementation specific and relies on the fact that the backwards compat logic
  272. // is supposed to call into 'chatRoom.getMediaPresenceInfo' and return whatever it returns.
  273. // To be removed once legacy signaling is deprecated.
  274. chatRoom.getMediaPresenceInfo = () => {
  275. return {
  276. muted: true,
  277. videoType: 'desktop'
  278. };
  279. };
  280. const peerMediaInfo = signalingLayer.getPeerMediaInfo(endointId, MediaType.VIDEO);
  281. expect(peerMediaInfo).toEqual({
  282. muted: true,
  283. videoType: 'desktop'
  284. });
  285. });
  286. });
  287. });
  288. describe('with: sourceNameSignaling: false', () => {
  289. beforeEach(() => {
  290. FeatureFlags.init({ sourceNameSignaling: false });
  291. });
  292. it('should not read from SourceInfo element', () => {
  293. const signalingLayer = new SignalingLayerImpl();
  294. const chatRoom = createMockChatRoom();
  295. signalingLayer.setChatRoom(chatRoom);
  296. const endointId = '12345678';
  297. const sourceInfo = {
  298. '12345678-v0': {
  299. muted: true,
  300. videoType: 'desktop'
  301. }
  302. };
  303. chatRoom.mockSourceInfoPresence(endointId, sourceInfo);
  304. // This is the value the legacy flow will use (the values are different that the SourceInfo one).
  305. const legacyMediaInfoValue = {
  306. muted: false,
  307. videoType: 'camera'
  308. };
  309. chatRoom.getMediaPresenceInfo = () => legacyMediaInfoValue;
  310. const peerMediaInfo = signalingLayer.getPeerMediaInfo(endointId, MediaType.VIDEO);
  311. expect(peerMediaInfo).toEqual(legacyMediaInfoValue);
  312. });
  313. });
  314. });
  315. describe('will remove source info(cleanup corner cases)', () => {
  316. let signalingLayer;
  317. let chatRoom;
  318. const endpointId = '12345678';
  319. beforeEach(() => {
  320. FeatureFlags.init({ sourceNameSignaling: true });
  321. signalingLayer = new SignalingLayerImpl();
  322. chatRoom = createMockChatRoom();
  323. signalingLayer.setChatRoom(chatRoom);
  324. });
  325. it('when participant leaves', () => {
  326. const sourceInfo = {
  327. '12345678-v0': {
  328. muted: false,
  329. videoType: 'desktop'
  330. }
  331. };
  332. chatRoom.mockSourceInfoPresence(endpointId, sourceInfo);
  333. expect(signalingLayer.getPeerSourceInfo(endpointId, '12345678-v0')).toBeDefined();
  334. chatRoom.emitParticipantLeft(endpointId);
  335. expect(signalingLayer.getPeerSourceInfo(endpointId, '12345678-v0')).toBeUndefined();
  336. });
  337. it('when it\'s no longer in the presence', () => {
  338. chatRoom.mockSourceInfoPresence(endpointId, {
  339. '12345678-v0': { muted: false }
  340. });
  341. expect(signalingLayer.getPeerSourceInfo(endpointId, '12345678-v0')).toBeDefined();
  342. chatRoom.mockSourceInfoPresence(endpointId, {
  343. '12345678-v1': { muted: false }
  344. });
  345. expect(signalingLayer.getPeerSourceInfo(endpointId, '12345678-v0')).toBeUndefined();
  346. expect(signalingLayer.getPeerSourceInfo(endpointId, '12345678-v1')).toBeDefined();
  347. });
  348. });
  349. });