您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

LocalVideo.js 9.2KB

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