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

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