RTPSink.hh 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 Sinks
  17. // C++ header
  18. #ifndef _RTP_SINK_HH
  19. #define _RTP_SINK_HH
  20. #ifndef _MEDIA_SINK_HH
  21. #include "MediaSink.hh"
  22. #endif
  23. #ifndef _RTP_INTERFACE_HH
  24. #include "RTPInterface.hh"
  25. #endif
  26. class RTPTransmissionStatsDB; // forward
  27. class RTPSink: public MediaSink {
  28. public:
  29. static Boolean lookupByName(UsageEnvironment& env, char const* sinkName,
  30. RTPSink*& resultSink);
  31. // used by RTSP servers:
  32. Groupsock const& groupsockBeingUsed() const { return *(fRTPInterface.gs()); }
  33. Groupsock& groupsockBeingUsed() { return *(fRTPInterface.gs()); }
  34. unsigned char rtpPayloadType() const { return fRTPPayloadType; }
  35. unsigned rtpTimestampFrequency() const { return fTimestampFrequency; }
  36. void setRTPTimestampFrequency(unsigned freq) {
  37. fTimestampFrequency = freq;
  38. }
  39. char const* rtpPayloadFormatName() const {return fRTPPayloadFormatName;}
  40. unsigned numChannels() const { return fNumChannels; }
  41. virtual char const* sdpMediaType() const; // for use in SDP m= lines
  42. virtual char* rtpmapLine() const; // returns a string to be delete[]d
  43. virtual char const* auxSDPLine();
  44. // optional SDP line (e.g. a=fmtp:...)
  45. u_int16_t currentSeqNo() const { return fSeqNo; }
  46. u_int32_t presetNextTimestamp();
  47. // ensures that the next timestamp to be used will correspond to
  48. // the current 'wall clock' time.
  49. RTPTransmissionStatsDB& transmissionStatsDB() const {
  50. return *fTransmissionStatsDB;
  51. }
  52. Boolean nextTimestampHasBeenPreset() const { return fNextTimestampHasBeenPreset; }
  53. Boolean& enableRTCPReports() { return fEnableRTCPReports; }
  54. void getTotalBitrate(unsigned& outNumBytes, double& outElapsedTime);
  55. // returns the number of bytes sent since the last time that we
  56. // were called, and resets the counter.
  57. struct timeval const& creationTime() const { return fCreationTime; }
  58. struct timeval const& initialPresentationTime() const { return fInitialPresentationTime; }
  59. struct timeval const& mostRecentPresentationTime() const { return fMostRecentPresentationTime; }
  60. void resetPresentationTimes();
  61. // Hacks to allow sending RTP over TCP (RFC 2236, section 10.12):
  62. void setStreamSocket(int sockNum, unsigned char streamChannelId) {
  63. fRTPInterface.setStreamSocket(sockNum, streamChannelId);
  64. }
  65. void addStreamSocket(int sockNum, unsigned char streamChannelId) {
  66. fRTPInterface.addStreamSocket(sockNum, streamChannelId);
  67. }
  68. void removeStreamSocket(int sockNum, unsigned char streamChannelId) {
  69. fRTPInterface.removeStreamSocket(sockNum, streamChannelId);
  70. }
  71. unsigned& estimatedBitrate() { return fEstimatedBitrate; } // kbps; usually 0 (i.e., unset)
  72. u_int32_t SSRC() const {return fSSRC;}
  73. // later need a means of changing the SSRC if there's a collision #####
  74. protected:
  75. RTPSink(UsageEnvironment& env,
  76. Groupsock* rtpGS, unsigned char rtpPayloadType,
  77. u_int32_t rtpTimestampFrequency,
  78. char const* rtpPayloadFormatName,
  79. unsigned numChannels);
  80. // abstract base class
  81. virtual ~RTPSink();
  82. // used by RTCP:
  83. friend class RTCPInstance;
  84. friend class RTPTransmissionStats;
  85. u_int32_t convertToRTPTimestamp(struct timeval tv);
  86. unsigned packetCount() const {return fPacketCount;}
  87. unsigned octetCount() const {return fOctetCount;}
  88. protected:
  89. RTPInterface fRTPInterface;
  90. unsigned char fRTPPayloadType;
  91. unsigned fPacketCount, fOctetCount, fTotalOctetCount /*incl RTP hdr*/;
  92. struct timeval fTotalOctetCountStartTime, fInitialPresentationTime, fMostRecentPresentationTime;
  93. u_int32_t fCurrentTimestamp;
  94. u_int16_t fSeqNo;
  95. private:
  96. // redefined virtual functions:
  97. virtual Boolean isRTPSink() const;
  98. private:
  99. u_int32_t fSSRC, fTimestampBase;
  100. unsigned fTimestampFrequency;
  101. Boolean fNextTimestampHasBeenPreset;
  102. Boolean fEnableRTCPReports; // whether RTCP "SR" reports should be sent for this sink (default: True)
  103. char const* fRTPPayloadFormatName;
  104. unsigned fNumChannels;
  105. struct timeval fCreationTime;
  106. unsigned fEstimatedBitrate; // set on creation if known; otherwise 0
  107. RTPTransmissionStatsDB* fTransmissionStatsDB;
  108. };
  109. class RTPTransmissionStats; // forward
  110. class RTPTransmissionStatsDB {
  111. public:
  112. unsigned numReceivers() const { return fNumReceivers; }
  113. class Iterator {
  114. public:
  115. Iterator(RTPTransmissionStatsDB& receptionStatsDB);
  116. virtual ~Iterator();
  117. RTPTransmissionStats* next();
  118. // NULL if none
  119. private:
  120. HashTable::Iterator* fIter;
  121. };
  122. // The following is called whenever a RTCP RR packet is received:
  123. void noteIncomingRR(u_int32_t SSRC, struct sockaddr_in const& lastFromAddress,
  124. unsigned lossStats, unsigned lastPacketNumReceived,
  125. unsigned jitter, unsigned lastSRTime, unsigned diffSR_RRTime);
  126. // The following is called when a RTCP BYE packet is received:
  127. void removeRecord(u_int32_t SSRC);
  128. RTPTransmissionStats* lookup(u_int32_t SSRC) const;
  129. private: // constructor and destructor, called only by RTPSink:
  130. friend class RTPSink;
  131. RTPTransmissionStatsDB(RTPSink& rtpSink);
  132. virtual ~RTPTransmissionStatsDB();
  133. private:
  134. void add(u_int32_t SSRC, RTPTransmissionStats* stats);
  135. private:
  136. friend class Iterator;
  137. unsigned fNumReceivers;
  138. RTPSink& fOurRTPSink;
  139. HashTable* fTable;
  140. };
  141. class RTPTransmissionStats {
  142. public:
  143. u_int32_t SSRC() const {return fSSRC;}
  144. struct sockaddr_in const& lastFromAddress() const {return fLastFromAddress;}
  145. unsigned lastPacketNumReceived() const {return fLastPacketNumReceived;}
  146. unsigned firstPacketNumReported() const {return fFirstPacketNumReported;}
  147. unsigned totNumPacketsLost() const {return fTotNumPacketsLost;}
  148. unsigned jitter() const {return fJitter;}
  149. unsigned lastSRTime() const { return fLastSRTime; }
  150. unsigned diffSR_RRTime() const { return fDiffSR_RRTime; }
  151. unsigned roundTripDelay() const;
  152. // The round-trip delay (in units of 1/65536 seconds) computed from
  153. // the most recently-received RTCP RR packet.
  154. struct timeval const& timeCreated() const {return fTimeCreated;}
  155. struct timeval const& lastTimeReceived() const {return fTimeReceived;}
  156. void getTotalOctetCount(u_int32_t& hi, u_int32_t& lo);
  157. void getTotalPacketCount(u_int32_t& hi, u_int32_t& lo);
  158. // Information which requires at least two RRs to have been received:
  159. unsigned packetsReceivedSinceLastRR() const;
  160. u_int8_t packetLossRatio() const { return fPacketLossRatio; }
  161. // as an 8-bit fixed-point number
  162. int packetsLostBetweenRR() const;
  163. private:
  164. // called only by RTPTransmissionStatsDB:
  165. friend class RTPTransmissionStatsDB;
  166. RTPTransmissionStats(RTPSink& rtpSink, u_int32_t SSRC);
  167. virtual ~RTPTransmissionStats();
  168. void noteIncomingRR(struct sockaddr_in const& lastFromAddress,
  169. unsigned lossStats, unsigned lastPacketNumReceived,
  170. unsigned jitter,
  171. unsigned lastSRTime, unsigned diffSR_RRTime);
  172. private:
  173. RTPSink& fOurRTPSink;
  174. u_int32_t fSSRC;
  175. struct sockaddr_in fLastFromAddress;
  176. unsigned fLastPacketNumReceived;
  177. u_int8_t fPacketLossRatio;
  178. unsigned fTotNumPacketsLost;
  179. unsigned fJitter;
  180. unsigned fLastSRTime;
  181. unsigned fDiffSR_RRTime;
  182. struct timeval fTimeCreated, fTimeReceived;
  183. Boolean fAtLeastTwoRRsHaveBeenReceived;
  184. unsigned fOldLastPacketNumReceived;
  185. unsigned fOldTotNumPacketsLost;
  186. Boolean fFirstPacket;
  187. unsigned fFirstPacketNumReported;
  188. u_int32_t fLastOctetCount, fTotalOctetCount_hi, fTotalOctetCount_lo;
  189. u_int32_t fLastPacketCount, fTotalPacketCount_hi, fTotalPacketCount_lo;
  190. };
  191. #endif