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.0KB

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