您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

RemoteVideo.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. function RemoteVideo(id, VideoLayout, emitter) {
  8. this.id = id;
  9. this.emitter = emitter;
  10. this.videoSpanId = `participant_${id}`;
  11. SmallVideo.call(this, VideoLayout);
  12. this.addRemoteVideoContainer();
  13. this.connectionIndicator = new ConnectionIndicator(this, id);
  14. this.setDisplayName();
  15. this.bindHoverHandler();
  16. this.flipX = false;
  17. this.isLocal = false;
  18. }
  19. RemoteVideo.prototype = Object.create(SmallVideo.prototype);
  20. RemoteVideo.prototype.constructor = RemoteVideo;
  21. RemoteVideo.prototype.addRemoteVideoContainer = function() {
  22. this.container = RemoteVideo.createContainer(this.videoSpanId);
  23. if (APP.conference.isModerator) {
  24. this.addRemoteVideoMenu();
  25. }
  26. let {thumbWidth, thumbHeight} = this.VideoLayout.resizeThumbnails();
  27. AudioLevels.updateAudioLevelCanvas(this.id, thumbWidth, thumbHeight);
  28. return this.container;
  29. };
  30. /**
  31. * Adds the remote video menu element for the given <tt>id</tt> in the
  32. * given <tt>parentElement</tt>.
  33. *
  34. * @param id the id indicating the video for which we're adding a menu.
  35. * @param parentElement the parent element where this menu will be added
  36. */
  37. if (!interfaceConfig.filmStripOnly) {
  38. RemoteVideo.prototype.addRemoteVideoMenu = function () {
  39. var spanElement = document.createElement('span');
  40. spanElement.className = 'remotevideomenu';
  41. this.container.appendChild(spanElement);
  42. var menuElement = document.createElement('i');
  43. menuElement.className = 'fa fa-angle-down';
  44. menuElement.title = 'Remote user controls';
  45. spanElement.appendChild(menuElement);
  46. var popupmenuElement = document.createElement('ul');
  47. popupmenuElement.className = 'popupmenu';
  48. popupmenuElement.id = `remote_popupmenu_${this.id}`;
  49. spanElement.appendChild(popupmenuElement);
  50. var muteMenuItem = document.createElement('li');
  51. var muteLinkItem = document.createElement('a');
  52. var mutedIndicator = "<i style='float:left;' " +
  53. "class='icon-mic-disabled'></i>";
  54. if (!this.isMuted) {
  55. muteLinkItem.innerHTML = mutedIndicator +
  56. " <div style='width: 90px;margin-left: 20px;' " +
  57. "data-i18n='videothumbnail.domute'></div>";
  58. muteLinkItem.className = 'mutelink';
  59. }
  60. else {
  61. muteLinkItem.innerHTML = mutedIndicator +
  62. " <div style='width: 90px;margin-left: 20px;' " +
  63. "data-i18n='videothumbnail.muted'></div>";
  64. muteLinkItem.className = 'mutelink disabled';
  65. }
  66. muteLinkItem.onclick = (event) => {
  67. if ($(this).attr('disabled')) {
  68. event.preventDefault();
  69. }
  70. var isMute = !!this.isMuted;
  71. this.emitter.emit(UIEvents.REMOTE_AUDIO_MUTED, this.id);
  72. popupmenuElement.setAttribute('style', 'display:none;');
  73. if (isMute) {
  74. this.innerHTML = mutedIndicator +
  75. " <div style='width: 90px;margin-left: 20px;' " +
  76. "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;' " +
  82. "data-i18n='videothumbnail.domute'></div>";
  83. this.className = 'mutelink';
  84. }
  85. };
  86. muteMenuItem.appendChild(muteLinkItem);
  87. popupmenuElement.appendChild(muteMenuItem);
  88. var ejectIndicator = "<i style='float:left;' class='fa fa-eject'></i>";
  89. var ejectMenuItem = document.createElement('li');
  90. var ejectLinkItem = document.createElement('a');
  91. var ejectText = "<div style='width: 90px;margin-left: 20px;' " +
  92. "data-i18n='videothumbnail.kick'>&nbsp;</div>";
  93. ejectLinkItem.innerHTML = ejectIndicator + ' ' + ejectText;
  94. ejectLinkItem.onclick = (event) => {
  95. this.emitter.emit(UIEvents.USER_KICKED, this.id);
  96. popupmenuElement.setAttribute('style', 'display:none;');
  97. };
  98. ejectMenuItem.appendChild(ejectLinkItem);
  99. popupmenuElement.appendChild(ejectMenuItem);
  100. var paddingSpan = document.createElement('span');
  101. paddingSpan.className = 'popupmenuPadding';
  102. popupmenuElement.appendChild(paddingSpan);
  103. APP.translation.translateElement(
  104. $("#" + popupmenuElement.id + " > li > a > div"));
  105. };
  106. } else {
  107. RemoteVideo.prototype.addRemoteVideoMenu = function() {};
  108. }
  109. /**
  110. * Removes the remote stream element corresponding to the given stream and
  111. * parent container.
  112. *
  113. * @param stream the MediaStream
  114. * @param isVideo <tt>true</tt> if given <tt>stream</tt> is a video one.
  115. */
  116. RemoteVideo.prototype.removeRemoteStreamElement = function (stream) {
  117. if (!this.container)
  118. return false;
  119. var isVideo = stream.isVideoTrack();
  120. var elementID = SmallVideo.getStreamElementID(stream);
  121. var select = null;
  122. if (isVideo) {
  123. select = $('#' + elementID);
  124. }
  125. else
  126. select = $('#' + this.videoSpanId + '>audio');
  127. select.remove();
  128. console.info((isVideo ? "Video" : "Audio") +
  129. " removed " + this.id, select);
  130. if (isVideo)
  131. this.VideoLayout.updateRemovedVideo(this.id);
  132. };
  133. /**
  134. * Removes RemoteVideo from the page.
  135. */
  136. RemoteVideo.prototype.remove = function () {
  137. console.log("Remove thumbnail", this.id);
  138. this.removeConnectionIndicator();
  139. // Make sure that the large video is updated if are removing its
  140. // corresponding small video.
  141. this.VideoLayout.updateRemovedVideo(this.id);
  142. // Remove whole container
  143. if (this.container.parentNode) {
  144. this.container.parentNode.removeChild(this.container);
  145. }
  146. };
  147. RemoteVideo.prototype.waitForPlayback = function (streamElement, stream) {
  148. var webRtcStream = stream.getOriginalStream();
  149. var isVideo = stream.isVideoTrack();
  150. if (!isVideo || webRtcStream.id === 'mixedmslabel') {
  151. return;
  152. }
  153. var self = this;
  154. // Register 'onplaying' listener to trigger 'videoactive' on VideoLayout
  155. // when video playback starts
  156. var onPlayingHandler = function () {
  157. self.VideoLayout.videoactive(streamElement, self.id);
  158. streamElement.onplaying = null;
  159. };
  160. streamElement.onplaying = onPlayingHandler;
  161. };
  162. /**
  163. * Checks whether or not video stream exists and has started for this
  164. * RemoteVideo instance. This is checked by trying to select video element in
  165. * this container and checking if 'currentTime' field's value is greater than 0.
  166. *
  167. * @returns {*|boolean} true if this RemoteVideo has active video stream running
  168. */
  169. RemoteVideo.prototype.hasVideoStarted = function () {
  170. var videoSelector = this.selectVideoElement();
  171. return videoSelector.length && videoSelector[0].currentTime > 0;
  172. };
  173. RemoteVideo.prototype.addRemoteStreamElement = function (stream) {
  174. if (!this.container) {
  175. return;
  176. }
  177. let isVideo = stream.isVideoTrack();
  178. isVideo ? this.videoStream = stream : this.audioStream = stream;
  179. // Add click handler.
  180. let onClickHandler = (event) => {
  181. let source = event.target || event.srcElement;
  182. // ignore click if it was done in popup menu
  183. if ($(source).parents('.popupmenu').length === 0) {
  184. this.VideoLayout.handleVideoThumbClicked(false, this.id);
  185. }
  186. // On IE we need to populate this handler on video <object>
  187. // and it does not give event instance as an argument,
  188. // so we check here for methods.
  189. if (event.stopPropagation && event.preventDefault) {
  190. event.stopPropagation();
  191. event.preventDefault();
  192. }
  193. return false;
  194. };
  195. this.container.onclick = onClickHandler;
  196. if(!stream.getOriginalStream())
  197. return;
  198. let streamElement = SmallVideo.createStreamElement(stream);
  199. let newElementId = streamElement.id;
  200. // Put new stream element always in front
  201. UIUtils.prependChild(this.container, streamElement);
  202. // If we hide element when Temasys plugin is used then
  203. // we'll never receive 'onplay' event and other logic won't work as expected
  204. // NOTE: hiding will not have effect when Temasys plugin is in use, as
  205. // calling attach will show it back
  206. $(streamElement).hide();
  207. // If the container is currently visible
  208. // we attach the stream to the element.
  209. if (!isVideo || (this.container.offsetParent !== null && isVideo)) {
  210. this.waitForPlayback(streamElement, stream);
  211. streamElement = stream.attach(streamElement);
  212. }
  213. $(streamElement).click(onClickHandler);
  214. },
  215. /**
  216. * Show/hide peer container for the given id.
  217. */
  218. RemoteVideo.prototype.showPeerContainer = function (state) {
  219. if (!this.container)
  220. return;
  221. var isHide = state === 'hide';
  222. var resizeThumbnails = false;
  223. if (!isHide) {
  224. if (!$(this.container).is(':visible')) {
  225. resizeThumbnails = true;
  226. $(this.container).show();
  227. }
  228. // Call updateView, so that we'll figure out if avatar
  229. // should be displayed based on video muted status and whether or not
  230. // it's in the lastN set
  231. this.updateView();
  232. }
  233. else if ($(this.container).is(':visible') && isHide)
  234. {
  235. resizeThumbnails = true;
  236. $(this.container).hide();
  237. if(this.connectionIndicator)
  238. this.connectionIndicator.hide();
  239. }
  240. if (resizeThumbnails) {
  241. this.VideoLayout.resizeThumbnails();
  242. }
  243. // We want to be able to pin a participant from the contact list, even
  244. // if he's not in the lastN set!
  245. // ContactList.setClickable(id, !isHide);
  246. };
  247. RemoteVideo.prototype.updateResolution = function (resolution) {
  248. if (this.connectionIndicator) {
  249. this.connectionIndicator.updateResolution(resolution);
  250. }
  251. };
  252. RemoteVideo.prototype.removeConnectionIndicator = function () {
  253. if (this.connectionIndicator)
  254. this.connectionIndicator.remove();
  255. };
  256. RemoteVideo.prototype.hideConnectionIndicator = function () {
  257. if (this.connectionIndicator)
  258. this.connectionIndicator.hide();
  259. };
  260. /**
  261. * Updates the remote video menu.
  262. *
  263. * @param id the id indicating the video for which we're adding a menu.
  264. * @param isMuted indicates the current mute state
  265. */
  266. RemoteVideo.prototype.updateRemoteVideoMenu = function (isMuted) {
  267. var muteMenuItem = $(`#remote_popupmenu_${this.id}>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. * Sets the display name for the given video span id.
  283. */
  284. RemoteVideo.prototype.setDisplayName = function(displayName, key) {
  285. if (!this.container) {
  286. console.warn( "Unable to set displayName - " + this.videoSpanId +
  287. " does not exist");
  288. return;
  289. }
  290. var nameSpan = $('#' + this.videoSpanId + '>span.displayname');
  291. // If we already have a display name for this video.
  292. if (nameSpan.length > 0) {
  293. if (displayName && displayName.length > 0) {
  294. $('#' + this.videoSpanId + '_name').text(displayName);
  295. }
  296. else if (key && key.length > 0) {
  297. var nameHtml = APP.translation.generateTranslationHTML(key);
  298. $('#' + this.videoSpanId + '_name').html(nameHtml);
  299. }
  300. else
  301. $('#' + this.videoSpanId + '_name').text(
  302. interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME);
  303. } else {
  304. nameSpan = document.createElement('span');
  305. nameSpan.className = 'displayname';
  306. $('#' + this.videoSpanId)[0].appendChild(nameSpan);
  307. if (displayName && displayName.length > 0) {
  308. $(nameSpan).text(displayName);
  309. } else {
  310. nameSpan.innerHTML = interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME;
  311. }
  312. nameSpan.id = this.videoSpanId + '_name';
  313. }
  314. };
  315. /**
  316. * Removes remote video menu element from video element identified by
  317. * given <tt>videoElementId</tt>.
  318. *
  319. * @param videoElementId the id of local or remote video element.
  320. */
  321. RemoteVideo.prototype.removeRemoteVideoMenu = function() {
  322. var menuSpan = $('#' + this.videoSpanId + '>span.remotevideomenu');
  323. if (menuSpan.length) {
  324. menuSpan.remove();
  325. }
  326. };
  327. RemoteVideo.createContainer = function (spanId) {
  328. var container = document.createElement('span');
  329. container.id = spanId;
  330. container.className = 'videocontainer';
  331. var remotes = document.getElementById('remoteVideos');
  332. return remotes.appendChild(container);
  333. };
  334. export default RemoteVideo;