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