Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue: DTMF packets have different timestamps and support byealso #10

Open
wants to merge 5 commits 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
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.ArrayList;
import java.util.List;

import net.sourceforge.peers.rtp.DtmfRtpPacket;
import net.sourceforge.peers.rtp.RFC4733;
import net.sourceforge.peers.rtp.RtpPacket;

Expand All @@ -47,15 +48,15 @@ public List<RtpPacket> createDtmfPackets(char digit) {
// duration 8 bits
data[3] = -96;

RtpPacket rtpPacket = new RtpPacket();
DtmfRtpPacket rtpPacket = new DtmfRtpPacket();
rtpPacket.setData(data);
rtpPacket.setPayloadType(RFC4733.PAYLOAD_TYPE_TELEPHONE_EVENT);
rtpPacket.setMarker(true);
packets.add(rtpPacket);

// two classical packets

rtpPacket = new RtpPacket();
rtpPacket = new DtmfRtpPacket(rtpPacket);
// set duration to 320
data = data.clone();
data[2] = 1;
Expand All @@ -65,7 +66,7 @@ public List<RtpPacket> createDtmfPackets(char digit) {
rtpPacket.setPayloadType(RFC4733.PAYLOAD_TYPE_TELEPHONE_EVENT);
packets.add(rtpPacket);

rtpPacket = new RtpPacket();
rtpPacket = new DtmfRtpPacket(rtpPacket);
// set duration to 320
data = data.clone();
data[2] = 1;
Expand All @@ -82,7 +83,7 @@ public List<RtpPacket> createDtmfPackets(char digit) {
data[2] = 2; // duration 8 bits
data[3] = -128; // duration 8 bits
for (int r = 0; r < 3; r++) {
rtpPacket = new RtpPacket();
rtpPacket = new DtmfRtpPacket(rtpPacket);
rtpPacket.setData(data);
rtpPacket.setMarker(false);
rtpPacket.setPayloadType(RFC4733.PAYLOAD_TYPE_TELEPHONE_EVENT);
Expand Down
18 changes: 16 additions & 2 deletions peers-lib/src/main/java/net/sourceforge/peers/media/RtpSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.concurrent.CountDownLatch;

import net.sourceforge.peers.Logger;
import net.sourceforge.peers.rtp.DtmfRtpPacket;
import net.sourceforge.peers.rtp.RtpPacket;
import net.sourceforge.peers.rtp.RtpSession;
import net.sourceforge.peers.sdp.Codec;
Expand Down Expand Up @@ -135,17 +136,30 @@ public void run() {
rtpPacket.setPayloadType(pushedPacket.getPayloadType());
byte[] data = pushedPacket.getData();
rtpPacket.setData(data);
//if rtp packet is the first packet in one DTMF event.
if(pushedPacket instanceof DtmfRtpPacket&&pushedPacket.isMarker()){
timestamp += buf_size;
rtpPacket.setTimestamp(timestamp);
pushedPacket.setTimestamp(timestamp);
}else{
DtmfRtpPacket previousDtmfRtpPacket =((DtmfRtpPacket) pushedPacket).getPreviousDtmfRtpPacket();
long previousTimeStamp = previousDtmfRtpPacket.getTimestamp();
rtpPacket.setTimestamp(previousTimeStamp);
pushedPacket.setTimestamp(timestamp);
}

} else {
if (rtpPacket.getPayloadType() != codec.getPayloadType()) {
rtpPacket.setPayloadType(codec.getPayloadType());
rtpPacket.setMarker(false);
}
rtpPacket.setData(trimmedBuffer);
timestamp += buf_size;
rtpPacket.setTimestamp(timestamp);
}

rtpPacket.setSequenceNumber(sequenceNumber++);
timestamp += buf_size;
rtpPacket.setTimestamp(timestamp);

if (firstTime) {
rtpSession.send(rtpPacket);
lastSentTime = System.nanoTime();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package net.sourceforge.peers.rtp;

public class DtmfRtpPacket extends RtpPacket{

/**
* The previous DtmfRtpPacket for the same DTMF event.
*/
private DtmfRtpPacket previousDtmfRtpPacket;

public DtmfRtpPacket() {
super();
}


public DtmfRtpPacket(DtmfRtpPacket previousDtmfRtpPacket) {
super();
this.previousDtmfRtpPacket = previousDtmfRtpPacket;
}


public DtmfRtpPacket getPreviousDtmfRtpPacket() {
return previousDtmfRtpPacket;
}


public void setPreviousDtmfRtpPacket(DtmfRtpPacket previousDtmfRtpPacket) {
this.previousDtmfRtpPacket = previousDtmfRtpPacket;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public final class RFC3261 {
public static final String HDR_TO = "To";
public static final String HDR_VIA = "Via";
public static final String HDR_WWW_AUTHENTICATE = "WWW-Authenticate";
public static final String HDR_ALSO = "Also";



//Compact form

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import net.sourceforge.peers.sip.core.useragent.RequestManager;
import net.sourceforge.peers.sip.core.useragent.SipListener;
import net.sourceforge.peers.sip.core.useragent.UserAgent;
import net.sourceforge.peers.sip.syntaxencoding.SipHeaderFieldName;
import net.sourceforge.peers.sip.syntaxencoding.SipHeaderFieldValue;
import net.sourceforge.peers.sip.syntaxencoding.SipUriSyntaxException;
import net.sourceforge.peers.sip.transaction.ClientTransaction;
import net.sourceforge.peers.sip.transaction.ClientTransactionUser;
import net.sourceforge.peers.sip.transaction.NonInviteClientTransaction;
Expand Down Expand Up @@ -109,11 +112,28 @@ public void handleBye(SipRequest sipRequest, Dialog dialog) {
if (sipListener != null) {
sipListener.remoteHangup(sipRequest);
}

sendInviteIfNeedAlso(sipRequest);

// setChanged();
// notifyObservers(sipRequest);
}

private void sendInviteIfNeedAlso(SipRequest sipRequest) {
SipHeaderFieldName alsoHeaderFieldName = new SipHeaderFieldName(RFC3261.HDR_ALSO);
SipHeaderFieldValue alsoHeaderFieldValue = sipRequest.getSipHeaders().get(alsoHeaderFieldName);
if(alsoHeaderFieldValue!=null&&(!alsoHeaderFieldValue.getValue().isEmpty())){
String alsoHeaderStr=alsoHeaderFieldValue.getValue();
SipHeaderFieldValue oldCallId = sipRequest.getSipHeaders().get(new SipHeaderFieldName(RFC3261.HDR_CALLID));
String newCallId = oldCallId.getValue()+"_also";
try {
this.userAgent.invite(alsoHeaderStr,newCallId);
} catch (SipUriSyntaxException e) {
throw new RuntimeException("sip url syntax exception for:"+e.getMessage(), e);
}
}
}

///////////////////////////////////////
//ServerTransactionUser methods
///////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

import java.net.InetAddress;

import net.sourceforge.peers.sip.RFC3261;

public class SipTransportConnection {

public static final int EMPTY_PORT = -1;
Expand All @@ -45,21 +43,7 @@ public SipTransportConnection(InetAddress localInetAddress,
this.transport = transport;
}

@Override
public boolean equals(Object obj) {
if (obj.getClass() != SipTransportConnection.class) {
return false;
}
SipTransportConnection other = (SipTransportConnection)obj;
if (!transport.equalsIgnoreCase(other.transport)) {
return false;
}
if (RFC3261.TRANSPORT_UDP.equalsIgnoreCase(transport)) {
return localInetAddress.equals(other.localInetAddress) &&
localPort == other.localPort;
}
return false;
}


@Override
public String toString() {
Expand All @@ -68,12 +52,10 @@ public String toString() {
buf.append(':');
appendPort(buf, localPort);
buf.append('/');
if (!RFC3261.TRANSPORT_UDP.equalsIgnoreCase(transport)) {
appendInetAddress(buf, remoteInetAddress);
buf.append(':');
appendPort(buf, remotePort);
buf.append('/');
}
appendInetAddress(buf, remoteInetAddress);
buf.append(':');
appendPort(buf, remotePort);
buf.append('/');
buf.append(transport.toUpperCase());
return buf.toString();
}
Expand All @@ -93,11 +75,7 @@ private void appendPort(StringBuffer buf, int port) {
buf.append("-");
}
}

@Override
public int hashCode() {
return toString().hashCode();
}


public InetAddress getLocalInetAddress() {
return localInetAddress;
Expand All @@ -107,7 +85,53 @@ public int getLocalPort() {
return localPort;
}

public InetAddress getRemoteInetAddress() {
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((localInetAddress == null) ? 0 : localInetAddress.hashCode());
result = prime * result + localPort;
result = prime * result + ((remoteInetAddress == null) ? 0 : remoteInetAddress.hashCode());
result = prime * result + remotePort;
result = prime * result + ((transport == null) ? 0 : transport.hashCode());
return result;
}


@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
SipTransportConnection other = (SipTransportConnection) obj;
if (localInetAddress == null) {
if (other.localInetAddress != null)
return false;
} else if (!localInetAddress.equals(other.localInetAddress))
return false;
if (localPort != other.localPort)
return false;
if (remoteInetAddress == null) {
if (other.remoteInetAddress != null)
return false;
} else if (!remoteInetAddress.equals(other.remoteInetAddress))
return false;
if (remotePort != other.remotePort)
return false;
if (transport == null) {
if (other.transport != null)
return false;
} else if (!transport.equals(other.transport))
return false;
return true;
}



public InetAddress getRemoteInetAddress() {
return remoteInetAddress;
}

Expand Down