RTPSource.hh 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 Sources
  17. // C++ header
  18. #ifndef _RTP_SOURCE_HH
  19. #define _RTP_SOURCE_HH
  20. #ifndef _FRAMED_SOURCE_HH
  21. #include "FramedSource.hh"
  22. #endif
  23. #ifndef _RTP_INTERFACE_HH
  24. #include "RTPInterface.hh"
  25. #endif
  26. class RTPReceptionStatsDB; // forward
  27. class RTPSource: public FramedSource {
  28. public:
  29. static Boolean lookupByName(UsageEnvironment& env, char const* sourceName,
  30. RTPSource*& resultSource);
  31. Boolean curPacketMarkerBit() const { return fCurPacketMarkerBit; }
  32. unsigned char rtpPayloadFormat() const { return fRTPPayloadFormat; }
  33. virtual Boolean hasBeenSynchronizedUsingRTCP();
  34. Groupsock* RTPgs() const { return fRTPInterface.gs(); }
  35. virtual void setPacketReorderingThresholdTime(unsigned uSeconds) = 0;
  36. // used by RTCP:
  37. u_int32_t SSRC() const { return fSSRC; }
  38. // Note: This is *our* SSRC, not the SSRC in incoming RTP packets.
  39. // later need a means of changing the SSRC if there's a collision #####
  40. void registerForMultiplexedRTCPPackets(class RTCPInstance* rtcpInstance) {
  41. fRTCPInstanceForMultiplexedRTCPPackets = rtcpInstance;
  42. }
  43. void deregisterForMultiplexedRTCPPackets() { registerForMultiplexedRTCPPackets(NULL); }
  44. unsigned timestampFrequency() const {return fTimestampFrequency;}
  45. RTPReceptionStatsDB& receptionStatsDB() const {
  46. return *fReceptionStatsDB;
  47. }
  48. u_int32_t lastReceivedSSRC() const { return fLastReceivedSSRC; }
  49. // Note: This is the SSRC in the most recently received RTP packet; not *our* SSRC
  50. Boolean& enableRTCPReports() { return fEnableRTCPReports; }
  51. Boolean const& enableRTCPReports() const { return fEnableRTCPReports; }
  52. void setStreamSocket(int sockNum, unsigned char streamChannelId) {
  53. // hack to allow sending RTP over TCP (RFC 2236, section 10.12)
  54. fRTPInterface.setStreamSocket(sockNum, streamChannelId);
  55. }
  56. void setAuxilliaryReadHandler(AuxHandlerFunc* handlerFunc,
  57. void* handlerClientData) {
  58. fRTPInterface.setAuxilliaryReadHandler(handlerFunc,
  59. handlerClientData);
  60. }
  61. // Note that RTP receivers will usually not need to call either of the following two functions, because
  62. // RTP sequence numbers and timestamps are usually not useful to receivers.
  63. // (Our implementation of RTP reception already does all needed handling of RTP sequence numbers and timestamps.)
  64. u_int16_t curPacketRTPSeqNum() const { return fCurPacketRTPSeqNum; }
  65. private: friend class MediaSubsession; // "MediaSubsession" is the only outside class that ever needs to see RTP timestamps!
  66. u_int32_t curPacketRTPTimestamp() const { return fCurPacketRTPTimestamp; }
  67. protected:
  68. RTPSource(UsageEnvironment& env, Groupsock* RTPgs,
  69. unsigned char rtpPayloadFormat, u_int32_t rtpTimestampFrequency);
  70. // abstract base class
  71. virtual ~RTPSource();
  72. protected:
  73. RTPInterface fRTPInterface;
  74. u_int16_t fCurPacketRTPSeqNum;
  75. u_int32_t fCurPacketRTPTimestamp;
  76. Boolean fCurPacketMarkerBit;
  77. Boolean fCurPacketHasBeenSynchronizedUsingRTCP;
  78. u_int32_t fLastReceivedSSRC;
  79. class RTCPInstance* fRTCPInstanceForMultiplexedRTCPPackets;
  80. private:
  81. // redefined virtual functions:
  82. virtual Boolean isRTPSource() const;
  83. virtual void getAttributes() const;
  84. private:
  85. unsigned char fRTPPayloadFormat;
  86. unsigned fTimestampFrequency;
  87. u_int32_t fSSRC;
  88. Boolean fEnableRTCPReports; // whether RTCP "RR" reports should be sent for this source (default: True)
  89. RTPReceptionStatsDB* fReceptionStatsDB;
  90. };
  91. class RTPReceptionStats; // forward
  92. class RTPReceptionStatsDB {
  93. public:
  94. unsigned totNumPacketsReceived() const { return fTotNumPacketsReceived; }
  95. unsigned numActiveSourcesSinceLastReset() const {
  96. return fNumActiveSourcesSinceLastReset;
  97. }
  98. void reset();
  99. // resets periodic stats (called each time they're used to
  100. // generate a reception report)
  101. class Iterator {
  102. public:
  103. Iterator(RTPReceptionStatsDB& receptionStatsDB);
  104. virtual ~Iterator();
  105. RTPReceptionStats* next(Boolean includeInactiveSources = False);
  106. // NULL if none
  107. private:
  108. HashTable::Iterator* fIter;
  109. };
  110. // The following is called whenever a RTP packet is received:
  111. void noteIncomingPacket(u_int32_t SSRC, u_int16_t seqNum,
  112. u_int32_t rtpTimestamp,
  113. unsigned timestampFrequency,
  114. Boolean useForJitterCalculation,
  115. struct timeval& resultPresentationTime,
  116. Boolean& resultHasBeenSyncedUsingRTCP,
  117. unsigned packetSize /* payload only */);
  118. // The following is called whenever a RTCP SR packet is received:
  119. void noteIncomingSR(u_int32_t SSRC,
  120. u_int32_t ntpTimestampMSW, u_int32_t ntpTimestampLSW,
  121. u_int32_t rtpTimestamp);
  122. // The following is called when a RTCP BYE packet is received:
  123. void removeRecord(u_int32_t SSRC);
  124. RTPReceptionStats* lookup(u_int32_t SSRC) const;
  125. protected: // constructor and destructor, called only by RTPSource:
  126. friend class RTPSource;
  127. RTPReceptionStatsDB();
  128. virtual ~RTPReceptionStatsDB();
  129. protected:
  130. void add(u_int32_t SSRC, RTPReceptionStats* stats);
  131. protected:
  132. friend class Iterator;
  133. unsigned fNumActiveSourcesSinceLastReset;
  134. private:
  135. HashTable* fTable;
  136. unsigned fTotNumPacketsReceived; // for all SSRCs
  137. };
  138. class RTPReceptionStats {
  139. public:
  140. u_int32_t SSRC() const { return fSSRC; }
  141. unsigned numPacketsReceivedSinceLastReset() const {
  142. return fNumPacketsReceivedSinceLastReset;
  143. }
  144. unsigned totNumPacketsReceived() const { return fTotNumPacketsReceived; }
  145. double totNumKBytesReceived() const;
  146. unsigned totNumPacketsExpected() const {
  147. return (fHighestExtSeqNumReceived - fBaseExtSeqNumReceived) + 1;
  148. }
  149. unsigned baseExtSeqNumReceived() const { return fBaseExtSeqNumReceived; }
  150. unsigned lastResetExtSeqNumReceived() const {
  151. return fLastResetExtSeqNumReceived;
  152. }
  153. unsigned highestExtSeqNumReceived() const {
  154. return fHighestExtSeqNumReceived;
  155. }
  156. unsigned jitter() const;
  157. unsigned lastReceivedSR_NTPmsw() const { return fLastReceivedSR_NTPmsw; }
  158. unsigned lastReceivedSR_NTPlsw() const { return fLastReceivedSR_NTPlsw; }
  159. struct timeval const& lastReceivedSR_time() const {
  160. return fLastReceivedSR_time;
  161. }
  162. unsigned minInterPacketGapUS() const { return fMinInterPacketGapUS; }
  163. unsigned maxInterPacketGapUS() const { return fMaxInterPacketGapUS; }
  164. struct timeval const& totalInterPacketGaps() const {
  165. return fTotalInterPacketGaps;
  166. }
  167. protected:
  168. // called only by RTPReceptionStatsDB:
  169. friend class RTPReceptionStatsDB;
  170. RTPReceptionStats(u_int32_t SSRC, u_int16_t initialSeqNum);
  171. RTPReceptionStats(u_int32_t SSRC);
  172. virtual ~RTPReceptionStats();
  173. private:
  174. void noteIncomingPacket(u_int16_t seqNum, u_int32_t rtpTimestamp,
  175. unsigned timestampFrequency,
  176. Boolean useForJitterCalculation,
  177. struct timeval& resultPresentationTime,
  178. Boolean& resultHasBeenSyncedUsingRTCP,
  179. unsigned packetSize /* payload only */);
  180. void noteIncomingSR(u_int32_t ntpTimestampMSW, u_int32_t ntpTimestampLSW,
  181. u_int32_t rtpTimestamp);
  182. void init(u_int32_t SSRC);
  183. void initSeqNum(u_int16_t initialSeqNum);
  184. void reset();
  185. // resets periodic stats (called each time they're used to
  186. // generate a reception report)
  187. protected:
  188. u_int32_t fSSRC;
  189. unsigned fNumPacketsReceivedSinceLastReset;
  190. unsigned fTotNumPacketsReceived;
  191. u_int32_t fTotBytesReceived_hi, fTotBytesReceived_lo;
  192. Boolean fHaveSeenInitialSequenceNumber;
  193. unsigned fBaseExtSeqNumReceived;
  194. unsigned fLastResetExtSeqNumReceived;
  195. unsigned fHighestExtSeqNumReceived;
  196. int fLastTransit; // used in the jitter calculation
  197. u_int32_t fPreviousPacketRTPTimestamp;
  198. double fJitter;
  199. // The following are recorded whenever we receive a RTCP SR for this SSRC:
  200. unsigned fLastReceivedSR_NTPmsw; // NTP timestamp (from SR), most-signif
  201. unsigned fLastReceivedSR_NTPlsw; // NTP timestamp (from SR), least-signif
  202. struct timeval fLastReceivedSR_time;
  203. struct timeval fLastPacketReceptionTime;
  204. unsigned fMinInterPacketGapUS, fMaxInterPacketGapUS;
  205. struct timeval fTotalInterPacketGaps;
  206. private:
  207. // Used to convert from RTP timestamp to 'wall clock' time:
  208. Boolean fHasBeenSynchronized;
  209. u_int32_t fSyncTimestamp;
  210. struct timeval fSyncTime;
  211. };
  212. Boolean seqNumLT(u_int16_t s1, u_int16_t s2);
  213. // a 'less-than' on 16-bit sequence numbers
  214. #endif