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.

LocalVideo.js 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /* global $, config, interfaceConfig, APP */
  2. /* eslint-disable no-unused-vars */
  3. import React, { Component } from 'react';
  4. import ReactDOM from 'react-dom';
  5. import { Provider } from 'react-redux';
  6. import { JitsiTrackEvents } from '../../../react/features/base/lib-jitsi-meet';
  7. import { VideoTrack } from '../../../react/features/base/media';
  8. import {
  9. getAvatarURLByParticipantId
  10. } from '../../../react/features/base/participants';
  11. import { updateSettings } from '../../../react/features/base/settings';
  12. import { getLocalVideoTrack } from '../../../react/features/base/tracks';
  13. import { shouldDisplayTileView } from '../../../react/features/video-layout';
  14. /* eslint-enable no-unused-vars */
  15. const logger = require('jitsi-meet-logger').getLogger(__filename);
  16. import UIEvents from '../../../service/UI/UIEvents';
  17. import SmallVideo from './SmallVideo';
  18. /**
  19. *
  20. */
  21. function LocalVideo(VideoLayout, emitter, streamEndedCallback) {
  22. this.videoSpanId = 'localVideoContainer';
  23. this.streamEndedCallback = streamEndedCallback;
  24. this.container = this.createContainer();
  25. this.$container = $(this.container);
  26. this.updateDOMLocation();
  27. this.localVideoId = null;
  28. this.bindHoverHandler();
  29. if (config.enableLocalVideoFlip) {
  30. this._buildContextMenu();
  31. }
  32. this.isLocal = true;
  33. this.emitter = emitter;
  34. this.statsPopoverLocation = interfaceConfig.VERTICAL_FILMSTRIP
  35. ? 'left top' : 'top center';
  36. Object.defineProperty(this, 'id', {
  37. get() {
  38. return APP.conference.getMyUserId();
  39. }
  40. });
  41. this.initBrowserSpecificProperties();
  42. SmallVideo.call(this, VideoLayout);
  43. // Set default display name.
  44. this.setDisplayName();
  45. // Initialize the avatar display with an avatar url selected from the redux
  46. // state. Redux stores the local user with a hardcoded participant id of
  47. // 'local' if no id has been assigned yet.
  48. this.avatarChanged(
  49. getAvatarURLByParticipantId(APP.store.getState(), this.id));
  50. this.addAudioLevelIndicator();
  51. this.updateIndicators();
  52. this.container.onclick = this._onContainerClick.bind(this);
  53. }
  54. LocalVideo.prototype = Object.create(SmallVideo.prototype);
  55. LocalVideo.prototype.constructor = LocalVideo;
  56. LocalVideo.prototype.createContainer = function() {
  57. const containerSpan = document.createElement('span');
  58. containerSpan.classList.add('videocontainer');
  59. containerSpan.id = this.videoSpanId;
  60. containerSpan.innerHTML = `
  61. <div class = 'videocontainer__background'></div>
  62. <span id = 'localVideoWrapper'></span>
  63. <div class = 'videocontainer__toolbar'></div>
  64. <div class = 'videocontainer__toptoolbar'></div>
  65. <div class = 'videocontainer__hoverOverlay'></div>
  66. <div class = 'displayNameContainer'></div>
  67. <div class = 'avatar-container'></div>`;
  68. return containerSpan;
  69. };
  70. /**
  71. * Sets the display name for the given video span id.
  72. */
  73. LocalVideo.prototype.setDisplayName = function(displayName) {
  74. if (!this.container) {
  75. logger.warn(
  76. `Unable to set displayName - ${this.videoSpanId
  77. } does not exist`);
  78. return;
  79. }
  80. this.updateDisplayName({
  81. allowEditing: APP.store.getState()['features/base/jwt'].isGuest,
  82. displayName,
  83. displayNameSuffix: interfaceConfig.DEFAULT_LOCAL_DISPLAY_NAME,
  84. elementID: 'localDisplayName',
  85. participantID: this.id
  86. });
  87. };
  88. LocalVideo.prototype.changeVideo = function(stream) {
  89. this.videoStream = stream;
  90. this.localVideoId = `localVideo_${stream.getId()}`;
  91. this._updateVideoElement();
  92. // eslint-disable-next-line eqeqeq
  93. const isVideo = stream.videoType != 'desktop';
  94. const settings = APP.store.getState()['features/base/settings'];
  95. this._enableDisableContextMenu(isVideo);
  96. this.setFlipX(isVideo ? settings.localFlipX : false);
  97. const endedHandler = () => {
  98. const localVideoContainer
  99. = document.getElementById('localVideoWrapper');
  100. // Only remove if there is no video and not a transition state.
  101. // Previous non-react logic created a new video element with each track
  102. // removal whereas react reuses the video component so it could be the
  103. // stream ended but a new one is being used.
  104. if (localVideoContainer && this.videoStream.isEnded()) {
  105. ReactDOM.unmountComponentAtNode(localVideoContainer);
  106. }
  107. this._notifyOfStreamEnded();
  108. stream.off(JitsiTrackEvents.LOCAL_TRACK_STOPPED, endedHandler);
  109. };
  110. stream.on(JitsiTrackEvents.LOCAL_TRACK_STOPPED, endedHandler);
  111. };
  112. /**
  113. * Notify any subscribers of the local video stream ending.
  114. *
  115. * @private
  116. * @returns {void}
  117. */
  118. LocalVideo.prototype._notifyOfStreamEnded = function() {
  119. if (this.streamEndedCallback) {
  120. this.streamEndedCallback(this.id);
  121. }
  122. };
  123. /**
  124. * Shows or hides the local video container.
  125. * @param {boolean} true to make the local video container visible, false
  126. * otherwise
  127. */
  128. LocalVideo.prototype.setVisible = function(visible) {
  129. // We toggle the hidden class as an indication to other interested parties
  130. // that this container has been hidden on purpose.
  131. this.$container.toggleClass('hidden');
  132. // We still show/hide it as we need to overwrite the style property if we
  133. // want our action to take effect. Toggling the display property through
  134. // the above css class didn't succeed in overwriting the style.
  135. if (visible) {
  136. this.$container.show();
  137. } else {
  138. this.$container.hide();
  139. }
  140. };
  141. /**
  142. * Sets the flipX state of the video.
  143. * @param val {boolean} true for flipped otherwise false;
  144. */
  145. LocalVideo.prototype.setFlipX = function(val) {
  146. this.emitter.emit(UIEvents.LOCAL_FLIPX_CHANGED, val);
  147. if (!this.localVideoId) {
  148. return;
  149. }
  150. if (val) {
  151. this.selectVideoElement().addClass('flipVideoX');
  152. } else {
  153. this.selectVideoElement().removeClass('flipVideoX');
  154. }
  155. };
  156. /**
  157. * Builds the context menu for the local video.
  158. */
  159. LocalVideo.prototype._buildContextMenu = function() {
  160. $.contextMenu({
  161. selector: `#${this.videoSpanId}`,
  162. zIndex: 10000,
  163. items: {
  164. flip: {
  165. name: 'Flip',
  166. callback: () => {
  167. const { store } = APP;
  168. const val = !store.getState()['features/base/settings']
  169. .localFlipX;
  170. this.setFlipX(val);
  171. store.dispatch(updateSettings({
  172. localFlipX: val
  173. }));
  174. }
  175. }
  176. },
  177. events: {
  178. show(options) {
  179. options.items.flip.name
  180. = APP.translation.generateTranslationHTML(
  181. 'videothumbnail.flip');
  182. }
  183. }
  184. });
  185. };
  186. /**
  187. * Enables or disables the context menu for the local video.
  188. * @param enable {boolean} true for enable, false for disable
  189. */
  190. LocalVideo.prototype._enableDisableContextMenu = function(enable) {
  191. if (this.$container.contextMenu) {
  192. this.$container.contextMenu(enable);
  193. }
  194. };
  195. /**
  196. * Places the {@code LocalVideo} in the DOM based on the current video layout.
  197. *
  198. * @returns {void}
  199. */
  200. LocalVideo.prototype.updateDOMLocation = function() {
  201. if (!this.container) {
  202. return;
  203. }
  204. if (this.container.parentElement) {
  205. this.container.parentElement.removeChild(this.container);
  206. }
  207. const appendTarget = shouldDisplayTileView(APP.store.getState())
  208. ? document.getElementById('localVideoTileViewContainer')
  209. : document.getElementById('filmstripLocalVideoThumbnail');
  210. appendTarget && appendTarget.appendChild(this.container);
  211. this._updateVideoElement();
  212. };
  213. /**
  214. * Callback invoked when the thumbnail is clicked. Will directly call
  215. * VideoLayout to handle thumbnail click if certain elements have not been
  216. * clicked.
  217. *
  218. * @param {MouseEvent} event - The click event to intercept.
  219. * @private
  220. * @returns {void}
  221. */
  222. LocalVideo.prototype._onContainerClick = function(event) {
  223. // TODO Checking the classes is a workround to allow events to bubble into
  224. // the DisplayName component if it was clicked. React's synthetic events
  225. // will fire after jQuery handlers execute, so stop propogation at this
  226. // point will prevent DisplayName from getting click events. This workaround
  227. // should be removeable once LocalVideo is a React Component because then
  228. // the components share the same eventing system.
  229. const $source = $(event.target || event.srcElement);
  230. const { classList } = event.target;
  231. const clickedOnDisplayName
  232. = $source.parents('.displayNameContainer').length > 0;
  233. const clickedOnPopover = $source.parents('.popover').length > 0
  234. || classList.contains('popover');
  235. const ignoreClick = clickedOnDisplayName || clickedOnPopover;
  236. if (event.stopPropagation && !ignoreClick) {
  237. event.stopPropagation();
  238. }
  239. if (!ignoreClick) {
  240. this._togglePin();
  241. }
  242. };
  243. /**
  244. * Renders the React Element for displaying video in {@code LocalVideo}.
  245. *
  246. */
  247. LocalVideo.prototype._updateVideoElement = function() {
  248. const localVideoContainer = document.getElementById('localVideoWrapper');
  249. const videoTrack
  250. = getLocalVideoTrack(APP.store.getState()['features/base/tracks']);
  251. ReactDOM.render(
  252. <Provider store = { APP.store }>
  253. <VideoTrack
  254. id = 'localVideo_container'
  255. videoTrack = { videoTrack } />
  256. </Provider>,
  257. localVideoContainer
  258. );
  259. // Ensure the video gets play() called on it. This may be necessary in the
  260. // case where the local video container was moved and re-attached, in which
  261. // case video does not autoplay.
  262. const video = this.container.querySelector('video');
  263. video && video.play();
  264. };
  265. export default LocalVideo;