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 8.3KB

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