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

RemoteVideo.js 14KB

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