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

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