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

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