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

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