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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /* global $, config, interfaceConfig, APP, JitsiMeetJS */
  2. import ConnectionIndicator from "./ConnectionIndicator";
  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.createConnectionIndicator();
  13. this.bindHoverHandler();
  14. if(config.enableLocalVideoFlip)
  15. this._buildContextMenu();
  16. this.isLocal = true;
  17. this.emitter = emitter;
  18. Object.defineProperty(this, 'id', {
  19. get: function () {
  20. return APP.conference.getMyUserId();
  21. }
  22. });
  23. this.initBrowserSpecificProperties();
  24. SmallVideo.call(this, VideoLayout);
  25. // Set default display name.
  26. this.setDisplayName();
  27. this.addAudioLevelIndicator();
  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. console.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.createConnectionIndicator = function() {
  127. if(this.connectionIndicator)
  128. return;
  129. this.connectionIndicator = new ConnectionIndicator(this, null);
  130. };
  131. LocalVideo.prototype.changeVideo = function (stream) {
  132. this.videoStream = stream;
  133. let localVideoClick = (event) => {
  134. // FIXME: with Temasys plugin event arg is not an event, but
  135. // the clicked object itself, so we have to skip this call
  136. if (event.stopPropagation) {
  137. event.stopPropagation();
  138. }
  139. this.VideoLayout.handleVideoThumbClicked(this.id);
  140. };
  141. let localVideoContainerSelector = $('#localVideoContainer');
  142. localVideoContainerSelector.off('click');
  143. localVideoContainerSelector.on('click', localVideoClick);
  144. let localVideo = document.createElement('video');
  145. localVideo.id = this.localVideoId = 'localVideo_' + stream.getId();
  146. RTCUIUtils.setAutoPlay(localVideo, true);
  147. RTCUIUtils.setVolume(localVideo, 0);
  148. var localVideoContainer = document.getElementById('localVideoWrapper');
  149. // Put the new video always in front
  150. UIUtil.prependChild(localVideoContainer, localVideo);
  151. // Add click handler to both video and video wrapper elements in case
  152. // there's no video.
  153. // onclick has to be used with Temasys plugin
  154. localVideo.onclick = localVideoClick;
  155. let isVideo = stream.videoType != "desktop";
  156. this._enableDisableContextMenu(isVideo);
  157. this.setFlipX(isVideo? APP.settings.getLocalFlipX() : false);
  158. // Attach WebRTC stream
  159. localVideo = stream.attach(localVideo);
  160. let endedHandler = () => {
  161. localVideoContainer.removeChild(localVideo);
  162. // when removing only the video element and we are on stage
  163. // update the stage
  164. if(this.isCurrentlyOnLargeVideo())
  165. this.VideoLayout.updateLargeVideo(this.id);
  166. stream.off(TrackEvents.LOCAL_TRACK_STOPPED, endedHandler);
  167. };
  168. stream.on(TrackEvents.LOCAL_TRACK_STOPPED, endedHandler);
  169. };
  170. /**
  171. * Shows or hides the local video container.
  172. * @param {boolean} true to make the local video container visible, false
  173. * otherwise
  174. */
  175. LocalVideo.prototype.setVisible = function(visible) {
  176. // We toggle the hidden class as an indication to other interested parties
  177. // that this container has been hidden on purpose.
  178. $("#localVideoContainer").toggleClass("hidden");
  179. // We still show/hide it as we need to overwrite the style property if we
  180. // want our action to take effect. Toggling the display property through
  181. // the above css class didn't succeed in overwriting the style.
  182. if (visible) {
  183. $("#localVideoContainer").show();
  184. }
  185. else {
  186. $("#localVideoContainer").hide();
  187. }
  188. };
  189. /**
  190. * Sets the flipX state of the video.
  191. * @param val {boolean} true for flipped otherwise false;
  192. */
  193. LocalVideo.prototype.setFlipX = function (val) {
  194. this.emitter.emit(UIEvents.LOCAL_FLIPX_CHANGED, val);
  195. if(!this.localVideoId)
  196. return;
  197. if(val) {
  198. this.selectVideoElement().addClass("flipVideoX");
  199. } else {
  200. this.selectVideoElement().removeClass("flipVideoX");
  201. }
  202. };
  203. /**
  204. * Builds the context menu for the local video.
  205. */
  206. LocalVideo.prototype._buildContextMenu = function () {
  207. $.contextMenu({
  208. selector: '#' + this.videoSpanId,
  209. zIndex: 10000,
  210. items: {
  211. flip: {
  212. name: "Flip",
  213. callback: () => {
  214. let val = !APP.settings.getLocalFlipX();
  215. this.setFlipX(val);
  216. APP.settings.setLocalFlipX(val);
  217. }
  218. }
  219. },
  220. events: {
  221. show : function(options){
  222. options.items.flip.name =
  223. APP.translation.generateTranslationHTML(
  224. "videothumbnail.flip");
  225. }
  226. }
  227. });
  228. };
  229. /**
  230. * Enables or disables the context menu for the local video.
  231. * @param enable {boolean} true for enable, false for disable
  232. */
  233. LocalVideo.prototype._enableDisableContextMenu = function (enable) {
  234. if($('#' + this.videoSpanId).contextMenu)
  235. $('#' + this.videoSpanId).contextMenu(enable);
  236. };
  237. export default LocalVideo;