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.

RemoteVideo.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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 {
  14. getParticipantById,
  15. getPinnedParticipant,
  16. pinParticipant
  17. } from '../../../react/features/base/participants';
  18. import { isTestModeEnabled } from '../../../react/features/base/testing';
  19. import { updateLastTrackVideoMediaEvent } 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. * List of container events that we are going to process, will be added as listener to the
  32. * container for every event in the list. The latest event will be stored in redux.
  33. */
  34. const containerEvents = [
  35. 'abort', 'canplay', 'canplaythrough', 'emptied', 'ended', 'error', 'loadeddata', 'loadedmetadata', 'loadstart',
  36. 'pause', 'play', 'playing', 'ratechange', 'stalled', 'suspend', 'waiting'
  37. ];
  38. /**
  39. *
  40. * @param {*} spanId
  41. */
  42. function createContainer(spanId) {
  43. const container = document.createElement('span');
  44. container.id = spanId;
  45. container.className = 'videocontainer';
  46. container.innerHTML = `
  47. <div class = 'videocontainer__background'></div>
  48. <div class = 'videocontainer__toptoolbar'></div>
  49. <div class = 'videocontainer__toolbar'></div>
  50. <div class = 'videocontainer__hoverOverlay'></div>
  51. <div class = 'displayNameContainer'></div>
  52. <div class = 'avatar-container'></div>
  53. <div class ='presence-label-container'></div>
  54. <span class = 'remotevideomenu'></span>`;
  55. const remoteVideosContainer
  56. = document.getElementById('filmstripRemoteVideosContainer');
  57. const localVideoContainer
  58. = document.getElementById('localVideoTileViewContainer');
  59. remoteVideosContainer.insertBefore(container, localVideoContainer);
  60. return container;
  61. }
  62. /**
  63. *
  64. */
  65. export default class RemoteVideo extends SmallVideo {
  66. /**
  67. * Creates new instance of the <tt>RemoteVideo</tt>.
  68. * @param user {JitsiParticipant} the user for whom remote video instance will
  69. * be created.
  70. * @param {VideoLayout} VideoLayout the video layout instance.
  71. * @constructor
  72. */
  73. constructor(user, VideoLayout) {
  74. super(VideoLayout);
  75. this.user = user;
  76. this.id = user.getId();
  77. this.videoSpanId = `participant_${this.id}`;
  78. this._audioStreamElement = null;
  79. this._supportsRemoteControl = false;
  80. this.statsPopoverLocation = interfaceConfig.VERTICAL_FILMSTRIP ? 'left bottom' : 'top center';
  81. this.addRemoteVideoContainer();
  82. this.updateIndicators();
  83. this.updateDisplayName();
  84. this.bindHoverHandler();
  85. this.flipX = false;
  86. this.isLocal = false;
  87. this._isRemoteControlSessionActive = false;
  88. /**
  89. * The flag is set to <tt>true</tt> after the 'canplay' event has been
  90. * triggered on the current video element. It goes back to <tt>false</tt>
  91. * when the stream is removed. It is used to determine whether the video
  92. * playback has ever started.
  93. * @type {boolean}
  94. */
  95. this._canPlayEventReceived = false;
  96. // Bind event handlers so they are only bound once for every instance.
  97. // TODO The event handlers should be turned into actions so changes can be
  98. // handled through reducers and middleware.
  99. this._requestRemoteControlPermissions
  100. = this._requestRemoteControlPermissions.bind(this);
  101. this._setAudioVolume = this._setAudioVolume.bind(this);
  102. this._stopRemoteControl = this._stopRemoteControl.bind(this);
  103. this.container.onclick = this._onContainerClick;
  104. }
  105. /**
  106. *
  107. */
  108. addRemoteVideoContainer() {
  109. this.container = createContainer(this.videoSpanId);
  110. this.$container = $(this.container);
  111. this.initializeAvatar();
  112. this._setThumbnailSize();
  113. this.initBrowserSpecificProperties();
  114. this.updateRemoteVideoMenu();
  115. this.updateStatusBar();
  116. this.addAudioLevelIndicator();
  117. this.addPresenceLabel();
  118. return this.container;
  119. }
  120. /**
  121. * Generates the popup menu content.
  122. *
  123. * @returns {Element|*} the constructed element, containing popup menu items
  124. * @private
  125. */
  126. _generatePopupContent() {
  127. const remoteVideoMenuContainer
  128. = this.container.querySelector('.remotevideomenu');
  129. if (!remoteVideoMenuContainer) {
  130. return;
  131. }
  132. const { controller } = APP.remoteControl;
  133. let remoteControlState = null;
  134. let onRemoteControlToggle;
  135. if (this._supportsRemoteControl
  136. && ((!APP.remoteControl.active && !this._isRemoteControlSessionActive)
  137. || APP.remoteControl.controller.activeParticipant === this.id)) {
  138. if (controller.getRequestedParticipant() === this.id) {
  139. remoteControlState = REMOTE_CONTROL_MENU_STATES.REQUESTING;
  140. } else if (controller.isStarted()) {
  141. onRemoteControlToggle = this._stopRemoteControl;
  142. remoteControlState = REMOTE_CONTROL_MENU_STATES.STARTED;
  143. } else {
  144. onRemoteControlToggle = this._requestRemoteControlPermissions;
  145. remoteControlState = REMOTE_CONTROL_MENU_STATES.NOT_STARTED;
  146. }
  147. }
  148. const initialVolumeValue = this._audioStreamElement && this._audioStreamElement.volume;
  149. // hide volume when in silent mode
  150. const onVolumeChange
  151. = APP.store.getState()['features/base/config'].startSilent ? undefined : this._setAudioVolume;
  152. const participantID = this.id;
  153. const currentLayout = getCurrentLayout(APP.store.getState());
  154. let remoteMenuPosition;
  155. if (currentLayout === LAYOUTS.TILE_VIEW) {
  156. remoteMenuPosition = 'left top';
  157. } else if (currentLayout === LAYOUTS.VERTICAL_FILMSTRIP_VIEW) {
  158. remoteMenuPosition = 'left bottom';
  159. } else {
  160. remoteMenuPosition = 'top center';
  161. }
  162. ReactDOM.render(
  163. <Provider store = { APP.store }>
  164. <I18nextProvider i18n = { i18next }>
  165. <AtlasKitThemeProvider mode = 'dark'>
  166. <RemoteVideoMenuTriggerButton
  167. initialVolumeValue = { initialVolumeValue }
  168. menuPosition = { remoteMenuPosition }
  169. onMenuDisplay
  170. = {this._onRemoteVideoMenuDisplay.bind(this)}
  171. onRemoteControlToggle = { onRemoteControlToggle }
  172. onVolumeChange = { onVolumeChange }
  173. participantID = { participantID }
  174. remoteControlState = { remoteControlState } />
  175. </AtlasKitThemeProvider>
  176. </I18nextProvider>
  177. </Provider>,
  178. remoteVideoMenuContainer);
  179. }
  180. /**
  181. *
  182. */
  183. _onRemoteVideoMenuDisplay() {
  184. this.updateRemoteVideoMenu();
  185. }
  186. /**
  187. * Sets the remote control active status for the remote video.
  188. *
  189. * @param {boolean} isActive - The new remote control active status.
  190. * @returns {void}
  191. */
  192. setRemoteControlActiveStatus(isActive) {
  193. this._isRemoteControlSessionActive = isActive;
  194. this.updateRemoteVideoMenu();
  195. }
  196. /**
  197. * Sets the remote control supported value and initializes or updates the menu
  198. * depending on the remote control is supported or not.
  199. * @param {boolean} isSupported
  200. */
  201. setRemoteControlSupport(isSupported = false) {
  202. if (this._supportsRemoteControl === isSupported) {
  203. return;
  204. }
  205. this._supportsRemoteControl = isSupported;
  206. this.updateRemoteVideoMenu();
  207. }
  208. /**
  209. * Requests permissions for remote control session.
  210. */
  211. _requestRemoteControlPermissions() {
  212. APP.remoteControl.controller.requestPermissions(this.id, this.VideoLayout.getLargeVideoWrapper())
  213. .then(result => {
  214. if (result === null) {
  215. return;
  216. }
  217. this.updateRemoteVideoMenu();
  218. APP.UI.messageHandler.notify(
  219. 'dialog.remoteControlTitle',
  220. result === false ? 'dialog.remoteControlDeniedMessage' : 'dialog.remoteControlAllowedMessage',
  221. { user: this.user.getDisplayName() || interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME }
  222. );
  223. if (result === true) {
  224. // the remote control permissions has been granted
  225. // pin the controlled participant
  226. const pinnedParticipant = getPinnedParticipant(APP.store.getState()) || {};
  227. const pinnedId = pinnedParticipant.id;
  228. if (pinnedId !== this.id) {
  229. APP.store.dispatch(pinParticipant(this.id));
  230. }
  231. }
  232. }, error => {
  233. logger.error(error);
  234. this.updateRemoteVideoMenu();
  235. APP.UI.messageHandler.notify(
  236. 'dialog.remoteControlTitle',
  237. 'dialog.remoteControlErrorMessage',
  238. { user: this.user.getDisplayName() || interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME }
  239. );
  240. });
  241. this.updateRemoteVideoMenu();
  242. }
  243. /**
  244. * Stops remote control session.
  245. */
  246. _stopRemoteControl() {
  247. // send message about stopping
  248. APP.remoteControl.controller.stop();
  249. this.updateRemoteVideoMenu();
  250. }
  251. /**
  252. * Change the remote participant's volume level.
  253. *
  254. * @param {int} newVal - The value to set the slider to.
  255. */
  256. _setAudioVolume(newVal) {
  257. if (this._audioStreamElement) {
  258. this._audioStreamElement.volume = newVal;
  259. }
  260. }
  261. /**
  262. * Updates the remote video menu.
  263. */
  264. updateRemoteVideoMenu() {
  265. this._generatePopupContent();
  266. }
  267. /**
  268. * Removes the remote stream element corresponding to the given stream and
  269. * parent container.
  270. *
  271. * @param stream the MediaStream
  272. * @param isVideo <tt>true</tt> if given <tt>stream</tt> is a video one.
  273. */
  274. removeRemoteStreamElement(stream) {
  275. if (!this.container) {
  276. return false;
  277. }
  278. const isVideo = stream.isVideoTrack();
  279. const elementID = SmallVideo.getStreamElementID(stream);
  280. const select = $(`#${elementID}`);
  281. select.remove();
  282. if (isVideo) {
  283. this._canPlayEventReceived = false;
  284. }
  285. logger.info(`${isVideo ? 'Video' : 'Audio'} removed ${this.id}`, select);
  286. if (stream === this.videoStream) {
  287. this.videoStream = null;
  288. }
  289. this.updateView();
  290. }
  291. /**
  292. * The remote video is considered "playable" once the can play event has been received.
  293. *
  294. * @inheritdoc
  295. * @override
  296. */
  297. isVideoPlayable() {
  298. const participant = getParticipantById(APP.store.getState(), this.id);
  299. const { connectionStatus } = participant || {};
  300. return (
  301. super.isVideoPlayable()
  302. && this._canPlayEventReceived
  303. && connectionStatus === JitsiParticipantConnectionStatus.ACTIVE
  304. );
  305. }
  306. /**
  307. * @inheritDoc
  308. */
  309. updateView() {
  310. this.$container.toggleClass('audio-only', APP.conference.isAudioOnly());
  311. super.updateView();
  312. }
  313. /**
  314. * Removes RemoteVideo from the page.
  315. */
  316. remove() {
  317. super.remove();
  318. this.removePresenceLabel();
  319. this.removeRemoteVideoMenu();
  320. }
  321. /**
  322. *
  323. * @param {*} streamElement
  324. * @param {*} stream
  325. */
  326. waitForPlayback(streamElement, stream) {
  327. $(streamElement).hide();
  328. const webRtcStream = stream.getOriginalStream();
  329. const isVideo = stream.isVideoTrack();
  330. if (!isVideo || webRtcStream.id === 'mixedmslabel') {
  331. return;
  332. }
  333. const listener = () => {
  334. this._canPlayEventReceived = true;
  335. logger.info(`${this.id} video is now active`, streamElement);
  336. if (streamElement) {
  337. $(streamElement).show();
  338. }
  339. streamElement.removeEventListener('canplay', listener);
  340. // Refresh to show the video
  341. this.updateView();
  342. };
  343. streamElement.addEventListener('canplay', listener);
  344. }
  345. /**
  346. *
  347. * @param {*} stream
  348. */
  349. addRemoteStreamElement(stream) {
  350. if (!this.container) {
  351. logger.debug('Not attaching remote stream due to no container');
  352. return;
  353. }
  354. const isVideo = stream.isVideoTrack();
  355. if (isVideo) {
  356. this.videoStream = stream;
  357. } else {
  358. this.audioStream = stream;
  359. }
  360. if (!stream.getOriginalStream()) {
  361. logger.debug('Remote video stream has no original stream');
  362. return;
  363. }
  364. let streamElement = SmallVideo.createStreamElement(stream);
  365. // Put new stream element always in front
  366. streamElement = UIUtils.prependChild(this.container, streamElement);
  367. this.waitForPlayback(streamElement, stream);
  368. stream.attach(streamElement);
  369. if (!isVideo) {
  370. this._audioStreamElement = streamElement;
  371. // If the remote video menu was created before the audio stream was
  372. // attached we need to update the menu in order to show the volume
  373. // slider.
  374. this.updateRemoteVideoMenu();
  375. } else if (isTestModeEnabled(APP.store.getState())) {
  376. const cb = name => APP.store.dispatch(updateLastTrackVideoMediaEvent(stream, name));
  377. containerEvents.forEach(event => {
  378. streamElement.addEventListener(event, cb.bind(this, event));
  379. });
  380. }
  381. }
  382. /**
  383. * Triggers re-rendering of the display name using current instance state.
  384. *
  385. * @returns {void}
  386. */
  387. updateDisplayName() {
  388. if (!this.container) {
  389. logger.warn(`Unable to set displayName - ${this.videoSpanId} does not exist`);
  390. return;
  391. }
  392. this._renderDisplayName({
  393. elementID: `${this.videoSpanId}_name`,
  394. participantID: this.id
  395. });
  396. }
  397. /**
  398. * Removes remote video menu element from video element identified by
  399. * given <tt>videoElementId</tt>.
  400. *
  401. * @param videoElementId the id of local or remote video element.
  402. */
  403. removeRemoteVideoMenu() {
  404. const menuSpan = this.$container.find('.remotevideomenu');
  405. if (menuSpan.length) {
  406. ReactDOM.unmountComponentAtNode(menuSpan.get(0));
  407. menuSpan.remove();
  408. }
  409. }
  410. /**
  411. * Mounts the {@code PresenceLabel} for displaying the participant's current
  412. * presence status.
  413. *
  414. * @return {void}
  415. */
  416. addPresenceLabel() {
  417. const presenceLabelContainer = this.container.querySelector('.presence-label-container');
  418. if (presenceLabelContainer) {
  419. ReactDOM.render(
  420. <Provider store = { APP.store }>
  421. <I18nextProvider i18n = { i18next }>
  422. <PresenceLabel
  423. participantID = { this.id }
  424. className = 'presence-label' />
  425. </I18nextProvider>
  426. </Provider>,
  427. presenceLabelContainer);
  428. }
  429. }
  430. /**
  431. * Unmounts the {@code PresenceLabel} component.
  432. *
  433. * @return {void}
  434. */
  435. removePresenceLabel() {
  436. const presenceLabelContainer = this.container.querySelector('.presence-label-container');
  437. if (presenceLabelContainer) {
  438. ReactDOM.unmountComponentAtNode(presenceLabelContainer);
  439. }
  440. }
  441. }