MultiFramedRTPSource.hh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**********
  2. This library is free software; you can redistribute it and/or modify it under
  3. the terms of the GNU Lesser General Public License as published by the
  4. Free Software Foundation; either version 3 of the License, or (at your
  5. option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
  6. This library is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  9. more details.
  10. You should have received a copy of the GNU Lesser General Public License
  11. along with this library; if not, write to the Free Software Foundation, Inc.,
  12. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  13. **********/
  14. // "liveMedia"
  15. // Copyright (c) 1996-2019 Live Networks, Inc. All rights reserved.
  16. // RTP source for a common kind of payload format: Those which pack multiple,
  17. // complete codec frames (as many as possible) into each RTP packet.
  18. // C++ header
  19. #ifndef _MULTI_FRAMED_RTP_SOURCE_HH
  20. #define _MULTI_FRAMED_RTP_SOURCE_HH
  21. #ifndef _RTP_SOURCE_HH
  22. #include "RTPSource.hh"
  23. #endif
  24. class BufferedPacket; // forward
  25. class BufferedPacketFactory; // forward
  26. class MultiFramedRTPSource: public RTPSource {
  27. protected:
  28. MultiFramedRTPSource(UsageEnvironment& env, Groupsock* RTPgs,
  29. unsigned char rtpPayloadFormat,
  30. unsigned rtpTimestampFrequency,
  31. BufferedPacketFactory* packetFactory = NULL);
  32. // virtual base class
  33. virtual ~MultiFramedRTPSource();
  34. virtual Boolean processSpecialHeader(BufferedPacket* packet,
  35. unsigned& resultSpecialHeaderSize);
  36. // Subclasses redefine this to handle any special, payload format
  37. // specific header that follows the RTP header.
  38. virtual Boolean packetIsUsableInJitterCalculation(unsigned char* packet,
  39. unsigned packetSize);
  40. // The default implementation returns True, but this can be redefined
  41. protected:
  42. Boolean fCurrentPacketBeginsFrame;
  43. Boolean fCurrentPacketCompletesFrame;
  44. protected:
  45. // redefined virtual functions:
  46. virtual void doGetNextFrame();
  47. virtual void doStopGettingFrames();
  48. private:
  49. // redefined virtual functions:
  50. virtual void setPacketReorderingThresholdTime(unsigned uSeconds);
  51. private:
  52. void reset();
  53. void doGetNextFrame1();
  54. static void networkReadHandler(MultiFramedRTPSource* source, int /*mask*/);
  55. void networkReadHandler1();
  56. Boolean fAreDoingNetworkReads;
  57. BufferedPacket* fPacketReadInProgress;
  58. Boolean fNeedDelivery;
  59. Boolean fPacketLossInFragmentedFrame;
  60. unsigned char* fSavedTo;
  61. unsigned fSavedMaxSize;
  62. // A buffer to (optionally) hold incoming pkts that have been reorderered
  63. class ReorderingPacketBuffer* fReorderingBuffer;
  64. };
  65. // A 'packet data' class that's used to implement the above.
  66. // Note that this can be subclassed - if desired - to redefine
  67. // "nextEnclosedFrameParameters()".
  68. class BufferedPacket {
  69. public:
  70. BufferedPacket();
  71. virtual ~BufferedPacket();
  72. Boolean hasUsableData() const { return fTail > fHead; }
  73. unsigned useCount() const { return fUseCount; }
  74. Boolean fillInData(RTPInterface& rtpInterface, struct sockaddr_in& fromAddress, Boolean& packetReadWasIncomplete);
  75. void assignMiscParams(unsigned short rtpSeqNo, unsigned rtpTimestamp,
  76. struct timeval presentationTime,
  77. Boolean hasBeenSyncedUsingRTCP,
  78. Boolean rtpMarkerBit, struct timeval timeReceived);
  79. void skip(unsigned numBytes); // used to skip over an initial header
  80. void removePadding(unsigned numBytes); // used to remove trailing bytes
  81. void appendData(unsigned char* newData, unsigned numBytes);
  82. void use(unsigned char* to, unsigned toSize,
  83. unsigned& bytesUsed, unsigned& bytesTruncated,
  84. unsigned short& rtpSeqNo, unsigned& rtpTimestamp,
  85. struct timeval& presentationTime,
  86. Boolean& hasBeenSyncedUsingRTCP, Boolean& rtpMarkerBit);
  87. BufferedPacket*& nextPacket() { return fNextPacket; }
  88. unsigned short rtpSeqNo() const { return fRTPSeqNo; }
  89. struct timeval const& timeReceived() const { return fTimeReceived; }
  90. unsigned char* data() const { return &fBuf[fHead]; }
  91. unsigned dataSize() const { return fTail-fHead; }
  92. Boolean rtpMarkerBit() const { return fRTPMarkerBit; }
  93. Boolean& isFirstPacket() { return fIsFirstPacket; }
  94. unsigned bytesAvailable() const { return fPacketSize - fTail; }
  95. protected:
  96. virtual void reset();
  97. virtual unsigned nextEnclosedFrameSize(unsigned char*& framePtr,
  98. unsigned dataSize);
  99. // The above function has been deprecated. Instead, new subclasses should use:
  100. virtual void getNextEnclosedFrameParameters(unsigned char*& framePtr,
  101. unsigned dataSize,
  102. unsigned& frameSize,
  103. unsigned& frameDurationInMicroseconds);
  104. unsigned fPacketSize;
  105. unsigned char* fBuf;
  106. unsigned fHead;
  107. unsigned fTail;
  108. private:
  109. BufferedPacket* fNextPacket; // used to link together packets
  110. unsigned fUseCount;
  111. unsigned short fRTPSeqNo;
  112. unsigned fRTPTimestamp;
  113. struct timeval fPresentationTime; // corresponding to "fRTPTimestamp"
  114. Boolean fHasBeenSyncedUsingRTCP;
  115. Boolean fRTPMarkerBit;
  116. Boolean fIsFirstPacket;
  117. struct timeval fTimeReceived;
  118. };
  119. // A 'factory' class for creating "BufferedPacket" objects.
  120. // If you want to subclass "BufferedPacket", then you'll also
  121. // want to subclass this, to redefine createNewPacket()
  122. class BufferedPacketFactory {
  123. public:
  124. BufferedPacketFactory();
  125. virtual ~BufferedPacketFactory();
  126. virtual BufferedPacket* createNewPacket(MultiFramedRTPSource* ourSource);
  127. };
  128. #endif