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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. const emitterSpy = spyOn(signalingLayer.eventEmitter, 'emit');
  159. const sourceInfo = {
  160. '12345678-a0': {
  161. muted: true
  162. }
  163. };
  164. chatRoom.mockSourceInfoPresence('endpoint1', sourceInfo);
  165. // <audiomuted/> still included for backwards compat and ChatRoom will emit the presence event
  166. chatRoom.emitPresenceListener({
  167. tagName: 'audiomuted',
  168. value: 'true'
  169. }, 'endpoint1');
  170. // Just once event though the legacy presence is there as well
  171. expect(emitterSpy).toHaveBeenCalledTimes(1);
  172. expect(emitterSpy).toHaveBeenCalledWith(
  173. SignalingEvents.SOURCE_MUTED_CHANGED,
  174. '12345678-a0',
  175. true
  176. );
  177. });
  178. it('from a user with sourceInfo and ssrc-rewriting enabled', () => {
  179. FeatureFlags.init({ ssrcRewritingEnabled: true });
  180. const emitterSpy = spyOn(signalingLayer.eventEmitter, 'emit');
  181. const sourceInfo = {
  182. '12345678-a0': {
  183. muted: true
  184. }
  185. };
  186. chatRoom.mockSourceInfoPresence('endpoint1', sourceInfo);
  187. // <audiomuted/> still included for backwards compat and ChatRoom will emit the presence event
  188. chatRoom.emitPresenceListener({
  189. tagName: 'audiomuted',
  190. value: 'true'
  191. }, 'endpoint1');
  192. expect(emitterSpy).toHaveBeenCalledTimes(2);
  193. expect(emitterSpy.calls.argsFor(1)).toEqual([
  194. SignalingEvents.SOURCE_UPDATED,
  195. '12345678-a0',
  196. 'endpoint1',
  197. true,
  198. undefined
  199. ]);
  200. });
  201. });
  202. });
  203. describe('getPeerMediaInfo', () => {
  204. describe('with: sourceNameSignaling: true', () => {
  205. let signalingLayer;
  206. let chatRoom;
  207. beforeEach(() => {
  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, '12345678-a0');
  233. expect(peerMediaInfo).toEqual({
  234. muted: true,
  235. sourceName: '12345678-a0'
  236. });
  237. });
  238. it('for video', () => {
  239. const endointId = '12345678';
  240. const sourceInfo = {
  241. '12345678-v0': {
  242. muted: true,
  243. videoType: 'desktop'
  244. }
  245. };
  246. chatRoom.mockSourceInfoPresence(endointId, sourceInfo);
  247. const peerMediaInfo = signalingLayer.getPeerMediaInfo(endointId, MediaType.VIDEO, '12345678-v0');
  248. expect(peerMediaInfo).toEqual({
  249. muted: true,
  250. sourceName: '12345678-v0',
  251. videoType: 'desktop'
  252. });
  253. });
  254. });
  255. });
  256. });
  257. describe('will remove source info(cleanup corner cases)', () => {
  258. let signalingLayer;
  259. let chatRoom;
  260. const endpointId = '12345678';
  261. beforeEach(() => {
  262. signalingLayer = new SignalingLayerImpl();
  263. chatRoom = createMockChatRoom();
  264. signalingLayer.setChatRoom(chatRoom);
  265. });
  266. it('when participant leaves', () => {
  267. const sourceInfo = {
  268. '12345678-v0': {
  269. muted: false,
  270. videoType: 'desktop'
  271. }
  272. };
  273. chatRoom.mockSourceInfoPresence(endpointId, sourceInfo);
  274. expect(signalingLayer.getPeerSourceInfo(endpointId, '12345678-v0')).toBeDefined();
  275. chatRoom.emitParticipantLeft(endpointId);
  276. expect(signalingLayer.getPeerSourceInfo(endpointId, '12345678-v0')).toBeUndefined();
  277. });
  278. });
  279. });