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

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