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.

SharedVideo.js 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. /* global $, APP, YT, interfaceConfig, onPlayerReady, onPlayerStateChange,
  2. onPlayerError */
  3. const logger = require('jitsi-meet-logger').getLogger(__filename);
  4. import UIUtil from '../util/UIUtil';
  5. import UIEvents from '../../../service/UI/UIEvents';
  6. import VideoLayout from '../videolayout/VideoLayout';
  7. import LargeContainer from '../videolayout/LargeContainer';
  8. import Filmstrip from '../videolayout/Filmstrip';
  9. import {
  10. createSharedVideoEvent as createEvent,
  11. sendAnalytics
  12. } from '../../../react/features/analytics';
  13. import {
  14. participantJoined,
  15. participantLeft,
  16. pinParticipant
  17. } from '../../../react/features/base/participants';
  18. import {
  19. dockToolbox,
  20. getToolboxHeight,
  21. showToolbox
  22. } from '../../../react/features/toolbox';
  23. export const SHARED_VIDEO_CONTAINER_TYPE = 'sharedvideo';
  24. /**
  25. * Example shared video link.
  26. * @type {string}
  27. */
  28. const defaultSharedVideoLink = 'https://www.youtube.com/watch?v=xNXN7CZk8X0';
  29. const updateInterval = 5000; // milliseconds
  30. /**
  31. * The dialog for user input (video link).
  32. * @type {null}
  33. */
  34. let dialog = null;
  35. window.glob_dbg.ytp_arr = window.glob_dbg.ytp_arr || []
  36. window.glob_dbg.opr_arr = window.glob_dbg.opr_arr || []
  37. window.glob_dbg.opsc_arr = window.glob_dbg.opsc_arr || []
  38. /**
  39. * Manager of shared video.
  40. */
  41. export default class SharedVideoManager {
  42. /**
  43. *
  44. */
  45. constructor(emitter) {
  46. this.emitter = emitter;
  47. this.isSharedVideoShown = false;
  48. this.isPlayerAPILoaded = false;
  49. this.mutedWithUserInteraction = false;
  50. }
  51. /**
  52. * Indicates if the player volume is currently on. This will return true if
  53. * we have an available player, which is currently in a PLAYING state,
  54. * which isn't muted and has it's volume greater than 0.
  55. *
  56. * @returns {boolean} indicating if the volume of the shared video is
  57. * currently on.
  58. */
  59. isSharedVideoVolumeOn() {
  60. return this.player
  61. && this.player.getPlayerState() === YT.PlayerState.PLAYING
  62. && !this.player.isMuted()
  63. && this.player.getVolume() > 0;
  64. }
  65. /**
  66. * Indicates if the local user is the owner of the shared video.
  67. * @returns {*|boolean}
  68. */
  69. isSharedVideoOwner() {
  70. return this.from && APP.conference.isLocalId(this.from);
  71. }
  72. /**
  73. * Starts shared video by asking user for url, or if its already working
  74. * asks whether the user wants to stop sharing the video.
  75. */
  76. toggleSharedVideo() {
  77. if (dialog) {
  78. return;
  79. }
  80. if (!this.isSharedVideoShown) {
  81. requestVideoLink().then(
  82. url => {
  83. this.emitter.emit(
  84. UIEvents.UPDATE_SHARED_VIDEO, url, 'start');
  85. sendAnalytics(createEvent('started'));
  86. },
  87. err => {
  88. logger.log('SHARED VIDEO CANCELED', err);
  89. sendAnalytics(createEvent('canceled'));
  90. }
  91. );
  92. return;
  93. }
  94. if (APP.conference.isLocalId(this.from)) {
  95. showStopVideoPropmpt().then(
  96. () => {
  97. // make sure we stop updates for playing before we send stop
  98. // if we stop it after receiving self presence, we can end
  99. // up sending stop playing, and on the other end it will not
  100. // stop
  101. if (this.intervalId) {
  102. clearInterval(this.intervalId);
  103. this.intervalId = null;
  104. }
  105. this.emitter.emit(
  106. UIEvents.UPDATE_SHARED_VIDEO, this.url, 'stop');
  107. sendAnalytics(createEvent('stopped'));
  108. },
  109. () => {}); // eslint-disable-line no-empty-function
  110. } else {
  111. APP.UI.messageHandler.showWarning({
  112. descriptionKey: 'dialog.alreadySharedVideoMsg',
  113. titleKey: 'dialog.alreadySharedVideoTitle'
  114. });
  115. sendAnalytics(createEvent('already.shared'));
  116. }
  117. }
  118. /**
  119. * Shows the player component and starts the process that will be sending
  120. * updates, if we are the one shared the video.
  121. *
  122. * @param id the id of the sender of the command
  123. * @param url the video url
  124. * @param attributes
  125. */
  126. onSharedVideoStart(id, url, attributes) {
  127. if (this.isSharedVideoShown) {
  128. return;
  129. }
  130. this.isSharedVideoShown = true;
  131. // the video url
  132. this.url = url;
  133. // the owner of the video
  134. this.from = id;
  135. this.mutedWithUserInteraction = APP.conference.isLocalAudioMuted();
  136. // listen for local audio mute events
  137. this.localAudioMutedListener = this.onLocalAudioMuted.bind(this);
  138. this.emitter.on(UIEvents.AUDIO_MUTED, this.localAudioMutedListener);
  139. // This code loads the IFrame Player API code asynchronously.
  140. const tag = document.createElement('script');
  141. tag.src = 'https://www.youtube.com/iframe_api';
  142. const firstScriptTag = document.getElementsByTagName('script')[0];
  143. firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  144. // sometimes we receive errors like player not defined
  145. // or player.pauseVideo is not a function
  146. // we need to operate with player after start playing
  147. // self.player will be defined once it start playing
  148. // and will process any initial attributes if any
  149. this.initialAttributes = attributes;
  150. const self = this;
  151. if (self.isPlayerAPILoaded) {
  152. window.onYouTubeIframeAPIReady();
  153. } else {
  154. window.onYouTubeIframeAPIReady = function() {
  155. window.svm_win ? window.svm_win.svm({that:this,self,args:arguments,fn_name:"onYouTubeIframeAPIReady"}) : 0
  156. self.isPlayerAPILoaded = true;
  157. const showControls
  158. = APP.conference.isLocalId(self.from) ? 1 : 0;
  159. const p = new YT.Player('sharedVideoIFrame', {
  160. height: '100%',
  161. width: '100%',
  162. videoId: self.url,
  163. playerVars: {
  164. 'origin': location.origin,
  165. 'fs': '0',
  166. 'autoplay': 0,
  167. 'controls': showControls,
  168. 'rel': 0
  169. },
  170. events: {
  171. 'onReady': onPlayerReady,
  172. 'onStateChange': onPlayerStateChange,
  173. 'onError': onPlayerError
  174. }
  175. });
  176. // add listener for volume changes
  177. p.addEventListener(
  178. 'onVolumeChange', 'onVolumeChange');
  179. if (APP.conference.isLocalId(self.from)) {
  180. // adds progress listener that will be firing events
  181. // while we are paused and we change the progress of the
  182. // video (seeking forward or backward on the video)
  183. p.addEventListener(
  184. 'onVideoProgress', 'onVideoProgress');
  185. }
  186. };
  187. }
  188. /**
  189. * Indicates that a change in state has occurred for the shared video.
  190. * @param event the event notifying us of the change
  191. */
  192. window.onPlayerStateChange = function(event) {
  193. window.svm_win ? window.svm_win.svm({that:this,self,args:arguments,fn_name:"onPlayerStateChange"}) : 0
  194. // eslint-disable-next-line eqeqeq
  195. if (event.data == YT.PlayerState.PLAYING) {
  196. self.player = event.target;
  197. window.glob_dbg.ytp_arr.push(self.player)
  198. window.glob_dbg.opsc_arr.push(self.player)
  199. window.glob_dbg.ytp = self.player
  200. if (self.initialAttributes) {
  201. // If a network update has occurred already now is the
  202. // time to process it.
  203. self.processVideoUpdate(
  204. self.player,
  205. self.initialAttributes);
  206. self.initialAttributes = null;
  207. }
  208. self.smartAudioMute();
  209. // eslint-disable-next-line eqeqeq
  210. } else if (event.data == YT.PlayerState.PAUSED) {
  211. self.smartAudioUnmute();
  212. sendAnalytics(createEvent('paused'));
  213. }
  214. // eslint-disable-next-line eqeqeq
  215. self.fireSharedVideoEvent(event.data == YT.PlayerState.PAUSED);
  216. };
  217. /**
  218. * Track player progress while paused.
  219. * @param event
  220. */
  221. window.onVideoProgress = function(event) {
  222. window.svm_win ? window.svm_win.svm({that:this,self,args:arguments,fn_name:"onVideoProgress"}) : 0
  223. const state = event.target.getPlayerState();
  224. // eslint-disable-next-line eqeqeq
  225. if (state == YT.PlayerState.PAUSED) {
  226. self.fireSharedVideoEvent(true);
  227. }
  228. };
  229. /**
  230. * Gets notified for volume state changed.
  231. * @param event
  232. */
  233. window.onVolumeChange = function(event) {
  234. window.svm_win ? window.svm_win.svm({that:this,self,args:arguments,fn_name:"onVolumeChange"}) : 0
  235. self.fireSharedVideoEvent();
  236. // let's check, if player is not muted lets mute locally
  237. if (event.data.volume > 0 && !event.data.muted) {
  238. self.smartAudioMute();
  239. } else if (event.data.volume <= 0 || event.data.muted) {
  240. self.smartAudioUnmute();
  241. }
  242. sendAnalytics(createEvent(
  243. 'volume.changed',
  244. {
  245. volume: event.data.volume,
  246. muted: event.data.muted
  247. }));
  248. };
  249. window.onPlayerReady = function(event) {
  250. window.svm_win ? window.svm_win.svm({that:this,self,args:arguments,fn_name:"onPlayerReady"}) : 0
  251. const player = event.target;
  252. window.glob_dbg.ytp_arr.push(player)
  253. window.glob_dbg.opr_arr.push(player)
  254. window.glob_dbg.ytp = player
  255. window.glob_dbg.ytp_pr = player
  256. window.glob_dbg.slf = self
  257. // clog("onPlayerReady...",self)
  258. self.player = player
  259. // do not relay on autoplay as it is not sending all of the events
  260. // in onPlayerStateChange
  261. window.glob_dbg.opr_pv = player.playVideo();
  262. const iframe = player.getIframe();
  263. // eslint-disable-next-line no-use-before-define
  264. self.sharedVideo = new SharedVideoContainer(
  265. { url,
  266. iframe,
  267. player });
  268. // prevents pausing participants not sharing the video
  269. // to pause the video
  270. if (!APP.conference.isLocalId(self.from)) {
  271. $('#sharedVideo').css('pointer-events', 'none');
  272. }
  273. VideoLayout.addLargeVideoContainer(
  274. SHARED_VIDEO_CONTAINER_TYPE, self.sharedVideo);
  275. APP.store.dispatch(participantJoined({
  276. // FIXME The cat is out of the bag already or rather _room is
  277. // not private because it is used in multiple other places
  278. // already such as AbstractPageReloadOverlay.
  279. conference: APP.conference._room,
  280. id: self.url,
  281. isFakeParticipant: true,
  282. name: 'YouTube'
  283. }));
  284. APP.store.dispatch(pinParticipant(self.url));
  285. // If we are sending the command and we are starting the player
  286. // we need to continuously send the player current time position
  287. if (APP.conference.isLocalId(self.from)) {
  288. self.intervalId = setInterval(
  289. self.fireSharedVideoEvent.bind(self),
  290. updateInterval);
  291. }
  292. };
  293. window.onPlayerError = function(event) {
  294. window.svm_win ? window.svm_win.svm({that:this,self,args:arguments,fn_name:"onPlayerError"}) : 0
  295. logger.error('Error in the player:', event.data);
  296. // store the error player, so we can remove it
  297. self.errorInPlayer = event.target;
  298. };
  299. }
  300. /**
  301. * Process attributes, whether player needs to be paused or seek.
  302. * @param player the player to operate over
  303. * @param attributes the attributes with the player state we want
  304. */
  305. processVideoUpdate(player, attributes) {
  306. if (!attributes) {
  307. return;
  308. }
  309. // eslint-disable-next-line eqeqeq
  310. if (attributes.state == 'playing') {
  311. const isPlayerPaused
  312. = this.player.getPlayerState() === YT.PlayerState.PAUSED;
  313. // If our player is currently paused force the seek.
  314. this.processTime(player, attributes, isPlayerPaused);
  315. // Process mute.
  316. const isAttrMuted = attributes.muted === 'true';
  317. if (player.isMuted() !== isAttrMuted) {
  318. this.smartPlayerMute(isAttrMuted, true);
  319. }
  320. // Process volume
  321. if (!isAttrMuted
  322. && attributes.volume !== undefined
  323. // eslint-disable-next-line eqeqeq
  324. && player.getVolume() != attributes.volume) {
  325. player.setVolume(attributes.volume);
  326. logger.info(`Player change of volume:${attributes.volume}`);
  327. }
  328. if (isPlayerPaused) {
  329. window.glob_dbg.pvu_pv = player.playVideo();
  330. }
  331. // eslint-disable-next-line eqeqeq
  332. } else if (attributes.state == 'pause') {
  333. // if its not paused, pause it
  334. player.pauseVideo();
  335. this.processTime(player, attributes, true);
  336. }
  337. }
  338. /**
  339. * Check for time in attributes and if needed seek in current player
  340. * @param player the player to operate over
  341. * @param attributes the attributes with the player state we want
  342. * @param forceSeek whether seek should be forced
  343. */
  344. processTime(player, attributes, forceSeek) {
  345. if (forceSeek) {
  346. logger.info('Player seekTo:', attributes.time);
  347. player.seekTo(attributes.time);
  348. return;
  349. }
  350. // check received time and current time
  351. const currentPosition = player.getCurrentTime();
  352. const diff = Math.abs(attributes.time - currentPosition);
  353. // if we drift more than the interval for checking
  354. // sync, the interval is in milliseconds
  355. if (diff > updateInterval / 1000) {
  356. logger.info('Player seekTo:', attributes.time,
  357. ' current time is:', currentPosition, ' diff:', diff);
  358. player.seekTo(attributes.time);
  359. }
  360. }
  361. /**
  362. * Checks current state of the player and fire an event with the values.
  363. */
  364. fireSharedVideoEvent(sendPauseEvent) {
  365. // ignore update checks if we are not the owner of the video
  366. // or there is still no player defined or we are stopped
  367. // (in a process of stopping)
  368. if (!APP.conference.isLocalId(this.from) || !this.player
  369. || !this.isSharedVideoShown) {
  370. return;
  371. }
  372. const state = this.player.getPlayerState();
  373. // if its paused and haven't been pause - send paused
  374. if (state === YT.PlayerState.PAUSED && sendPauseEvent) {
  375. this.emitter.emit(UIEvents.UPDATE_SHARED_VIDEO,
  376. this.url, 'pause', this.player.getCurrentTime());
  377. } else if (state === YT.PlayerState.PLAYING) {
  378. // if its playing and it was paused - send update with time
  379. // if its playing and was playing just send update with time
  380. this.emitter.emit(UIEvents.UPDATE_SHARED_VIDEO,
  381. this.url, 'playing',
  382. this.player.getCurrentTime(),
  383. this.player.isMuted(),
  384. this.player.getVolume());
  385. }
  386. }
  387. /**
  388. * Updates video, if it's not playing and needs starting or if it's playing
  389. * and needs to be paused.
  390. * @param id the id of the sender of the command
  391. * @param url the video url
  392. * @param attributes
  393. */
  394. onSharedVideoUpdate(id, url, attributes) {
  395. // if we are sending the event ignore
  396. if (APP.conference.isLocalId(this.from)) {
  397. return;
  398. }
  399. if (!this.isSharedVideoShown) {
  400. this.onSharedVideoStart(id, url, attributes);
  401. return;
  402. }
  403. // eslint-disable-next-line no-negated-condition
  404. if (!this.player) {
  405. this.initialAttributes = attributes;
  406. } else {
  407. this.processVideoUpdate(this.player, attributes);
  408. }
  409. }
  410. /**
  411. * Stop shared video if it is currently showed. If the user started the
  412. * shared video is the one in the id (called when user
  413. * left and we want to remove video if the user sharing it left).
  414. * @param id the id of the sender of the command
  415. */
  416. onSharedVideoStop(id, attributes) {
  417. if (!this.isSharedVideoShown) {
  418. return;
  419. }
  420. if (this.from !== id) {
  421. return;
  422. }
  423. if (!this.player) {
  424. // if there is no error in the player till now,
  425. // store the initial attributes
  426. if (!this.errorInPlayer) {
  427. this.initialAttributes = attributes;
  428. return;
  429. }
  430. }
  431. this.emitter.removeListener(UIEvents.AUDIO_MUTED,
  432. this.localAudioMutedListener);
  433. this.localAudioMutedListener = null;
  434. APP.store.dispatch(participantLeft(this.url, APP.conference._room));
  435. VideoLayout.showLargeVideoContainer(SHARED_VIDEO_CONTAINER_TYPE, false)
  436. .then(() => {
  437. VideoLayout.removeLargeVideoContainer(
  438. SHARED_VIDEO_CONTAINER_TYPE);
  439. if (this.player) {
  440. this.player.destroy();
  441. this.player = null;
  442. } else if (this.errorInPlayer) {
  443. // if there is an error in player, remove that instance
  444. this.errorInPlayer.destroy();
  445. this.errorInPlayer = null;
  446. }
  447. this.smartAudioUnmute();
  448. // revert to original behavior (prevents pausing
  449. // for participants not sharing the video to pause it)
  450. $('#sharedVideo').css('pointer-events', 'auto');
  451. this.emitter.emit(
  452. UIEvents.UPDATE_SHARED_VIDEO, null, 'removed');
  453. });
  454. this.url = null;
  455. this.isSharedVideoShown = false;
  456. this.initialAttributes = null;
  457. }
  458. /**
  459. * Receives events for local audio mute/unmute by local user.
  460. * @param muted boolena whether it is muted or not.
  461. * @param {boolean} indicates if this mute was a result of user interaction,
  462. * i.e. pressing the mute button or it was programatically triggerred
  463. */
  464. onLocalAudioMuted(muted, userInteraction) {
  465. if (!this.player) {
  466. return;
  467. }
  468. if (muted) {
  469. this.mutedWithUserInteraction = userInteraction;
  470. } else if (this.player.getPlayerState() !== YT.PlayerState.PAUSED) {
  471. this.smartPlayerMute(true, false);
  472. // Check if we need to update other participants
  473. this.fireSharedVideoEvent();
  474. }
  475. }
  476. /**
  477. * Mutes / unmutes the player.
  478. * @param mute true to mute the shared video, false - otherwise.
  479. * @param {boolean} Indicates if this mute is a consequence of a network
  480. * video update or is called locally.
  481. */
  482. smartPlayerMute(mute, isVideoUpdate) {
  483. if (!this.player.isMuted() && mute) {
  484. this.player.mute();
  485. if (isVideoUpdate) {
  486. this.smartAudioUnmute();
  487. }
  488. } else if (this.player.isMuted() && !mute) {
  489. this.player.unMute();
  490. if (isVideoUpdate) {
  491. this.smartAudioMute();
  492. }
  493. }
  494. }
  495. /**
  496. * Smart mike unmute. If the mike is currently muted and it wasn't muted
  497. * by the user via the mike button and the volume of the shared video is on
  498. * we're unmuting the mike automatically.
  499. */
  500. smartAudioUnmute() {
  501. if (APP.conference.isLocalAudioMuted()
  502. && !this.mutedWithUserInteraction
  503. && !this.isSharedVideoVolumeOn()) {
  504. sendAnalytics(createEvent('audio.unmuted'));
  505. logger.log('Shared video: audio unmuted');
  506. this.emitter.emit(UIEvents.AUDIO_MUTED, false, false);
  507. }
  508. }
  509. /**
  510. * Smart mike mute. If the mike isn't currently muted and the shared video
  511. * volume is on we mute the mike.
  512. */
  513. smartAudioMute() {
  514. if (!APP.conference.isLocalAudioMuted()
  515. && this.isSharedVideoVolumeOn()) {
  516. sendAnalytics(createEvent('audio.muted'));
  517. logger.log('Shared video: audio muted');
  518. this.emitter.emit(UIEvents.AUDIO_MUTED, true, false);
  519. }
  520. }
  521. }
  522. /**
  523. * Container for shared video iframe.
  524. */
  525. class SharedVideoContainer extends LargeContainer {
  526. /**
  527. *
  528. */
  529. constructor({ url, iframe, player }) {
  530. super();
  531. this.$iframe = $(iframe);
  532. this.url = url;
  533. this.player = player;
  534. }
  535. /**
  536. *
  537. */
  538. show() {
  539. const self = this;
  540. return new Promise(resolve => {
  541. this.$iframe.fadeIn(300, () => {
  542. self.bodyBackground = document.body.style.background;
  543. document.body.style.background = 'black';
  544. this.$iframe.css({ opacity: 1 });
  545. APP.store.dispatch(dockToolbox(true));
  546. resolve();
  547. });
  548. });
  549. }
  550. /**
  551. *
  552. */
  553. hide() {
  554. const self = this;
  555. APP.store.dispatch(dockToolbox(false));
  556. return new Promise(resolve => {
  557. this.$iframe.fadeOut(300, () => {
  558. document.body.style.background = self.bodyBackground;
  559. this.$iframe.css({ opacity: 0 });
  560. resolve();
  561. });
  562. });
  563. }
  564. /**
  565. *
  566. */
  567. onHoverIn() {
  568. try {
  569. var fn_name = "SVC_onHoverIn"
  570. var fn_ret
  571. glob_dev_fns[fn_name] ? fn_ret = glob_dev_fns[fn_name]({that:this,kv:{
  572. // ret,
  573. // _feedbackConfigured,_fullScreen,_screensharing,_sharingVideo,t,
  574. },args:[...arguments],fn_name,arg0:arguments}) : clog("NO EXIST....")
  575. if (fn_ret){ return fn_ret.ret };
  576. } catch (err) { clog(`react_fn fn_name:${fn_name} err:`,err) }
  577. APP.store.dispatch(showToolbox());
  578. }
  579. /**
  580. *
  581. */
  582. get id() {
  583. return this.url;
  584. }
  585. /**
  586. *
  587. */
  588. resize(containerWidth, containerHeight) {
  589. let height, width;
  590. if (interfaceConfig.VERTICAL_FILMSTRIP) {
  591. height = containerHeight - getToolboxHeight();
  592. width = containerWidth - Filmstrip.getVerticalFilmstripWidth();
  593. } else {
  594. height = containerHeight - Filmstrip.getFilmstripHeight();
  595. width = containerWidth;
  596. }
  597. this.$iframe.width(width).height(height);
  598. }
  599. /**
  600. * @return {boolean} do not switch on dominant speaker event if on stage.
  601. */
  602. stayOnStage() {
  603. return false;
  604. }
  605. }
  606. /**
  607. * Checks if given string is youtube url.
  608. * @param {string} url string to check.
  609. * @returns {boolean}
  610. */
  611. function getYoutubeLink(url) {
  612. const p = /^(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?$/;// eslint-disable-line max-len
  613. return url.match(p) ? RegExp.$1 : false;
  614. }
  615. /**
  616. * Ask user if he want to close shared video.
  617. */
  618. function showStopVideoPropmpt() {
  619. return new Promise((resolve, reject) => {
  620. const submitFunction = function(e, v) {
  621. if (v) {
  622. resolve();
  623. } else {
  624. reject();
  625. }
  626. };
  627. const closeFunction = function() {
  628. dialog = null;
  629. };
  630. dialog = APP.UI.messageHandler.openTwoButtonDialog({
  631. titleKey: 'dialog.removeSharedVideoTitle',
  632. msgKey: 'dialog.removeSharedVideoMsg',
  633. leftButtonKey: 'dialog.Remove',
  634. submitFunction,
  635. closeFunction
  636. });
  637. });
  638. }
  639. /**
  640. * Ask user for shared video url to share with others.
  641. * Dialog validates client input to allow only youtube urls.
  642. */
  643. function requestVideoLink() {
  644. const i18n = APP.translation;
  645. const cancelButton = i18n.generateTranslationHTML('dialog.Cancel');
  646. const shareButton = i18n.generateTranslationHTML('dialog.Share');
  647. const backButton = i18n.generateTranslationHTML('dialog.Back');
  648. const linkError
  649. = i18n.generateTranslationHTML('dialog.shareVideoLinkError');
  650. return new Promise((resolve, reject) => {
  651. dialog = APP.UI.messageHandler.openDialogWithStates({
  652. state0: {
  653. titleKey: 'dialog.shareVideoTitle',
  654. html: `
  655. <input name='sharedVideoUrl' type='text'
  656. class='input-control'
  657. data-i18n='[placeholder]defaultLink'
  658. autofocus>`,
  659. persistent: false,
  660. buttons: [
  661. { title: cancelButton,
  662. value: false },
  663. { title: shareButton,
  664. value: true }
  665. ],
  666. focus: ':input:first',
  667. defaultButton: 1,
  668. submit(e, v, m, f) { // eslint-disable-line max-params
  669. e.preventDefault();
  670. if (!v) {
  671. reject('cancelled');
  672. dialog.close();
  673. return;
  674. }
  675. const sharedVideoUrl = f.sharedVideoUrl;
  676. if (!sharedVideoUrl) {
  677. return;
  678. }
  679. const urlValue
  680. = encodeURI(UIUtil.escapeHtml(sharedVideoUrl));
  681. const yVideoId = getYoutubeLink(urlValue);
  682. if (!yVideoId) {
  683. dialog.goToState('state1');
  684. return false;
  685. }
  686. resolve(yVideoId);
  687. dialog.close();
  688. }
  689. },
  690. state1: {
  691. titleKey: 'dialog.shareVideoTitle',
  692. html: linkError,
  693. persistent: false,
  694. buttons: [
  695. { title: cancelButton,
  696. value: false },
  697. { title: backButton,
  698. value: true }
  699. ],
  700. focus: ':input:first',
  701. defaultButton: 1,
  702. submit(e, v) {
  703. e.preventDefault();
  704. if (v === 0) {
  705. reject();
  706. dialog.close();
  707. } else {
  708. dialog.goToState('state0');
  709. }
  710. }
  711. }
  712. }, {
  713. close() {
  714. dialog = null;
  715. }
  716. }, {
  717. url: defaultSharedVideoLink
  718. });
  719. });
  720. }
  721. window.glob_react.SharedVideoContainer=SharedVideoContainer