Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

RemoteVideo.js 13KB

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