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

RemoteVideo.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 LargeVideo = require("./LargeVideo");
  6. var Avatar = require("../avatar/Avatar");
  7. var RTCBrowserType = require("../../RTC/RTCBrowserType");
  8. function RemoteVideo(peerJid, VideoLayout) {
  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. if(!config.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') != undefined) {
  72. event.preventDefault();
  73. }
  74. var isMute = self.isMuted == true;
  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.VideoLayout.getPeerVideoSel(resourceJid);
  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. this.container.appendChild(streamElement);
  181. var sel = $('#' + newElementId);
  182. sel.hide();
  183. // If the container is currently visible we attach the stream.
  184. if (!isVideo || (this.container.offsetParent !== null && isVideo)) {
  185. this.waitForPlayback(sel, stream);
  186. APP.RTC.attachMediaStream(sel, stream);
  187. }
  188. stream.onended = function () {
  189. console.log('stream ended', this);
  190. self.removeRemoteStreamElement(stream, isVideo, newElementId);
  191. };
  192. // Name of video element name is different for IE/Safari
  193. var videoElem = APP.RTC.getVideoElementName();
  194. // Add click handler.
  195. var onClickHandler = function (event) {
  196. self.VideoLayout.handleVideoThumbClicked(false, self.getResourceJid());
  197. // On IE we need to populate this handler on video <object>
  198. // and it does not give event instance as an argument,
  199. // so we check here for methods.
  200. if (event.stopPropagation && event.preventDefault) {
  201. event.stopPropagation();
  202. event.preventDefault();
  203. }
  204. return false;
  205. };
  206. this.container.onclick = onClickHandler;
  207. // reselect
  208. if (RTCBrowserType.isTemasysPluginUsed())
  209. sel = $('#' + newElementId);
  210. sel[0].onclick = onClickHandler;
  211. //FIXME
  212. // Add hover handler
  213. $(this.container).hover(
  214. function() {
  215. self.showDisplayName(true);
  216. },
  217. function() {
  218. // If the video has been "pinned" by the user we want to
  219. // keep the display name on place.
  220. if (!LargeVideo.isLargeVideoVisible() ||
  221. !LargeVideo.isCurrentlyOnLarge(self.getResourceJid()))
  222. self.showDisplayName(false);
  223. }
  224. );
  225. },
  226. /**
  227. * Show/hide peer container for the given resourceJid.
  228. */
  229. RemoteVideo.prototype.showPeerContainer = function (state) {
  230. if (!this.container)
  231. return;
  232. var isHide = state === 'hide';
  233. var resizeThumbnails = false;
  234. if (!isHide) {
  235. if (!$(this.container).is(':visible')) {
  236. resizeThumbnails = true;
  237. $(this.container).show();
  238. }
  239. this.showAvatar(state !== 'show');
  240. }
  241. else if ($(this.container).is(':visible') && isHide)
  242. {
  243. resizeThumbnails = true;
  244. $(this.container).hide();
  245. if(this.connectionIndicator)
  246. this.connectionIndicator.hide();
  247. }
  248. if (resizeThumbnails) {
  249. this.VideoLayout.resizeThumbnails();
  250. }
  251. // We want to be able to pin a participant from the contact list, even
  252. // if he's not in the lastN set!
  253. // ContactList.setClickable(resourceJid, !isHide);
  254. };
  255. RemoteVideo.prototype.removeConnectionIndicator = function () {
  256. if (this.connectionIndicator)
  257. this.connectionIndicator.remove();
  258. };
  259. RemoteVideo.prototype.hideConnectionIndicator = function () {
  260. if (this.connectionIndicator)
  261. this.connectionIndicator.hide();
  262. };
  263. /**
  264. * Updates the remote video menu.
  265. *
  266. * @param jid the jid indicating the video for which we're adding a menu.
  267. * @param isMuted indicates the current mute state
  268. */
  269. RemoteVideo.prototype.updateRemoteVideoMenu = function (isMuted) {
  270. var muteMenuItem
  271. = $('#remote_popupmenu_' + this.getResourceJid() + '>li>a.mutelink');
  272. var mutedIndicator = "<i class='icon-mic-disabled'></i>";
  273. if (muteMenuItem.length) {
  274. var muteLink = muteMenuItem.get(0);
  275. if (isMuted === 'true') {
  276. muteLink.innerHTML = mutedIndicator + ' Muted';
  277. muteLink.className = 'mutelink disabled';
  278. }
  279. else {
  280. muteLink.innerHTML = mutedIndicator + ' Mute';
  281. muteLink.className = 'mutelink';
  282. }
  283. }
  284. };
  285. /**
  286. * Sets the display name for the given video span id.
  287. */
  288. RemoteVideo.prototype.setDisplayName = function(displayName, key) {
  289. if (!this.container) {
  290. console.warn( "Unable to set displayName - " + this.videoSpanId +
  291. " does not exist");
  292. return;
  293. }
  294. var nameSpan = $('#' + this.videoSpanId + '>span.displayname');
  295. // If we already have a display name for this video.
  296. if (nameSpan.length > 0) {
  297. if (displayName && displayName.length > 0) {
  298. $('#' + this.videoSpanId + '_name').html(displayName);
  299. }
  300. else if (key && key.length > 0) {
  301. var nameHtml = APP.translation.generateTranslationHTML(key);
  302. $('#' + this.videoSpanId + '_name').html(nameHtml);
  303. }
  304. else
  305. $('#' + this.videoSpanId + '_name').text(
  306. interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME);
  307. } else {
  308. nameSpan = document.createElement('span');
  309. nameSpan.className = 'displayname';
  310. $('#' + this.videoSpanId)[0].appendChild(nameSpan);
  311. if (displayName && displayName.length > 0) {
  312. nameSpan.innerText = displayName;
  313. }
  314. else
  315. nameSpan.innerText = interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME;
  316. nameSpan.id = this.videoSpanId + '_name';
  317. }
  318. };
  319. /**
  320. * Removes remote video menu element from video element identified by
  321. * given <tt>videoElementId</tt>.
  322. *
  323. * @param videoElementId the id of local or remote video element.
  324. */
  325. RemoteVideo.prototype.removeRemoteVideoMenu = function() {
  326. var menuSpan = $('#' + this.videoSpanId + '>span.remotevideomenu');
  327. if (menuSpan.length) {
  328. menuSpan.remove();
  329. }
  330. };
  331. RemoteVideo.prototype.getResourceJid = function () {
  332. if (!this.resourceJid) {
  333. console.error("Undefined resource jid");
  334. }
  335. return this.resourceJid;
  336. };
  337. RemoteVideo.createContainer = function (spanId) {
  338. var container = document.createElement('span');
  339. container.id = spanId;
  340. container.className = 'videocontainer';
  341. var remotes = document.getElementById('remoteVideos');
  342. return remotes.appendChild(container);
  343. };
  344. module.exports = RemoteVideo;