Skip to content
This repository has been archived by the owner on Mar 21, 2023. It is now read-only.

Addint SRTP and rtcp-mux + few bugfixes #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 66 additions & 43 deletions src/com/xonami/javaBells/IceAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.ice4j.ice.RemoteCandidate;
import org.ice4j.ice.harvest.StunCandidateHarvester;
import org.ice4j.ice.harvest.TurnCandidateHarvester;
import org.ice4j.security.LongTermCredential;
//import org.ice4j.security.LongTermCredential;


Expand Down Expand Up @@ -54,7 +55,7 @@ public class IceAgent {

static final int MIN_STREAM_PORT = 5000;
static final int MAX_STREAM_PORT = 9000;
static int streamPort = (int) ( random.nextFloat() * ( MAX_STREAM_PORT - MIN_STREAM_PORT ) + MIN_STREAM_PORT );
static volatile int streamPort = (int) ( random.nextFloat() * ( MAX_STREAM_PORT - MIN_STREAM_PORT ) + MIN_STREAM_PORT );

private final Agent agent;

Expand All @@ -75,6 +76,25 @@ public IceAgent( final boolean controling, StunTurnAddress sta ) {
for( TransportAddress ta : sta.turnAddresses )
agent.addCandidateHarvester(new TurnCandidateHarvester(ta) );
}

public IceAgent(final boolean controlling,
final TransportAddress[] stunAddresses, final TransportAddress[] turnAddresses,
final LongTermCredential ltCreds) {
this.agent = new Agent();
agent.setControlling(controlling);
if( controlling )
agent.setNominationStrategy(NominationStrategy.NOMINATE_HIGHEST_PRIO);

//stun and turn
if (stunAddresses != null)
for( TransportAddress ta : stunAddresses )
agent.addCandidateHarvester(new StunCandidateHarvester(ta) );

if (turnAddresses != null)
for( TransportAddress ta : turnAddresses )
agent.addCandidateHarvester(new TurnCandidateHarvester(ta, ltCreds) );

}
public void createStreams( Collection<String> streamnames ) throws IOException {
for( String s : streamnames ) {
createStream( s );
Expand All @@ -98,6 +118,10 @@ public void removeAgentStateChangeListener( PropertyChangeListener pcl ) {
public List<String> getStreamNames() {
return agent.getStreamNames();
}
public List<IceMediaStream> getStreams() {
return agent.getStreams();
}
public void removeStream(IceMediaStream stream) {agent.removeStream(stream);}
public void freeAgent() {
agent.free();
}
Expand All @@ -109,7 +133,6 @@ public void addRemoteCandidates(JingleIQ jiq) {
IceMediaStream ims = agent.getStream(name);
if (ims != null) {
for (IceUdpTransportPacketExtension tpe : contentpe.getChildExtensionsOfType(IceUdpTransportPacketExtension.class)) {
System.out.println( "\t" + tpe );
if (tpe.getPassword() != null)
ims.setRemotePassword(tpe.getPassword());
if (tpe.getUfrag() != null)
Expand Down Expand Up @@ -149,7 +172,7 @@ public void addRemoteCandidates(JingleIQ jiq) {
RemoteCandidate rc = new RemoteCandidate( ta,
component,
convertType(cpe.getType()),
Integer.toString(cpe.getFoundation()),
cpe.getFoundation(),
cpe.getPriority(),
relatedCandidate);
component.addRemoteCandidate(rc);
Expand Down Expand Up @@ -184,50 +207,50 @@ public void addLocalCandidateToContents(List<ContentPacketExtension> contentList
cpe.addChildExtension(ext);
}
}
public IceUdpTransportPacketExtension getLocalCandidatePacketExtensionForStream( String streamName ) {
IceUdpTransportPacketExtension transport = new IceUdpTransportPacketExtension();
transport.setPassword( agent.getLocalPassword() );
transport.setUfrag( agent.getLocalUfrag() );

try {
IceMediaStream ims = agent.getStream(streamName);
if( ims == null )
return null;

for( Component c : ims.getComponents() ) {
for( Candidate<?> can : c.getLocalCandidates() ) {
CandidatePacketExtension candidate = new CandidatePacketExtension();
candidate.setComponent(c.getComponentID());
candidate.setFoundation(Integer.parseInt(can.getFoundation()));
candidate.setGeneration(agent.getGeneration());
candidate.setID(nextCandidateId());
candidate.setNetwork(0); //FIXME: we need to identify the network card properly.
TransportAddress ta = can.getTransportAddress();
candidate.setIP( ta.getHostAddress() );
candidate.setPort( ta.getPort() );
candidate.setPriority(can.getPriority());
candidate.setProtocol(can.getTransport().toString());
if( can.getRelatedAddress() != null ) {
candidate.setRelAddr(can.getRelatedAddress().getHostAddress());
candidate.setRelPort(can.getRelatedAddress().getPort());
}
candidate.setType(convertType(can.getType()));

transport.addCandidate(candidate);
}
}
} catch( Exception e ) {
e.printStackTrace();
System.exit(0);
}
return transport;
}

private CandidateType convertType(org.ice4j.ice.CandidateType type) {
String ts = type.toString();
return CandidateType.valueOf(ts);
}
private org.ice4j.ice.CandidateType convertType(CandidateType type) {

public IceUdpTransportPacketExtension getLocalCandidatePacketExtensionForStream( String streamName ) {
IceUdpTransportPacketExtension transport = new IceUdpTransportPacketExtension();
transport.setPassword( agent.getLocalPassword() );
transport.setUfrag( agent.getLocalUfrag() );

try {
IceMediaStream ims = agent.getStream(streamName);
if( ims == null )
return null;

for( Component c : ims.getComponents() ) {
for( Candidate<?> can : c.getLocalCandidates() ) {
CandidatePacketExtension candidate = new CandidatePacketExtension();
candidate.setComponent(c.getComponentID());
candidate.setFoundation(can.getFoundation());
candidate.setGeneration(agent.getGeneration());
candidate.setID(nextCandidateId());
candidate.setNetwork(0); //FIXME: we need to identify the network card properly.
TransportAddress ta = can.getTransportAddress();
candidate.setIP( ta.getHostAddress() );
candidate.setPort( ta.getPort() );
candidate.setPriority(can.getPriority());
candidate.setProtocol(can.getTransport().toString());
if( can.getRelatedAddress() != null ) {
candidate.setRelAddr(can.getRelatedAddress().getHostAddress());
candidate.setRelPort(can.getRelatedAddress().getPort());
}
candidate.setType(convertType(can.getType()));

transport.addCandidate(candidate);
}
}
} catch( Exception e ) {
e.printStackTrace();
System.exit(0);
}
return transport;
}
private org.ice4j.ice.CandidateType convertType(CandidateType type) {
String ts = type.toString();
return org.ice4j.ice.CandidateType.parse(ts);
}
Expand Down
60 changes: 45 additions & 15 deletions src/com/xonami/javaBells/JinglePacketHandler.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.xonami.javaBells;

import java.util.HashMap;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

import net.java.sip.communicator.impl.protocol.jabber.extensions.jingle.JingleIQ;

import net.java.sip.communicator.impl.protocol.jabber.extensions.jingle.JinglePacketFactory;
import net.java.sip.communicator.impl.protocol.jabber.extensions.jingle.Reason;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.PacketFilter;
Expand All @@ -18,8 +21,10 @@
*
*/
public class JinglePacketHandler implements PacketListener, PacketFilter {
private final HashMap<String,JingleSession> jingleSessions = new HashMap<String,JingleSession>();
private final HashMap<String,JingleSession> deadSessions = new HashMap<String,JingleSession>();
// private final Map<String,JingleSession> jingleSessions = new ConcurrentHashMap<String,JingleSession>();
private volatile JingleSession jingleSession;
private volatile String sid;
private volatile String jid;
protected final XMPPConnection connection;

public JinglePacketHandler( XMPPConnection connection ) {
Expand All @@ -32,13 +37,19 @@ public void processPacket(Packet packet) {
JingleIQ jiq = (JingleIQ) packet;

String sid = jiq.getSID();
JingleSession js = jingleSessions.get(sid);
if( js == null )
js = deadSessions.get(sid);
if( js == null ) {
js = createJingleSession( sid, jiq );
jingleSessions.put( sid, js );
}
// JingleSession js = jingleSessions.get(sid);
if( jingleSession == null ) {
createSession(jiq, sid);
// jingleSessions.put( sid, js );
} else if (!jingleSession.getSessionId().equals(sid)) {
JingleIQ sessionTerminate = JinglePacketFactory
.createSessionTerminate(this.jid, jiq.getFrom(), this.sid, Reason.GONE,
"New session request from " + jiq.getFrom());
connection.sendPacket(sessionTerminate);
destroySession();
createSession(jiq, sid);
}
JingleSession js = jingleSession;
switch( jiq.getAction() ) {
case CONTENT_ACCEPT:
js.handleContentAcept( jiq );
Expand Down Expand Up @@ -87,11 +98,26 @@ public void processPacket(Packet packet) {
break;
}
}

public JingleSession removeJingleSession( JingleSession session ) {
JingleSession ret = jingleSessions.remove( session.getSessionId() );
deadSessions.put( session.getSessionId(), session );
return ret;

private void destroySession() {
jingleSession.handleSessionTerminate(null);
}

private void createSession(final JingleIQ jiq, final String sid) {
jingleSession = createJingleSession( sid, jiq );
this.sid = sid;
this.jid = jiq.getTo();
}

public JingleSession removeJingleSession( JingleSession session ) {
JingleSession ret = null;
if (jingleSession == session) {
ret = jingleSession;
jingleSession = null;
this.sid = null;
this.jid = null;
}
return ret;
}

/**
Expand All @@ -112,4 +138,8 @@ public JingleSession createJingleSession( String sid, JingleIQ jiq ) {
public boolean accept(Packet packet) {
return packet.getClass() == JingleIQ.class;
}

public Collection<JingleSession> getSessions(){
return new HashSet<JingleSession>(Arrays.asList(jingleSession));
}
}
1 change: 1 addition & 0 deletions src/com/xonami/javaBells/JingleStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ private void updateVisualComponent() {
}

public void shutdown() {
// mediaStream.stop();
mediaStream.close();
}
}
Loading