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

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