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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. .appendChild(nameSpan);
  65. if (displayName && displayName.length > 0) {
  66. meHTML = APP.translation.generateTranslationHTML("me");
  67. nameSpan.innerHTML = UIUtil.escapeHtml(displayName) + meHTML;
  68. }
  69. else {
  70. nameSpan.innerHTML = defaultLocalDisplayName;
  71. }
  72. nameSpan.id = 'localDisplayName';
  73. //translates popover of edit button
  74. APP.translation.translateElement($("a.displayname"));
  75. var editableText = document.createElement('input');
  76. editableText.className = 'editdisplayname';
  77. editableText.type = 'text';
  78. editableText.id = 'editDisplayName';
  79. if (displayName && displayName.length) {
  80. editableText.value = displayName;
  81. }
  82. editableText.setAttribute('style', 'display:none;');
  83. editableText.setAttribute('data-i18n',
  84. '[placeholder]defaultNickname');
  85. editableText.setAttribute("data-i18n-options",
  86. JSON.stringify({name: "Jane Pink"}));
  87. APP.translation.translateElement($(editableText));
  88. this.container
  89. .querySelector('.videocontainer__toolbar')
  90. .appendChild(editableText);
  91. var self = this;
  92. $('#localVideoContainer .displayname')
  93. .bind("click", function (e) {
  94. let $editDisplayName = $('#editDisplayName');
  95. let $localDisplayName = $('#localDisplayName');
  96. e.preventDefault();
  97. e.stopPropagation();
  98. $localDisplayName.hide();
  99. $editDisplayName.show();
  100. $editDisplayName.focus();
  101. $editDisplayName.select();
  102. $editDisplayName.one("focusout", function () {
  103. self.emitter.emit(UIEvents.NICKNAME_CHANGED, this.value);
  104. $editDisplayName.hide();
  105. $localDisplayName.show();
  106. });
  107. $editDisplayName.on('keydown', function (e) {
  108. if (e.keyCode === 13) {
  109. e.preventDefault();
  110. $('#editDisplayName').hide();
  111. // focusout handler will save display name
  112. }
  113. });
  114. });
  115. }
  116. };
  117. LocalVideo.prototype.createConnectionIndicator = function() {
  118. if(this.connectionIndicator)
  119. return;
  120. this.connectionIndicator = new ConnectionIndicator(this, null);
  121. };
  122. LocalVideo.prototype.changeVideo = function (stream) {
  123. this.videoStream = stream;
  124. let localVideoClick = (event) => {
  125. // FIXME: with Temasys plugin event arg is not an event, but
  126. // the clicked object itself, so we have to skip this call
  127. if (event.stopPropagation) {
  128. event.stopPropagation();
  129. }
  130. this.VideoLayout.handleVideoThumbClicked(this.id);
  131. };
  132. let localVideoContainerSelector = $('#localVideoContainer');
  133. localVideoContainerSelector.off('click');
  134. localVideoContainerSelector.on('click', localVideoClick);
  135. let localVideo = document.createElement('video');
  136. localVideo.id = this.localVideoId = 'localVideo_' + stream.getId();
  137. RTCUIUtils.setAutoPlay(localVideo, true);
  138. RTCUIUtils.setVolume(localVideo, 0);
  139. var localVideoContainer = document.getElementById('localVideoWrapper');
  140. // Put the new video always in front
  141. UIUtil.prependChild(localVideoContainer, localVideo);
  142. // Add click handler to both video and video wrapper elements in case
  143. // there's no video.
  144. // onclick has to be used with Temasys plugin
  145. localVideo.onclick = localVideoClick;
  146. let isVideo = stream.videoType != "desktop";
  147. this._enableDisableContextMenu(isVideo);
  148. this.setFlipX(isVideo? APP.settings.getLocalFlipX() : false);
  149. // Attach WebRTC stream
  150. localVideo = stream.attach(localVideo);
  151. let endedHandler = () => {
  152. localVideoContainer.removeChild(localVideo);
  153. // when removing only the video element and we are on stage
  154. // update the stage
  155. if(this.isCurrentlyOnLargeVideo())
  156. this.VideoLayout.updateLargeVideo(this.id);
  157. stream.off(TrackEvents.LOCAL_TRACK_STOPPED, endedHandler);
  158. };
  159. stream.on(TrackEvents.LOCAL_TRACK_STOPPED, endedHandler);
  160. };
  161. /**
  162. * Shows or hides the local video container.
  163. * @param {boolean} true to make the local video container visible, false
  164. * otherwise
  165. */
  166. LocalVideo.prototype.setVisible = function(visible) {
  167. // We toggle the hidden class as an indication to other interested parties
  168. // that this container has been hidden on purpose.
  169. $("#localVideoContainer").toggleClass("hidden");
  170. // We still show/hide it as we need to overwrite the style property if we
  171. // want our action to take effect. Toggling the display property through
  172. // the above css class didn't succeed in overwriting the style.
  173. if (visible) {
  174. $("#localVideoContainer").show();
  175. }
  176. else {
  177. $("#localVideoContainer").hide();
  178. }
  179. };
  180. /**
  181. * Sets the flipX state of the video.
  182. * @param val {boolean} true for flipped otherwise false;
  183. */
  184. LocalVideo.prototype.setFlipX = function (val) {
  185. this.emitter.emit(UIEvents.LOCAL_FLIPX_CHANGED, val);
  186. if(!this.localVideoId)
  187. return;
  188. if(val) {
  189. this.selectVideoElement().addClass("flipVideoX");
  190. } else {
  191. this.selectVideoElement().removeClass("flipVideoX");
  192. }
  193. };
  194. /**
  195. * Builds the context menu for the local video.
  196. */
  197. LocalVideo.prototype._buildContextMenu = function () {
  198. $.contextMenu({
  199. selector: '#' + this.videoSpanId,
  200. zIndex: 10000,
  201. items: {
  202. flip: {
  203. name: "Flip",
  204. callback: () => {
  205. let val = !APP.settings.getLocalFlipX();
  206. this.setFlipX(val);
  207. APP.settings.setLocalFlipX(val);
  208. }
  209. }
  210. },
  211. events: {
  212. show : function(options){
  213. options.items.flip.name =
  214. APP.translation.generateTranslationHTML(
  215. "videothumbnail.flip");
  216. }
  217. }
  218. });
  219. };
  220. /**
  221. * Enables or disables the context menu for the local video.
  222. * @param enable {boolean} true for enable, false for disable
  223. */
  224. LocalVideo.prototype._enableDisableContextMenu = function (enable) {
  225. if($('#' + this.videoSpanId).contextMenu)
  226. $('#' + this.videoSpanId).contextMenu(enable);
  227. };
  228. export default LocalVideo;