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