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

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