您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

LocalVideo.js 8.4KB

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