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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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', () => {
  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. });
  179. });
  180. describe('getPeerMediaInfo', () => {
  181. describe('with: sourceNameSignaling: true', () => {
  182. let signalingLayer;
  183. let chatRoom;
  184. beforeEach(() => {
  185. signalingLayer = new SignalingLayerImpl();
  186. chatRoom = createMockChatRoom();
  187. signalingLayer.setChatRoom(chatRoom);
  188. });
  189. it('will provide default value if only empty source info was sent so far', () => {
  190. const endpointId = '12345678';
  191. chatRoom.mockSourceInfoPresence(endpointId, { });
  192. const audioPeerMediaInfo = signalingLayer.getPeerMediaInfo(endpointId, MediaType.AUDIO);
  193. expect(audioPeerMediaInfo).toEqual({ muted: true });
  194. const videoPeerMediaInfo = signalingLayer.getPeerMediaInfo(endpointId, MediaType.VIDEO);
  195. expect(videoPeerMediaInfo).toEqual({
  196. muted: true,
  197. videoType: undefined
  198. });
  199. });
  200. describe('will read from SourceInfo if available', () => {
  201. it('for audio', () => {
  202. const endpointId = '12345678';
  203. const sourceInfo = {
  204. '12345678-a0': {
  205. muted: true
  206. }
  207. };
  208. chatRoom.mockSourceInfoPresence(endpointId, sourceInfo);
  209. const peerMediaInfo = signalingLayer.getPeerMediaInfo(endpointId, MediaType.AUDIO, '12345678-a0');
  210. expect(peerMediaInfo).toEqual({
  211. muted: true,
  212. sourceName: '12345678-a0'
  213. });
  214. });
  215. it('for video', () => {
  216. const endointId = '12345678';
  217. const sourceInfo = {
  218. '12345678-v0': {
  219. muted: true,
  220. videoType: 'desktop'
  221. }
  222. };
  223. chatRoom.mockSourceInfoPresence(endointId, sourceInfo);
  224. const peerMediaInfo = signalingLayer.getPeerMediaInfo(endointId, MediaType.VIDEO, '12345678-v0');
  225. expect(peerMediaInfo).toEqual({
  226. muted: true,
  227. sourceName: '12345678-v0',
  228. videoType: 'desktop'
  229. });
  230. });
  231. });
  232. });
  233. });
  234. describe('will remove source info(cleanup corner cases)', () => {
  235. let signalingLayer;
  236. let chatRoom;
  237. const endpointId = '12345678';
  238. beforeEach(() => {
  239. signalingLayer = new SignalingLayerImpl();
  240. chatRoom = createMockChatRoom();
  241. signalingLayer.setChatRoom(chatRoom);
  242. });
  243. it('when participant leaves', () => {
  244. const sourceInfo = {
  245. '12345678-v0': {
  246. muted: false,
  247. videoType: 'desktop'
  248. }
  249. };
  250. chatRoom.mockSourceInfoPresence(endpointId, sourceInfo);
  251. expect(signalingLayer.getPeerSourceInfo(endpointId, '12345678-v0')).toBeDefined();
  252. chatRoom.emitParticipantLeft(endpointId);
  253. expect(signalingLayer.getPeerSourceInfo(endpointId, '12345678-v0')).toBeUndefined();
  254. });
  255. });
  256. });