RTCP.hh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. // RTCP
  17. // C++ header
  18. #ifndef _RTCP_HH
  19. #define _RTCP_HH
  20. #ifndef _RTP_SINK_HH
  21. #include "RTPSink.hh"
  22. #endif
  23. #ifndef _RTP_SOURCE_HH
  24. #include "RTPSource.hh"
  25. #endif
  26. #ifndef _SRTP_CRYPTOGRAPHIC_CONTEXT_HH
  27. #include "SRTPCryptographicContext.hh"
  28. #endif
  29. class SDESItem {
  30. public:
  31. SDESItem(unsigned char tag, unsigned char const* value);
  32. unsigned char const* data() const {return fData;}
  33. unsigned totalSize() const;
  34. private:
  35. unsigned char fData[2 + 0xFF]; // first 2 bytes are tag and length
  36. };
  37. typedef void RTCPAppHandlerFunc(void* clientData,
  38. u_int8_t subtype, u_int32_t nameBytes/*big-endian order*/,
  39. u_int8_t* appDependentData, unsigned appDependentDataSize);
  40. class RTCPMemberDatabase; // forward
  41. typedef void ByeWithReasonHandlerFunc(void* clientData, char const* reason);
  42. class RTCPInstance: public Medium {
  43. public:
  44. static RTCPInstance* createNew(UsageEnvironment& env, Groupsock* RTCPgs,
  45. unsigned totSessionBW, /* in kbps */
  46. unsigned char const* cname,
  47. RTPSink* sink,
  48. RTPSource* source,
  49. Boolean isSSMTransmitter = False,
  50. SRTPCryptographicContext* crypto = NULL);
  51. static Boolean lookupByName(UsageEnvironment& env, char const* instanceName,
  52. RTCPInstance*& resultInstance);
  53. unsigned numMembers() const;
  54. unsigned totSessionBW() const { return fTotSessionBW; }
  55. void setByeHandler(TaskFunc* handlerTask, void* clientData,
  56. Boolean handleActiveParticipantsOnly = True);
  57. // Assigns a handler routine to be called if a "BYE" arrives.
  58. // The handler is called once only; for subsequent "BYE"s,
  59. // "setByeHandler()" would need to be called again.
  60. // If "handleActiveParticipantsOnly" is True, then the handler is called
  61. // only if the SSRC is for a known sender (if we have a "RTPSource"),
  62. // or if the SSRC is for a known receiver (if we have a "RTPSink").
  63. // This prevents (for example) the handler for a multicast receiver being
  64. // called if some other multicast receiver happens to exit.
  65. // If "handleActiveParticipantsOnly" is False, then the handler is called
  66. // for any incoming RTCP "BYE".
  67. // (To remove an existing "BYE" handler, call "setByeHandler()" again, with a "handlerTask" of NULL.)
  68. void setByeWithReasonHandler(ByeWithReasonHandlerFunc* handlerTask, void* clientData,
  69. Boolean handleActiveParticipantsOnly = True);
  70. // Like "setByeHandler()", except that a string 'reason for the bye' (received as part of
  71. // the RTCP "BYE" packet) is passed to the handler function (along with "clientData").
  72. // (The 'reason' parameter to the handler function will be a dynamically-allocated string,
  73. // or NULL, and should be delete[]d by the handler function.)
  74. void setSRHandler(TaskFunc* handlerTask, void* clientData);
  75. void setRRHandler(TaskFunc* handlerTask, void* clientData);
  76. // Assigns a handler routine to be called if a "SR" or "RR" packet
  77. // (respectively) arrives. Unlike "setByeHandler()", the handler will
  78. // be called once for each incoming "SR" or "RR". (To turn off handling,
  79. // call the function again with "handlerTask" (and "clientData") as NULL.)
  80. void setSpecificRRHandler(netAddressBits fromAddress, Port fromPort,
  81. TaskFunc* handlerTask, void* clientData);
  82. // Like "setRRHandler()", but applies only to "RR" packets that come from
  83. // a specific source address and port. (Note that if both a specific
  84. // and a general "RR" handler function is set, then both will be called.)
  85. void unsetSpecificRRHandler(netAddressBits fromAddress, Port fromPort); // equivalent to setSpecificRRHandler(..., NULL, NULL);
  86. void setAppHandler(RTCPAppHandlerFunc* handlerTask, void* clientData);
  87. // Assigns a handler routine to be called whenever an "APP" packet arrives. (To turn off
  88. // handling, call the function again with "handlerTask" (and "clientData") as NULL.)
  89. void sendAppPacket(u_int8_t subtype, char const* name,
  90. u_int8_t* appDependentData, unsigned appDependentDataSize);
  91. // Sends a custom RTCP "APP" packet to the peer(s). The parameters correspond to their
  92. // respective fields as described in the RTP/RTCP definition (RFC 3550).
  93. // Note that only the low-order 5 bits of "subtype" are used, and only the first 4 bytes
  94. // of "name" are used. (If "name" has fewer than 4 bytes, or is NULL,
  95. // then the remaining bytes are '\0'.)
  96. Groupsock* RTCPgs() const { return fRTCPInterface.gs(); }
  97. void setStreamSocket(int sockNum, unsigned char streamChannelId);
  98. void addStreamSocket(int sockNum, unsigned char streamChannelId);
  99. void removeStreamSocket(int sockNum, unsigned char streamChannelId) {
  100. fRTCPInterface.removeStreamSocket(sockNum, streamChannelId);
  101. }
  102. // hacks to allow sending RTP over TCP (RFC 2236, section 10.12)
  103. void setAuxilliaryReadHandler(AuxHandlerFunc* handlerFunc,
  104. void* handlerClientData) {
  105. fRTCPInterface.setAuxilliaryReadHandler(handlerFunc,
  106. handlerClientData);
  107. }
  108. void injectReport(u_int8_t const* packet, unsigned packetSize, struct sockaddr_in const& fromAddress);
  109. // Allows an outside party to inject an RTCP report (from other than the network interface)
  110. protected:
  111. RTCPInstance(UsageEnvironment& env, Groupsock* RTPgs, unsigned totSessionBW,
  112. unsigned char const* cname,
  113. RTPSink* sink, RTPSource* source,
  114. Boolean isSSMTransmitter,
  115. SRTPCryptographicContext* crypto);
  116. // called only by createNew()
  117. virtual ~RTCPInstance();
  118. virtual void noteArrivingRR(struct sockaddr_in const& fromAddressAndPort,
  119. int tcpSocketNum, unsigned char tcpStreamChannelId);
  120. void incomingReportHandler1();
  121. private:
  122. // redefined virtual functions:
  123. virtual Boolean isRTCPInstance() const;
  124. private:
  125. Boolean addReport(Boolean alwaysAdd = False);
  126. void addSR();
  127. void addRR();
  128. void enqueueCommonReportPrefix(unsigned char packetType, u_int32_t SSRC,
  129. unsigned numExtraWords = 0);
  130. void enqueueCommonReportSuffix();
  131. void enqueueReportBlock(RTPReceptionStats* receptionStats);
  132. void addSDES();
  133. void addBYE(char const* reason);
  134. void sendBuiltPacket();
  135. static void onExpire(RTCPInstance* instance);
  136. void onExpire1();
  137. static void incomingReportHandler(RTCPInstance* instance, int /*mask*/);
  138. void processIncomingReport(unsigned packetSize, struct sockaddr_in const& fromAddressAndPort,
  139. int tcpSocketNum, unsigned char tcpStreamChannelId);
  140. void onReceive(int typeOfPacket, int totPacketSize, u_int32_t ssrc);
  141. private:
  142. u_int8_t* fInBuf;
  143. unsigned fNumBytesAlreadyRead;
  144. OutPacketBuffer* fOutBuf;
  145. RTPInterface fRTCPInterface;
  146. unsigned fTotSessionBW;
  147. RTPSink* fSink;
  148. RTPSource* fSource;
  149. Boolean fIsSSMTransmitter;
  150. SRTPCryptographicContext* fCrypto;
  151. SDESItem fCNAME;
  152. RTCPMemberDatabase* fKnownMembers;
  153. unsigned fOutgoingReportCount; // used for SSRC member aging
  154. double fAveRTCPSize;
  155. int fIsInitial;
  156. double fPrevReportTime;
  157. double fNextReportTime;
  158. int fPrevNumMembers;
  159. int fLastSentSize;
  160. int fLastReceivedSize;
  161. u_int32_t fLastReceivedSSRC;
  162. int fTypeOfEvent;
  163. int fTypeOfPacket;
  164. Boolean fHaveJustSentPacket;
  165. unsigned fLastPacketSentSize;
  166. TaskFunc* fByeHandlerTask;
  167. ByeWithReasonHandlerFunc* fByeWithReasonHandlerTask;
  168. void* fByeHandlerClientData;
  169. Boolean fByeHandleActiveParticipantsOnly;
  170. TaskFunc* fSRHandlerTask;
  171. void* fSRHandlerClientData;
  172. TaskFunc* fRRHandlerTask;
  173. void* fRRHandlerClientData;
  174. AddressPortLookupTable* fSpecificRRHandlerTable;
  175. RTCPAppHandlerFunc* fAppHandlerTask;
  176. void* fAppHandlerClientData;
  177. public: // because this stuff is used by an external "C" function
  178. void schedule(double nextTime);
  179. void reschedule(double nextTime);
  180. void sendReport();
  181. void sendBYE(char const* reason = NULL);
  182. int typeOfEvent() {return fTypeOfEvent;}
  183. int sentPacketSize() {return fLastSentSize;}
  184. int packetType() {return fTypeOfPacket;}
  185. int receivedPacketSize() {return fLastReceivedSize;}
  186. int checkNewSSRC();
  187. void removeLastReceivedSSRC();
  188. void removeSSRC(u_int32_t ssrc, Boolean alsoRemoveStats);
  189. };
  190. // RTCP packet types:
  191. const unsigned char RTCP_PT_SR = 200;
  192. const unsigned char RTCP_PT_RR = 201;
  193. const unsigned char RTCP_PT_SDES = 202;
  194. const unsigned char RTCP_PT_BYE = 203;
  195. const unsigned char RTCP_PT_APP = 204;
  196. const unsigned char RTCP_PT_RTPFB = 205; // Generic RTP Feedback [RFC4585]
  197. const unsigned char RTCP_PT_PSFB = 206; // Payload-specific [RFC4585]
  198. const unsigned char RTCP_PT_XR = 207; // extended report [RFC3611]
  199. const unsigned char RTCP_PT_AVB = 208; // AVB RTCP packet ["Standard for Layer 3 Transport Protocol for Time Sensitive Applications in Local Area Networks." Work in progress.]
  200. const unsigned char RTCP_PT_RSI = 209; // Receiver Summary Information [RFC5760]
  201. const unsigned char RTCP_PT_TOKEN = 210; // Port Mapping [RFC6284]
  202. const unsigned char RTCP_PT_IDMS = 211; // IDMS Settings [RFC7272]
  203. // SDES tags:
  204. const unsigned char RTCP_SDES_END = 0;
  205. const unsigned char RTCP_SDES_CNAME = 1;
  206. const unsigned char RTCP_SDES_NAME = 2;
  207. const unsigned char RTCP_SDES_EMAIL = 3;
  208. const unsigned char RTCP_SDES_PHONE = 4;
  209. const unsigned char RTCP_SDES_LOC = 5;
  210. const unsigned char RTCP_SDES_TOOL = 6;
  211. const unsigned char RTCP_SDES_NOTE = 7;
  212. const unsigned char RTCP_SDES_PRIV = 8;
  213. #endif