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

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