Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /* global $, APP, interfaceConfig */
  2. /* eslint-disable no-unused-vars */
  3. import React from 'react';
  4. import ReactDOM from 'react-dom';
  5. import { Provider } from 'react-redux';
  6. import { I18nextProvider } from 'react-i18next';
  7. import { AtlasKitThemeProvider } from '@atlaskit/theme';
  8. import { i18next } from '../../../react/features/base/i18n';
  9. import {
  10. JitsiParticipantConnectionStatus
  11. } from '../../../react/features/base/lib-jitsi-meet';
  12. import {
  13. getPinnedParticipant,
  14. pinParticipant
  15. } from '../../../react/features/base/participants';
  16. import { PresenceLabel } from '../../../react/features/presence-status';
  17. import {
  18. REMOTE_CONTROL_MENU_STATES,
  19. RemoteVideoMenuTriggerButton
  20. } from '../../../react/features/remote-video-menu';
  21. import { LAYOUTS, getCurrentLayout } from '../../../react/features/video-layout';
  22. /* eslint-enable no-unused-vars */
  23. const logger = require('jitsi-meet-logger').getLogger(__filename);
  24. import SmallVideo from './SmallVideo';
  25. import UIUtils from '../util/UIUtil';
  26. /**
  27. *
  28. * @param {*} spanId
  29. */
  30. function createContainer(spanId) {
  31. const container = document.createElement('span');
  32. container.id = spanId;
  33. container.className = 'videocontainer';
  34. container.innerHTML = `
  35. <div class = 'videocontainer__background'></div>
  36. <div class = 'videocontainer__toptoolbar'></div>
  37. <div class = 'videocontainer__toolbar'></div>
  38. <div class = 'videocontainer__hoverOverlay'></div>
  39. <div class = 'displayNameContainer'></div>
  40. <div class = 'avatar-container'></div>
  41. <div class ='presence-label-container'></div>
  42. <span class = 'remotevideomenu'></span>`;
  43. const remoteVideosContainer
  44. = document.getElementById('filmstripRemoteVideosContainer');
  45. const localVideoContainer
  46. = document.getElementById('localVideoTileViewContainer');
  47. remoteVideosContainer.insertBefore(container, localVideoContainer);
  48. return container;
  49. }
  50. /**
  51. *
  52. */
  53. export default class RemoteVideo extends SmallVideo {
  54. /**
  55. * Creates new instance of the <tt>RemoteVideo</tt>.
  56. * @param user {JitsiParticipant} the user for whom remote video instance will
  57. * be created.
  58. * @param {VideoLayout} VideoLayout the video layout instance.
  59. * @constructor
  60. */
  61. constructor(user, VideoLayout) {
  62. super(VideoLayout);
  63. this.user = user;
  64. this.id = user.getId();
  65. this.videoSpanId = `participant_${this.id}`;
  66. this._audioStreamElement = null;
  67. this._supportsRemoteControl = false;
  68. // dev mod
  69. // this.statsPopoverLocation = interfaceConfig.VERTICAL_FILMSTRIP ? 'left bottom' : 'top center';
  70. this.statsPopoverLocation = interfaceConfig.VERTICAL_FILMSTRIP ? 'left bottom' : 'left bottom';
  71. this.addRemoteVideoContainer();
  72. this.updateIndicators();
  73. this.updateDisplayName();
  74. this.bindHoverHandler();
  75. this.flipX = false;
  76. this.isLocal = false;
  77. this.popupMenuIsHovered = false;
  78. this._isRemoteControlSessionActive = false;
  79. /**
  80. * The flag is set to <tt>true</tt> after the 'canplay' event has been
  81. * triggered on the current video element. It goes back to <tt>false</tt>
  82. * when the stream is removed. It is used to determine whether the video
  83. * playback has ever started.
  84. * @type {boolean}
  85. */
  86. this._canPlayEventReceived = false;
  87. /**
  88. * The flag is set to <tt>true</tt> if remote participant's video gets muted
  89. * during his media connection disruption. This is to prevent black video
  90. * being render on the thumbnail, because even though once the video has
  91. * been played the image usually remains on the video element it seems that
  92. * after longer period of the video element being hidden this image can be
  93. * lost.
  94. * @type {boolean}
  95. */
  96. this.mutedWhileDisconnected = false;
  97. // Bind event handlers so they are only bound once for every instance.
  98. // TODO The event handlers should be turned into actions so changes can be
  99. // handled through reducers and middleware.
  100. this._requestRemoteControlPermissions
  101. = this._requestRemoteControlPermissions.bind(this);
  102. this._setAudioVolume = this._setAudioVolume.bind(this);
  103. this._stopRemoteControl = this._stopRemoteControl.bind(this);
  104. this.container.onclick = this._onContainerClick;
  105. }
  106. /**
  107. *
  108. */
  109. addRemoteVideoContainer() {
  110. this.container = createContainer(this.videoSpanId);
  111. this.$container = $(this.container);
  112. this.initializeAvatar();
  113. this._setThumbnailSize();
  114. this.initBrowserSpecificProperties();
  115. this.updateRemoteVideoMenu();
  116. this.updateStatusBar();
  117. this.addAudioLevelIndicator();
  118. this.addPresenceLabel();
  119. return this.container;
  120. }
  121. /**
  122. * Checks whether current video is considered hovered. Currently it is hovered
  123. * if the mouse is over the video, or if the connection indicator or the popup
  124. * menu is shown(hovered).
  125. * @private
  126. * NOTE: extends SmallVideo's method
  127. */
  128. _isHovered() {
  129. return super._isHovered() || this.popupMenuIsHovered;
  130. }
  131. /**
  132. * Generates the popup menu content.
  133. *
  134. * @returns {Element|*} the constructed element, containing popup menu items
  135. * @private
  136. */
  137. _generatePopupContent() {
  138. if (interfaceConfig.filmStripOnly) {
  139. return;
  140. }
  141. const remoteVideoMenuContainer
  142. = this.container.querySelector('.remotevideomenu');
  143. if (!remoteVideoMenuContainer) {
  144. return;
  145. }
  146. const { controller } = APP.remoteControl;
  147. let remoteControlState = null;
  148. let onRemoteControlToggle;
  149. if (this._supportsRemoteControl
  150. && ((!APP.remoteControl.active && !this._isRemoteControlSessionActive)
  151. || APP.remoteControl.controller.activeParticipant === this.id)) {
  152. if (controller.getRequestedParticipant() === this.id) {
  153. remoteControlState = REMOTE_CONTROL_MENU_STATES.REQUESTING;
  154. } else if (controller.isStarted()) {
  155. onRemoteControlToggle = this._stopRemoteControl;
  156. remoteControlState = REMOTE_CONTROL_MENU_STATES.STARTED;
  157. } else {
  158. onRemoteControlToggle = this._requestRemoteControlPermissions;
  159. remoteControlState = REMOTE_CONTROL_MENU_STATES.NOT_STARTED;
  160. }
  161. }
  162. const initialVolumeValue = this._audioStreamElement && this._audioStreamElement.volume;
  163. // hide volume when in silent mode
  164. const onVolumeChange
  165. = APP.store.getState()['features/base/config'].startSilent ? undefined : this._setAudioVolume;
  166. const participantID = this.id;
  167. const currentLayout = getCurrentLayout(APP.store.getState());
  168. let remoteMenuPosition;
  169. if (currentLayout === LAYOUTS.TILE_VIEW) {
  170. remoteMenuPosition = 'left top';
  171. } else if (currentLayout === LAYOUTS.VERTICAL_FILMSTRIP_VIEW) {
  172. remoteMenuPosition = 'left bottom';
  173. } else {
  174. remoteMenuPosition = 'top center';
  175. }
  176. // dev mod
  177. remoteMenuPosition = 'left top';
  178. ReactDOM.render(
  179. <Provider store = { APP.store }>
  180. <I18nextProvider i18n = { i18next }>
  181. <AtlasKitThemeProvider mode = 'dark'>
  182. <RemoteVideoMenuTriggerButton
  183. initialVolumeValue = { initialVolumeValue }
  184. isAudioMuted = { this.isAudioMuted }
  185. menuPosition = { remoteMenuPosition }
  186. onMenuDisplay
  187. = {this._onRemoteVideoMenuDisplay.bind(this)}
  188. onRemoteControlToggle = { onRemoteControlToggle }
  189. onVolumeChange = { onVolumeChange }
  190. participantID = { participantID }
  191. remoteControlState = { remoteControlState } />
  192. </AtlasKitThemeProvider>
  193. </I18nextProvider>
  194. </Provider>,
  195. remoteVideoMenuContainer);
  196. }
  197. /**
  198. *
  199. */
  200. _onRemoteVideoMenuDisplay() {
  201. this.updateRemoteVideoMenu();
  202. }
  203. /**
  204. * Sets the remote control active status for the remote video.
  205. *
  206. * @param {boolean} isActive - The new remote control active status.
  207. * @returns {void}
  208. */
  209. setRemoteControlActiveStatus(isActive) {
  210. this._isRemoteControlSessionActive = isActive;
  211. this.updateRemoteVideoMenu();
  212. }
  213. /**
  214. * Sets the remote control supported value and initializes or updates the menu
  215. * depending on the remote control is supported or not.
  216. * @param {boolean} isSupported
  217. */
  218. setRemoteControlSupport(isSupported = false) {
  219. if (this._supportsRemoteControl === isSupported) {
  220. return;
  221. }
  222. this._supportsRemoteControl = isSupported;
  223. this.updateRemoteVideoMenu();
  224. }
  225. /**
  226. * Requests permissions for remote control session.
  227. */
  228. _requestRemoteControlPermissions() {
  229. APP.remoteControl.controller.requestPermissions(this.id, this.VideoLayout.getLargeVideoWrapper())
  230. .then(result => {
  231. if (result === null) {
  232. return;
  233. }
  234. this.updateRemoteVideoMenu();
  235. APP.UI.messageHandler.notify(
  236. 'dialog.remoteControlTitle',
  237. result === false ? 'dialog.remoteControlDeniedMessage' : 'dialog.remoteControlAllowedMessage',
  238. { user: this.user.getDisplayName() || interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME }
  239. );
  240. if (result === true) {
  241. // the remote control permissions has been granted
  242. // pin the controlled participant
  243. const pinnedParticipant = getPinnedParticipant(APP.store.getState()) || {};
  244. const pinnedId = pinnedParticipant.id;
  245. if (pinnedId !== this.id) {
  246. APP.store.dispatch(pinParticipant(this.id));
  247. }
  248. }
  249. }, error => {
  250. logger.error(error);
  251. this.updateRemoteVideoMenu();
  252. APP.UI.messageHandler.notify(
  253. 'dialog.remoteControlTitle',
  254. 'dialog.remoteControlErrorMessage',
  255. { user: this.user.getDisplayName() || interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME }
  256. );
  257. });
  258. this.updateRemoteVideoMenu();
  259. }
  260. /**
  261. * Stops remote control session.
  262. */
  263. _stopRemoteControl() {
  264. // send message about stopping
  265. APP.remoteControl.controller.stop();
  266. this.updateRemoteVideoMenu();
  267. }
  268. /**
  269. * Change the remote participant's volume level.
  270. *
  271. * @param {int} newVal - The value to set the slider to.
  272. */
  273. _setAudioVolume(newVal) {
  274. if (this._audioStreamElement) {
  275. this._audioStreamElement.volume = newVal;
  276. }
  277. }
  278. /**
  279. * Updates the remote video menu.
  280. *
  281. * @param isMuted the new muted state to update to
  282. */
  283. updateRemoteVideoMenu(isMuted) {
  284. if (typeof isMuted !== 'undefined') {
  285. this.isAudioMuted = isMuted;
  286. }
  287. this._generatePopupContent();
  288. }
  289. /**
  290. * @inheritDoc
  291. * @override
  292. */
  293. setVideoMutedView(isMuted) {
  294. super.setVideoMutedView(isMuted);
  295. // Update 'mutedWhileDisconnected' flag
  296. this._figureOutMutedWhileDisconnected();
  297. }
  298. /**
  299. * Figures out the value of {@link #mutedWhileDisconnected} flag by taking into
  300. * account remote participant's network connectivity and video muted status.
  301. *
  302. * @private
  303. */
  304. _figureOutMutedWhileDisconnected() {
  305. const isActive = this.isConnectionActive();
  306. if (!isActive && this.isVideoMuted) {
  307. this.mutedWhileDisconnected = true;
  308. } else if (isActive && !this.isVideoMuted) {
  309. this.mutedWhileDisconnected = false;
  310. }
  311. }
  312. /**
  313. * Removes the remote stream element corresponding to the given stream and
  314. * parent container.
  315. *
  316. * @param stream the MediaStream
  317. * @param isVideo <tt>true</tt> if given <tt>stream</tt> is a video one.
  318. */
  319. removeRemoteStreamElement(stream) {
  320. if (!this.container) {
  321. return false;
  322. }
  323. const isVideo = stream.isVideoTrack();
  324. const elementID = SmallVideo.getStreamElementID(stream);
  325. const select = $(`#${elementID}`);
  326. select.remove();
  327. if (isVideo) {
  328. this._canPlayEventReceived = false;
  329. }
  330. logger.info(`${isVideo ? 'Video' : 'Audio'} removed ${this.id}`, select);
  331. if (stream === this.videoStream) {
  332. this.videoStream = null;
  333. }
  334. this.updateView();
  335. }
  336. /**
  337. * Checks whether the remote user associated with this <tt>RemoteVideo</tt>
  338. * has connectivity issues.
  339. *
  340. * @return {boolean} <tt>true</tt> if the user's connection is fine or
  341. * <tt>false</tt> otherwise.
  342. */
  343. isConnectionActive() {
  344. return this.user.getConnectionStatus() === JitsiParticipantConnectionStatus.ACTIVE;
  345. }
  346. /**
  347. * The remote video is considered "playable" once the can play event has been received. It will be allowed to
  348. * display video also in {@link JitsiParticipantConnectionStatus.INTERRUPTED} if the video has received the canplay
  349. * event and was not muted while not in ACTIVE state. This basically means that there is stalled video image cached
  350. * that could be displayed. It's used to show "grey video image" in user's thumbnail when there are connectivity
  351. * issues.
  352. *
  353. * @inheritdoc
  354. * @override
  355. */
  356. isVideoPlayable() {
  357. const connectionState = APP.conference.getParticipantConnectionStatus(this.id);
  358. return super.isVideoPlayable()
  359. && this._canPlayEventReceived
  360. && (connectionState === JitsiParticipantConnectionStatus.ACTIVE
  361. || (connectionState === JitsiParticipantConnectionStatus.INTERRUPTED && !this.mutedWhileDisconnected));
  362. }
  363. /**
  364. * @inheritDoc
  365. */
  366. updateView() {
  367. this.$container.toggleClass('audio-only', APP.conference.isAudioOnly());
  368. this.updateConnectionStatusIndicator();
  369. // This must be called after 'updateConnectionStatusIndicator' because it
  370. // affects the display mode by modifying 'mutedWhileDisconnected' flag
  371. super.updateView();
  372. }
  373. /**
  374. * Updates the UI to reflect user's connectivity status.
  375. */
  376. updateConnectionStatusIndicator() {
  377. const connectionStatus = this.user.getConnectionStatus();
  378. logger.debug(`${this.id} thumbnail connection status: ${connectionStatus}`);
  379. // FIXME rename 'mutedWhileDisconnected' to 'mutedWhileNotRendering'
  380. // Update 'mutedWhileDisconnected' flag
  381. this._figureOutMutedWhileDisconnected();
  382. this.updateConnectionStatus(connectionStatus);
  383. }
  384. /**
  385. * Removes RemoteVideo from the page.
  386. */
  387. remove() {
  388. super.remove();
  389. this.removePresenceLabel();
  390. this.removeRemoteVideoMenu();
  391. }
  392. /**
  393. *
  394. * @param {*} streamElement
  395. * @param {*} stream
  396. */
  397. waitForPlayback(streamElement, stream) {
  398. const webRtcStream = stream.getOriginalStream();
  399. const isVideo = stream.isVideoTrack();
  400. if (!isVideo || webRtcStream.id === 'mixedmslabel') {
  401. return;
  402. }
  403. streamElement.oncanplay = () => {
  404. this._canPlayEventReceived = true;
  405. this.VideoLayout.remoteVideoActive(streamElement, this.id);
  406. streamElement.oncanplay = undefined;
  407. // Refresh to show the video
  408. this.updateView();
  409. };
  410. }
  411. /**
  412. *
  413. * @param {*} stream
  414. */
  415. addRemoteStreamElement(stream) {
  416. if (!this.container) {
  417. logger.debug('Not attaching remote stream due to no container');
  418. return;
  419. }
  420. const isVideo = stream.isVideoTrack();
  421. isVideo ? this.videoStream = stream : this.audioStream = stream;
  422. if (!stream.getOriginalStream()) {
  423. logger.debug('Remote video stream has no original stream');
  424. return;
  425. }
  426. let streamElement = SmallVideo.createStreamElement(stream);
  427. // Put new stream element always in front
  428. streamElement = UIUtils.prependChild(this.container, streamElement);
  429. $(streamElement).hide();
  430. this.waitForPlayback(streamElement, stream);
  431. stream.attach(streamElement);
  432. if (!isVideo) {
  433. this._audioStreamElement = streamElement;
  434. // If the remote video menu was created before the audio stream was
  435. // attached we need to update the menu in order to show the volume
  436. // slider.
  437. this.updateRemoteVideoMenu();
  438. }
  439. }
  440. /**
  441. * Triggers re-rendering of the display name using current instance state.
  442. *
  443. * @returns {void}
  444. */
  445. updateDisplayName() {
  446. if (!this.container) {
  447. logger.warn(`Unable to set displayName - ${this.videoSpanId} does not exist`);
  448. return;
  449. }
  450. this._renderDisplayName({
  451. elementID: `${this.videoSpanId}_name`,
  452. participantID: this.id
  453. });
  454. }
  455. /**
  456. * Removes remote video menu element from video element identified by
  457. * given <tt>videoElementId</tt>.
  458. *
  459. * @param videoElementId the id of local or remote video element.
  460. */
  461. removeRemoteVideoMenu() {
  462. const menuSpan = this.$container.find('.remotevideomenu');
  463. if (menuSpan.length) {
  464. ReactDOM.unmountComponentAtNode(menuSpan.get(0));
  465. menuSpan.remove();
  466. }
  467. }
  468. /**
  469. * Mounts the {@code PresenceLabel} for displaying the participant's current
  470. * presence status.
  471. *
  472. * @return {void}
  473. */
  474. addPresenceLabel() {
  475. const presenceLabelContainer = this.container.querySelector('.presence-label-container');
  476. if (presenceLabelContainer) {
  477. ReactDOM.render(
  478. <Provider store = { APP.store }>
  479. <I18nextProvider i18n = { i18next }>
  480. <PresenceLabel
  481. participantID = { this.id }
  482. className = 'presence-label' />
  483. </I18nextProvider>
  484. </Provider>,
  485. presenceLabelContainer);
  486. }
  487. }
  488. /**
  489. * Unmounts the {@code PresenceLabel} component.
  490. *
  491. * @return {void}
  492. */
  493. removePresenceLabel() {
  494. const presenceLabelContainer = this.container.querySelector('.presence-label-container');
  495. if (presenceLabelContainer) {
  496. ReactDOM.unmountComponentAtNode(presenceLabelContainer);
  497. }
  498. }
  499. }