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

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