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.

LocalVideo.js 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /* global $, config, interfaceConfig, APP, JitsiMeetJS */
  2. const logger = require("jitsi-meet-logger").getLogger(__filename);
  3. import UIUtil from "../util/UIUtil";
  4. import UIEvents from "../../../service/UI/UIEvents";
  5. import SmallVideo from "./SmallVideo";
  6. const RTCUIUtils = JitsiMeetJS.util.RTCUIHelper;
  7. const TrackEvents = JitsiMeetJS.events.track;
  8. function LocalVideo(VideoLayout, emitter) {
  9. this.videoSpanId = "localVideoContainer";
  10. this.container = $("#localVideoContainer").get(0);
  11. this.localVideoId = null;
  12. this.bindHoverHandler();
  13. if(config.enableLocalVideoFlip)
  14. this._buildContextMenu();
  15. this.isLocal = true;
  16. this.emitter = emitter;
  17. Object.defineProperty(this, 'id', {
  18. get: function () {
  19. return APP.conference.getMyUserId();
  20. }
  21. });
  22. this.initBrowserSpecificProperties();
  23. SmallVideo.call(this, VideoLayout);
  24. // Set default display name.
  25. this.setDisplayName();
  26. this.addAudioLevelIndicator();
  27. this.updateConnectionIndicator();
  28. }
  29. LocalVideo.prototype = Object.create(SmallVideo.prototype);
  30. LocalVideo.prototype.constructor = LocalVideo;
  31. /**
  32. * Sets the display name for the given video span id.
  33. */
  34. LocalVideo.prototype.setDisplayName = function(displayName) {
  35. if (!this.container) {
  36. logger.warn(
  37. "Unable to set displayName - " + this.videoSpanId +
  38. " does not exist");
  39. return;
  40. }
  41. var nameSpan = $('#' + this.videoSpanId + ' .displayname');
  42. var defaultLocalDisplayName = APP.translation.generateTranslationHTML(
  43. interfaceConfig.DEFAULT_LOCAL_DISPLAY_NAME);
  44. var meHTML;
  45. // If we already have a display name for this video.
  46. if (nameSpan.length > 0) {
  47. if (nameSpan.text() !== displayName) {
  48. if (displayName && displayName.length > 0) {
  49. meHTML = APP.translation.generateTranslationHTML("me");
  50. $('#localDisplayName').html(
  51. `${UIUtil.escapeHtml(displayName)} (${meHTML})`
  52. );
  53. $('#editDisplayName').val(
  54. `${UIUtil.escapeHtml(displayName)}`
  55. );
  56. } else {
  57. $('#localDisplayName').html(defaultLocalDisplayName);
  58. }
  59. }
  60. this.updateView();
  61. } else {
  62. nameSpan = document.createElement('span');
  63. nameSpan.className = 'displayname';
  64. document.getElementById(this.videoSpanId)
  65. .appendChild(nameSpan);
  66. if (displayName && displayName.length > 0) {
  67. meHTML = APP.translation.generateTranslationHTML("me");
  68. nameSpan.innerHTML = UIUtil.escapeHtml(displayName) + meHTML;
  69. }
  70. else {
  71. nameSpan.innerHTML = defaultLocalDisplayName;
  72. }
  73. nameSpan.id = 'localDisplayName';
  74. //translates popover of edit button
  75. APP.translation.translateElement($("a.displayname"));
  76. var editableText = document.createElement('input');
  77. editableText.className = 'editdisplayname';
  78. editableText.type = 'text';
  79. editableText.id = 'editDisplayName';
  80. if (displayName && displayName.length) {
  81. editableText.value = displayName;
  82. }
  83. editableText.setAttribute('style', 'display:none;');
  84. editableText.setAttribute('data-i18n',
  85. '[placeholder]defaultNickname');
  86. editableText.setAttribute("data-i18n-options",
  87. JSON.stringify({name: "Jane Pink"}));
  88. APP.translation.translateElement($(editableText));
  89. this.container
  90. .appendChild(editableText);
  91. var self = this;
  92. $('#localVideoContainer .displayname')
  93. .bind("click", function (e) {
  94. let $editDisplayName = $('#editDisplayName');
  95. e.preventDefault();
  96. e.stopPropagation();
  97. // we set display to be hidden
  98. self.hideDisplayName = true;
  99. // update the small video vide to hide the display name
  100. self.updateView();
  101. // disables further updates in the thumbnail to stay in the
  102. // edit mode
  103. self.disableUpdateView = true;
  104. $editDisplayName.show();
  105. $editDisplayName.focus();
  106. $editDisplayName.select();
  107. $editDisplayName.one("focusout", function () {
  108. self.emitter.emit(UIEvents.NICKNAME_CHANGED, this.value);
  109. $editDisplayName.hide();
  110. // stop editing, display displayName and resume updating
  111. // the thumbnail
  112. self.hideDisplayName = false;
  113. self.disableUpdateView = false;
  114. self.updateView();
  115. });
  116. $editDisplayName.on('keydown', function (e) {
  117. if (e.keyCode === 13) {
  118. e.preventDefault();
  119. $('#editDisplayName').hide();
  120. // focusout handler will save display name
  121. }
  122. });
  123. });
  124. }
  125. };
  126. LocalVideo.prototype.changeVideo = function (stream) {
  127. this.videoStream = stream;
  128. let localVideoClick = (event) => {
  129. // FIXME: with Temasys plugin event arg is not an event, but
  130. // the clicked object itself, so we have to skip this call
  131. if (event.stopPropagation) {
  132. event.stopPropagation();
  133. }
  134. this.VideoLayout.handleVideoThumbClicked(this.id);
  135. };
  136. let localVideoContainerSelector = $('#localVideoContainer');
  137. localVideoContainerSelector.off('click');
  138. localVideoContainerSelector.on('click', localVideoClick);
  139. let localVideo = document.createElement('video');
  140. localVideo.id = this.localVideoId = 'localVideo_' + stream.getId();
  141. RTCUIUtils.setAutoPlay(localVideo, true);
  142. RTCUIUtils.setVolume(localVideo, 0);
  143. var localVideoContainer = document.getElementById('localVideoWrapper');
  144. // Put the new video always in front
  145. UIUtil.prependChild(localVideoContainer, localVideo);
  146. // Add click handler to both video and video wrapper elements in case
  147. // there's no video.
  148. // onclick has to be used with Temasys plugin
  149. localVideo.onclick = localVideoClick;
  150. let isVideo = stream.videoType != "desktop";
  151. this._enableDisableContextMenu(isVideo);
  152. this.setFlipX(isVideo? APP.settings.getLocalFlipX() : false);
  153. // Attach WebRTC stream
  154. localVideo = stream.attach(localVideo);
  155. let endedHandler = () => {
  156. localVideoContainer.removeChild(localVideo);
  157. // when removing only the video element and we are on stage
  158. // update the stage
  159. if(this.isCurrentlyOnLargeVideo())
  160. this.VideoLayout.updateLargeVideo(this.id);
  161. stream.off(TrackEvents.LOCAL_TRACK_STOPPED, endedHandler);
  162. };
  163. stream.on(TrackEvents.LOCAL_TRACK_STOPPED, endedHandler);
  164. };
  165. /**
  166. * Shows or hides the local video container.
  167. * @param {boolean} true to make the local video container visible, false
  168. * otherwise
  169. */
  170. LocalVideo.prototype.setVisible = function(visible) {
  171. // We toggle the hidden class as an indication to other interested parties
  172. // that this container has been hidden on purpose.
  173. $("#localVideoContainer").toggleClass("hidden");
  174. // We still show/hide it as we need to overwrite the style property if we
  175. // want our action to take effect. Toggling the display property through
  176. // the above css class didn't succeed in overwriting the style.
  177. if (visible) {
  178. $("#localVideoContainer").show();
  179. }
  180. else {
  181. $("#localVideoContainer").hide();
  182. }
  183. };
  184. /**
  185. * Sets the flipX state of the video.
  186. * @param val {boolean} true for flipped otherwise false;
  187. */
  188. LocalVideo.prototype.setFlipX = function (val) {
  189. this.emitter.emit(UIEvents.LOCAL_FLIPX_CHANGED, val);
  190. if(!this.localVideoId)
  191. return;
  192. if(val) {
  193. this.selectVideoElement().addClass("flipVideoX");
  194. } else {
  195. this.selectVideoElement().removeClass("flipVideoX");
  196. }
  197. };
  198. /**
  199. * Builds the context menu for the local video.
  200. */
  201. LocalVideo.prototype._buildContextMenu = function () {
  202. $.contextMenu({
  203. selector: '#' + this.videoSpanId,
  204. zIndex: 10000,
  205. items: {
  206. flip: {
  207. name: "Flip",
  208. callback: () => {
  209. let val = !APP.settings.getLocalFlipX();
  210. this.setFlipX(val);
  211. APP.settings.setLocalFlipX(val);
  212. }
  213. }
  214. },
  215. events: {
  216. show : function(options){
  217. options.items.flip.name =
  218. APP.translation.generateTranslationHTML(
  219. "videothumbnail.flip");
  220. }
  221. }
  222. });
  223. };
  224. /**
  225. * Enables or disables the context menu for the local video.
  226. * @param enable {boolean} true for enable, false for disable
  227. */
  228. LocalVideo.prototype._enableDisableContextMenu = function (enable) {
  229. if($('#' + this.videoSpanId).contextMenu)
  230. $('#' + this.videoSpanId).contextMenu(enable);
  231. };
  232. export default LocalVideo;