Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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