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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* global $, interfaceConfig, APP */
  2. var SmallVideo = require("./SmallVideo");
  3. var ConnectionIndicator = require("./ConnectionIndicator");
  4. var UIUtil = require("../util/UIUtil");
  5. var UIEvents = require("../../../service/UI/UIEvents");
  6. var LargeVideo = require("./LargeVideo");
  7. var RTCBrowserType = require("../../RTC/RTCBrowserType");
  8. function LocalVideo(VideoLayout, emitter) {
  9. this.videoSpanId = "localVideoContainer";
  10. this.container = $("#localVideoContainer").get(0);
  11. this.bindHoverHandler();
  12. this.VideoLayout = VideoLayout;
  13. this.flipX = true;
  14. this.isLocal = true;
  15. this.peerJid = null;
  16. this.emitter = emitter;
  17. }
  18. LocalVideo.prototype = Object.create(SmallVideo.prototype);
  19. LocalVideo.prototype.constructor = LocalVideo;
  20. /**
  21. * Creates the edit display name button.
  22. *
  23. * @returns {object} the edit button
  24. */
  25. function createEditDisplayNameButton() {
  26. var editButton = document.createElement('a');
  27. editButton.className = 'displayname';
  28. UIUtil.setTooltip(editButton,
  29. "videothumbnail.editnickname",
  30. "top");
  31. editButton.innerHTML = '<i class="fa fa-pencil"></i>';
  32. return editButton;
  33. }
  34. /**
  35. * Sets the display name for the given video span id.
  36. */
  37. LocalVideo.prototype.setDisplayName = function(displayName, key) {
  38. if (!this.container) {
  39. console.warn(
  40. "Unable to set displayName - " + this.videoSpanId +
  41. " does not exist");
  42. return;
  43. }
  44. var nameSpan = $('#' + this.videoSpanId + '>span.displayname');
  45. var defaultLocalDisplayName = APP.translation.generateTranslationHTML(
  46. interfaceConfig.DEFAULT_LOCAL_DISPLAY_NAME);
  47. var meHTML;
  48. // If we already have a display name for this video.
  49. if (nameSpan.length > 0) {
  50. if (nameSpan.text() !== displayName) {
  51. if (displayName && displayName.length > 0) {
  52. meHTML = APP.translation.generateTranslationHTML("me");
  53. $('#localDisplayName').html(displayName + ' (' + meHTML + ')');
  54. } else {
  55. $('#localDisplayName').html(defaultLocalDisplayName);
  56. }
  57. }
  58. } else {
  59. var editButton = createEditDisplayNameButton();
  60. nameSpan = document.createElement('span');
  61. nameSpan.className = 'displayname';
  62. $('#' + this.videoSpanId)[0].appendChild(nameSpan);
  63. if (displayName && displayName.length > 0) {
  64. meHTML = APP.translation.generateTranslationHTML("me");
  65. nameSpan.innerHTML = displayName + meHTML;
  66. }
  67. else {
  68. nameSpan.innerHTML = defaultLocalDisplayName;
  69. }
  70. nameSpan.id = 'localDisplayName';
  71. this.container.appendChild(editButton);
  72. //translates popover of edit button
  73. APP.translation.translateElement($("a.displayname"));
  74. var editableText = document.createElement('input');
  75. editableText.className = 'displayname';
  76. editableText.type = 'text';
  77. editableText.id = 'editDisplayName';
  78. if (displayName && displayName.length) {
  79. editableText.value = displayName;
  80. }
  81. var defaultNickname = APP.translation.translateString(
  82. "defaultNickname", {name: "Jane Pink"});
  83. editableText.setAttribute('style', 'display:none;');
  84. editableText.setAttribute('data-18n',
  85. '[placeholder]defaultNickname');
  86. editableText.setAttribute("data-i18n-options",
  87. JSON.stringify({name: "Jane Pink"}));
  88. editableText.setAttribute("placeholder", defaultNickname);
  89. this.container.appendChild(editableText);
  90. var self = this;
  91. $('#localVideoContainer .displayname')
  92. .bind("click", function (e) {
  93. var editDisplayName = $('#editDisplayName');
  94. e.preventDefault();
  95. e.stopPropagation();
  96. $('#localDisplayName').hide();
  97. editDisplayName.show();
  98. editDisplayName.focus();
  99. editDisplayName.select();
  100. editDisplayName.one("focusout", function (e) {
  101. self.VideoLayout.inputDisplayNameHandler(this.value);
  102. $('#editDisplayName').hide();
  103. });
  104. editDisplayName.on('keydown', function (e) {
  105. if (e.keyCode === 13) {
  106. e.preventDefault();
  107. $('#editDisplayName').hide();
  108. self.VideoLayout.inputDisplayNameHandler(this.value);
  109. }
  110. });
  111. });
  112. }
  113. };
  114. LocalVideo.prototype.inputDisplayNameHandler = function (name) {
  115. this.emitter.emit(UIEvents.NICKNAME_CHANGED, UIUtil.escapeHtml(name));
  116. };
  117. LocalVideo.prototype.createConnectionIndicator = function() {
  118. if(this.connectionIndicator)
  119. return;
  120. this.connectionIndicator = new ConnectionIndicator(this, null);
  121. };
  122. LocalVideo.prototype.changeVideo = function (stream, isMuted) {
  123. var self = this;
  124. function localVideoClick(event) {
  125. // FIXME: with Temasys plugin event arg is not an event, but
  126. // the clicked object itself, so we have to skip this call
  127. if (event.stopPropagation) {
  128. event.stopPropagation();
  129. }
  130. self.VideoLayout.handleVideoThumbClicked(
  131. true,
  132. APP.xmpp.myResource());
  133. }
  134. var localVideoContainerSelector = $('#localVideoContainer');
  135. localVideoContainerSelector.off('click');
  136. localVideoContainerSelector.on('click', localVideoClick);
  137. if(isMuted) {
  138. APP.UI.setVideoMute(true);
  139. return;
  140. }
  141. this.flipX = stream.videoType != "screen";
  142. var localVideo = document.createElement('video');
  143. localVideo.id = 'localVideo_' +
  144. APP.RTC.getStreamID(stream.getOriginalStream());
  145. if (!RTCBrowserType.isIExplorer()) {
  146. localVideo.autoplay = true;
  147. localVideo.volume = 0; // is it required if audio is separated ?
  148. }
  149. localVideo.oncontextmenu = function () { return false; };
  150. var localVideoContainer = document.getElementById('localVideoWrapper');
  151. // Put the new video always in front
  152. UIUtil.prependChild(localVideoContainer, localVideo);
  153. var localVideoSelector = $('#' + localVideo.id);
  154. // Add click handler to both video and video wrapper elements in case
  155. // there's no video.
  156. // onclick has to be used with Temasys plugin
  157. localVideo.onclick = localVideoClick;
  158. if (this.flipX) {
  159. localVideoSelector.addClass("flipVideoX");
  160. }
  161. // Attach WebRTC stream
  162. APP.RTC.attachMediaStream(localVideoSelector, stream.getOriginalStream());
  163. // Add stream ended handler
  164. APP.RTC.addMediaStreamInactiveHandler(
  165. stream.getOriginalStream(), function () {
  166. // We have to re-select after attach when Temasys plugin is used,
  167. // because <video> element is replaced with <object>
  168. localVideo = $('#' + localVideo.id)[0];
  169. localVideoContainer.removeChild(localVideo);
  170. self.VideoLayout.updateRemovedVideo(APP.xmpp.myResource());
  171. });
  172. };
  173. LocalVideo.prototype.joined = function (jid) {
  174. this.peerJid = jid;
  175. };
  176. LocalVideo.prototype.getResourceJid = function () {
  177. var myResource = APP.xmpp.myResource();
  178. if (!myResource) {
  179. console.error("Requested local resource before we're in the MUC");
  180. }
  181. return myResource;
  182. };
  183. module.exports = LocalVideo;