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

SharedVideo.js 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  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(
  483. 'microphone', 'micMutedPopup', show, 5000);
  484. }
  485. /**
  486. * Shows a popup under the shared video toolbar icon that notifies the user
  487. * of automatic mute of the shared video after the user has unmuted their
  488. * mic.
  489. * @param show boolean, show or hide the notification
  490. */
  491. showSharedVideoMutedPopup (show) {
  492. if(show)
  493. this.showMicMutedPopup(false);
  494. APP.UI.showCustomToolbarPopup(
  495. 'sharedvideo', 'sharedVideoMutedPopup', show, 5000);
  496. }
  497. }
  498. /**
  499. * Container for shared video iframe.
  500. */
  501. class SharedVideoContainer extends LargeContainer {
  502. constructor ({url, iframe, player}) {
  503. super();
  504. this.$iframe = $(iframe);
  505. this.url = url;
  506. this.player = player;
  507. }
  508. show () {
  509. let self = this;
  510. return new Promise(resolve => {
  511. this.$iframe.fadeIn(300, () => {
  512. self.bodyBackground = document.body.style.background;
  513. document.body.style.background = 'black';
  514. this.$iframe.css({opacity: 1});
  515. APP.store.dispatch(dockToolbox(true));
  516. resolve();
  517. });
  518. });
  519. }
  520. hide () {
  521. let self = this;
  522. APP.store.dispatch(dockToolbox(false));
  523. return new Promise(resolve => {
  524. this.$iframe.fadeOut(300, () => {
  525. document.body.style.background = self.bodyBackground;
  526. this.$iframe.css({opacity: 0});
  527. resolve();
  528. });
  529. });
  530. }
  531. onHoverIn () {
  532. APP.store.dispatch(showToolbox());
  533. }
  534. get id () {
  535. return this.url;
  536. }
  537. resize (containerWidth, containerHeight) {
  538. let height = containerHeight - Filmstrip.getFilmstripHeight();
  539. let width = containerWidth;
  540. this.$iframe.width(width).height(height);
  541. }
  542. /**
  543. * @return {boolean} do not switch on dominant speaker event if on stage.
  544. */
  545. stayOnStage () {
  546. return false;
  547. }
  548. }
  549. function SharedVideoThumb (url)
  550. {
  551. this.id = url;
  552. this.url = url;
  553. this.setVideoType(SHARED_VIDEO_CONTAINER_TYPE);
  554. this.videoSpanId = "sharedVideoContainer";
  555. this.container = this.createContainer(this.videoSpanId);
  556. this.container.onclick = this.videoClick.bind(this);
  557. this.bindHoverHandler();
  558. SmallVideo.call(this, VideoLayout);
  559. this.isVideoMuted = true;
  560. }
  561. SharedVideoThumb.prototype = Object.create(SmallVideo.prototype);
  562. SharedVideoThumb.prototype.constructor = SharedVideoThumb;
  563. /**
  564. * hide display name
  565. */
  566. SharedVideoThumb.prototype.setDeviceAvailabilityIcons = function () {};
  567. SharedVideoThumb.prototype.avatarChanged = function () {};
  568. SharedVideoThumb.prototype.createContainer = function (spanId) {
  569. var container = document.createElement('span');
  570. container.id = spanId;
  571. container.className = 'videocontainer';
  572. // add the avatar
  573. var avatar = document.createElement('img');
  574. avatar.className = 'sharedVideoAvatar';
  575. avatar.src = "https://img.youtube.com/vi/" + this.url + "/0.jpg";
  576. container.appendChild(avatar);
  577. const displayNameContainer = document.createElement('div');
  578. displayNameContainer.className = 'displayNameContainer';
  579. container.appendChild(displayNameContainer);
  580. var remotes = document.getElementById('filmstripRemoteVideosContainer');
  581. return remotes.appendChild(container);
  582. };
  583. /**
  584. * The thumb click handler.
  585. */
  586. SharedVideoThumb.prototype.videoClick = function () {
  587. VideoLayout.handleVideoThumbClicked(this.url);
  588. };
  589. /**
  590. * Removes RemoteVideo from the page.
  591. */
  592. SharedVideoThumb.prototype.remove = function () {
  593. logger.log("Remove shared video thumb", this.id);
  594. // Make sure that the large video is updated if are removing its
  595. // corresponding small video.
  596. this.VideoLayout.updateAfterThumbRemoved(this.id);
  597. // Remove whole container
  598. if (this.container.parentNode) {
  599. this.container.parentNode.removeChild(this.container);
  600. }
  601. };
  602. /**
  603. * Sets the display name for the thumb.
  604. */
  605. SharedVideoThumb.prototype.setDisplayName = function(displayName) {
  606. if (!this.container) {
  607. logger.warn( "Unable to set displayName - " + this.videoSpanId +
  608. " does not exist");
  609. return;
  610. }
  611. this.updateDisplayName({
  612. displayName: displayName || '',
  613. elementID: `${this.videoSpanId}_name`,
  614. participantID: this.id
  615. });
  616. };
  617. /**
  618. * Checks if given string is youtube url.
  619. * @param {string} url string to check.
  620. * @returns {boolean}
  621. */
  622. function getYoutubeLink(url) {
  623. let p = /^(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?$/;//jshint ignore:line
  624. return (url.match(p)) ? RegExp.$1 : false;
  625. }
  626. /**
  627. * Ask user if he want to close shared video.
  628. */
  629. function showStopVideoPropmpt() {
  630. return new Promise(function (resolve, reject) {
  631. let submitFunction = function(e,v) {
  632. if (v) {
  633. resolve();
  634. } else {
  635. reject();
  636. }
  637. };
  638. let closeFunction = function () {
  639. dialog = null;
  640. };
  641. dialog = APP.UI.messageHandler.openTwoButtonDialog({
  642. titleKey: "dialog.removeSharedVideoTitle",
  643. msgKey: "dialog.removeSharedVideoMsg",
  644. leftButtonKey: "dialog.Remove",
  645. submitFunction,
  646. closeFunction
  647. });
  648. });
  649. }
  650. /**
  651. * Ask user for shared video url to share with others.
  652. * Dialog validates client input to allow only youtube urls.
  653. */
  654. function requestVideoLink() {
  655. let i18n = APP.translation;
  656. const cancelButton = i18n.generateTranslationHTML("dialog.Cancel");
  657. const shareButton = i18n.generateTranslationHTML("dialog.Share");
  658. const backButton = i18n.generateTranslationHTML("dialog.Back");
  659. const linkError
  660. = i18n.generateTranslationHTML("dialog.shareVideoLinkError");
  661. return new Promise(function (resolve, reject) {
  662. dialog = APP.UI.messageHandler.openDialogWithStates({
  663. state0: {
  664. titleKey: "dialog.shareVideoTitle",
  665. html: `
  666. <input name="sharedVideoUrl" type="text"
  667. class="input-control"
  668. data-i18n="[placeholder]defaultLink"
  669. autofocus>`,
  670. persistent: false,
  671. buttons: [
  672. {title: cancelButton, value: false},
  673. {title: shareButton, value: true}
  674. ],
  675. focus: ':input:first',
  676. defaultButton: 1,
  677. submit: function (e, v, m, f) {
  678. e.preventDefault();
  679. if (!v) {
  680. reject('cancelled');
  681. dialog.close();
  682. return;
  683. }
  684. let sharedVideoUrl = f.sharedVideoUrl;
  685. if (!sharedVideoUrl) {
  686. return;
  687. }
  688. let urlValue = encodeURI(UIUtil.escapeHtml(sharedVideoUrl));
  689. let yVideoId = getYoutubeLink(urlValue);
  690. if (!yVideoId) {
  691. dialog.goToState('state1');
  692. return false;
  693. }
  694. resolve(yVideoId);
  695. dialog.close();
  696. }
  697. },
  698. state1: {
  699. titleKey: "dialog.shareVideoTitle",
  700. html: linkError,
  701. persistent: false,
  702. buttons: [
  703. {title: cancelButton, value: false},
  704. {title: backButton, value: true}
  705. ],
  706. focus: ':input:first',
  707. defaultButton: 1,
  708. submit: function (e, v) {
  709. e.preventDefault();
  710. if (v === 0) {
  711. reject();
  712. dialog.close();
  713. } else {
  714. dialog.goToState('state0');
  715. }
  716. }
  717. }
  718. }, {
  719. close: function () {
  720. dialog = null;
  721. }
  722. }, {
  723. url: defaultSharedVideoLink
  724. });
  725. });
  726. }