RTCP.hh 9.8 KB

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