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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. /* eslint-enable no-unused-vars */
  9. const logger = require('jitsi-meet-logger').getLogger(__filename);
  10. import UIEvents from '../../../service/UI/UIEvents';
  11. import SmallVideo from './SmallVideo';
  12. /**
  13. *
  14. */
  15. function LocalVideo(VideoLayout, emitter) {
  16. this.videoSpanId = 'localVideoContainer';
  17. this.container = this.createContainer();
  18. this.$container = $(this.container);
  19. $('#filmstripLocalVideoThumbnail').append(this.container);
  20. this.localVideoId = null;
  21. this.bindHoverHandler();
  22. if (config.enableLocalVideoFlip) {
  23. this._buildContextMenu();
  24. }
  25. this.isLocal = true;
  26. this.emitter = emitter;
  27. this.statsPopoverLocation = interfaceConfig.VERTICAL_FILMSTRIP
  28. ? 'left top' : 'top center';
  29. Object.defineProperty(this, 'id', {
  30. get() {
  31. return APP.conference.getMyUserId();
  32. }
  33. });
  34. this.initBrowserSpecificProperties();
  35. SmallVideo.call(this, VideoLayout);
  36. // Set default display name.
  37. this.setDisplayName();
  38. this.addAudioLevelIndicator();
  39. this.updateIndicators();
  40. this.container.onclick = this._onContainerClick.bind(this);
  41. }
  42. LocalVideo.prototype = Object.create(SmallVideo.prototype);
  43. LocalVideo.prototype.constructor = LocalVideo;
  44. LocalVideo.prototype.createContainer = function() {
  45. const containerSpan = document.createElement('span');
  46. containerSpan.classList.add('videocontainer');
  47. containerSpan.id = this.videoSpanId;
  48. containerSpan.innerHTML = `
  49. <div class = 'videocontainer__background'></div>
  50. <span id = 'localVideoWrapper'></span>
  51. <div class = 'videocontainer__toolbar'></div>
  52. <div class = 'videocontainer__toptoolbar'></div>
  53. <div class = 'videocontainer__hoverOverlay'></div>
  54. <div class = 'displayNameContainer'></div>
  55. <div class = 'avatar-container'></div>`;
  56. return containerSpan;
  57. };
  58. /**
  59. * Sets the display name for the given video span id.
  60. */
  61. LocalVideo.prototype.setDisplayName = function(displayName) {
  62. if (!this.container) {
  63. logger.warn(
  64. `Unable to set displayName - ${this.videoSpanId
  65. } does not exist`);
  66. return;
  67. }
  68. this.updateDisplayName({
  69. allowEditing: true,
  70. displayName,
  71. displayNameSuffix: interfaceConfig.DEFAULT_LOCAL_DISPLAY_NAME,
  72. elementID: 'localDisplayName',
  73. participantID: this.id
  74. });
  75. };
  76. LocalVideo.prototype.changeVideo = function(stream) {
  77. this.videoStream = stream;
  78. this.localVideoId = `localVideo_${stream.getId()}`;
  79. const localVideoContainer = document.getElementById('localVideoWrapper');
  80. ReactDOM.render(
  81. <Provider store = { APP.store }>
  82. <VideoTrack
  83. id = { this.localVideoId }
  84. videoTrack = {{ jitsiTrack: stream }} />
  85. </Provider>,
  86. localVideoContainer
  87. );
  88. // eslint-disable-next-line eqeqeq
  89. const isVideo = stream.videoType != 'desktop';
  90. this._enableDisableContextMenu(isVideo);
  91. this.setFlipX(isVideo ? APP.settings.getLocalFlipX() : false);
  92. const endedHandler = () => {
  93. // Only remove if there is no video and not a transition state.
  94. // Previous non-react logic created a new video element with each track
  95. // removal whereas react reuses the video component so it could be the
  96. // stream ended but a new one is being used.
  97. if (this.videoStream.isEnded()) {
  98. ReactDOM.unmountComponentAtNode(localVideoContainer);
  99. }
  100. // when removing only the video element and we are on stage
  101. // update the stage
  102. if (this.isCurrentlyOnLargeVideo()) {
  103. this.VideoLayout.updateLargeVideo(this.id);
  104. }
  105. stream.off(JitsiTrackEvents.LOCAL_TRACK_STOPPED, endedHandler);
  106. };
  107. stream.on(JitsiTrackEvents.LOCAL_TRACK_STOPPED, endedHandler);
  108. };
  109. /**
  110. * Shows or hides the local video container.
  111. * @param {boolean} true to make the local video container visible, false
  112. * otherwise
  113. */
  114. LocalVideo.prototype.setVisible = function(visible) {
  115. // We toggle the hidden class as an indication to other interested parties
  116. // that this container has been hidden on purpose.
  117. this.$container.toggleClass('hidden');
  118. // We still show/hide it as we need to overwrite the style property if we
  119. // want our action to take effect. Toggling the display property through
  120. // the above css class didn't succeed in overwriting the style.
  121. if (visible) {
  122. this.$container.show();
  123. } else {
  124. this.$container.hide();
  125. }
  126. };
  127. /**
  128. * Sets the flipX state of the video.
  129. * @param val {boolean} true for flipped otherwise false;
  130. */
  131. LocalVideo.prototype.setFlipX = function(val) {
  132. this.emitter.emit(UIEvents.LOCAL_FLIPX_CHANGED, val);
  133. if (!this.localVideoId) {
  134. return;
  135. }
  136. if (val) {
  137. this.selectVideoElement().addClass('flipVideoX');
  138. } else {
  139. this.selectVideoElement().removeClass('flipVideoX');
  140. }
  141. };
  142. /**
  143. * Builds the context menu for the local video.
  144. */
  145. LocalVideo.prototype._buildContextMenu = function() {
  146. $.contextMenu({
  147. selector: `#${this.videoSpanId}`,
  148. zIndex: 10000,
  149. items: {
  150. flip: {
  151. name: 'Flip',
  152. callback: () => {
  153. const val = !APP.settings.getLocalFlipX();
  154. this.setFlipX(val);
  155. APP.settings.setLocalFlipX(val);
  156. }
  157. }
  158. },
  159. events: {
  160. show(options) {
  161. options.items.flip.name
  162. = APP.translation.generateTranslationHTML(
  163. 'videothumbnail.flip');
  164. }
  165. }
  166. });
  167. };
  168. /**
  169. * Enables or disables the context menu for the local video.
  170. * @param enable {boolean} true for enable, false for disable
  171. */
  172. LocalVideo.prototype._enableDisableContextMenu = function(enable) {
  173. if (this.$container.contextMenu) {
  174. this.$container.contextMenu(enable);
  175. }
  176. };
  177. /**
  178. * Callback invoked when the thumbnail is clicked. Will directly call
  179. * VideoLayout to handle thumbnail click if certain elements have not been
  180. * clicked.
  181. *
  182. * @param {MouseEvent} event - The click event to intercept.
  183. * @private
  184. * @returns {void}
  185. */
  186. LocalVideo.prototype._onContainerClick = function(event) {
  187. // TODO Checking the classes is a workround to allow events to bubble into
  188. // the DisplayName component if it was clicked. React's synthetic events
  189. // will fire after jQuery handlers execute, so stop propogation at this
  190. // point will prevent DisplayName from getting click events. This workaround
  191. // should be removeable once LocalVideo is a React Component because then
  192. // the components share the same eventing system.
  193. const $source = $(event.target || event.srcElement);
  194. const { classList } = event.target;
  195. const clickedOnDisplayName
  196. = $source.parents('.displayNameContainer').length > 0;
  197. const clickedOnPopover = $source.parents('.popover').length > 0
  198. || classList.contains('popover');
  199. const ignoreClick = clickedOnDisplayName || clickedOnPopover;
  200. // FIXME: with Temasys plugin event arg is not an event, but the clicked
  201. // object itself, so we have to skip this call
  202. if (event.stopPropagation && !ignoreClick) {
  203. event.stopPropagation();
  204. }
  205. if (!ignoreClick) {
  206. this.VideoLayout.handleVideoThumbClicked(this.id);
  207. }
  208. };
  209. export default LocalVideo;