Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /* global $, APP, interfaceConfig */
  2. import ConnectionIndicator from './ConnectionIndicator';
  3. import SmallVideo from "./SmallVideo";
  4. import AudioLevels from "../audio_levels/AudioLevels";
  5. import UIUtils from "../util/UIUtil";
  6. import UIEvents from '../../../service/UI/UIEvents';
  7. function RemoteVideo(id, VideoLayout, emitter) {
  8. this.id = id;
  9. this.emitter = emitter;
  10. this.videoSpanId = `participant_${id}`;
  11. this.VideoLayout = VideoLayout;
  12. this.addRemoteVideoContainer();
  13. this.connectionIndicator = new ConnectionIndicator(this, id);
  14. this.setDisplayName();
  15. var nickfield = document.createElement('span');
  16. nickfield.className = "nick";
  17. nickfield.appendChild(document.createTextNode(id));
  18. this.container.appendChild(nickfield);
  19. this.bindHoverHandler();
  20. this.flipX = false;
  21. this.isLocal = false;
  22. SmallVideo.call(this);
  23. }
  24. RemoteVideo.prototype = Object.create(SmallVideo.prototype);
  25. RemoteVideo.prototype.constructor = RemoteVideo;
  26. RemoteVideo.prototype.addRemoteVideoContainer = function() {
  27. this.container = RemoteVideo.createContainer(this.videoSpanId);
  28. if (APP.conference.isModerator) {
  29. this.addRemoteVideoMenu();
  30. }
  31. let {thumbWidth, thumbHeight} = this.VideoLayout.resizeThumbnails();
  32. AudioLevels.updateAudioLevelCanvas(this.id, thumbWidth, thumbHeight);
  33. return this.container;
  34. };
  35. /**
  36. * Adds the remote video menu element for the given <tt>id</tt> in the
  37. * given <tt>parentElement</tt>.
  38. *
  39. * @param id the id indicating the video for which we're adding a menu.
  40. * @param parentElement the parent element where this menu will be added
  41. */
  42. if (!interfaceConfig.filmStripOnly) {
  43. RemoteVideo.prototype.addRemoteVideoMenu = function () {
  44. var spanElement = document.createElement('span');
  45. spanElement.className = 'remotevideomenu';
  46. this.container.appendChild(spanElement);
  47. var menuElement = document.createElement('i');
  48. menuElement.className = 'fa fa-angle-down';
  49. menuElement.title = 'Remote user controls';
  50. spanElement.appendChild(menuElement);
  51. var popupmenuElement = document.createElement('ul');
  52. popupmenuElement.className = 'popupmenu';
  53. popupmenuElement.id = `remote_popupmenu_${this.id}`;
  54. spanElement.appendChild(popupmenuElement);
  55. var muteMenuItem = document.createElement('li');
  56. var muteLinkItem = document.createElement('a');
  57. var mutedIndicator = "<i style='float:left;' " +
  58. "class='icon-mic-disabled'></i>";
  59. if (!this.isMuted) {
  60. muteLinkItem.innerHTML = mutedIndicator +
  61. " <div style='width: 90px;margin-left: 20px;' " +
  62. "data-i18n='videothumbnail.domute'></div>";
  63. muteLinkItem.className = 'mutelink';
  64. }
  65. else {
  66. muteLinkItem.innerHTML = mutedIndicator +
  67. " <div style='width: 90px;margin-left: 20px;' " +
  68. "data-i18n='videothumbnail.muted'></div>";
  69. muteLinkItem.className = 'mutelink disabled';
  70. }
  71. muteLinkItem.onclick = (event) => {
  72. if ($(this).attr('disabled')) {
  73. event.preventDefault();
  74. }
  75. var isMute = !!this.isMuted;
  76. this.emitter.emit(UIEvents.REMOTE_AUDIO_MUTED, this.id);
  77. popupmenuElement.setAttribute('style', 'display:none;');
  78. if (isMute) {
  79. this.innerHTML = mutedIndicator +
  80. " <div style='width: 90px;margin-left: 20px;' " +
  81. "data-i18n='videothumbnail.muted'></div>";
  82. this.className = 'mutelink disabled';
  83. }
  84. else {
  85. this.innerHTML = mutedIndicator +
  86. " <div style='width: 90px;margin-left: 20px;' " +
  87. "data-i18n='videothumbnail.domute'></div>";
  88. this.className = 'mutelink';
  89. }
  90. };
  91. muteMenuItem.appendChild(muteLinkItem);
  92. popupmenuElement.appendChild(muteMenuItem);
  93. var ejectIndicator = "<i style='float:left;' class='fa fa-eject'></i>";
  94. var ejectMenuItem = document.createElement('li');
  95. var ejectLinkItem = document.createElement('a');
  96. var ejectText = "<div style='width: 90px;margin-left: 20px;' " +
  97. "data-i18n='videothumbnail.kick'>&nbsp;</div>";
  98. ejectLinkItem.innerHTML = ejectIndicator + ' ' + ejectText;
  99. ejectLinkItem.onclick = (event) => {
  100. this.emitter.emit(UIEvents.USER_KICKED, this.id);
  101. popupmenuElement.setAttribute('style', 'display:none;');
  102. };
  103. ejectMenuItem.appendChild(ejectLinkItem);
  104. popupmenuElement.appendChild(ejectMenuItem);
  105. var paddingSpan = document.createElement('span');
  106. paddingSpan.className = 'popupmenuPadding';
  107. popupmenuElement.appendChild(paddingSpan);
  108. APP.translation.translateElement(
  109. $("#" + popupmenuElement.id + " > li > a > div"));
  110. };
  111. } else {
  112. RemoteVideo.prototype.addRemoteVideoMenu = function() {};
  113. }
  114. /**
  115. * Removes the remote stream element corresponding to the given stream and
  116. * parent container.
  117. *
  118. * @param stream the MediaStream
  119. * @param isVideo <tt>true</tt> if given <tt>stream</tt> is a video one.
  120. */
  121. RemoteVideo.prototype.removeRemoteStreamElement = function (stream) {
  122. if (!this.container)
  123. return false;
  124. var isVideo = stream.isVideoTrack();
  125. var elementID = SmallVideo.getStreamElementID(stream);
  126. var select = null;
  127. if (isVideo) {
  128. select = $('#' + elementID);
  129. }
  130. else
  131. select = $('#' + this.videoSpanId + '>audio');
  132. select.remove();
  133. console.info((isVideo ? "Video" : "Audio") +
  134. " removed " + this.id, select);
  135. if (isVideo)
  136. this.VideoLayout.updateRemovedVideo(this.id);
  137. };
  138. /**
  139. * Removes RemoteVideo from the page.
  140. */
  141. RemoteVideo.prototype.remove = function () {
  142. console.log("Remove thumbnail", this.id);
  143. this.removeConnectionIndicator();
  144. // Make sure that the large video is updated if are removing its
  145. // corresponding small video.
  146. this.VideoLayout.updateRemovedVideo(this.id);
  147. // Remove whole container
  148. if (this.container.parentNode) {
  149. this.container.parentNode.removeChild(this.container);
  150. }
  151. };
  152. RemoteVideo.prototype.waitForPlayback = function (streamElement, stream) {
  153. var webRtcStream = stream.getOriginalStream();
  154. var isVideo = stream.isVideoTrack();
  155. if (!isVideo || webRtcStream.id === 'mixedmslabel') {
  156. return;
  157. }
  158. var self = this;
  159. // Register 'onplaying' listener to trigger 'videoactive' on VideoLayout
  160. // when video playback starts
  161. var onPlayingHandler = function () {
  162. self.VideoLayout.videoactive(streamElement, self.id);
  163. streamElement.onplaying = null;
  164. };
  165. streamElement.onplaying = onPlayingHandler;
  166. };
  167. /**
  168. * Checks whether or not video stream exists and has started for this
  169. * RemoteVideo instance. This is checked by trying to select video element in
  170. * this container and checking if 'currentTime' field's value is greater than 0.
  171. *
  172. * @returns {*|boolean} true if this RemoteVideo has active video stream running
  173. */
  174. RemoteVideo.prototype.hasVideoStarted = function () {
  175. var videoSelector = this.selectVideoElement();
  176. return videoSelector.length && videoSelector[0].currentTime > 0;
  177. };
  178. RemoteVideo.prototype.addRemoteStreamElement = function (stream) {
  179. if (!this.container) {
  180. return;
  181. }
  182. let isVideo = stream.isVideoTrack();
  183. isVideo ? this.videoStream = stream : this.audioStream = stream;
  184. // Add click handler.
  185. let onClickHandler = (event) => {
  186. let source = event.target || event.srcElement;
  187. // ignore click if it was done in popup menu
  188. if ($(source).parents('.popupmenu').length === 0) {
  189. this.VideoLayout.handleVideoThumbClicked(false, this.id);
  190. }
  191. // On IE we need to populate this handler on video <object>
  192. // and it does not give event instance as an argument,
  193. // so we check here for methods.
  194. if (event.stopPropagation && event.preventDefault) {
  195. event.stopPropagation();
  196. event.preventDefault();
  197. }
  198. return false;
  199. };
  200. this.container.onclick = onClickHandler;
  201. if(!stream.getOriginalStream())
  202. return;
  203. let streamElement = SmallVideo.createStreamElement(stream);
  204. let newElementId = streamElement.id;
  205. // Put new stream element always in front
  206. UIUtils.prependChild(this.container, streamElement);
  207. // If we hide element when Temasys plugin is used then
  208. // we'll never receive 'onplay' event and other logic won't work as expected
  209. // NOTE: hiding will not have effect when Temasys plugin is in use, as
  210. // calling attach will show it back
  211. $(streamElement).hide();
  212. // If the container is currently visible
  213. // we attach the stream to the element.
  214. if (!isVideo || (this.container.offsetParent !== null && isVideo)) {
  215. this.waitForPlayback(streamElement, stream);
  216. streamElement = stream.attach(streamElement);
  217. }
  218. $(streamElement).click(onClickHandler);
  219. },
  220. /**
  221. * Show/hide peer container for the given id.
  222. */
  223. RemoteVideo.prototype.showPeerContainer = function (state) {
  224. if (!this.container)
  225. return;
  226. var isHide = state === 'hide';
  227. var resizeThumbnails = false;
  228. if (!isHide) {
  229. if (!$(this.container).is(':visible')) {
  230. resizeThumbnails = true;
  231. $(this.container).show();
  232. }
  233. // Call updateView, so that we'll figure out if avatar
  234. // should be displayed based on video muted status and whether or not
  235. // it's in the lastN set
  236. this.updateView();
  237. }
  238. else if ($(this.container).is(':visible') && isHide)
  239. {
  240. resizeThumbnails = true;
  241. $(this.container).hide();
  242. if(this.connectionIndicator)
  243. this.connectionIndicator.hide();
  244. }
  245. if (resizeThumbnails) {
  246. this.VideoLayout.resizeThumbnails();
  247. }
  248. // We want to be able to pin a participant from the contact list, even
  249. // if he's not in the lastN set!
  250. // ContactList.setClickable(id, !isHide);
  251. };
  252. RemoteVideo.prototype.updateResolution = function (resolution) {
  253. if (this.connectionIndicator) {
  254. this.connectionIndicator.updateResolution(resolution);
  255. }
  256. };
  257. RemoteVideo.prototype.removeConnectionIndicator = function () {
  258. if (this.connectionIndicator)
  259. this.connectionIndicator.remove();
  260. };
  261. RemoteVideo.prototype.hideConnectionIndicator = function () {
  262. if (this.connectionIndicator)
  263. this.connectionIndicator.hide();
  264. };
  265. /**
  266. * Updates the remote video menu.
  267. *
  268. * @param id the id indicating the video for which we're adding a menu.
  269. * @param isMuted indicates the current mute state
  270. */
  271. RemoteVideo.prototype.updateRemoteVideoMenu = function (isMuted) {
  272. var muteMenuItem = $(`#remote_popupmenu_${this.id}>li>a.mutelink`);
  273. var mutedIndicator = "<i class='icon-mic-disabled'></i>";
  274. if (muteMenuItem.length) {
  275. var muteLink = muteMenuItem.get(0);
  276. if (isMuted) {
  277. muteLink.innerHTML = mutedIndicator + ' Muted';
  278. muteLink.className = 'mutelink disabled';
  279. }
  280. else {
  281. muteLink.innerHTML = mutedIndicator + ' Mute';
  282. muteLink.className = 'mutelink';
  283. }
  284. }
  285. };
  286. /**
  287. * Sets the display name for the given video span id.
  288. */
  289. RemoteVideo.prototype.setDisplayName = function(displayName, key) {
  290. if (!this.container) {
  291. console.warn( "Unable to set displayName - " + this.videoSpanId +
  292. " does not exist");
  293. return;
  294. }
  295. var nameSpan = $('#' + this.videoSpanId + '>span.displayname');
  296. // If we already have a display name for this video.
  297. if (nameSpan.length > 0) {
  298. if (displayName && displayName.length > 0) {
  299. $('#' + this.videoSpanId + '_name').text(displayName);
  300. }
  301. else if (key && key.length > 0) {
  302. var nameHtml = APP.translation.generateTranslationHTML(key);
  303. $('#' + this.videoSpanId + '_name').html(nameHtml);
  304. }
  305. else
  306. $('#' + this.videoSpanId + '_name').text(
  307. interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME);
  308. } else {
  309. nameSpan = document.createElement('span');
  310. nameSpan.className = 'displayname';
  311. $('#' + this.videoSpanId)[0].appendChild(nameSpan);
  312. if (displayName && displayName.length > 0) {
  313. $(nameSpan).text(displayName);
  314. } else {
  315. nameSpan.innerHTML = interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME;
  316. }
  317. nameSpan.id = this.videoSpanId + '_name';
  318. }
  319. };
  320. /**
  321. * Removes remote video menu element from video element identified by
  322. * given <tt>videoElementId</tt>.
  323. *
  324. * @param videoElementId the id of local or remote video element.
  325. */
  326. RemoteVideo.prototype.removeRemoteVideoMenu = function() {
  327. var menuSpan = $('#' + this.videoSpanId + '>span.remotevideomenu');
  328. if (menuSpan.length) {
  329. menuSpan.remove();
  330. }
  331. };
  332. RemoteVideo.createContainer = function (spanId) {
  333. var container = document.createElement('span');
  334. container.id = spanId;
  335. container.className = 'videocontainer';
  336. var remotes = document.getElementById('remoteVideos');
  337. return remotes.appendChild(container);
  338. };
  339. export default RemoteVideo;