RTP packet handler


/ Published in: C++
Save to your folder(s)

cpp code to parse packet headers and find the actual audio frames in the packet


Copy this code and paste it in your HTML
  1. void MultiFramedRTPSource::networkReadHandler1() {
  2. BufferedPacket* bPacket = fPacketReadInProgress;
  3. if (bPacket == NULL) {
  4. // Normal case: Get a free BufferedPacket descriptor to hold the new network packet:
  5. bPacket = fReorderingBuffer->getFreePacket(this);
  6. }
  7.  
  8. // Read the network packet, and perform sanity checks on the RTP header:
  9. Boolean readSuccess = False;
  10. do {
  11. Boolean packetReadWasIncomplete = fPacketReadInProgress != NULL;
  12. if (!bPacket->fillInData(fRTPInterface, packetReadWasIncomplete)) break;
  13. if (packetReadWasIncomplete) {
  14. // We need additional read(s) before we can process the incoming packet:
  15. fPacketReadInProgress = bPacket;
  16. return;
  17. } else {
  18. fPacketReadInProgress = NULL;
  19. }
  20. #ifdef TEST_LOSS
  21. setPacketReorderingThresholdTime(0);
  22. // don't wait for 'lost' packets to arrive out-of-order later
  23. if ((our_random()%10) == 0) break; // simulate 10% packet loss
  24. #endif
  25.  
  26. // Check for the 12-byte RTP header:
  27. if (bPacket->dataSize() < 12) break;
  28. unsigned rtpHdr = ntohl(*(u_int32_t*)(bPacket->data())); ADVANCE(4);
  29. Boolean rtpMarkerBit = (rtpHdr&0x00800000) >> 23;
  30. unsigned rtpTimestamp = ntohl(*(u_int32_t*)(bPacket->data()));ADVANCE(4);
  31. unsigned rtpSSRC = ntohl(*(u_int32_t*)(bPacket->data())); ADVANCE(4);
  32. // rcr it looks like the java version has 10 extra bytes after SSRC
  33. // Check the RTP version number (it should be 2):
  34. if ((rtpHdr&0xC0000000) != 0x80000000) break;
  35.  
  36. // Skip over any CSRC identifiers in the header:
  37. unsigned cc = (rtpHdr>>24)&0xF;
  38. if (bPacket->dataSize() < cc) break;
  39. ADVANCE(cc*4);
  40.  
  41. //rcr the above does not seem to be skipping any bytes
  42.  
  43. // Check for (& ignore) any RTP header extension
  44. if (rtpHdr&0x10000000) {
  45. if (bPacket->dataSize() < 4) break;
  46. unsigned extHdr = ntohl(*(u_int32_t*)(bPacket->data())); ADVANCE(4);
  47. unsigned remExtSize = 4*(extHdr&0xFFFF);
  48. if (bPacket->dataSize() < remExtSize) break;
  49. ADVANCE(remExtSize);
  50. }
  51. //rcr the above does not seem to b skipping
  52.  
  53. // Discard any padding bytes:
  54. if (rtpHdr&0x20000000) {
  55. if (bPacket->dataSize() == 0) break;
  56. unsigned numPaddingBytes
  57. = (unsigned)(bPacket->data())[bPacket->dataSize()-1];
  58. if (bPacket->dataSize() < numPaddingBytes) break;
  59. bPacket->removePadding(numPaddingBytes);
  60. }
  61.  
  62. //rcr the above does not seem skipping bytes
  63.  
  64. // Check the Payload Type.
  65. if ((unsigned char)((rtpHdr&0x007F0000)>>16)
  66. != rtpPayloadFormat()) {
  67. break;
  68. }
  69.  
  70. // The rest of the packet is the usable data. Record and save it:
  71. fLastReceivedSSRC = rtpSSRC;
  72. unsigned short rtpSeqNo = (unsigned short)(rtpHdr&0xFFFF);
  73. Boolean usableInJitterCalculation
  74. = packetIsUsableInJitterCalculation((bPacket->data()),
  75. bPacket->dataSize());
  76. struct timeval presentationTime; // computed by:
  77. Boolean hasBeenSyncedUsingRTCP; // computed by:
  78. receptionStatsDB()
  79. .noteIncomingPacket(rtpSSRC, rtpSeqNo, rtpTimestamp,
  80. timestampFrequency(),
  81. usableInJitterCalculation, presentationTime,
  82. hasBeenSyncedUsingRTCP, bPacket->dataSize());
  83. //rcr this dataSize is the same as the java getPacketData().length
  84. // envir() << "M-frmRTPsrc.readHndlr pck sz " << bPacket->dataSize();
  85.  
  86. // Fill in the rest of the packet descriptor, and store it:
  87. struct timeval timeNow;
  88. gettimeofday(&timeNow, NULL);
  89. bPacket->assignMiscParams(rtpSeqNo, rtpTimestamp, presentationTime,
  90. hasBeenSyncedUsingRTCP, rtpMarkerBit,
  91. timeNow);
  92. if (!fReorderingBuffer->storePacket(bPacket)) break;
  93.  
  94. readSuccess = True;
  95. } while (0);
  96. if (!readSuccess) fReorderingBuffer->freePacket(bPacket);
  97.  
  98. doGetNextFrame1();
  99. // If we didn't get proper data this time, we'll get another chance
  100. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.