live
RTPSource.hh
Go to the documentation of this file.
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 
7 This library is distributed in the hope that it will be useful, but WITHOUT
8 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
10 more details.
11 
12 You should have received a copy of the GNU Lesser General Public License
13 along with this library; if not, write to the Free Software Foundation, Inc.,
14 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15 **********/
16 // "liveMedia"
17 // Copyright (c) 1996-2021 Live Networks, Inc. All rights reserved.
18 // RTP Sources
19 // C++ header
20 
21 #ifndef _RTP_SOURCE_HH
22 #define _RTP_SOURCE_HH
23 
24 #ifndef _FRAMED_SOURCE_HH
25 #include "FramedSource.hh"
26 #endif
27 #ifndef _RTP_INTERFACE_HH
28 #include "RTPInterface.hh"
29 #endif
30 #ifndef _SRTP_CRYPTOGRAPHIC_CONTEXT_HH
32 #endif
33 
34 class RTPReceptionStatsDB; // forward
35 
36 class RTPSource: public FramedSource {
37 public:
38  static Boolean lookupByName(UsageEnvironment& env, char const* sourceName,
39  RTPSource*& resultSource);
40 
42 
43  unsigned char rtpPayloadFormat() const { return fRTPPayloadFormat; }
44 
46 
47  Groupsock* RTPgs() const { return fRTPInterface.gs(); }
48 
49  virtual void setPacketReorderingThresholdTime(unsigned uSeconds) = 0;
50 
51  void setCrypto(SRTPCryptographicContext* crypto) { fCrypto = crypto; }
52 
53  // used by RTCP:
54  u_int32_t SSRC() const { return fSSRC; }
55  // Note: This is *our* SSRC, not the SSRC in incoming RTP packets.
56  // later need a means of changing the SSRC if there's a collision #####
57  void registerForMultiplexedRTCPPackets(class RTCPInstance* rtcpInstance) {
59  }
61 
62  unsigned timestampFrequency() const {return fTimestampFrequency;}
63 
65  return *fReceptionStatsDB;
66  }
67 
68  u_int32_t lastReceivedSSRC() const { return fLastReceivedSSRC; }
69  // Note: This is the SSRC in the most recently received RTP packet; not *our* SSRC
70 
72  Boolean const& enableRTCPReports() const { return fEnableRTCPReports; }
73 
74  void setStreamSocket(int sockNum, unsigned char streamChannelId, TLSState* tlsState) {
75  // hack to allow sending RTP over TCP (RFC 2236, section 10.12)
76  fRTPInterface.setStreamSocket(sockNum, streamChannelId, tlsState);
77  }
78 
80  void* handlerClientData) {
82  handlerClientData);
83  }
84 
85  // Note that RTP receivers will usually not need to call either of the following two functions, because
86  // RTP sequence numbers and timestamps are usually not useful to receivers.
87  // (Our implementation of RTP reception already does all needed handling of RTP sequence numbers and timestamps.)
88  u_int16_t curPacketRTPSeqNum() const { return fCurPacketRTPSeqNum; }
89 private: friend class MediaSubsession; // "MediaSubsession" is the only outside class that ever needs to see RTP timestamps!
90  u_int32_t curPacketRTPTimestamp() const { return fCurPacketRTPTimestamp; }
91 
92 protected:
94  unsigned char rtpPayloadFormat, u_int32_t rtpTimestampFrequency);
95  // abstract base class
96  virtual ~RTPSource();
97 
98 protected:
104  u_int32_t fLastReceivedSSRC;
107 
108 private:
109  // redefined virtual functions:
110  virtual Boolean isRTPSource() const;
111  virtual void getAttributes() const;
112 
113 private:
114  unsigned char fRTPPayloadFormat;
116  u_int32_t fSSRC;
117  Boolean fEnableRTCPReports; // whether RTCP "RR" reports should be sent for this source (default: True)
118 
120 };
121 
122 
123 class RTPReceptionStats; // forward
124 
126 public:
127  unsigned totNumPacketsReceived() const { return fTotNumPacketsReceived; }
130  }
131 
132  void reset();
133  // resets periodic stats (called each time they're used to
134  // generate a reception report)
135 
136  class Iterator {
137  public:
138  Iterator(RTPReceptionStatsDB& receptionStatsDB);
139  virtual ~Iterator();
140 
141  RTPReceptionStats* next(Boolean includeInactiveSources = False);
142  // NULL if none
143 
144  private:
146  };
147 
148  // The following is called whenever a RTP packet is received:
149  void noteIncomingPacket(u_int32_t SSRC, u_int16_t seqNum,
150  u_int32_t rtpTimestamp,
151  unsigned timestampFrequency,
152  Boolean useForJitterCalculation,
153  struct timeval& resultPresentationTime,
154  Boolean& resultHasBeenSyncedUsingRTCP,
155  unsigned packetSize /* payload only */);
156 
157  // The following is called whenever a RTCP SR packet is received:
158  void noteIncomingSR(u_int32_t SSRC,
159  u_int32_t ntpTimestampMSW, u_int32_t ntpTimestampLSW,
160  u_int32_t rtpTimestamp);
161 
162  // The following is called when a RTCP BYE packet is received:
163  void removeRecord(u_int32_t SSRC);
164 
165  RTPReceptionStats* lookup(u_int32_t SSRC) const;
166 
167 protected: // constructor and destructor, called only by RTPSource:
168  friend class RTPSource;
171 
172 protected:
173  void add(u_int32_t SSRC, RTPReceptionStats* stats);
174 
175 protected:
176  friend class Iterator;
178 
179 private:
181  unsigned fTotNumPacketsReceived; // for all SSRCs
182 };
183 
185 public:
186  u_int32_t SSRC() const { return fSSRC; }
189  }
190  unsigned totNumPacketsReceived() const { return fTotNumPacketsReceived; }
191  double totNumKBytesReceived() const;
192 
193  unsigned totNumPacketsExpected() const {
195  }
196 
197  unsigned baseExtSeqNumReceived() const { return fBaseExtSeqNumReceived; }
198  unsigned lastResetExtSeqNumReceived() const {
200  }
201  unsigned highestExtSeqNumReceived() const {
203  }
204 
205  unsigned jitter() const;
206 
207  unsigned lastReceivedSR_NTPmsw() const { return fLastReceivedSR_NTPmsw; }
208  unsigned lastReceivedSR_NTPlsw() const { return fLastReceivedSR_NTPlsw; }
209  struct timeval const& lastReceivedSR_time() const {
210  return fLastReceivedSR_time;
211  }
212 
213  unsigned minInterPacketGapUS() const { return fMinInterPacketGapUS; }
214  unsigned maxInterPacketGapUS() const { return fMaxInterPacketGapUS; }
215  struct timeval const& totalInterPacketGaps() const {
216  return fTotalInterPacketGaps;
217  }
218 
219 protected:
220  // called only by RTPReceptionStatsDB:
221  friend class RTPReceptionStatsDB;
222  RTPReceptionStats(u_int32_t SSRC, u_int16_t initialSeqNum);
225 
226 private:
227  void noteIncomingPacket(u_int16_t seqNum, u_int32_t rtpTimestamp,
228  unsigned timestampFrequency,
229  Boolean useForJitterCalculation,
230  struct timeval& resultPresentationTime,
231  Boolean& resultHasBeenSyncedUsingRTCP,
232  unsigned packetSize /* payload only */);
233  void noteIncomingSR(u_int32_t ntpTimestampMSW, u_int32_t ntpTimestampLSW,
234  u_int32_t rtpTimestamp);
235  void init(u_int32_t SSRC);
236  void initSeqNum(u_int16_t initialSeqNum);
237  void reset();
238  // resets periodic stats (called each time they're used to
239  // generate a reception report)
240 
241 protected:
242  u_int32_t fSSRC;
250  int fLastTransit; // used in the jitter calculation
252  double fJitter;
253  // The following are recorded whenever we receive a RTCP SR for this SSRC:
254  unsigned fLastReceivedSR_NTPmsw; // NTP timestamp (from SR), most-signif
255  unsigned fLastReceivedSR_NTPlsw; // NTP timestamp (from SR), least-signif
256  struct timeval fLastReceivedSR_time;
257  struct timeval fLastPacketReceptionTime;
259  struct timeval fTotalInterPacketGaps;
260 
261 private:
262  // Used to convert from RTP timestamp to 'wall clock' time:
264  u_int32_t fSyncTimestamp;
265  struct timeval fSyncTime;
266 };
267 
268 
269 Boolean seqNumLT(u_int16_t s1, u_int16_t s2);
270  // a 'less-than' on 16-bit sequence numbers
271 
272 #endif
const Boolean False
Definition: Boolean.hh:28
unsigned char Boolean
Definition: Boolean.hh:25
void AuxHandlerFunc(void *clientData, unsigned char *packet, unsigned &packetSize)
Definition: RTPInterface.hh:38
Boolean seqNumLT(u_int16_t s1, u_int16_t s2)
#define NULL
unsigned rtpTimestampFrequency() const
UsageEnvironment & env()
Groupsock * gs() const
Definition: RTPInterface.hh:51
void setStreamSocket(int sockNum, unsigned char streamChannelId, TLSState *tlsState)
void setAuxilliaryReadHandler(AuxHandlerFunc *handlerFunc, void *handlerClientData)
Definition: RTPInterface.hh:79
Iterator(RTPReceptionStatsDB &receptionStatsDB)
RTPReceptionStats * next(Boolean includeInactiveSources=False)
HashTable::Iterator * fIter
Definition: RTPSource.hh:145
HashTable * fTable
Definition: RTPSource.hh:180
void noteIncomingPacket(u_int32_t SSRC, u_int16_t seqNum, u_int32_t rtpTimestamp, unsigned timestampFrequency, Boolean useForJitterCalculation, struct timeval &resultPresentationTime, Boolean &resultHasBeenSyncedUsingRTCP, unsigned packetSize)
RTPReceptionStats * lookup(u_int32_t SSRC) const
unsigned numActiveSourcesSinceLastReset() const
Definition: RTPSource.hh:128
virtual ~RTPReceptionStatsDB()
void noteIncomingSR(u_int32_t SSRC, u_int32_t ntpTimestampMSW, u_int32_t ntpTimestampLSW, u_int32_t rtpTimestamp)
unsigned fTotNumPacketsReceived
Definition: RTPSource.hh:181
unsigned fNumActiveSourcesSinceLastReset
Definition: RTPSource.hh:177
void removeRecord(u_int32_t SSRC)
unsigned totNumPacketsReceived() const
Definition: RTPSource.hh:127
void add(u_int32_t SSRC, RTPReceptionStats *stats)
unsigned fTotNumPacketsReceived
Definition: RTPSource.hh:244
unsigned fMinInterPacketGapUS
Definition: RTPSource.hh:258
RTPReceptionStats(u_int32_t SSRC)
unsigned fBaseExtSeqNumReceived
Definition: RTPSource.hh:247
struct timeval const & lastReceivedSR_time() const
Definition: RTPSource.hh:209
struct timeval fLastPacketReceptionTime
Definition: RTPSource.hh:257
unsigned lastResetExtSeqNumReceived() const
Definition: RTPSource.hh:198
void noteIncomingPacket(u_int16_t seqNum, u_int32_t rtpTimestamp, unsigned timestampFrequency, Boolean useForJitterCalculation, struct timeval &resultPresentationTime, Boolean &resultHasBeenSyncedUsingRTCP, unsigned packetSize)
unsigned lastReceivedSR_NTPmsw() const
Definition: RTPSource.hh:207
unsigned fLastReceivedSR_NTPlsw
Definition: RTPSource.hh:255
RTPReceptionStats(u_int32_t SSRC, u_int16_t initialSeqNum)
unsigned fHighestExtSeqNumReceived
Definition: RTPSource.hh:249
struct timeval const & totalInterPacketGaps() const
Definition: RTPSource.hh:215
unsigned fNumPacketsReceivedSinceLastReset
Definition: RTPSource.hh:243
unsigned fLastResetExtSeqNumReceived
Definition: RTPSource.hh:248
unsigned fMaxInterPacketGapUS
Definition: RTPSource.hh:258
unsigned maxInterPacketGapUS() const
Definition: RTPSource.hh:214
unsigned numPacketsReceivedSinceLastReset() const
Definition: RTPSource.hh:187
u_int32_t fSyncTimestamp
Definition: RTPSource.hh:264
unsigned highestExtSeqNumReceived() const
Definition: RTPSource.hh:201
unsigned lastReceivedSR_NTPlsw() const
Definition: RTPSource.hh:208
struct timeval fTotalInterPacketGaps
Definition: RTPSource.hh:259
struct timeval fSyncTime
Definition: RTPSource.hh:265
unsigned jitter() const
unsigned fLastReceivedSR_NTPmsw
Definition: RTPSource.hh:254
unsigned totNumPacketsReceived() const
Definition: RTPSource.hh:190
void init(u_int32_t SSRC)
void noteIncomingSR(u_int32_t ntpTimestampMSW, u_int32_t ntpTimestampLSW, u_int32_t rtpTimestamp)
u_int32_t SSRC() const
Definition: RTPSource.hh:186
unsigned minInterPacketGapUS() const
Definition: RTPSource.hh:213
double totNumKBytesReceived() const
struct timeval fLastReceivedSR_time
Definition: RTPSource.hh:256
void initSeqNum(u_int16_t initialSeqNum)
Boolean fHaveSeenInitialSequenceNumber
Definition: RTPSource.hh:246
u_int32_t fTotBytesReceived_lo
Definition: RTPSource.hh:245
u_int32_t fTotBytesReceived_hi
Definition: RTPSource.hh:245
u_int32_t fSSRC
Definition: RTPSource.hh:242
Boolean fHasBeenSynchronized
Definition: RTPSource.hh:263
unsigned baseExtSeqNumReceived() const
Definition: RTPSource.hh:197
unsigned totNumPacketsExpected() const
Definition: RTPSource.hh:193
virtual ~RTPReceptionStats()
u_int32_t fPreviousPacketRTPTimestamp
Definition: RTPSource.hh:251
u_int16_t curPacketRTPSeqNum() const
Definition: RTPSource.hh:88
RTPInterface fRTPInterface
Definition: RTPSource.hh:99
u_int32_t curPacketRTPTimestamp() const
Definition: RTPSource.hh:90
Groupsock * RTPgs() const
Definition: RTPSource.hh:47
Boolean curPacketMarkerBit() const
Definition: RTPSource.hh:41
u_int32_t fLastReceivedSSRC
Definition: RTPSource.hh:104
void registerForMultiplexedRTCPPackets(class RTCPInstance *rtcpInstance)
Definition: RTPSource.hh:57
Boolean const & enableRTCPReports() const
Definition: RTPSource.hh:72
RTPReceptionStatsDB & receptionStatsDB() const
Definition: RTPSource.hh:64
u_int32_t SSRC() const
Definition: RTPSource.hh:54
void setCrypto(SRTPCryptographicContext *crypto)
Definition: RTPSource.hh:51
u_int32_t fSSRC
Definition: RTPSource.hh:116
u_int32_t lastReceivedSSRC() const
Definition: RTPSource.hh:68
virtual Boolean hasBeenSynchronizedUsingRTCP()
void setStreamSocket(int sockNum, unsigned char streamChannelId, TLSState *tlsState)
Definition: RTPSource.hh:74
class RTCPInstance * fRTCPInstanceForMultiplexedRTCPPackets
Definition: RTPSource.hh:105
static Boolean lookupByName(UsageEnvironment &env, char const *sourceName, RTPSource *&resultSource)
Boolean fCurPacketMarkerBit
Definition: RTPSource.hh:102
virtual ~RTPSource()
unsigned timestampFrequency() const
Definition: RTPSource.hh:62
unsigned char fRTPPayloadFormat
Definition: RTPSource.hh:114
void deregisterForMultiplexedRTCPPackets()
Definition: RTPSource.hh:60
RTPSource(UsageEnvironment &env, Groupsock *RTPgs, unsigned char rtpPayloadFormat, u_int32_t rtpTimestampFrequency)
unsigned fTimestampFrequency
Definition: RTPSource.hh:115
virtual void getAttributes() const
virtual void setPacketReorderingThresholdTime(unsigned uSeconds)=0
Boolean & enableRTCPReports()
Definition: RTPSource.hh:71
virtual Boolean isRTPSource() const
Boolean fCurPacketHasBeenSynchronizedUsingRTCP
Definition: RTPSource.hh:103
void setAuxilliaryReadHandler(AuxHandlerFunc *handlerFunc, void *handlerClientData)
Definition: RTPSource.hh:79
u_int32_t fCurPacketRTPTimestamp
Definition: RTPSource.hh:101
unsigned char rtpPayloadFormat() const
Definition: RTPSource.hh:43
u_int16_t fCurPacketRTPSeqNum
Definition: RTPSource.hh:100
Boolean fEnableRTCPReports
Definition: RTPSource.hh:117
RTPReceptionStatsDB * fReceptionStatsDB
Definition: RTPSource.hh:119
SRTPCryptographicContext * fCrypto
Definition: RTPSource.hh:106