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

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