Skip to content

Commit

Permalink
feat: Moves xmpp in room mute logic to AudioModeration.
Browse files Browse the repository at this point in the history
  • Loading branch information
damencho committed Oct 1, 2021
1 parent 18d24fe commit 68a764e
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 93 deletions.
105 changes: 101 additions & 4 deletions src/main/java/org/jitsi/jigasi/AudioModeration.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.jitsi.jigasi;

import net.java.sip.communicator.impl.protocol.jabber.*;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.util.*;
import org.jitsi.jigasi.sip.*;
Expand All @@ -28,6 +29,7 @@
import org.jxmpp.jid.*;

import org.json.simple.*;
import org.jxmpp.jid.impl.*;

import java.util.*;

Expand Down Expand Up @@ -67,6 +69,16 @@ public class AudioModeration
*/
private final JvbConference jvbConference;

/**
* We always start the call unmuted, we keep the instance, so we can remove it from the list of extensions that
* we always add after joining and when sending a presence.
*/
private static final AudioMutedExtension initialAudioMutedExtension = new AudioMutedExtension();
static
{
initialAudioMutedExtension.setAudioMuted(false);
}

/**
* The call context used to create this conference, contains info as
* room name and room password and other optional parameters.
Expand Down Expand Up @@ -121,6 +133,13 @@ public void clean()
*/
void notifyWillJoinJvbRoom()
{
ChatRoom mucRoom = this.jvbConference.getJvbRoom();
if (mucRoom instanceof ChatRoomJabberImpl)
{
// we always start the call unmuted
((ChatRoomJabberImpl) mucRoom).addPresencePacketExtensions(initialAudioMutedExtension);
}

if (isMutingSupported())
{
if (muteIqHandler == null)
Expand Down Expand Up @@ -208,7 +227,7 @@ public void onJSONReceived(CallPeer callPeer, JSONObject jsonObject, Map<String,
boolean bMute = (boolean)data.get("audio");

// Send presence audio muted
this.jvbConference.setChatRoomAudioMuted(bMute);
this.setChatRoomAudioMuted(bMute);
}
}
else if (type.equalsIgnoreCase("muteRequest"))
Expand All @@ -218,14 +237,14 @@ else if (type.equalsIgnoreCase("muteRequest"))
boolean bAudioMute = (boolean)data.get("audio");

// Send request to jicofo
if (jvbConference.requestAudioMute(bAudioMute))
if (this.requestAudioMuteByJicofo(bAudioMute))
{
// Send response through sip, respondRemoteAudioMute
this.gatewaySession.sendJson(callPeer,
SipInfoJsonProtocol.createSIPJSONAudioMuteResponse(bAudioMute, true, id));

// Send presence if response succeeded
this.jvbConference.setChatRoomAudioMuted(bAudioMute);
this.setChatRoomAudioMuted(bAudioMute);
}
else
{
Expand All @@ -241,6 +260,84 @@ else if (type.equalsIgnoreCase("muteRequest"))
}
}

/**
* Sets the chatroom presence for the participant.
*
* @param muted <tt>true</tt> for presence as muted,
* false otherwise
*/
void setChatRoomAudioMuted(boolean muted)
{
ChatRoom mucRoom = this.jvbConference.getJvbRoom();

if (mucRoom != null)
{
if (mucRoom instanceof ChatRoomJabberImpl)
{
// remove the initial extension otherwise it will overwrite our new setting
((ChatRoomJabberImpl) mucRoom).removePresencePacketExtensions(initialAudioMutedExtension);
}

AudioMutedExtension audioMutedExtension = new AudioMutedExtension();

audioMutedExtension.setAudioMuted(muted);

OperationSetJitsiMeetTools jitsiMeetTools
= this.jvbConference.getXmppProvider().getOperationSet(OperationSetJitsiMeetTools.class);

jitsiMeetTools.sendPresenceExtension(mucRoom, audioMutedExtension);
}
}

/**
* Request Jicofo on behalf to mute/unmute us.
*
* @param bMuted <tt>true</tt> if request is to mute audio,
* false otherwise
* @return <tt>true</tt> if request succeeded, false
* otherwise
*/
public boolean requestAudioMuteByJicofo(boolean bMuted)
{
ChatRoom mucRoom = this.jvbConference.getJvbRoom();

StanzaCollector collector = null;
try
{
String roomName = mucRoom.getIdentifier();

String jidString = roomName + "/" + this.jvbConference.getResourceIdentifier().toString();
Jid memberJid = JidCreate.from(jidString);
String roomJidString = roomName + "/" + this.gatewaySession.getFocusResourceAddr();
Jid roomJid = JidCreate.from(roomJidString);

MuteIq muteIq = new MuteIq();
muteIq.setJid(memberJid);
muteIq.setMute(bMuted);
muteIq.setType(IQ.Type.set);
muteIq.setTo(roomJid);

collector = this.jvbConference.getConnection().createStanzaCollectorAndSend(muteIq);

collector.nextResultOrThrow();
}
catch(Exception ex)
{
logger.error(this.callContext + " Error sending xmpp request for audio mute", ex);

return false;
}
finally
{
if (collector != null)
{
collector.cancel();
}
}

return true;
}

/**
* Processes start muted in case:
* - we had received that flag
Expand All @@ -260,7 +357,7 @@ && isMutingSupported()
&& sipCall != null
&& sipCall.getCallState() == CallState.CALL_IN_PROGRESS)
{
if (jvbConference.requestAudioMute(startAudioMuted))
if (this.requestAudioMuteByJicofo(startAudioMuted))
{
mute();
}
Expand Down
96 changes: 7 additions & 89 deletions src/main/java/org/jitsi/jigasi/JvbConference.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,6 @@ public class JvbConference
*/
private static final int JVB_ACTIVITY_CHECK_DELAY = 5000;

/**
* We always start the call unmuted, we keep the instance, so we can remove it from the list of extensions that
* we always add after joining and when sending a presence.
*/
private static final AudioMutedExtension initialAudioMutedExtension = new AudioMutedExtension();
static
{
initialAudioMutedExtension.setAudioMuted(false);
}

/**
* A timer which will be used to schedule a quick non-blocking check whether there is any activity
* on the bridge side of the call.
Expand Down Expand Up @@ -408,7 +398,7 @@ public AudioModeration getAudioModeration()
return audioModeration;
}

private Localpart getResourceIdentifier()
public Localpart getResourceIdentifier()
{
Localpart resourceIdentifier = null;
if (JigasiBundleActivator.getConfigurationService()
Expand Down Expand Up @@ -645,6 +635,11 @@ private synchronized void setXmppProvider(
}
}

public ProtocolProviderService getXmppProvider()
{
return xmppProvider;
}

@Override
public synchronized void registrationStateChanged(
RegistrationStateChangeEvent evt)
Expand Down Expand Up @@ -815,14 +810,10 @@ public void joinConferenceRoom()
});
if (initiator.getChildExtensions().size() > 0)
{
((ChatRoomJabberImpl)mucRoom)
.addPresencePacketExtensions(initiator);
((ChatRoomJabberImpl)mucRoom).addPresencePacketExtensions(initiator);
}

((ChatRoomJabberImpl)mucRoom).addPresencePacketExtensions(features);

// we always start the call unmuted
((ChatRoomJabberImpl)mucRoom).addPresencePacketExtensions(initialAudioMutedExtension);
}
else
{
Expand Down Expand Up @@ -1744,79 +1735,6 @@ private void setJvbCall(Call newJvbCall)
}
}

/**
* Sets the chatroom presence for the participant.
*
* @param muted <tt>true</tt> for presence as muted,
* false otherwise
*/
void setChatRoomAudioMuted(boolean muted)
{
if (mucRoom != null)
{
// remove the initial extension otherwise it will overwrite our new setting
((ChatRoomJabberImpl)mucRoom).removePresencePacketExtensions(initialAudioMutedExtension);

AudioMutedExtension audioMutedExtension = new AudioMutedExtension();

audioMutedExtension.setAudioMuted(muted);

OperationSetJitsiMeetTools jitsiMeetTools
= xmppProvider.getOperationSet(OperationSetJitsiMeetTools.class);

jitsiMeetTools
.sendPresenceExtension(mucRoom, audioMutedExtension);

}
}

/**
* Request Jicofo on behalf of Jigasi to mute a participant.
*
* @param bMuted <tt>true</tt> if request is to mute audio,
* false otherwise
* @return <tt>true</tt> if request succeeded, false
* otherwise
*/
public boolean requestAudioMute(boolean bMuted)
{
StanzaCollector collector = null;
try
{
String roomName = mucRoom.getIdentifier();

String jidString = roomName + "/" + getResourceIdentifier().toString();
Jid memberJid = JidCreate.from(jidString);
String roomJidString = roomName + "/" + this.gatewaySession.getFocusResourceAddr();
Jid roomJid = JidCreate.from(roomJidString);

MuteIq muteIq = new MuteIq();
muteIq.setJid(memberJid);
muteIq.setMute(bMuted);
muteIq.setType(IQ.Type.set);
muteIq.setTo(roomJid);

collector = getConnection()
.createStanzaCollectorAndSend(muteIq);

collector.nextResultOrThrow();
}
catch(Exception ex)
{
logger.error(this.callContext + " " + ex.getMessage());
return false;
}
finally
{
if (collector != null)
{
collector.cancel();
}
}

return true;
}

/**
* Retrieves the connection from ProtocolProviderService if it is the JabberImpl.
* @return the XMPPConnection.
Expand Down

0 comments on commit 68a764e

Please sign in to comment.