var ConnectionIndicator = require("./ConnectionIndicator");
var SmallVideo = require("./SmallVideo");
var AudioLevels = require("../audio_levels/AudioLevels");
var LargeVideo = require("./LargeVideo");
var Avatar = require("../avatar/Avatar");
var RTCBrowserType = require("../../RTC/RTCBrowserType");
function RemoteVideo(peerJid, VideoLayout)
{
this.peerJid = peerJid;
this.resourceJid = Strophe.getResourceFromJid(peerJid);
this.videoSpanId = 'participant_' + this.resourceJid;
this.VideoLayout = VideoLayout;
this.addRemoteVideoContainer();
this.connectionIndicator = new ConnectionIndicator(
this, this.peerJid);
this.setDisplayName();
var nickfield = document.createElement('span');
nickfield.className = "nick";
nickfield.appendChild(document.createTextNode(this.resourceJid));
this.container.appendChild(nickfield);
this.flipX = false;
}
RemoteVideo.prototype = Object.create(SmallVideo.prototype);
RemoteVideo.prototype.constructor = RemoteVideo;
RemoteVideo.prototype.addRemoteVideoContainer = function() {
this.container = RemoteVideo.createContainer(this.videoSpanId);
if (APP.xmpp.isModerator())
this.addRemoteVideoMenu();
AudioLevels.updateAudioLevelCanvas(this.peerJid, this.VideoLayout);
return this.container;
};
/**
* Adds the remote video menu element for the given jid in the
* given parentElement.
*
* @param jid the jid indicating the video for which we're adding a menu.
* @param parentElement the parent element where this menu will be added
*/
RemoteVideo.prototype.addRemoteVideoMenu = function () {
var spanElement = document.createElement('span');
spanElement.className = 'remotevideomenu';
this.container.appendChild(spanElement);
var menuElement = document.createElement('i');
menuElement.className = 'fa fa-angle-down';
menuElement.title = 'Remote user controls';
spanElement.appendChild(menuElement);
var popupmenuElement = document.createElement('ul');
popupmenuElement.className = 'popupmenu';
popupmenuElement.id
= 'remote_popupmenu_' + this.resourceJid;
spanElement.appendChild(popupmenuElement);
var muteMenuItem = document.createElement('li');
var muteLinkItem = document.createElement('a');
var mutedIndicator = "";
if (!this.isMuted) {
muteLinkItem.innerHTML = mutedIndicator +
"
";
ejectLinkItem.innerHTML = ejectIndicator + ' ' + ejectText;
ejectLinkItem.onclick = function(){
APP.xmpp.eject(self.peerJid);
popupmenuElement.setAttribute('style', 'display:none;');
};
ejectMenuItem.appendChild(ejectLinkItem);
popupmenuElement.appendChild(ejectMenuItem);
var paddingSpan = document.createElement('span');
paddingSpan.className = 'popupmenuPadding';
popupmenuElement.appendChild(paddingSpan);
APP.translation.translateElement($("#" + popupmenuElement.id + " > li > a > div"));
}
/**
* Removes the remote stream element corresponding to the given stream and
* parent container.
*
* @param stream the stream
* @param isVideo true if given stream is a video one.
* @param container
*/
RemoteVideo.prototype.removeRemoteStreamElement = function (stream, isVideo, id) {
if (!this.container)
return false;
var select = null;
if (isVideo) {
select = $('#' + id);
}
else
select = $('#' + this.videoSpanId + '>audio');
// Mark video as removed to cancel waiting loop(if video is removed
// before has started)
select.removed = true;
select.remove();
var audioCount = $('#' + this.videoSpanId + '>audio').length;
var videoCount = $('#' + this.videoSpanId + '>' + APP.RTC.getVideoElementName()).length;
if (!audioCount && !videoCount) {
console.log("Remove whole user", this.videoSpanId);
if(this.connectionIndicator)
this.connectionIndicator.remove();
// Remove whole container
if (this.container.parentNode)
this.container.parentNode.removeChild(this.container);
this.VideoLayout.resizeThumbnails();
}
if (isVideo)
this.VideoLayout.updateRemovedVideo(this.resourceJid);
};
RemoteVideo.prototype.addRemoteStreamElement = function (sid, stream, thessrc) {
if (!this.container)
return;
var self = this;
var isVideo = stream.getVideoTracks().length > 0;
var streamElement = SmallVideo.createStreamElement(sid, stream);
var newElementId = streamElement.id;
this.container.appendChild(streamElement);
var sel = $('#' + newElementId);
sel.hide();
// If the container is currently visible we attach the stream.
if (!isVideo
|| (this.container.offsetParent !== null && isVideo)) {
// Register 'onplaying' listener to trigger 'videoactive' on VideoLayout
// when video playback starts
if (isVideo && stream.id !== 'mixedmslabel') {
var onPlayingHandler = function () {
// FIXME: why do i have to do this for FF?
if (RTCBrowserType.isFirefox()) {
APP.RTC.attachMediaStream(sel, stream);
}
if (RTCBrowserType.isTemasysPluginUsed()) {
sel = $('#' + newElementId);
}
self.VideoLayout.videoactive(sel, self.resourceJid);
sel[0].onplaying = null;
if (RTCBrowserType.isTemasysPluginUsed()) {
// 'currentTime' is used to check if the video has started
// and the value is not set by the plugin, so we do it
sel[0].currentTime = 1;
}
};
sel[0].onplaying = onPlayingHandler;
}
APP.RTC.attachMediaStream(sel, stream);
}
var self = this;
stream.onended = function () {
console.log('stream ended', this);
self.removeRemoteStreamElement(stream, isVideo, newElementId);
};
// Name of video element name is different for IE/Safari
var videoElem = APP.RTC.getVideoElementName();
// Add click handler.
var onClickHandler = function (event) {
/*
* FIXME It turns out that videoThumb may not exist (if there is
* no actual video).
*/
var videoThumb = $('#' + self.videoSpanId + '>' + videoElem).get(0);
if (videoThumb) {
self.VideoLayout.handleVideoThumbClicked(
false,
self.resourceJid);
}
// On IE we need to populate this handler on video