RTPSource.hh 9.1 KB

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