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 8.8KB

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