RTPpacket.java

  1. //class RTPpacket
  2.  
  3. public class RTPpacket{
  4.  
  5. //size of the RTP header:
  6. static int HEADER_SIZE = 12;
  7.  
  8. //Fields that compose the RTP header
  9. public int Version;
  10. public int Padding;
  11. public int Extension;
  12. public int CC;
  13. public int Marker;
  14. public int PayloadType;
  15. public int SequenceNumber;
  16. public int TimeStamp;
  17. public int Ssrc;
  18.  
  19. //Bitstream of the RTP header
  20. public byte[] header;
  21.  
  22. //size of the RTP payload
  23. public int payload_size;
  24. //Bitstream of the RTP payload
  25. public byte[] payload;
  26.  
  27.  
  28.  
  29. //--------------------------
  30. //Constructor of an RTPpacket object from header fields and payload bitstream
  31. //--------------------------
  32. public RTPpacket(int PType, int Framenb, int Time, byte[] data, int data_length){
  33. //fill by default header fields:
  34. Version = 2;
  35. Padding = 0;
  36. Extension = 0;
  37. CC = 0;
  38. Marker = 0;
  39. Ssrc = 0;
  40.  
  41. //fill changing header fields:
  42. SequenceNumber = Framenb;
  43. TimeStamp = Time;
  44. PayloadType = PType;
  45.  
  46. //build the header bistream:
  47. //--------------------------
  48. header = new byte[HEADER_SIZE];
  49.  
  50. //.............
  51. //TO COMPLETE
  52. //.............
  53. //fill the header array of byte with RTP header fields
  54.  
  55. //header[0] = ...
  56. // .....
  57.  
  58.  
  59. //fill the payload bitstream:
  60. //--------------------------
  61. payload_size = data_length;
  62. payload = new byte[data_length];
  63.  
  64. //fill payload array of byte from data (given in parameter of the constructor)
  65. //......
  66.  
  67. // ! Do not forget to uncomment method printheader() below !
  68.  
  69. }
  70.  
  71. //--------------------------
  72. //Constructor of an RTPpacket object from the packet bistream
  73. //--------------------------
  74. public RTPpacket(byte[] packet, int packet_size)
  75. {
  76. //fill default fields:
  77. Version = 2;
  78. Padding = 0;
  79. Extension = 0;
  80. CC = 0;
  81. Marker = 0;
  82. Ssrc = 0;
  83.  
  84. //check if total packet size is lower than the header size
  85. if (packet_size >= HEADER_SIZE)
  86. {
  87. //get the header bitsream:
  88. header = new byte[HEADER_SIZE];
  89. for (int i=0; i < HEADER_SIZE; i++)
  90. header[i] = packet[i];
  91.  
  92. //get the payload bitstream:
  93. payload_size = packet_size - HEADER_SIZE;
  94. payload = new byte[payload_size];
  95. for (int i=HEADER_SIZE; i < packet_size; i++)
  96. payload[i-HEADER_SIZE] = packet[i];
  97.  
  98. //interpret the changing fields of the header:
  99. PayloadType = header[1] & 127;
  100. SequenceNumber = unsigned_int(header[3]) + 256*unsigned_int(header[2]);
  101. TimeStamp = unsigned_int(header[7]) + 256*unsigned_int(header[6]) + 65536*unsigned_int(header[5]) + 16777216*unsigned_int(header[4]);
  102. }
  103. }
  104.  
  105. //--------------------------
  106. //getpayload: return the payload bistream of the RTPpacket and its size
  107. //--------------------------
  108. public int getpayload(byte[] data) {
  109.  
  110. for (int i=0; i < payload_size; i++)
  111. data[i] = payload[i];
  112.  
  113. return(payload_size);
  114. }
  115.  
  116. //--------------------------
  117. //getpayload_length: return the length of the payload
  118. //--------------------------
  119. public int getpayload_length() {
  120. return(payload_size);
  121. }
  122.  
  123. //--------------------------
  124. //getlength: return the total length of the RTP packet
  125. //--------------------------
  126. public int getlength() {
  127. return(payload_size + HEADER_SIZE);
  128. }
  129.  
  130. //--------------------------
  131. //getpacket: returns the packet bitstream and its length
  132. //--------------------------
  133. public int getpacket(byte[] packet)
  134. {
  135. //construct the packet = header + payload
  136. for (int i=0; i < HEADER_SIZE; i++)
  137. packet[i] = header[i];
  138. for (int i=0; i < payload_size; i++)
  139. packet[i+HEADER_SIZE] = payload[i];
  140.  
  141. //return total size of the packet
  142. return(payload_size + HEADER_SIZE);
  143. }
  144.  
  145. //--------------------------
  146. //gettimestamp
  147. //--------------------------
  148.  
  149. public int gettimestamp() {
  150. return(TimeStamp);
  151. }
  152.  
  153. //--------------------------
  154. //getsequencenumber
  155. //--------------------------
  156. public int getsequencenumber() {
  157. return(SequenceNumber);
  158. }
  159.  
  160. //--------------------------
  161. //getpayloadtype
  162. //--------------------------
  163. public int getpayloadtype() {
  164. return(PayloadType);
  165. }
  166.  
  167.  
  168. //--------------------------
  169. //print headers without the SSRC
  170. //--------------------------
  171. public void printheader()
  172. {
  173. //TO DO: uncomment
  174. /*
  175. for (int i=0; i < (HEADER_SIZE-4); i++)
  176. {
  177. for (int j = 7; j>=0 ; j--)
  178. if (((1<<j) & header[i] ) != 0)
  179. System.out.print("1");
  180. else
  181. System.out.print("0");
  182. System.out.print(" ");
  183. }
  184.  
  185. System.out.println();
  186. */
  187. }
  188.  
  189. //return the unsigned value of 8-bit integer nb
  190. static int unsigned_int(int nb) {
  191. if (nb >= 0)
  192. return(nb);
  193. else
  194. return(256+nb);
  195. }
  196.  
  197. }