How i can read pre-recorded Audio packet(RTP) from the pcap file

24 replies [Last post]
parasuraman
Offline
Joined: 04/01/2009

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

Mark Bednarczyk
Mark Bednarczyk's picture
Offline
Joined: 03/22/2008
jNetPcap doesn't have RTP

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 Smile

@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.

Sly Technologies, Inc.
R&D

parasuraman
Offline
Joined: 04/01/2009
How i can read pre-recorded Audio packet(RTP) from the pcap file

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

Mark Bednarczyk
Mark Bednarczyk's picture
Offline
Joined: 03/22/2008
You can look at the example

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.

Sly Technologies, Inc.
R&D

parasuraman
Offline
Joined: 04/01/2009
Hi Mark, Thanks for your

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.(Pcap.java:364)
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

Mark Bednarczyk
Mark Bednarczyk's picture
Offline
Joined: 03/22/2008
Ubuntu is not officially

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.

Sly Technologies, Inc.
R&D

parasuraman
Offline
Joined: 04/01/2009
How i can read pre-recorded Audio packet(RTP) from the pcap file

Hi Mark,

Thanks for your support and Information...
do you know any other option to read a pcap file in java?

Regards
Parasuraman

Mark Bednarczyk
Mark Bednarczyk's picture
Offline
Joined: 03/22/2008
http://jnetstream.com will do

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.

Sly Technologies, Inc.
R&D

parasuraman
Offline
Joined: 04/01/2009
Hi Mark, with JNetStream i

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

Mark Bednarczyk
Mark Bednarczyk's picture
Offline
Joined: 03/22/2008
> From the packet i need to

> 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.

Sly Technologies, Inc.
R&D

parasuraman
Offline
Joined: 04/01/2009
Licensing term of JNetStreamStandalone.jar Version 0.2.4

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

Mark Bednarczyk
Mark Bednarczyk's picture
Offline
Joined: 03/22/2008
jNetStream uses LGPL license

jNetStream uses LGPL license same as jNetPcap.

Sly Technologies, Inc.
R&D

parasuraman
Offline
Joined: 04/01/2009
Thank you Mark

Hi Mark,

Thanks you so much..

Regards
Parasuraman

pkgoms
Offline
Joined: 05/31/2009
Hai Parasuraman, I am new to

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

Mark Bednarczyk
Mark Bednarczyk's picture
Offline
Joined: 03/22/2008
A quick update. I have added

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.

Sly Technologies, Inc.
R&D

Mark Bednarczyk
Mark Bednarczyk's picture
Offline
Joined: 03/22/2008
Just wanted to let you know,

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 Cool

Sly Technologies, Inc.
R&D

mardy
Offline
Joined: 03/30/2009
Where is "BYTE" defined

Mark, In your new Rtp class, where is the constant "BYTE" defined?

Mark Bednarczyk
Mark Bednarczyk's picture
Offline
Joined: 03/22/2008
Its in the updated JHeader

Its in the updated JHeader class as a public static final constant. If you SVN update your project, it should be there now.

Sly Technologies, Inc.
R&D

jollysar
Offline
Joined: 05/12/2010
getIterable

Hy Mark,

Could you post the method getIterable?

Thanks for your support,
Best Regards

Mark Bednarczyk
Mark Bednarczyk's picture
Offline
Joined: 03/22/2008
Not sure I understand your

Not sure I understand your request. Please be more specific.

Sly Technologies, Inc.
R&D

jollysar
Offline
Joined: 05/12/2010
GetIterable

for (PcapPacket packet: super.getIterable(SIP_G711))
{
.
.
.
.
.
}

Could you post The Method getIterable(String) ?
Thanks a lot
bye

Mark Bednarczyk
Mark Bednarczyk's picture
Offline
Joined: 03/22/2008
I'm still not sure what you

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...

Sly Technologies, Inc.
R&D

jollysar
Offline
Joined: 05/12/2010
Hy Markthanks a lot for you

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

Mark Bednarczyk
Mark Bednarczyk's picture
Offline
Joined: 03/22/2008
What does the errbuf say?

What does the errbuf say?

Sly Technologies, Inc.
R&D

jollysar
Offline
Joined: 05/12/2010
Hy Mark, sorry the dump file

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

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.