live
RTPSink.hh
Go to the documentation of this file.
1/**********
2This library is free software; you can redistribute it and/or modify it under
3the terms of the GNU Lesser General Public License as published by the
4Free Software Foundation; either version 3 of the License, or (at your
5option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
6
7This library is distributed in the hope that it will be useful, but WITHOUT
8ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
10more details.
11
12You should have received a copy of the GNU Lesser General Public License
13along with this library; if not, write to the Free Software Foundation, Inc.,
1451 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15**********/
16// "liveMedia"
17// Copyright (c) 1996-2025 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#ifndef _SRTP_CRYPTOGRAPHIC_CONTEXT_HH
32#endif
33
34class RTPTransmissionStatsDB; // forward
35
36class RTPSink: public MediaSink {
37public:
38 static Boolean lookupByName(UsageEnvironment& env, char const* sinkName,
39 RTPSink*& resultSink);
40
41 // used by RTSP servers:
42 Groupsock const& groupsockBeingUsed() const { return *(fRTPInterface.gs()); }
44
45 unsigned char rtpPayloadType() const { return fRTPPayloadType; }
46 unsigned rtpTimestampFrequency() const { return fTimestampFrequency; }
47 void setRTPTimestampFrequency(unsigned freq) { fTimestampFrequency = freq; }
48 char const* rtpPayloadFormatName() const { return fRTPPayloadFormatName; }
49
50 unsigned numChannels() const { return fNumChannels; }
51
52 void setupForSRTP(Boolean useEncryption, u_int32_t roc);
53 // sets up keying/encryption state for streaming via SRTP, using default values.
54 u_int8_t* setupForSRTP(Boolean useEncryption, u_int32_t roc,
55 unsigned& resultMIKEYStateMessageSize);
56 // as above, but returns the binary MIKEY state
57 void setupForSRTP(u_int8_t const* MIKEYStateMessage, unsigned MIKEYStateMessageSize,
58 u_int32_t roc);
59 // as above, but takes a MIKEY state message as parameter
60
61 virtual char const* sdpMediaType() const; // for use in SDP m= lines
62 virtual char* rtpmapLine() const; // returns a string to be delete[]d
63 virtual char* keyMgmtLine(); // returns a string to be delete[]d
64 virtual char const* auxSDPLine();
65 // optional SDP line (e.g. a=fmtp:...)
66
67 u_int16_t currentSeqNo() const { return fSeqNo; }
69 // ensures that the next timestamp to be used will correspond to
70 // the current 'wall clock' time.
71
74 }
75
78
79 void getTotalBitrate(unsigned& outNumBytes, double& outElapsedTime);
80 // returns the number of bytes sent since the last time that we
81 // were called, and resets the counter.
82
83 struct timeval const& creationTime() const { return fCreationTime; }
84 struct timeval const& initialPresentationTime() const { return fInitialPresentationTime; }
85 struct timeval const& mostRecentPresentationTime() const { return fMostRecentPresentationTime; }
87
88 // Hacks to allow sending RTP over TCP (RFC 2236, section 10.12):
89 void setStreamSocket(int sockNum, unsigned char streamChannelId, TLSState* tlsState) {
90 fRTPInterface.setStreamSocket(sockNum, streamChannelId, tlsState);
91 }
92 void addStreamSocket(int sockNum, unsigned char streamChannelId, TLSState* tlsState) {
93 fRTPInterface.addStreamSocket(sockNum, streamChannelId, tlsState);
94 }
95 void removeStreamSocket(int sockNum, unsigned char streamChannelId) {
96 fRTPInterface.removeStreamSocket(sockNum, streamChannelId);
97 }
98 unsigned& estimatedBitrate() { return fEstimatedBitrate; } // kbps; usually 0 (i.e., unset)
99
100 u_int32_t SSRC() const { return fSSRC; }
101 // later need a means of changing the SSRC if there's a collision #####
102
104 u_int32_t srtpROC() const;
105
106protected:
108 Groupsock* rtpGS, unsigned char rtpPayloadType,
109 u_int32_t rtpTimestampFrequency,
110 char const* rtpPayloadFormatName,
111 unsigned numChannels);
112 // abstract base class
113
114 virtual ~RTPSink();
115
116 // used by RTCP:
117 friend class RTCPInstance;
119 u_int32_t convertToRTPTimestamp(struct timeval tv);
120 unsigned packetCount() const {return fPacketCount;}
121 unsigned octetCount() const {return fOctetCount;}
122
123protected:
125 unsigned char fRTPPayloadType;
126 unsigned fPacketCount, fOctetCount, fTotalOctetCount /*incl RTP hdr*/;
127 struct timeval fTotalOctetCountStartTime, fInitialPresentationTime, fMostRecentPresentationTime;
129 u_int16_t fSeqNo;
130
131 // Optional key management and crypto state; used if we are streaming SRTP
134
135private:
136 // redefined virtual functions:
137 virtual Boolean isRTPSink() const;
138
139private:
143 Boolean fEnableRTCPReports; // whether RTCP "SR" reports should be sent for this sink (default: True)
145 unsigned fNumChannels;
146 struct timeval fCreationTime;
147 unsigned fEstimatedBitrate; // set on creation if known; otherwise 0
148
150};
151
152
153class RTPTransmissionStats; // forward
154
156public:
157 unsigned numReceivers() const { return fNumReceivers; }
158
159 class Iterator {
160 public:
162 virtual ~Iterator();
163
165 // NULL if none
166
167 private:
169 };
170
171 // The following is called whenever a RTCP RR packet is received:
172 void noteIncomingRR(u_int32_t SSRC, struct sockaddr_storage const& lastFromAddress,
173 unsigned lossStats, unsigned lastPacketNumReceived,
174 unsigned jitter, unsigned lastSRTime, unsigned diffSR_RRTime);
175
176 // The following is called when a RTCP BYE packet is received:
177 void removeRecord(u_int32_t SSRC);
178
179 RTPTransmissionStats* lookup(u_int32_t SSRC) const;
180
181private: // constructor and destructor, called only by RTPSink:
182 friend class RTPSink;
185
186private:
187 void add(u_int32_t SSRC, RTPTransmissionStats* stats);
188
189private:
190 friend class Iterator;
194};
195
197public:
198 u_int32_t SSRC() const {return fSSRC;}
199 struct sockaddr_storage const& lastFromAddress() const {return fLastFromAddress;}
202 unsigned totNumPacketsLost() const {return fTotNumPacketsLost;}
203 unsigned jitter() const {return fJitter;}
204 unsigned lastSRTime() const { return fLastSRTime; }
205 unsigned diffSR_RRTime() const { return fDiffSR_RRTime; }
206 unsigned roundTripDelay() const;
207 // The round-trip delay (in units of 1/65536 seconds) computed from
208 // the most recently-received RTCP RR packet.
209 struct timeval const& timeCreated() const {return fTimeCreated;}
210 struct timeval const& lastTimeReceived() const {return fTimeReceived;}
211 void getTotalOctetCount(u_int32_t& hi, u_int32_t& lo);
212 void getTotalPacketCount(u_int32_t& hi, u_int32_t& lo);
213
214 // Information which requires at least two RRs to have been received:
216 u_int8_t packetLossRatio() const { return fPacketLossRatio; }
217 // as an 8-bit fixed-point number
219
220private:
221 // called only by RTPTransmissionStatsDB:
223 RTPTransmissionStats(RTPSink& rtpSink, u_int32_t SSRC);
225
226 void noteIncomingRR(struct sockaddr_storage const& lastFromAddress,
227 unsigned lossStats, unsigned lastPacketNumReceived,
228 unsigned jitter,
229 unsigned lastSRTime, unsigned diffSR_RRTime);
230
231private:
233 u_int32_t fSSRC;
234 struct sockaddr_storage fLastFromAddress;
238 unsigned fJitter;
239 unsigned fLastSRTime;
241 struct timeval fTimeCreated, fTimeReceived;
249};
250
251#endif
unsigned char Boolean
Definition: Boolean.hh:25
void removeStreamSocket(int sockNum, unsigned char streamChannelId)
void setStreamSocket(int sockNum, unsigned char streamChannelId, TLSState *tlsState)
void addStreamSocket(int sockNum, unsigned char streamChannelId, TLSState *tlsState)
Groupsock * gs() const
Definition: RTPInterface.hh:51
virtual char const * sdpMediaType() const
MIKEYState * fMIKEYState
Definition: RTPSink.hh:132
unsigned fEstimatedBitrate
Definition: RTPSink.hh:147
virtual ~RTPSink()
void setStreamSocket(int sockNum, unsigned char streamChannelId, TLSState *tlsState)
Definition: RTPSink.hh:89
u_int32_t convertToRTPTimestamp(struct timeval tv)
unsigned packetCount() const
Definition: RTPSink.hh:120
unsigned fPacketCount
Definition: RTPSink.hh:126
unsigned fTotalOctetCount
Definition: RTPSink.hh:126
void setupForSRTP(u_int8_t const *MIKEYStateMessage, unsigned MIKEYStateMessageSize, u_int32_t roc)
u_int8_t * setupForSRTP(Boolean useEncryption, u_int32_t roc, unsigned &resultMIKEYStateMessageSize)
unsigned octetCount() const
Definition: RTPSink.hh:121
char const * fRTPPayloadFormatName
Definition: RTPSink.hh:144
u_int16_t fSeqNo
Definition: RTPSink.hh:129
Boolean & enableRTCPReports()
Definition: RTPSink.hh:77
void addStreamSocket(int sockNum, unsigned char streamChannelId, TLSState *tlsState)
Definition: RTPSink.hh:92
void resetPresentationTimes()
u_int32_t presetNextTimestamp()
SRTPCryptographicContext * getCrypto() const
Definition: RTPSink.hh:103
unsigned fNumChannels
Definition: RTPSink.hh:145
static Boolean lookupByName(UsageEnvironment &env, char const *sinkName, RTPSink *&resultSink)
unsigned rtpTimestampFrequency() const
Definition: RTPSink.hh:46
void removeStreamSocket(int sockNum, unsigned char streamChannelId)
Definition: RTPSink.hh:95
u_int32_t fCurrentTimestamp
Definition: RTPSink.hh:128
RTPTransmissionStatsDB * fTransmissionStatsDB
Definition: RTPSink.hh:149
virtual char * rtpmapLine() const
Groupsock & groupsockBeingUsed()
Definition: RTPSink.hh:43
Boolean fEnableRTCPReports
Definition: RTPSink.hh:143
struct timeval fTotalOctetCountStartTime fInitialPresentationTime fMostRecentPresentationTime
Definition: RTPSink.hh:127
void getTotalBitrate(unsigned &outNumBytes, double &outElapsedTime)
unsigned fTimestampFrequency
Definition: RTPSink.hh:141
virtual char * keyMgmtLine()
u_int32_t srtpROC() const
struct timeval const & creationTime() const
Definition: RTPSink.hh:83
unsigned numChannels() const
Definition: RTPSink.hh:50
struct timeval const & mostRecentPresentationTime() const
Definition: RTPSink.hh:85
virtual Boolean isRTPSink() const
u_int32_t fTimestampBase
Definition: RTPSink.hh:140
Boolean fNextTimestampHasBeenPreset
Definition: RTPSink.hh:142
unsigned char rtpPayloadType() const
Definition: RTPSink.hh:45
void setRTPTimestampFrequency(unsigned freq)
Definition: RTPSink.hh:47
struct timeval const & initialPresentationTime() const
Definition: RTPSink.hh:84
unsigned char fRTPPayloadType
Definition: RTPSink.hh:125
RTPTransmissionStatsDB & transmissionStatsDB() const
Definition: RTPSink.hh:72
u_int32_t SSRC() const
Definition: RTPSink.hh:100
void setupForSRTP(Boolean useEncryption, u_int32_t roc)
RTPSink(UsageEnvironment &env, Groupsock *rtpGS, unsigned char rtpPayloadType, u_int32_t rtpTimestampFrequency, char const *rtpPayloadFormatName, unsigned numChannels)
Boolean nextTimestampHasBeenPreset() const
Definition: RTPSink.hh:76
struct timeval fCreationTime
Definition: RTPSink.hh:146
char const * rtpPayloadFormatName() const
Definition: RTPSink.hh:48
unsigned fOctetCount
Definition: RTPSink.hh:126
Groupsock const & groupsockBeingUsed() const
Definition: RTPSink.hh:42
u_int32_t fSSRC
Definition: RTPSink.hh:140
u_int16_t currentSeqNo() const
Definition: RTPSink.hh:67
SRTPCryptographicContext * fCrypto
Definition: RTPSink.hh:133
virtual char const * auxSDPLine()
RTPInterface fRTPInterface
Definition: RTPSink.hh:124
unsigned & estimatedBitrate()
Definition: RTPSink.hh:98
HashTable::Iterator * fIter
Definition: RTPSink.hh:168
Iterator(RTPTransmissionStatsDB &receptionStatsDB)
RTPTransmissionStats * next()
HashTable * fTable
Definition: RTPSink.hh:193
RTPTransmissionStats * lookup(u_int32_t SSRC) const
unsigned numReceivers() const
Definition: RTPSink.hh:157
RTPSink & fOurRTPSink
Definition: RTPSink.hh:192
RTPTransmissionStatsDB(RTPSink &rtpSink)
void add(u_int32_t SSRC, RTPTransmissionStats *stats)
virtual ~RTPTransmissionStatsDB()
unsigned fNumReceivers
Definition: RTPSink.hh:191
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:234
unsigned totNumPacketsLost() const
Definition: RTPSink.hh:202
unsigned fFirstPacketNumReported
Definition: RTPSink.hh:246
struct timeval const & timeCreated() const
Definition: RTPSink.hh:209
unsigned fLastSRTime
Definition: RTPSink.hh:239
RTPTransmissionStats(RTPSink &rtpSink, u_int32_t SSRC)
unsigned fOldTotNumPacketsLost
Definition: RTPSink.hh:244
u_int32_t fTotalPacketCount_lo
Definition: RTPSink.hh:248
u_int32_t fTotalOctetCount_hi
Definition: RTPSink.hh:247
void getTotalOctetCount(u_int32_t &hi, u_int32_t &lo)
unsigned fOldLastPacketNumReceived
Definition: RTPSink.hh:243
u_int32_t fLastOctetCount
Definition: RTPSink.hh:247
unsigned fLastPacketNumReceived
Definition: RTPSink.hh:235
unsigned packetsReceivedSinceLastRR() const
struct timeval fTimeCreated fTimeReceived
Definition: RTPSink.hh:241
unsigned lastPacketNumReceived() const
Definition: RTPSink.hh:200
RTPSink & fOurRTPSink
Definition: RTPSink.hh:232
struct sockaddr_storage const & lastFromAddress() const
Definition: RTPSink.hh:199
int packetsLostBetweenRR() const
unsigned jitter() const
Definition: RTPSink.hh:203
unsigned lastSRTime() const
Definition: RTPSink.hh:204
struct timeval const & lastTimeReceived() const
Definition: RTPSink.hh:210
u_int32_t fTotalPacketCount_hi
Definition: RTPSink.hh:248
u_int32_t fLastPacketCount
Definition: RTPSink.hh:248
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:198
Boolean fFirstPacket
Definition: RTPSink.hh:245
void getTotalPacketCount(u_int32_t &hi, u_int32_t &lo)
unsigned firstPacketNumReported() const
Definition: RTPSink.hh:201
u_int32_t fTotalOctetCount_lo
Definition: RTPSink.hh:247
unsigned fDiffSR_RRTime
Definition: RTPSink.hh:240
u_int8_t packetLossRatio() const
Definition: RTPSink.hh:216
unsigned diffSR_RRTime() const
Definition: RTPSink.hh:205
unsigned fTotNumPacketsLost
Definition: RTPSink.hh:237
Boolean fAtLeastTwoRRsHaveBeenReceived
Definition: RTPSink.hh:242
u_int8_t fPacketLossRatio
Definition: RTPSink.hh:236