Hi Mark,
I am trying to read RTP packet from pcap file which has a pre-recorded Audio packet(RTP) as per RFC 1889.
Can you help me to read pcap using jNetpack
Regards
Parasuraman
jNetPcap doesn't have RTP protocol yet. You can just get the packet's UDP header at this point, and look for source or destination port 5004. The header has simple structure but the protocol itself and what it carries is a bit more complex.
Here is a header definition I just did on the spot. There could be issues, but can be a starting point for anyone wanting to write this one up 
@Header
public class Rtp extends JHeader {
@Bind(to=Udp.class)
public static boolean bindToUdp(JPacket packet, Udp udp) {
return udp.destination() == 5004 || udp.source() == 5004;
}
@HeaderLength
public static int headerLength(JBuffer buffer, int offset) {
return (buffer.getByte(0) & 0xF0) >> 4 * 4 + 12;
}
public int front() {
return super.getUShort(0);
}
public int cc() {
return (super.getByte(0) & 0xF0) >> 4;
}
public int sequence() {
return super.getUShort(2);
}
public long timestamp() {
return super.getUInt(4);
}
public long ssrc() {
return super.getUInt(8);
}
public long[] csrc() {
final int count = cc();
final long[] csrc = new long[count];
for (int i = 0; i < count; i ++) {
csrc[i] = super.getUInt(12 + i * 4);
}
return csrc;
}
}
(THIS ISN'T TESTED, COULD HAVE ERRORS)
It won't print out nicely, until you also add @Field annotation to the field accessor methods, but that is not strictly needed and the above should decode the Rtp header. Of course when the production version will be released, it will be tested and fully annotated.
To register the above. You have to call JRegistry.register(Rtp.class) only once, typically done in a static initializer of your application.
Hi Mark,
Thanks for your quick reply..
i think the above program will listen to the port 5004 and 5005. and it will capture a packet from the
live. but i dont want to like this..
i have file, that has pre-recorded RTP packet.. i need to read a RTP packets from the file?
how i can do to so?
i have sample pcap file..
how i can attach it in the fourm?
Regards
Parasuraman
You can look at the example code:
http://jnetpcap.com/classic
but instead of using Pcap.openLive use Pcap.openOffline which will read from a file. Then you can use loop or dispatch to read the packets.
Hi Mark,
Thanks for your information...
I have used openOffline() function.
sample:
******
Pcap pcap = Pcap.openOffline("g711a.pcap", sb);
I am getting an Exception:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no jnetpcap in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1698)
at java.lang.Runtime.loadLibrary0(Runtime.java:840)
at java.lang.System.loadLibrary(System.java:1047)
at org.jnetpcap.Pcap.
at com.mitester.media.read.main(read.java:26)
I am using ubuntu OS.
How i can resolve this issue?
can you help me to fix this issue..
Regards
Parasuraman
Ubuntu is not officially supported. People have installed the debian package and coersed the libpcap.so for ubuntu, but unfortunately I can't help you with that.
Hi Mark,
Thanks for your support and Information...
do you know any other option to read a pcap file in java?
Regards
Parasuraman
http://jnetstream.com will do it. Its pure java and supports pcap file format. It doesn't do packet decoding though at this time. Also ubuntu will be supported starting with jNetPcap 1.3 release, but it will be a few months before that is released. I'm still working heavily on 1.2.
Hi Mark,
with JNetStream i can able to open pcap file.
here is my sample code..
from the packet i need to read RTP pcaket?
how i can read it?
try {
for (FilePacket packet : Captures.openFile("g711a.pcap")) {
if (packet.hasCompleteHeader(Udp.class)) {
Udp ip = packet.getHeader(Udp.class);
System.out.println("length: " + ip.length());
System.out.println("Source: " + ip.source());
System.out.println("Designation: " + ip.destination());
System.out.println("Crc16: " + ip.crc());
System.out.println("length: " + ip.length());
}
}
Captures.close();
} catch (FileFormatException e2) {
System.out.println("FileFormatException: " + e2.getMessage());
} catch (IOException e2) {
System.out.println("IOException: " + e2.getMessage());
} catch (CodecCreateException e) {
System.out.println("CodecCreateException: " + e.getMessage());
} catch (IllegalStateException e) {
System.out.println("IllegalStateException: " + e.getMessage());
}
Regards
Parasuraman
> From the packet i need to read RTP pcaket?
I haven't integrated the jNetPcap's decoding capabilities yet. Its possible to capture via jNetStream and decode via jNetPcap by creating a jNetPcap's jMemoryPacket. You can peer jNetStream packet buffer with a jNetPcap packet and decode it:
UPdate: never mind, I forgot you are on ubuntu platform. You can't use jNetPcap on ubuntu to decode the packets. jNetStream 2.4 is your best bet. Packet jNSPacket = // JPacket jNPPacket = new JMemoryPacket(jNSPacket.getByteBuffer()); jNPPacket.scan(Ethernet.ID);
But there is no Rtp header definition yet. So like I said, it can only be hacked at this point.
I think you may have better luck with jNetStream version 2.4. It has Rtp defined. Uses different API then 3.0 version. 2.4 is pretty stable and has been around for many years.
Hi Mark,
Now i can able to read pre-recorded audio file (pcap file) through a JNetStreamStandalone.jar.
Now i would like to know the Licensing term of the JNetStreamStandalone.jar, which is published in the open source.
can you share the licensing term with ours..
Regards,
Parasuraman
jNetStream uses LGPL license same as jNetPcap.
Hi Mark,
Thanks you so much..
Regards
Parasuraman
Hai Parasuraman,
I am new to this forum.
I am also using jNetstrem lib for decoding captured packets.
Have u got solution to identify RTP packets??
have u decoded RTP packtes and u got media output file????
if u plz help me to do the same.
Regards,
Gomathi
A quick update. I have added the Rtp protocol to the dev tree. You can see its definition here:
http://jnetpcap.svn.sourceforge.net/viewvc/jnetpcap/jnetpcap/trunk/src/j...
I have not defined the RTCP or any of the Rtp payload types yet.
Just wanted to let you know, that I was successfully able to read audio out of a RTP/PCM capture file. I used the new Rtp header definition and simply read out all the rtp payloads into its own separate audio file grouped by SSRC id. Here is the simple program that does it:
private static final String SIP_G711 = "tests/test-sip-rtp-g711.pcap";
public void testRtpAudioExtract() throws IOException {
Rtp rtp = new Rtp();
for (PcapPacket packet: super.getIterable(SIP_G711)) {
if (packet.hasHeader(rtp)) {
FileOutputStream out = getOutput(rtp.ssrc());
out.write(rtp.payload());
}
}
for (FileOutputStream o: map.values()) {
o.close();
}
}
Map map = new HashMap();
private FileOutputStream getOutput(long id) throws FileNotFoundException {
if (map.containsKey(id)) {
return map.get(id);
} else {
File file = new File("C:\\temp\\" + id + ".au");
if (file.exists()) {
file.delete();
}
FileOutputStream out = new FileOutputStream(file);
map.put(id, out);
return out;
}
}
The super.getIterable(String) is a little utility method that opens up the specified file and returns all the packets as an iterator. The file name constant is a rtp capture file found under tests directory.
I can playback the audio using winamp. This particular file generates 3 .au files. All I can say, is that I didn't expect this to be this easy
Mark, In your new Rtp class, where is the constant "BYTE" defined?
Its in the updated JHeader class as a public static final constant. If you SVN update your project, it should be there now.
Hy Mark,
Could you post the method getIterable?
Thanks for your support,
Best Regards
Not sure I understand your request. Please be more specific.
for (PcapPacket packet: super.getIterable(SIP_G711))
{
.
.
.
.
.
}
Could you post The Method getIterable(String) ?
Thanks a lot
bye
I'm still not sure what you are asking for since jnetpcap API doesn't provide getIterable() method anywhere. I think you might be referring to TestUtils.getIterable which is a little utility function that I wrote to help out with reading packets, for junit test purposes, from different kind of sources. Here is the link to source code for TestUtils.java:
http://jnetpcap.svn.sourceforge.net/viewvc/jnetpcap/jnetpcap/trunk/tests...
Hy Mark
thanks a lot for you support.
When I open the file whith
Pcap pcap = Pcap.openOffline("c:/test/ces_rtp.pcap", errbuf);
pcap is" bad dump file format" !
Can you help me?
Thanks
What does the errbuf say?
Hy Mark,
sorry the dump file is bad.
I resolve it.
but Now I not save the au file.
What does the library RTP use?
thanks a lot for you support
Best Regards