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

LocalVideo.js 9.6KB

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