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

RemoteVideo.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /* global $, APP, interfaceConfig */
  2. import ConnectionIndicator from './ConnectionIndicator';
  3. import SmallVideo from "./SmallVideo";
  4. import AudioLevels from "../audio_levels/AudioLevels";
  5. import UIUtils from "../util/UIUtil";
  6. import UIEvents from '../../../service/UI/UIEvents';
  7. var RTCBrowserType = require("../../RTC/RTCBrowserType");
  8. function RemoteVideo(id, VideoLayout, emitter) {
  9. this.id = id;
  10. this.emitter = emitter;
  11. this.videoSpanId = `participant_${id}`;
  12. this.VideoLayout = VideoLayout;
  13. this.addRemoteVideoContainer();
  14. this.connectionIndicator = new ConnectionIndicator(this, id);
  15. this.setDisplayName();
  16. var nickfield = document.createElement('span');
  17. nickfield.className = "nick";
  18. nickfield.appendChild(document.createTextNode(id));
  19. this.container.appendChild(nickfield);
  20. this.bindHoverHandler();
  21. this.flipX = false;
  22. this.isLocal = false;
  23. SmallVideo.call(this);
  24. }
  25. RemoteVideo.prototype = Object.create(SmallVideo.prototype);
  26. RemoteVideo.prototype.constructor = RemoteVideo;
  27. RemoteVideo.prototype.addRemoteVideoContainer = function() {
  28. this.container = RemoteVideo.createContainer(this.videoSpanId);
  29. if (APP.conference.isModerator) {
  30. this.addRemoteVideoMenu();
  31. }
  32. let {thumbWidth, thumbHeight} = this.VideoLayout.calculateThumbnailSize();
  33. AudioLevels.updateAudioLevelCanvas(this.id, thumbWidth, thumbHeight);
  34. return this.container;
  35. };
  36. /**
  37. * Adds the remote video menu element for the given <tt>id</tt> in the
  38. * given <tt>parentElement</tt>.
  39. *
  40. * @param id the id indicating the video for which we're adding a menu.
  41. * @param parentElement the parent element where this menu will be added
  42. */
  43. if (!interfaceConfig.filmStripOnly) {
  44. RemoteVideo.prototype.addRemoteVideoMenu = function () {
  45. var spanElement = document.createElement('span');
  46. spanElement.className = 'remotevideomenu';
  47. this.container.appendChild(spanElement);
  48. var menuElement = document.createElement('i');
  49. menuElement.className = 'fa fa-angle-down';
  50. menuElement.title = 'Remote user controls';
  51. spanElement.appendChild(menuElement);
  52. var popupmenuElement = document.createElement('ul');
  53. popupmenuElement.className = 'popupmenu';
  54. popupmenuElement.id = `remote_popupmenu_${this.id}`;
  55. spanElement.appendChild(popupmenuElement);
  56. var muteMenuItem = document.createElement('li');
  57. var muteLinkItem = document.createElement('a');
  58. var mutedIndicator = "<i style='float:left;' " +
  59. "class='icon-mic-disabled'></i>";
  60. if (!this.isMuted) {
  61. muteLinkItem.innerHTML = mutedIndicator +
  62. " <div style='width: 90px;margin-left: 20px;' " +
  63. "data-i18n='videothumbnail.domute'></div>";
  64. muteLinkItem.className = 'mutelink';
  65. }
  66. else {
  67. muteLinkItem.innerHTML = mutedIndicator +
  68. " <div style='width: 90px;margin-left: 20px;' " +
  69. "data-i18n='videothumbnail.muted'></div>";
  70. muteLinkItem.className = 'mutelink disabled';
  71. }
  72. muteLinkItem.onclick = (event) => {
  73. if ($(this).attr('disabled')) {
  74. event.preventDefault();
  75. }
  76. var isMute = !!this.isMuted;
  77. this.emitter.emit(UIEvents.REMOTE_AUDIO_MUTED, this.id);
  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 = (event) => {
  101. this.emitter.emit(UIEvents.USER_KICKED, this.id);
  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 MediaStream
  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.id, select);
  135. if (isVideo)
  136. this.VideoLayout.updateRemovedVideo(this.id);
  137. };
  138. /**
  139. * Removes RemoteVideo from the page.
  140. */
  141. RemoteVideo.prototype.remove = function () {
  142. console.log("Remove thumbnail", this.id);
  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.id);
  147. // Remove whole container
  148. if (this.container.parentNode) {
  149. this.container.parentNode.removeChild(this.container);
  150. }
  151. };
  152. RemoteVideo.prototype.waitForPlayback = function (sel, stream) {
  153. var webRtcStream = stream.getOriginalStream();
  154. var isVideo = stream.isVideoTrack();
  155. if (!isVideo || webRtcStream.id === 'mixedmslabel') {
  156. return;
  157. }
  158. var self = this;
  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. //FIXME: weshould use the lib here
  165. //APP.RTC.attachMediaStream(sel, webRtcStream);
  166. }
  167. if (RTCBrowserType.isTemasysPluginUsed()) {
  168. sel = self.selectVideoElement();
  169. }
  170. self.VideoLayout.videoactive(sel, self.id);
  171. sel[0].onplaying = null;
  172. if (RTCBrowserType.isTemasysPluginUsed()) {
  173. // 'currentTime' is used to check if the video has started
  174. // and the value is not set by the plugin, so we do it
  175. sel[0].currentTime = 1;
  176. }
  177. };
  178. sel[0].onplaying = onPlayingHandler;
  179. };
  180. RemoteVideo.prototype.addRemoteStreamElement = function (stream) {
  181. if (!this.container) {
  182. return;
  183. }
  184. let isVideo = stream.isVideoTrack();
  185. isVideo ? this.videoStream = stream : this.audioStream = stream;
  186. // Add click handler.
  187. let onClickHandler = (event) => {
  188. let source = event.target || event.srcElement;
  189. // ignore click if it was done in popup menu
  190. if ($(source).parents('.popupmenu').length === 0) {
  191. this.VideoLayout.handleVideoThumbClicked(false, this.id);
  192. }
  193. // On IE we need to populate this handler on video <object>
  194. // and it does not give event instance as an argument,
  195. // so we check here for methods.
  196. if (event.stopPropagation && event.preventDefault) {
  197. event.stopPropagation();
  198. event.preventDefault();
  199. }
  200. return false;
  201. };
  202. this.container.onclick = onClickHandler;
  203. if(!stream.getOriginalStream())
  204. return;
  205. let streamElement = SmallVideo.createStreamElement(stream);
  206. let newElementId = streamElement.id;
  207. // Put new stream element always in front
  208. UIUtils.prependChild(this.container, streamElement);
  209. let sel = $(`#${newElementId}`);
  210. // If the container is currently visible we attach the stream to the element.
  211. if (!isVideo || (this.container.offsetParent !== null && isVideo)) {
  212. this.waitForPlayback(sel, stream);
  213. stream.attach(sel);
  214. }
  215. // hide element only after stream was (maybe) attached
  216. // because Temasys plugin requires video element
  217. // to be visible to attach the stream
  218. sel.hide();
  219. // reselect
  220. if (RTCBrowserType.isTemasysPluginUsed()) {
  221. sel = $(`#${newElementId}`);
  222. }
  223. sel.click(onClickHandler);
  224. },
  225. /**
  226. * Show/hide peer container for the given id.
  227. */
  228. RemoteVideo.prototype.showPeerContainer = function (state) {
  229. if (!this.container)
  230. return;
  231. var isHide = state === 'hide';
  232. var resizeThumbnails = false;
  233. if (!isHide) {
  234. if (!$(this.container).is(':visible')) {
  235. resizeThumbnails = true;
  236. $(this.container).show();
  237. }
  238. // Call updateView, so that we'll figure out if avatar
  239. // should be displayed based on video muted status and whether or not
  240. // it's in the lastN set
  241. this.updateView();
  242. }
  243. else if ($(this.container).is(':visible') && isHide)
  244. {
  245. resizeThumbnails = true;
  246. $(this.container).hide();
  247. if(this.connectionIndicator)
  248. this.connectionIndicator.hide();
  249. }
  250. if (resizeThumbnails) {
  251. this.VideoLayout.resizeThumbnails();
  252. }
  253. // We want to be able to pin a participant from the contact list, even
  254. // if he's not in the lastN set!
  255. // ContactList.setClickable(id, !isHide);
  256. };
  257. RemoteVideo.prototype.updateResolution = function (resolution) {
  258. if (this.connectionIndicator) {
  259. this.connectionIndicator.updateResolution(resolution);
  260. }
  261. };
  262. RemoteVideo.prototype.removeConnectionIndicator = function () {
  263. if (this.connectionIndicator)
  264. this.connectionIndicator.remove();
  265. };
  266. RemoteVideo.prototype.hideConnectionIndicator = function () {
  267. if (this.connectionIndicator)
  268. this.connectionIndicator.hide();
  269. };
  270. /**
  271. * Updates the remote video menu.
  272. *
  273. * @param id the id indicating the video for which we're adding a menu.
  274. * @param isMuted indicates the current mute state
  275. */
  276. RemoteVideo.prototype.updateRemoteVideoMenu = function (isMuted) {
  277. var muteMenuItem = $(`#remote_popupmenu_${this.id}>li>a.mutelink`);
  278. var mutedIndicator = "<i class='icon-mic-disabled'></i>";
  279. if (muteMenuItem.length) {
  280. var muteLink = muteMenuItem.get(0);
  281. if (isMuted) {
  282. muteLink.innerHTML = mutedIndicator + ' Muted';
  283. muteLink.className = 'mutelink disabled';
  284. }
  285. else {
  286. muteLink.innerHTML = mutedIndicator + ' Mute';
  287. muteLink.className = 'mutelink';
  288. }
  289. }
  290. };
  291. /**
  292. * Updates the Indicator for dominant speaker.
  293. *
  294. * @param isSpeaker indicates the current indicator state
  295. */
  296. RemoteVideo.prototype.updateDominantSpeakerIndicator = function (isSpeaker) {
  297. if (!this.container) {
  298. console.warn( "Unable to set dominant speaker indicator - "
  299. + this.videoSpanId + " does not exist");
  300. return;
  301. }
  302. var indicatorSpan
  303. = $('#' + this.videoSpanId + '>span.dominantspeakerindicator');
  304. // If we do not have an indicator for this video.
  305. if (indicatorSpan.length <= 0) {
  306. indicatorSpan = document.createElement('span');
  307. indicatorSpan.innerHTML
  308. = "<i id='speakerindicatoricon' class='fa fa-bullhorn'></i>";
  309. indicatorSpan.className = 'dominantspeakerindicator';
  310. $('#' + this.videoSpanId)[0].appendChild(indicatorSpan);
  311. // adds a tooltip
  312. UIUtils.setTooltip(indicatorSpan, "speaker", "left");
  313. APP.translation.translateElement($(indicatorSpan));
  314. }
  315. $(indicatorSpan).css("visibility", isSpeaker ? "visible" : "hidden");
  316. };
  317. /**
  318. * Sets the display name for the given video span id.
  319. */
  320. RemoteVideo.prototype.setDisplayName = function(displayName, key) {
  321. if (!this.container) {
  322. console.warn( "Unable to set displayName - " + this.videoSpanId +
  323. " does not exist");
  324. return;
  325. }
  326. var nameSpan = $('#' + this.videoSpanId + '>span.displayname');
  327. // If we already have a display name for this video.
  328. if (nameSpan.length > 0) {
  329. if (displayName && displayName.length > 0) {
  330. $('#' + this.videoSpanId + '_name').html(displayName);
  331. }
  332. else if (key && key.length > 0) {
  333. var nameHtml = APP.translation.generateTranslationHTML(key);
  334. $('#' + this.videoSpanId + '_name').html(nameHtml);
  335. }
  336. else
  337. $('#' + this.videoSpanId + '_name').text(
  338. interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME);
  339. } else {
  340. nameSpan = document.createElement('span');
  341. nameSpan.className = 'displayname';
  342. $('#' + this.videoSpanId)[0].appendChild(nameSpan);
  343. if (displayName && displayName.length > 0) {
  344. nameSpan.innerHTML = displayName;
  345. }
  346. else
  347. nameSpan.innerHTML = interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME;
  348. nameSpan.id = this.videoSpanId + '_name';
  349. }
  350. };
  351. /**
  352. * Removes remote video menu element from video element identified by
  353. * given <tt>videoElementId</tt>.
  354. *
  355. * @param videoElementId the id of local or remote video element.
  356. */
  357. RemoteVideo.prototype.removeRemoteVideoMenu = function() {
  358. var menuSpan = $('#' + this.videoSpanId + '>span.remotevideomenu');
  359. if (menuSpan.length) {
  360. menuSpan.remove();
  361. }
  362. };
  363. RemoteVideo.createContainer = function (spanId) {
  364. var container = document.createElement('span');
  365. container.id = spanId;
  366. container.className = 'videocontainer';
  367. var remotes = document.getElementById('remoteVideos');
  368. return remotes.appendChild(container);
  369. };
  370. export default RemoteVideo;