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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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. // do not relay on autoplay as it is not sending all of the events
  257. // in onPlayerStateChange
  258. window.glob_dbg.opr_pv = player.playVideo();
  259. const iframe = player.getIframe();
  260. // eslint-disable-next-line no-use-before-define
  261. self.sharedVideo = new SharedVideoContainer(
  262. { url,
  263. iframe,
  264. player });
  265. // prevents pausing participants not sharing the video
  266. // to pause the video
  267. if (!APP.conference.isLocalId(self.from)) {
  268. $('#sharedVideo').css('pointer-events', 'none');
  269. }
  270. VideoLayout.addLargeVideoContainer(
  271. SHARED_VIDEO_CONTAINER_TYPE, self.sharedVideo);
  272. APP.store.dispatch(participantJoined({
  273. // FIXME The cat is out of the bag already or rather _room is
  274. // not private because it is used in multiple other places
  275. // already such as AbstractPageReloadOverlay.
  276. conference: APP.conference._room,
  277. id: self.url,
  278. isFakeParticipant: true,
  279. name: 'YouTube'
  280. }));
  281. APP.store.dispatch(pinParticipant(self.url));
  282. // If we are sending the command and we are starting the player
  283. // we need to continuously send the player current time position
  284. if (APP.conference.isLocalId(self.from)) {
  285. self.intervalId = setInterval(
  286. self.fireSharedVideoEvent.bind(self),
  287. updateInterval);
  288. }
  289. };
  290. window.onPlayerError = function(event) {
  291. window.svm_win ? window.svm_win.svm({that:this,self,args:arguments,fn_name:"onPlayerError"}) : 0
  292. logger.error('Error in the player:', event.data);
  293. // store the error player, so we can remove it
  294. self.errorInPlayer = event.target;
  295. };
  296. }
  297. /**
  298. * Process attributes, whether player needs to be paused or seek.
  299. * @param player the player to operate over
  300. * @param attributes the attributes with the player state we want
  301. */
  302. processVideoUpdate(player, attributes) {
  303. if (!attributes) {
  304. return;
  305. }
  306. // eslint-disable-next-line eqeqeq
  307. if (attributes.state == 'playing') {
  308. const isPlayerPaused
  309. = this.player.getPlayerState() === YT.PlayerState.PAUSED;
  310. // If our player is currently paused force the seek.
  311. this.processTime(player, attributes, isPlayerPaused);
  312. // Process mute.
  313. const isAttrMuted = attributes.muted === 'true';
  314. if (player.isMuted() !== isAttrMuted) {
  315. this.smartPlayerMute(isAttrMuted, true);
  316. }
  317. // Process volume
  318. if (!isAttrMuted
  319. && attributes.volume !== undefined
  320. // eslint-disable-next-line eqeqeq
  321. && player.getVolume() != attributes.volume) {
  322. player.setVolume(attributes.volume);
  323. logger.info(`Player change of volume:${attributes.volume}`);
  324. }
  325. if (isPlayerPaused) {
  326. window.glob_dbg.pvu_pv = player.playVideo();
  327. }
  328. // eslint-disable-next-line eqeqeq
  329. } else if (attributes.state == 'pause') {
  330. // if its not paused, pause it
  331. player.pauseVideo();
  332. this.processTime(player, attributes, true);
  333. }
  334. }
  335. /**
  336. * Check for time in attributes and if needed seek in current player
  337. * @param player the player to operate over
  338. * @param attributes the attributes with the player state we want
  339. * @param forceSeek whether seek should be forced
  340. */
  341. processTime(player, attributes, forceSeek) {
  342. if (forceSeek) {
  343. logger.info('Player seekTo:', attributes.time);
  344. player.seekTo(attributes.time);
  345. return;
  346. }
  347. // check received time and current time
  348. const currentPosition = player.getCurrentTime();
  349. const diff = Math.abs(attributes.time - currentPosition);
  350. // if we drift more than the interval for checking
  351. // sync, the interval is in milliseconds
  352. if (diff > updateInterval / 1000) {
  353. logger.info('Player seekTo:', attributes.time,
  354. ' current time is:', currentPosition, ' diff:', diff);
  355. player.seekTo(attributes.time);
  356. }
  357. }
  358. /**
  359. * Checks current state of the player and fire an event with the values.
  360. */
  361. fireSharedVideoEvent(sendPauseEvent) {
  362. // ignore update checks if we are not the owner of the video
  363. // or there is still no player defined or we are stopped
  364. // (in a process of stopping)
  365. if (!APP.conference.isLocalId(this.from) || !this.player
  366. || !this.isSharedVideoShown) {
  367. return;
  368. }
  369. const state = this.player.getPlayerState();
  370. // if its paused and haven't been pause - send paused
  371. if (state === YT.PlayerState.PAUSED && sendPauseEvent) {
  372. this.emitter.emit(UIEvents.UPDATE_SHARED_VIDEO,
  373. this.url, 'pause', this.player.getCurrentTime());
  374. } else if (state === YT.PlayerState.PLAYING) {
  375. // if its playing and it was paused - send update with time
  376. // if its playing and was playing just send update with time
  377. this.emitter.emit(UIEvents.UPDATE_SHARED_VIDEO,
  378. this.url, 'playing',
  379. this.player.getCurrentTime(),
  380. this.player.isMuted(),
  381. this.player.getVolume());
  382. }
  383. }
  384. /**
  385. * Updates video, if it's not playing and needs starting or if it's playing
  386. * and needs to be paused.
  387. * @param id the id of the sender of the command
  388. * @param url the video url
  389. * @param attributes
  390. */
  391. onSharedVideoUpdate(id, url, attributes) {
  392. // if we are sending the event ignore
  393. if (APP.conference.isLocalId(this.from)) {
  394. return;
  395. }
  396. if (!this.isSharedVideoShown) {
  397. this.onSharedVideoStart(id, url, attributes);
  398. return;
  399. }
  400. // eslint-disable-next-line no-negated-condition
  401. if (!this.player) {
  402. this.initialAttributes = attributes;
  403. } else {
  404. this.processVideoUpdate(this.player, attributes);
  405. }
  406. }
  407. /**
  408. * Stop shared video if it is currently showed. If the user started the
  409. * shared video is the one in the id (called when user
  410. * left and we want to remove video if the user sharing it left).
  411. * @param id the id of the sender of the command
  412. */
  413. onSharedVideoStop(id, attributes) {
  414. if (!this.isSharedVideoShown) {
  415. return;
  416. }
  417. if (this.from !== id) {
  418. return;
  419. }
  420. if (!this.player) {
  421. // if there is no error in the player till now,
  422. // store the initial attributes
  423. if (!this.errorInPlayer) {
  424. this.initialAttributes = attributes;
  425. return;
  426. }
  427. }
  428. this.emitter.removeListener(UIEvents.AUDIO_MUTED,
  429. this.localAudioMutedListener);
  430. this.localAudioMutedListener = null;
  431. APP.store.dispatch(participantLeft(this.url, APP.conference._room));
  432. VideoLayout.showLargeVideoContainer(SHARED_VIDEO_CONTAINER_TYPE, false)
  433. .then(() => {
  434. VideoLayout.removeLargeVideoContainer(
  435. SHARED_VIDEO_CONTAINER_TYPE);
  436. if (this.player) {
  437. this.player.destroy();
  438. this.player = null;
  439. } else if (this.errorInPlayer) {
  440. // if there is an error in player, remove that instance
  441. this.errorInPlayer.destroy();
  442. this.errorInPlayer = null;
  443. }
  444. this.smartAudioUnmute();
  445. // revert to original behavior (prevents pausing
  446. // for participants not sharing the video to pause it)
  447. $('#sharedVideo').css('pointer-events', 'auto');
  448. this.emitter.emit(
  449. UIEvents.UPDATE_SHARED_VIDEO, null, 'removed');
  450. });
  451. this.url = null;
  452. this.isSharedVideoShown = false;
  453. this.initialAttributes = null;
  454. }
  455. /**
  456. * Receives events for local audio mute/unmute by local user.
  457. * @param muted boolena whether it is muted or not.
  458. * @param {boolean} indicates if this mute was a result of user interaction,
  459. * i.e. pressing the mute button or it was programatically triggerred
  460. */
  461. onLocalAudioMuted(muted, userInteraction) {
  462. if (!this.player) {
  463. return;
  464. }
  465. if (muted) {
  466. this.mutedWithUserInteraction = userInteraction;
  467. } else if (this.player.getPlayerState() !== YT.PlayerState.PAUSED) {
  468. this.smartPlayerMute(true, false);
  469. // Check if we need to update other participants
  470. this.fireSharedVideoEvent();
  471. }
  472. }
  473. /**
  474. * Mutes / unmutes the player.
  475. * @param mute true to mute the shared video, false - otherwise.
  476. * @param {boolean} Indicates if this mute is a consequence of a network
  477. * video update or is called locally.
  478. */
  479. smartPlayerMute(mute, isVideoUpdate) {
  480. if (!this.player.isMuted() && mute) {
  481. this.player.mute();
  482. if (isVideoUpdate) {
  483. this.smartAudioUnmute();
  484. }
  485. } else if (this.player.isMuted() && !mute) {
  486. this.player.unMute();
  487. if (isVideoUpdate) {
  488. this.smartAudioMute();
  489. }
  490. }
  491. }
  492. /**
  493. * Smart mike unmute. If the mike is currently muted and it wasn't muted
  494. * by the user via the mike button and the volume of the shared video is on
  495. * we're unmuting the mike automatically.
  496. */
  497. smartAudioUnmute() {
  498. if (APP.conference.isLocalAudioMuted()
  499. && !this.mutedWithUserInteraction
  500. && !this.isSharedVideoVolumeOn()) {
  501. sendAnalytics(createEvent('audio.unmuted'));
  502. logger.log('Shared video: audio unmuted');
  503. this.emitter.emit(UIEvents.AUDIO_MUTED, false, false);
  504. }
  505. }
  506. /**
  507. * Smart mike mute. If the mike isn't currently muted and the shared video
  508. * volume is on we mute the mike.
  509. */
  510. smartAudioMute() {
  511. if (!APP.conference.isLocalAudioMuted()
  512. && this.isSharedVideoVolumeOn()) {
  513. sendAnalytics(createEvent('audio.muted'));
  514. logger.log('Shared video: audio muted');
  515. this.emitter.emit(UIEvents.AUDIO_MUTED, true, false);
  516. }
  517. }
  518. }
  519. /**
  520. * Container for shared video iframe.
  521. */
  522. class SharedVideoContainer extends LargeContainer {
  523. /**
  524. *
  525. */
  526. constructor({ url, iframe, player }) {
  527. super();
  528. this.$iframe = $(iframe);
  529. this.url = url;
  530. this.player = player;
  531. }
  532. /**
  533. *
  534. */
  535. show() {
  536. const self = this;
  537. return new Promise(resolve => {
  538. this.$iframe.fadeIn(300, () => {
  539. self.bodyBackground = document.body.style.background;
  540. document.body.style.background = 'black';
  541. this.$iframe.css({ opacity: 1 });
  542. APP.store.dispatch(dockToolbox(true));
  543. resolve();
  544. });
  545. });
  546. }
  547. /**
  548. *
  549. */
  550. hide() {
  551. const self = this;
  552. APP.store.dispatch(dockToolbox(false));
  553. return new Promise(resolve => {
  554. this.$iframe.fadeOut(300, () => {
  555. document.body.style.background = self.bodyBackground;
  556. this.$iframe.css({ opacity: 0 });
  557. resolve();
  558. });
  559. });
  560. }
  561. /**
  562. *
  563. */
  564. onHoverIn() {
  565. try {
  566. var fn_name = "SVC_onHoverIn"
  567. var fn_ret
  568. glob_dev_fns[fn_name] ? fn_ret = glob_dev_fns[fn_name]({that:this,kv:{
  569. // ret,
  570. // _feedbackConfigured,_fullScreen,_screensharing,_sharingVideo,t,
  571. },args:[...arguments],fn_name,arg0:arguments}) : clog("NO EXIST....")
  572. if (fn_ret){ return fn_ret.ret };
  573. } catch (err) { clog(`react_fn fn_name:${fn_name} err:`,err) }
  574. APP.store.dispatch(showToolbox());
  575. }
  576. /**
  577. *
  578. */
  579. get id() {
  580. return this.url;
  581. }
  582. /**
  583. *
  584. */
  585. resize(containerWidth, containerHeight) {
  586. let height, width;
  587. if (interfaceConfig.VERTICAL_FILMSTRIP) {
  588. height = containerHeight - getToolboxHeight();
  589. width = containerWidth - Filmstrip.getVerticalFilmstripWidth();
  590. } else {
  591. height = containerHeight - Filmstrip.getFilmstripHeight();
  592. width = containerWidth;
  593. }
  594. this.$iframe.width(width).height(height);
  595. }
  596. /**
  597. * @return {boolean} do not switch on dominant speaker event if on stage.
  598. */
  599. stayOnStage() {
  600. return false;
  601. }
  602. }
  603. /**
  604. * Checks if given string is youtube url.
  605. * @param {string} url string to check.
  606. * @returns {boolean}
  607. */
  608. function getYoutubeLink(url) {
  609. const p = /^(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?$/;// eslint-disable-line max-len
  610. return url.match(p) ? RegExp.$1 : false;
  611. }
  612. /**
  613. * Ask user if he want to close shared video.
  614. */
  615. function showStopVideoPropmpt() {
  616. return new Promise((resolve, reject) => {
  617. const submitFunction = function(e, v) {
  618. if (v) {
  619. resolve();
  620. } else {
  621. reject();
  622. }
  623. };
  624. const closeFunction = function() {
  625. dialog = null;
  626. };
  627. dialog = APP.UI.messageHandler.openTwoButtonDialog({
  628. titleKey: 'dialog.removeSharedVideoTitle',
  629. msgKey: 'dialog.removeSharedVideoMsg',
  630. leftButtonKey: 'dialog.Remove',
  631. submitFunction,
  632. closeFunction
  633. });
  634. });
  635. }
  636. /**
  637. * Ask user for shared video url to share with others.
  638. * Dialog validates client input to allow only youtube urls.
  639. */
  640. function requestVideoLink() {
  641. const i18n = APP.translation;
  642. const cancelButton = i18n.generateTranslationHTML('dialog.Cancel');
  643. const shareButton = i18n.generateTranslationHTML('dialog.Share');
  644. const backButton = i18n.generateTranslationHTML('dialog.Back');
  645. const linkError
  646. = i18n.generateTranslationHTML('dialog.shareVideoLinkError');
  647. return new Promise((resolve, reject) => {
  648. dialog = APP.UI.messageHandler.openDialogWithStates({
  649. state0: {
  650. titleKey: 'dialog.shareVideoTitle',
  651. html: `
  652. <input name='sharedVideoUrl' type='text'
  653. class='input-control'
  654. data-i18n='[placeholder]defaultLink'
  655. autofocus>`,
  656. persistent: false,
  657. buttons: [
  658. { title: cancelButton,
  659. value: false },
  660. { title: shareButton,
  661. value: true }
  662. ],
  663. focus: ':input:first',
  664. defaultButton: 1,
  665. submit(e, v, m, f) { // eslint-disable-line max-params
  666. e.preventDefault();
  667. if (!v) {
  668. reject('cancelled');
  669. dialog.close();
  670. return;
  671. }
  672. const sharedVideoUrl = f.sharedVideoUrl;
  673. if (!sharedVideoUrl) {
  674. return;
  675. }
  676. const urlValue
  677. = encodeURI(UIUtil.escapeHtml(sharedVideoUrl));
  678. const yVideoId = getYoutubeLink(urlValue);
  679. if (!yVideoId) {
  680. dialog.goToState('state1');
  681. return false;
  682. }
  683. resolve(yVideoId);
  684. dialog.close();
  685. }
  686. },
  687. state1: {
  688. titleKey: 'dialog.shareVideoTitle',
  689. html: linkError,
  690. persistent: false,
  691. buttons: [
  692. { title: cancelButton,
  693. value: false },
  694. { title: backButton,
  695. value: true }
  696. ],
  697. focus: ':input:first',
  698. defaultButton: 1,
  699. submit(e, v) {
  700. e.preventDefault();
  701. if (v === 0) {
  702. reject();
  703. dialog.close();
  704. } else {
  705. dialog.goToState('state0');
  706. }
  707. }
  708. }
  709. }, {
  710. close() {
  711. dialog = null;
  712. }
  713. }, {
  714. url: defaultSharedVideoLink
  715. });
  716. });
  717. }