您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

RemoteVideo.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. // Bind event handlers so they are only bound once for every instance.
  89. // TODO The event handlers should be turned into actions so changes can be
  90. // handled through reducers and middleware.
  91. this._requestRemoteControlPermissions
  92. = this._requestRemoteControlPermissions.bind(this);
  93. this._setAudioVolume = this._setAudioVolume.bind(this);
  94. this._stopRemoteControl = this._stopRemoteControl.bind(this);
  95. this.container.onclick = this._onContainerClick;
  96. }
  97. /**
  98. *
  99. */
  100. addRemoteVideoContainer() {
  101. this.container = createContainer(this.videoSpanId);
  102. this.$container = $(this.container);
  103. this.initializeAvatar();
  104. this._setThumbnailSize();
  105. this.initBrowserSpecificProperties();
  106. this.updateRemoteVideoMenu();
  107. this.updateStatusBar();
  108. this.addAudioLevelIndicator();
  109. this.addPresenceLabel();
  110. return this.container;
  111. }
  112. /**
  113. * Generates the popup menu content.
  114. *
  115. * @returns {Element|*} the constructed element, containing popup menu items
  116. * @private
  117. */
  118. _generatePopupContent() {
  119. if (interfaceConfig.filmStripOnly) {
  120. return;
  121. }
  122. const remoteVideoMenuContainer
  123. = this.container.querySelector('.remotevideomenu');
  124. if (!remoteVideoMenuContainer) {
  125. return;
  126. }
  127. const { controller } = APP.remoteControl;
  128. let remoteControlState = null;
  129. let onRemoteControlToggle;
  130. if (this._supportsRemoteControl
  131. && ((!APP.remoteControl.active && !this._isRemoteControlSessionActive)
  132. || APP.remoteControl.controller.activeParticipant === this.id)) {
  133. if (controller.getRequestedParticipant() === this.id) {
  134. remoteControlState = REMOTE_CONTROL_MENU_STATES.REQUESTING;
  135. } else if (controller.isStarted()) {
  136. onRemoteControlToggle = this._stopRemoteControl;
  137. remoteControlState = REMOTE_CONTROL_MENU_STATES.STARTED;
  138. } else {
  139. onRemoteControlToggle = this._requestRemoteControlPermissions;
  140. remoteControlState = REMOTE_CONTROL_MENU_STATES.NOT_STARTED;
  141. }
  142. }
  143. const initialVolumeValue = this._audioStreamElement && this._audioStreamElement.volume;
  144. // hide volume when in silent mode
  145. const onVolumeChange
  146. = APP.store.getState()['features/base/config'].startSilent ? undefined : this._setAudioVolume;
  147. const participantID = this.id;
  148. const currentLayout = getCurrentLayout(APP.store.getState());
  149. let remoteMenuPosition;
  150. if (currentLayout === LAYOUTS.TILE_VIEW) {
  151. remoteMenuPosition = 'left top';
  152. } else if (currentLayout === LAYOUTS.VERTICAL_FILMSTRIP_VIEW) {
  153. remoteMenuPosition = 'left bottom';
  154. } else {
  155. remoteMenuPosition = 'top center';
  156. }
  157. ReactDOM.render(
  158. <Provider store = { APP.store }>
  159. <I18nextProvider i18n = { i18next }>
  160. <AtlasKitThemeProvider mode = 'dark'>
  161. <RemoteVideoMenuTriggerButton
  162. initialVolumeValue = { initialVolumeValue }
  163. menuPosition = { remoteMenuPosition }
  164. onMenuDisplay
  165. = {this._onRemoteVideoMenuDisplay.bind(this)}
  166. onRemoteControlToggle = { onRemoteControlToggle }
  167. onVolumeChange = { onVolumeChange }
  168. participantID = { participantID }
  169. remoteControlState = { remoteControlState } />
  170. </AtlasKitThemeProvider>
  171. </I18nextProvider>
  172. </Provider>,
  173. remoteVideoMenuContainer);
  174. }
  175. /**
  176. *
  177. */
  178. _onRemoteVideoMenuDisplay() {
  179. this.updateRemoteVideoMenu();
  180. }
  181. /**
  182. * Sets the remote control active status for the remote video.
  183. *
  184. * @param {boolean} isActive - The new remote control active status.
  185. * @returns {void}
  186. */
  187. setRemoteControlActiveStatus(isActive) {
  188. this._isRemoteControlSessionActive = isActive;
  189. this.updateRemoteVideoMenu();
  190. }
  191. /**
  192. * Sets the remote control supported value and initializes or updates the menu
  193. * depending on the remote control is supported or not.
  194. * @param {boolean} isSupported
  195. */
  196. setRemoteControlSupport(isSupported = false) {
  197. if (this._supportsRemoteControl === isSupported) {
  198. return;
  199. }
  200. this._supportsRemoteControl = isSupported;
  201. this.updateRemoteVideoMenu();
  202. }
  203. /**
  204. * Requests permissions for remote control session.
  205. */
  206. _requestRemoteControlPermissions() {
  207. APP.remoteControl.controller.requestPermissions(this.id, this.VideoLayout.getLargeVideoWrapper())
  208. .then(result => {
  209. if (result === null) {
  210. return;
  211. }
  212. this.updateRemoteVideoMenu();
  213. APP.UI.messageHandler.notify(
  214. 'dialog.remoteControlTitle',
  215. result === false ? 'dialog.remoteControlDeniedMessage' : 'dialog.remoteControlAllowedMessage',
  216. { user: this.user.getDisplayName() || interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME }
  217. );
  218. if (result === true) {
  219. // the remote control permissions has been granted
  220. // pin the controlled participant
  221. const pinnedParticipant = getPinnedParticipant(APP.store.getState()) || {};
  222. const pinnedId = pinnedParticipant.id;
  223. if (pinnedId !== this.id) {
  224. APP.store.dispatch(pinParticipant(this.id));
  225. }
  226. }
  227. }, error => {
  228. logger.error(error);
  229. this.updateRemoteVideoMenu();
  230. APP.UI.messageHandler.notify(
  231. 'dialog.remoteControlTitle',
  232. 'dialog.remoteControlErrorMessage',
  233. { user: this.user.getDisplayName() || interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME }
  234. );
  235. });
  236. this.updateRemoteVideoMenu();
  237. }
  238. /**
  239. * Stops remote control session.
  240. */
  241. _stopRemoteControl() {
  242. // send message about stopping
  243. APP.remoteControl.controller.stop();
  244. this.updateRemoteVideoMenu();
  245. }
  246. /**
  247. * Change the remote participant's volume level.
  248. *
  249. * @param {int} newVal - The value to set the slider to.
  250. */
  251. _setAudioVolume(newVal) {
  252. if (this._audioStreamElement) {
  253. this._audioStreamElement.volume = newVal;
  254. }
  255. }
  256. /**
  257. * Updates the remote video menu.
  258. */
  259. updateRemoteVideoMenu() {
  260. this._generatePopupContent();
  261. }
  262. /**
  263. * Removes the remote stream element corresponding to the given stream and
  264. * parent container.
  265. *
  266. * @param stream the MediaStream
  267. * @param isVideo <tt>true</tt> if given <tt>stream</tt> is a video one.
  268. */
  269. removeRemoteStreamElement(stream) {
  270. if (!this.container) {
  271. return false;
  272. }
  273. const isVideo = stream.isVideoTrack();
  274. const elementID = SmallVideo.getStreamElementID(stream);
  275. const select = $(`#${elementID}`);
  276. select.remove();
  277. if (isVideo) {
  278. this._canPlayEventReceived = false;
  279. }
  280. logger.info(`${isVideo ? 'Video' : 'Audio'} removed ${this.id}`, select);
  281. if (stream === this.videoStream) {
  282. this.videoStream = null;
  283. }
  284. this.updateView();
  285. }
  286. /**
  287. * The remote video is considered "playable" once the can play event has been received. It will be allowed to
  288. * display video also in {@link JitsiParticipantConnectionStatus.INTERRUPTED} if the video has received the canplay
  289. * event and was not muted while not in ACTIVE state. This basically means that there is stalled video image cached
  290. * that could be displayed. It's used to show "grey video image" in user's thumbnail when there are connectivity
  291. * issues.
  292. *
  293. * @inheritdoc
  294. * @override
  295. */
  296. isVideoPlayable() {
  297. const participant = getParticipantById(APP.store.getState(), this.id);
  298. const { connectionStatus, mutedWhileDisconnected } = participant || {};
  299. return super.isVideoPlayable()
  300. && this._canPlayEventReceived
  301. && (connectionStatus === JitsiParticipantConnectionStatus.ACTIVE
  302. || (connectionStatus === JitsiParticipantConnectionStatus.INTERRUPTED && !mutedWhileDisconnected));
  303. }
  304. /**
  305. * @inheritDoc
  306. */
  307. updateView() {
  308. this.$container.toggleClass('audio-only', APP.conference.isAudioOnly());
  309. super.updateView();
  310. }
  311. /**
  312. * Removes RemoteVideo from the page.
  313. */
  314. remove() {
  315. super.remove();
  316. this.removePresenceLabel();
  317. this.removeRemoteVideoMenu();
  318. }
  319. /**
  320. *
  321. * @param {*} streamElement
  322. * @param {*} stream
  323. */
  324. waitForPlayback(streamElement, stream) {
  325. const webRtcStream = stream.getOriginalStream();
  326. const isVideo = stream.isVideoTrack();
  327. if (!isVideo || webRtcStream.id === 'mixedmslabel') {
  328. return;
  329. }
  330. const listener = () => {
  331. this._canPlayEventReceived = true;
  332. this.VideoLayout.remoteVideoActive(streamElement, this.id);
  333. streamElement.removeEventListener('canplay', listener);
  334. // Refresh to show the video
  335. this.updateView();
  336. };
  337. streamElement.addEventListener('canplay', listener);
  338. }
  339. /**
  340. *
  341. * @param {*} stream
  342. */
  343. addRemoteStreamElement(stream) {
  344. if (!this.container) {
  345. logger.debug('Not attaching remote stream due to no container');
  346. return;
  347. }
  348. const isVideo = stream.isVideoTrack();
  349. if (isVideo) {
  350. this.videoStream = stream;
  351. } else {
  352. this.audioStream = stream;
  353. }
  354. if (!stream.getOriginalStream()) {
  355. logger.debug('Remote video stream has no original stream');
  356. return;
  357. }
  358. let streamElement = SmallVideo.createStreamElement(stream);
  359. // Put new stream element always in front
  360. streamElement = UIUtils.prependChild(this.container, streamElement);
  361. $(streamElement).hide();
  362. this.waitForPlayback(streamElement, stream);
  363. stream.attach(streamElement);
  364. if (!isVideo) {
  365. this._audioStreamElement = streamElement;
  366. // If the remote video menu was created before the audio stream was
  367. // attached we need to update the menu in order to show the volume
  368. // slider.
  369. this.updateRemoteVideoMenu();
  370. }
  371. }
  372. /**
  373. * Triggers re-rendering of the display name using current instance state.
  374. *
  375. * @returns {void}
  376. */
  377. updateDisplayName() {
  378. if (!this.container) {
  379. logger.warn(`Unable to set displayName - ${this.videoSpanId} does not exist`);
  380. return;
  381. }
  382. this._renderDisplayName({
  383. elementID: `${this.videoSpanId}_name`,
  384. participantID: this.id
  385. });
  386. }
  387. /**
  388. * Removes remote video menu element from video element identified by
  389. * given <tt>videoElementId</tt>.
  390. *
  391. * @param videoElementId the id of local or remote video element.
  392. */
  393. removeRemoteVideoMenu() {
  394. const menuSpan = this.$container.find('.remotevideomenu');
  395. if (menuSpan.length) {
  396. ReactDOM.unmountComponentAtNode(menuSpan.get(0));
  397. menuSpan.remove();
  398. }
  399. }
  400. /**
  401. * Mounts the {@code PresenceLabel} for displaying the participant's current
  402. * presence status.
  403. *
  404. * @return {void}
  405. */
  406. addPresenceLabel() {
  407. const presenceLabelContainer = this.container.querySelector('.presence-label-container');
  408. if (presenceLabelContainer) {
  409. ReactDOM.render(
  410. <Provider store = { APP.store }>
  411. <I18nextProvider i18n = { i18next }>
  412. <PresenceLabel
  413. participantID = { this.id }
  414. className = 'presence-label' />
  415. </I18nextProvider>
  416. </Provider>,
  417. presenceLabelContainer);
  418. }
  419. }
  420. /**
  421. * Unmounts the {@code PresenceLabel} component.
  422. *
  423. * @return {void}
  424. */
  425. removePresenceLabel() {
  426. const presenceLabelContainer = this.container.querySelector('.presence-label-container');
  427. if (presenceLabelContainer) {
  428. ReactDOM.unmountComponentAtNode(presenceLabelContainer);
  429. }
  430. }
  431. }