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

RemoteVideo.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. var ConnectionIndicator = require("./ConnectionIndicator");
  2. var SmallVideo = require("./SmallVideo");
  3. var AudioLevels = require("../audio_levels/AudioLevels");
  4. var LargeVideo = require("./LargeVideo");
  5. var Avatar = require("../avatar/Avatar");
  6. var RTCBrowserType = require("../../RTC/RTCBrowserType");
  7. function RemoteVideo(peerJid, VideoLayout)
  8. {
  9. this.peerJid = peerJid;
  10. this.resourceJid = Strophe.getResourceFromJid(peerJid);
  11. this.videoSpanId = 'participant_' + this.resourceJid;
  12. this.VideoLayout = VideoLayout;
  13. this.addRemoteVideoContainer();
  14. this.connectionIndicator = new ConnectionIndicator(
  15. this, this.peerJid);
  16. this.setDisplayName();
  17. var nickfield = document.createElement('span');
  18. nickfield.className = "nick";
  19. nickfield.appendChild(document.createTextNode(this.resourceJid));
  20. this.container.appendChild(nickfield);
  21. this.flipX = false;
  22. this.isLocal = false;
  23. }
  24. RemoteVideo.prototype = Object.create(SmallVideo.prototype);
  25. RemoteVideo.prototype.constructor = RemoteVideo;
  26. RemoteVideo.prototype.addRemoteVideoContainer = function() {
  27. this.container = RemoteVideo.createContainer(this.videoSpanId);
  28. if (APP.xmpp.isModerator())
  29. this.addRemoteVideoMenu();
  30. AudioLevels.updateAudioLevelCanvas(this.peerJid, this.VideoLayout);
  31. return this.container;
  32. };
  33. /**
  34. * Adds the remote video menu element for the given <tt>jid</tt> in the
  35. * given <tt>parentElement</tt>.
  36. *
  37. * @param jid the jid indicating the video for which we're adding a menu.
  38. * @param parentElement the parent element where this menu will be added
  39. */
  40. RemoteVideo.prototype.addRemoteVideoMenu = function () {
  41. var spanElement = document.createElement('span');
  42. spanElement.className = 'remotevideomenu';
  43. this.container.appendChild(spanElement);
  44. var menuElement = document.createElement('i');
  45. menuElement.className = 'fa fa-angle-down';
  46. menuElement.title = 'Remote user controls';
  47. spanElement.appendChild(menuElement);
  48. var popupmenuElement = document.createElement('ul');
  49. popupmenuElement.className = 'popupmenu';
  50. popupmenuElement.id
  51. = 'remote_popupmenu_' + this.getResourceJid();
  52. spanElement.appendChild(popupmenuElement);
  53. var muteMenuItem = document.createElement('li');
  54. var muteLinkItem = document.createElement('a');
  55. var mutedIndicator = "<i style='float:left;' class='icon-mic-disabled'></i>";
  56. if (!this.isMuted) {
  57. muteLinkItem.innerHTML = mutedIndicator +
  58. " <div style='width: 90px;margin-left: 20px;' data-i18n='videothumbnail.domute'></div>";
  59. muteLinkItem.className = 'mutelink';
  60. }
  61. else {
  62. muteLinkItem.innerHTML = mutedIndicator +
  63. " <div style='width: 90px;margin-left: 20px;' data-i18n='videothumbnail.muted'></div>";
  64. muteLinkItem.className = 'mutelink disabled';
  65. }
  66. var self = this;
  67. muteLinkItem.onclick = function(){
  68. if ($(this).attr('disabled') != undefined) {
  69. event.preventDefault();
  70. }
  71. var isMute = self.isMuted == true;
  72. APP.xmpp.setMute(self.peerJid, !isMute);
  73. popupmenuElement.setAttribute('style', 'display:none;');
  74. if (isMute) {
  75. this.innerHTML = mutedIndicator +
  76. " <div style='width: 90px;margin-left: 20px;' data-i18n='videothumbnail.muted'></div>";
  77. this.className = 'mutelink disabled';
  78. }
  79. else {
  80. this.innerHTML = mutedIndicator +
  81. " <div style='width: 90px;margin-left: 20px;' data-i18n='videothumbnail.domute'></div>";
  82. this.className = 'mutelink';
  83. }
  84. };
  85. muteMenuItem.appendChild(muteLinkItem);
  86. popupmenuElement.appendChild(muteMenuItem);
  87. var ejectIndicator = "<i style='float:left;' class='fa fa-eject'></i>";
  88. var ejectMenuItem = document.createElement('li');
  89. var ejectLinkItem = document.createElement('a');
  90. var ejectText = "<div style='width: 90px;margin-left: 20px;' data-i18n='videothumbnail.kick'>&nbsp;</div>";
  91. ejectLinkItem.innerHTML = ejectIndicator + ' ' + ejectText;
  92. ejectLinkItem.onclick = function(){
  93. APP.xmpp.eject(self.peerJid);
  94. popupmenuElement.setAttribute('style', 'display:none;');
  95. };
  96. ejectMenuItem.appendChild(ejectLinkItem);
  97. popupmenuElement.appendChild(ejectMenuItem);
  98. var paddingSpan = document.createElement('span');
  99. paddingSpan.className = 'popupmenuPadding';
  100. popupmenuElement.appendChild(paddingSpan);
  101. APP.translation.translateElement($("#" + popupmenuElement.id + " > li > a > div"));
  102. }
  103. /**
  104. * Removes the remote stream element corresponding to the given stream and
  105. * parent container.
  106. *
  107. * @param stream the stream
  108. * @param isVideo <tt>true</tt> if given <tt>stream</tt> is a video one.
  109. * @param container
  110. */
  111. RemoteVideo.prototype.removeRemoteStreamElement = function (stream, isVideo, id) {
  112. if (!this.container)
  113. return false;
  114. var select = null;
  115. if (isVideo) {
  116. select = $('#' + id);
  117. }
  118. else
  119. select = $('#' + this.videoSpanId + '>audio');
  120. // Mark video as removed to cancel waiting loop(if video is removed
  121. // before has started)
  122. select.removed = true;
  123. select.remove();
  124. var audioCount = $('#' + this.videoSpanId + '>audio').length;
  125. var videoCount = $('#' + this.videoSpanId + '>' + APP.RTC.getVideoElementName()).length;
  126. if (!audioCount && !videoCount) {
  127. console.log("Remove whole user", this.videoSpanId);
  128. if(this.connectionIndicator)
  129. this.connectionIndicator.remove();
  130. // Remove whole container
  131. if (this.container.parentNode)
  132. this.container.parentNode.removeChild(this.container);
  133. this.VideoLayout.resizeThumbnails();
  134. }
  135. if (isVideo)
  136. this.VideoLayout.updateRemovedVideo(this.getResourceJid());
  137. };
  138. RemoteVideo.prototype.waitForPlayback = function (sel, stream) {
  139. var isVideo = stream.getVideoTracks().length > 0;
  140. if (!isVideo || stream.id === 'mixedmslabel') {
  141. return;
  142. }
  143. var self = this;
  144. var resourceJid = this.getResourceJid();
  145. // Register 'onplaying' listener to trigger 'videoactive' on VideoLayout
  146. // when video playback starts
  147. var onPlayingHandler = function () {
  148. // FIXME: why do i have to do this for FF?
  149. if (RTCBrowserType.isFirefox()) {
  150. APP.RTC.attachMediaStream(sel, stream);
  151. }
  152. if (RTCBrowserType.isTemasysPluginUsed()) {
  153. sel = self.VideoLayout.getPeerVideoSel(resourceJid);
  154. }
  155. self.VideoLayout.videoactive(sel, resourceJid);
  156. sel[0].onplaying = null;
  157. if (RTCBrowserType.isTemasysPluginUsed()) {
  158. // 'currentTime' is used to check if the video has started
  159. // and the value is not set by the plugin, so we do it
  160. sel[0].currentTime = 1;
  161. }
  162. };
  163. sel[0].onplaying = onPlayingHandler;
  164. };
  165. RemoteVideo.prototype.addRemoteStreamElement = function (sid, stream, thessrc) {
  166. if (!this.container)
  167. return;
  168. var self = this;
  169. var isVideo = stream.getVideoTracks().length > 0;
  170. var streamElement = SmallVideo.createStreamElement(sid, stream);
  171. var newElementId = streamElement.id;
  172. this.container.appendChild(streamElement);
  173. var sel = $('#' + newElementId);
  174. sel.hide();
  175. // If the container is currently visible we attach the stream.
  176. if (!isVideo || (this.container.offsetParent !== null && isVideo)) {
  177. this.waitForPlayback(sel, stream);
  178. APP.RTC.attachMediaStream(sel, stream);
  179. }
  180. stream.onended = function () {
  181. console.log('stream ended', this);
  182. self.removeRemoteStreamElement(stream, isVideo, newElementId);
  183. };
  184. // Name of video element name is different for IE/Safari
  185. var videoElem = APP.RTC.getVideoElementName();
  186. // Add click handler.
  187. var onClickHandler = function (event) {
  188. self.VideoLayout.handleVideoThumbClicked(false, self.getResourceJid());
  189. // On IE we need to populate this handler on video <object>
  190. // and it does not give event instance as an argument,
  191. // so we check here for methods.
  192. if (event.stopPropagation && event.preventDefault) {
  193. event.stopPropagation();
  194. event.preventDefault();
  195. }
  196. return false;
  197. };
  198. this.container.onclick = onClickHandler;
  199. // reselect
  200. if (RTCBrowserType.isTemasysPluginUsed())
  201. sel = $('#' + newElementId);
  202. sel[0].onclick = onClickHandler;
  203. //FIXME
  204. // Add hover handler
  205. $(this.container).hover(
  206. function() {
  207. self.showDisplayName(true);
  208. },
  209. function() {
  210. // If the video has been "pinned" by the user we want to
  211. // keep the display name on place.
  212. if (!LargeVideo.isLargeVideoVisible() ||
  213. !LargeVideo.isCurrentlyOnLarge(self.getResourceJid()))
  214. self.showDisplayName(false);
  215. }
  216. );
  217. },
  218. /**
  219. * Show/hide peer container for the given resourceJid.
  220. */
  221. RemoteVideo.prototype.showPeerContainer = function (state) {
  222. if (!this.container)
  223. return;
  224. var isHide = state === 'hide';
  225. var resizeThumbnails = false;
  226. if (!isHide) {
  227. if (!$(this.container).is(':visible')) {
  228. resizeThumbnails = true;
  229. $(this.container).show();
  230. }
  231. this.showAvatar(state !== 'show');
  232. }
  233. else if ($(this.container).is(':visible') && isHide)
  234. {
  235. resizeThumbnails = true;
  236. $(this.container).hide();
  237. if(this.connectionIndicator)
  238. this.connectionIndicator.hide();
  239. }
  240. if (resizeThumbnails) {
  241. this.VideoLayout.resizeThumbnails();
  242. }
  243. // We want to be able to pin a participant from the contact list, even
  244. // if he's not in the lastN set!
  245. // ContactList.setClickable(resourceJid, !isHide);
  246. };
  247. RemoteVideo.prototype.removeConnectionIndicator = function () {
  248. if(this.connectionIndicator)
  249. this.connectionIndicator.remove();
  250. }
  251. RemoteVideo.prototype.hideConnectionIndicator = function () {
  252. if(this.connectionIndicator)
  253. this.connectionIndicator.hide();
  254. }
  255. /**
  256. * Updates the remote video menu.
  257. *
  258. * @param jid the jid indicating the video for which we're adding a menu.
  259. * @param isMuted indicates the current mute state
  260. */
  261. RemoteVideo.prototype.updateRemoteVideoMenu = function (isMuted) {
  262. var muteMenuItem
  263. = $('#remote_popupmenu_' + this.getResourceJid() + '>li>a.mutelink');
  264. var mutedIndicator = "<i class='icon-mic-disabled'></i>";
  265. if (muteMenuItem.length) {
  266. var muteLink = muteMenuItem.get(0);
  267. if (isMuted === 'true') {
  268. muteLink.innerHTML = mutedIndicator + ' Muted';
  269. muteLink.className = 'mutelink disabled';
  270. }
  271. else {
  272. muteLink.innerHTML = mutedIndicator + ' Mute';
  273. muteLink.className = 'mutelink';
  274. }
  275. }
  276. };
  277. /**
  278. * Sets the display name for the given video span id.
  279. */
  280. RemoteVideo.prototype.setDisplayName = function(displayName, key) {
  281. if (!this.container) {
  282. console.warn(
  283. "Unable to set displayName - " + this.videoSpanId + " does not exist");
  284. return;
  285. }
  286. var nameSpan = $('#' + this.videoSpanId + '>span.displayname');
  287. // If we already have a display name for this video.
  288. if (nameSpan.length > 0) {
  289. if (displayName && displayName.length > 0)
  290. {
  291. $('#' + this.videoSpanId + '_name').html(displayName);
  292. }
  293. else if (key && key.length > 0)
  294. {
  295. var nameHtml = APP.translation.generateTranslationHTML(key);
  296. $('#' + this.videoSpanId + '_name').html(nameHtml);
  297. }
  298. else
  299. $('#' + this.videoSpanId + '_name').text(
  300. interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME);
  301. } else {
  302. nameSpan = document.createElement('span');
  303. nameSpan.className = 'displayname';
  304. $('#' + this.videoSpanId)[0].appendChild(nameSpan);
  305. if (displayName && displayName.length > 0) {
  306. nameSpan.innerText = displayName;
  307. }
  308. else
  309. nameSpan.innerText = interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME;
  310. nameSpan.id = this.videoSpanId + '_name';
  311. }
  312. }
  313. /**
  314. * Removes remote video menu element from video element identified by
  315. * given <tt>videoElementId</tt>.
  316. *
  317. * @param videoElementId the id of local or remote video element.
  318. */
  319. RemoteVideo.prototype.removeRemoteVideoMenu = function() {
  320. var menuSpan = $('#' + this.videoSpanId + '>span.remotevideomenu');
  321. if (menuSpan.length) {
  322. menuSpan.remove();
  323. }
  324. };
  325. RemoteVideo.prototype.getResourceJid = function () {
  326. if (!this.resourceJid) {
  327. console.error("Undefined resource jid");
  328. }
  329. return this.resourceJid;
  330. };
  331. RemoteVideo.createContainer = function (spanId) {
  332. var container = document.createElement('span');
  333. container.id = spanId;
  334. container.className = 'videocontainer';
  335. var remotes = document.getElementById('remoteVideos');
  336. return remotes.appendChild(container);
  337. };
  338. module.exports = RemoteVideo;