3

I do some learning of using voip over udp in a small network. I know there are bundles of libraries ready to do and overdo everything I ever need with a few method calls, but as I said I am learning, so need to reinvent the wheel to see how it works.

I am currently investigating the DatagramPacket class and I've noticed that there is no method that would set header information(ie packet order sequence number which I need to know to do interleaving) in DatagramPacket class.

A little code to reflect the environment:

byte[] block;
DatagramPacket packet; // UDP packet                

/* x Bytes per block , y blocks per second,
   z ms time block playback duration */

block = recorder.getBlock(); // assume I have class that handles audio
                              // recording and returns speech in a
                              // uncompressed form of bytes

packet = new DatagramPacket(block, block.length, clientIP, PORT);

Firstly, I assume that because it is UDP, the sender doesnt really care anything whatsoever besides the simple fact that he throws packets somewhere. So that is why there is no such method inside.

Secondly, I assume that I need to do it myself - add extra bytes to the byte block to be sent , which would contain a sequence number of a packet? However am also concerned that if I do that, then how do I recognize if bytes are header bytes not audio bytes? I can make assumption that first byte represents a number, however we know that byte can only represent 258 numbers. I've never really worked on byte level before. Or there maybe other techniques?

Shortly saying, to do interleaving I need to know how to set up packet sequence number as I can't order unordered packets :-)

Thank You,

skaffman
  • 398,947
  • 96
  • 818
  • 769
Aubergine
  • 5,862
  • 19
  • 66
  • 110

1 Answers1

6

You'll need to serialize/deserialize data types your program uses onto a byte array.

Lets assume you're talking about RTP, and you'd want to send a packet with these fields - look at chapter 5 in the RTP specs:

Version = 2 padding = 0 extension = 0 CSRC count = 1 marker = 0 payload type = 8 (G711 alaw) sequence number = 1234 timestamp = 1 one CSRC = 4321

Lets put these into some variables, using integers for ease, or long when we need to deal with an unsigned 32 bit value:

int version = 2;
int padding = 0;
int extension = 0;
int csrcCount = 1;
int marker = 0;
int payloadType = 8;
int sequenceNumber = 1234;
long timestamp = 1;
long ourCsrc = 4321;

byte buf[] = ...; //allocate this big enough to hold the RTP header + audio data

//assemble the first bytes according to the RTP spec (note, the spec marks version as bit 0 and 1, but
//this is really the high bits of the first byte ...
buf[0] = (byte) ((version & 0x3) << 6 | (padding & 0x1) << 5 | (extension & 0x1) << 4 | (csrcCount & 0xf));

//2.byte
buf[1] = (byte)((marker & 0x1) << 7 | payloadType & 0x7f);

//squence number, 2 bytes, in big endian format. So the MSB first, then the LSB.
buf[2] = (byte)((sequenceNumber & 0xff00) >> 8);
buf[3] = (byte)(sequenceNumber  & 0x00ff);

//packet timestamp , 4 bytes in big endian format
buf[4] = (byte)((timestamp & 0xff000000) >> 24);
buf[5] = (byte)((timestamp & 0x00ff0000) >> 16);
buf[6] = (byte)((timestamp & 0x0000ff00) >> 8);
buf[7] = (byte) (timestamp & 0x000000ff);
//our CSRC , 4 bytes in big endian format
buf[ 8] = (byte)((sequenceNumber & 0xff000000) >> 24);
buf[ 9] = (byte)((sequenceNumber & 0x00ff0000) >> 16);
buf[10] = (byte)((sequenceNumber & 0x0000ff00) >> 8);
buf[11] = (byte) (sequenceNumber & 0x000000ff);

That's the header, now you can copy the audio bytes into buf, starting at buf[12] and send buf as one packet.

Now, the above is ofcourse just to show the principles, an actual serializer for a RTP packet would have to deal with much more, in accordance to the RTP specificaion (e.g. you might need some extension headers, you might need more than one CSRC, you need the correct payload type according to the format of the audio data you have, you need to packetize and schedule those audio data correctly - e.g. for G.711Alaw you'll should fill each RTP packet with 160 bytes of audio data and send one packet every 20 milisecond.

nos
  • 223,662
  • 58
  • 417
  • 506
  • oh thats exactly what I was looking for. So much to learn and analyse, however I now have direction and that is important! Thanks – Aubergine Feb 01 '12 at 17:17
  • I asked phd guys about bit shifting showing your code, they said that casting alone is enough. So do I strictly need to use the shifting you provided if I can just call: buf[4] = (byte) sequenceNumber ? Thank You. – Aubergine Feb 06 '12 at 16:56
  • @Aubergine You need to put the most significant byte of the 4 byte integer into buf[4], and you cannot do that by casting an int to a byte. For the least significant byte, `buf[7] = (byte) timestamp;` would be sufficient though, instead of `buf[7] = (byte) (timestamp & 0x000000ff);` – nos Feb 07 '12 at 08:04
  • @Nos First, thanks I found your answer here helpful and have a question. Why AND the values before shifting the bits. For example, why `version & 0x3` or `timestamp & 0xff000000`? It seems that you are applying a bit mask with 1's. Wouldn't that be unnecessary or am I missing something? – strwils Jan 12 '15 at 15:58
  • @strwils Many of those are just for readability and clarity. `version&0x3` makes it clear that the version field is just 2 bits. `timestamp & 0xff000000` makes it clear that you're extracting the top 8 bits, and just `(timestamp >> 24)` would not do what you'd want because of sign extension when right shifting. If you need to, you can very well optimize the code if it's needed, but more often than not the JIT takes care of such optimizations anyway, and it's not worth it if you lose code readability . – nos Jan 12 '15 at 16:20