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

RemoteVideo.js 16KB

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