modified lib-jitsi-meet dev repo
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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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({ });
  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. signalingLayer = new SignalingLayerImpl();
  107. chatRoom = createMockChatRoom();
  108. signalingLayer.setChatRoom(chatRoom);
  109. // Initial value is set in signalingLayer.setChatRoom
  110. expect(chatRoom.addOrReplaceInPresence)
  111. .toHaveBeenCalledWith(
  112. SOURCE_INFO_PRESENCE_ELEMENT,
  113. INITIAL_SOURCE_INFO);
  114. });
  115. it('sends video type in chat room presence', () => {
  116. const videoSourceName = getSourceNameForJitsiTrack(endpointId, MediaType.VIDEO, 0);
  117. signalingLayer.setTrackVideoType(videoSourceName, VideoType.CAMERA);
  118. expect(chatRoom.addOrReplaceInPresence)
  119. .toHaveBeenCalledWith(
  120. SOURCE_INFO_PRESENCE_ELEMENT,
  121. { value: '{"abcdef12-v0":{}}' });
  122. signalingLayer.setTrackVideoType(videoSourceName, VideoType.DESKTOP);
  123. expect(chatRoom.addOrReplaceInPresence)
  124. .toHaveBeenCalledWith(
  125. SOURCE_INFO_PRESENCE_ELEMENT,
  126. { value: '{"abcdef12-v0":{"videoType":"desktop"}}' });
  127. signalingLayer.setTrackVideoType(videoSourceName, VideoType.CAMERA);
  128. expect(chatRoom.addOrReplaceInPresence)
  129. .toHaveBeenCalledWith(
  130. SOURCE_INFO_PRESENCE_ELEMENT,
  131. { value: '{"abcdef12-v0":{}}' });
  132. });
  133. });
  134. describe('should emit muted/video type events based on presence', () => {
  135. describe('with: sourceNameSignaling: true', () => {
  136. let signalingLayer;
  137. let chatRoom = createMockChatRoom();
  138. beforeEach(() => {
  139. signalingLayer = new SignalingLayerImpl();
  140. chatRoom = createMockChatRoom();
  141. signalingLayer.setChatRoom(chatRoom);
  142. });
  143. it('from a legacy user (no SourceInfo)', () => {
  144. const emitterSpy = spyOn(signalingLayer.eventEmitter, 'emit');
  145. chatRoom.getLastPresence = () => [];
  146. chatRoom.emitPresenceListener({
  147. tagName: 'audiomuted',
  148. value: 'true'
  149. }, 'endpoint1');
  150. expect(emitterSpy).toHaveBeenCalledWith(
  151. SignalingEvents.PEER_MUTED_CHANGED,
  152. 'endpoint1',
  153. 'audio',
  154. true
  155. );
  156. });
  157. it('from a user with SourceInfo and ssrc-rewriting disabled', () => {
  158. FeatureFlags.init({ });
  159. const emitterSpy = spyOn(signalingLayer.eventEmitter, 'emit');
  160. const sourceInfo = {
  161. '12345678-a0': {
  162. muted: true
  163. }
  164. };
  165. chatRoom.mockSourceInfoPresence('endpoint1', sourceInfo);
  166. // <audiomuted/> still included for backwards compat and ChatRoom will emit the presence event
  167. chatRoom.emitPresenceListener({
  168. tagName: 'audiomuted',
  169. value: 'true'
  170. }, 'endpoint1');
  171. // Just once event though the legacy presence is there as well
  172. expect(emitterSpy).toHaveBeenCalledTimes(1);
  173. expect(emitterSpy).toHaveBeenCalledWith(
  174. SignalingEvents.SOURCE_MUTED_CHANGED,
  175. '12345678-a0',
  176. true
  177. );
  178. });
  179. it('from a user with sourceInfo and ssrc-rewriting enabled', () => {
  180. FeatureFlags.init({ ssrcRewritingEnabled: true });
  181. const emitterSpy = spyOn(signalingLayer.eventEmitter, 'emit');
  182. const sourceInfo = {
  183. '12345678-a0': {
  184. muted: true
  185. }
  186. };
  187. chatRoom.mockSourceInfoPresence('endpoint1', sourceInfo);
  188. // <audiomuted/> still included for backwards compat and ChatRoom will emit the presence event
  189. chatRoom.emitPresenceListener({
  190. tagName: 'audiomuted',
  191. value: 'true'
  192. }, 'endpoint1');
  193. expect(emitterSpy).toHaveBeenCalledTimes(2);
  194. expect(emitterSpy.calls.argsFor(1)).toEqual([
  195. SignalingEvents.SOURCE_UPDATED,
  196. '12345678-a0',
  197. 'endpoint1',
  198. true,
  199. undefined
  200. ]);
  201. });
  202. });
  203. });
  204. describe('getPeerMediaInfo', () => {
  205. describe('with: sourceNameSignaling: true', () => {
  206. let signalingLayer;
  207. let chatRoom;
  208. beforeEach(() => {
  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, '12345678-a0');
  234. expect(peerMediaInfo).toEqual({
  235. muted: true,
  236. sourceName: '12345678-a0'
  237. });
  238. });
  239. it('for video', () => {
  240. const endointId = '12345678';
  241. const sourceInfo = {
  242. '12345678-v0': {
  243. muted: true,
  244. videoType: 'desktop'
  245. }
  246. };
  247. chatRoom.mockSourceInfoPresence(endointId, sourceInfo);
  248. const peerMediaInfo = signalingLayer.getPeerMediaInfo(endointId, MediaType.VIDEO, '12345678-v0');
  249. expect(peerMediaInfo).toEqual({
  250. muted: true,
  251. sourceName: '12345678-v0',
  252. videoType: 'desktop'
  253. });
  254. });
  255. });
  256. });
  257. });
  258. describe('will remove source info(cleanup corner cases)', () => {
  259. let signalingLayer;
  260. let chatRoom;
  261. const endpointId = '12345678';
  262. beforeEach(() => {
  263. signalingLayer = new SignalingLayerImpl();
  264. chatRoom = createMockChatRoom();
  265. signalingLayer.setChatRoom(chatRoom);
  266. });
  267. it('when participant leaves', () => {
  268. const sourceInfo = {
  269. '12345678-v0': {
  270. muted: false,
  271. videoType: 'desktop'
  272. }
  273. };
  274. chatRoom.mockSourceInfoPresence(endpointId, sourceInfo);
  275. expect(signalingLayer.getPeerSourceInfo(endpointId, '12345678-v0')).toBeDefined();
  276. chatRoom.emitParticipantLeft(endpointId);
  277. expect(signalingLayer.getPeerSourceInfo(endpointId, '12345678-v0')).toBeUndefined();
  278. });
  279. });
  280. });