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

RemoteVideo.js 13KB

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