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

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