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-2024 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) {
49 }
50 char const* rtpPayloadFormatName() const {return fRTPPayloadFormatName;}
51
52 unsigned numChannels() const { return fNumChannels; }
53
54 void setupForSRTP(Boolean useEncryption);
55 // sets up keying/encryption state for streaming via SRTP, using default values.
56 u_int8_t* setupForSRTP(Boolean useEncryption, unsigned& resultMIKEYStateMessageSize);
57 // as above, but returns the binary MIKEY state
58 void setupForSRTP(u_int8_t const* MIKEYStateMessage, unsigned MIKEYStateMessageSize);
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
105protected:
107 Groupsock* rtpGS, unsigned char rtpPayloadType,
108 u_int32_t rtpTimestampFrequency,
109 char const* rtpPayloadFormatName,
110 unsigned numChannels);
111 // abstract base class
112
113 virtual ~RTPSink();
114
115 // used by RTCP:
116 friend class RTCPInstance;
118 u_int32_t convertToRTPTimestamp(struct timeval tv);
119 unsigned packetCount() const {return fPacketCount;}
120 unsigned octetCount() const {return fOctetCount;}
121
122protected:
124 unsigned char fRTPPayloadType;
125 unsigned fPacketCount, fOctetCount, fTotalOctetCount /*incl RTP hdr*/;
126 struct timeval fTotalOctetCountStartTime, fInitialPresentationTime, fMostRecentPresentationTime;
128 u_int16_t fSeqNo;
129
130 // Optional key management and crypto state; used if we are streaming SRTP
133
134private:
135 // redefined virtual functions:
136 virtual Boolean isRTPSink() const;
137
138private:
142 Boolean fEnableRTCPReports; // whether RTCP "SR" reports should be sent for this sink (default: True)
144 unsigned fNumChannels;
145 struct timeval fCreationTime;
146 unsigned fEstimatedBitrate; // set on creation if known; otherwise 0
147
149};
150
151
152class RTPTransmissionStats; // forward
153
155public:
156 unsigned numReceivers() const { return fNumReceivers; }
157
158 class Iterator {
159 public:
161 virtual ~Iterator();
162
164 // NULL if none
165
166 private:
168 };
169
170 // The following is called whenever a RTCP RR packet is received:
171 void noteIncomingRR(u_int32_t SSRC, struct sockaddr_storage const& lastFromAddress,
172 unsigned lossStats, unsigned lastPacketNumReceived,
173 unsigned jitter, unsigned lastSRTime, unsigned diffSR_RRTime);
174
175 // The following is called when a RTCP BYE packet is received:
176 void removeRecord(u_int32_t SSRC);
177
178 RTPTransmissionStats* lookup(u_int32_t SSRC) const;
179
180private: // constructor and destructor, called only by RTPSink:
181 friend class RTPSink;
184
185private:
186 void add(u_int32_t SSRC, RTPTransmissionStats* stats);
187
188private:
189 friend class Iterator;
193};
194
196public:
197 u_int32_t SSRC() const {return fSSRC;}
198 struct sockaddr_storage const& lastFromAddress() const {return fLastFromAddress;}
201 unsigned totNumPacketsLost() const {return fTotNumPacketsLost;}
202 unsigned jitter() const {return fJitter;}
203 unsigned lastSRTime() const { return fLastSRTime; }
204 unsigned diffSR_RRTime() const { return fDiffSR_RRTime; }
205 unsigned roundTripDelay() const;
206 // The round-trip delay (in units of 1/65536 seconds) computed from
207 // the most recently-received RTCP RR packet.
208 struct timeval const& timeCreated() const {return fTimeCreated;}
209 struct timeval const& lastTimeReceived() const {return fTimeReceived;}
210 void getTotalOctetCount(u_int32_t& hi, u_int32_t& lo);
211 void getTotalPacketCount(u_int32_t& hi, u_int32_t& lo);
212
213 // Information which requires at least two RRs to have been received:
215 u_int8_t packetLossRatio() const { return fPacketLossRatio; }
216 // as an 8-bit fixed-point number
218
219private:
220 // called only by RTPTransmissionStatsDB:
222 RTPTransmissionStats(RTPSink& rtpSink, u_int32_t SSRC);
224
225 void noteIncomingRR(struct sockaddr_storage const& lastFromAddress,
226 unsigned lossStats, unsigned lastPacketNumReceived,
227 unsigned jitter,
228 unsigned lastSRTime, unsigned diffSR_RRTime);
229
230private:
232 u_int32_t fSSRC;
233 struct sockaddr_storage fLastFromAddress;
237 unsigned fJitter;
238 unsigned fLastSRTime;
240 struct timeval fTimeCreated, fTimeReceived;
248};
249
250#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:131
unsigned fEstimatedBitrate
Definition: RTPSink.hh:146
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:119
unsigned fPacketCount
Definition: RTPSink.hh:125
unsigned fTotalOctetCount
Definition: RTPSink.hh:125
unsigned octetCount() const
Definition: RTPSink.hh:120
char const * fRTPPayloadFormatName
Definition: RTPSink.hh:143
u_int16_t fSeqNo
Definition: RTPSink.hh:128
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:144
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:127
RTPTransmissionStatsDB * fTransmissionStatsDB
Definition: RTPSink.hh:148
virtual char * rtpmapLine() const
Groupsock & groupsockBeingUsed()
Definition: RTPSink.hh:43
Boolean fEnableRTCPReports
Definition: RTPSink.hh:142
struct timeval fTotalOctetCountStartTime fInitialPresentationTime fMostRecentPresentationTime
Definition: RTPSink.hh:126
void getTotalBitrate(unsigned &outNumBytes, double &outElapsedTime)
unsigned fTimestampFrequency
Definition: RTPSink.hh:140
virtual char * keyMgmtLine()
void setupForSRTP(Boolean useEncryption)
struct timeval const & creationTime() const
Definition: RTPSink.hh:83
unsigned numChannels() const
Definition: RTPSink.hh:52
struct timeval const & mostRecentPresentationTime() const
Definition: RTPSink.hh:85
virtual Boolean isRTPSink() const
u_int32_t fTimestampBase
Definition: RTPSink.hh:139
Boolean fNextTimestampHasBeenPreset
Definition: RTPSink.hh:141
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:124
u_int8_t * setupForSRTP(Boolean useEncryption, unsigned &resultMIKEYStateMessageSize)
RTPTransmissionStatsDB & transmissionStatsDB() const
Definition: RTPSink.hh:72
u_int32_t SSRC() const
Definition: RTPSink.hh:100
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:145
char const * rtpPayloadFormatName() const
Definition: RTPSink.hh:50
unsigned fOctetCount
Definition: RTPSink.hh:125
Groupsock const & groupsockBeingUsed() const
Definition: RTPSink.hh:42
u_int32_t fSSRC
Definition: RTPSink.hh:139
u_int16_t currentSeqNo() const
Definition: RTPSink.hh:67
SRTPCryptographicContext * fCrypto
Definition: RTPSink.hh:132
virtual char const * auxSDPLine()
RTPInterface fRTPInterface
Definition: RTPSink.hh:123
void setupForSRTP(u_int8_t const *MIKEYStateMessage, unsigned MIKEYStateMessageSize)
unsigned & estimatedBitrate()
Definition: RTPSink.hh:98
HashTable::Iterator * fIter
Definition: RTPSink.hh:167
Iterator(RTPTransmissionStatsDB &receptionStatsDB)
RTPTransmissionStats * next()
HashTable * fTable
Definition: RTPSink.hh:192
RTPTransmissionStats * lookup(u_int32_t SSRC) const
unsigned numReceivers() const
Definition: RTPSink.hh:156
RTPSink & fOurRTPSink
Definition: RTPSink.hh:191
RTPTransmissionStatsDB(RTPSink &rtpSink)
void add(u_int32_t SSRC, RTPTransmissionStats *stats)
virtual ~RTPTransmissionStatsDB()
unsigned fNumReceivers
Definition: RTPSink.hh:190
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:233
unsigned totNumPacketsLost() const
Definition: RTPSink.hh:201
unsigned fFirstPacketNumReported
Definition: RTPSink.hh:245
struct timeval const & timeCreated() const
Definition: RTPSink.hh:208
unsigned fLastSRTime
Definition: RTPSink.hh:238
RTPTransmissionStats(RTPSink &rtpSink, u_int32_t SSRC)
unsigned fOldTotNumPacketsLost
Definition: RTPSink.hh:243
u_int32_t fTotalPacketCount_lo
Definition: RTPSink.hh:247
u_int32_t fTotalOctetCount_hi
Definition: RTPSink.hh:246
void getTotalOctetCount(u_int32_t &hi, u_int32_t &lo)
unsigned fOldLastPacketNumReceived
Definition: RTPSink.hh:242
u_int32_t fLastOctetCount
Definition: RTPSink.hh:246
unsigned fLastPacketNumReceived
Definition: RTPSink.hh:234
unsigned packetsReceivedSinceLastRR() const
struct timeval fTimeCreated fTimeReceived
Definition: RTPSink.hh:240
unsigned lastPacketNumReceived() const
Definition: RTPSink.hh:199
RTPSink & fOurRTPSink
Definition: RTPSink.hh:231
struct sockaddr_storage const & lastFromAddress() const
Definition: RTPSink.hh:198
int packetsLostBetweenRR() const
unsigned jitter() const
Definition: RTPSink.hh:202
unsigned lastSRTime() const
Definition: RTPSink.hh:203
struct timeval const & lastTimeReceived() const
Definition: RTPSink.hh:209
u_int32_t fTotalPacketCount_hi
Definition: RTPSink.hh:247
u_int32_t fLastPacketCount
Definition: RTPSink.hh:247
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:197
Boolean fFirstPacket
Definition: RTPSink.hh:244
void getTotalPacketCount(u_int32_t &hi, u_int32_t &lo)
unsigned firstPacketNumReported() const
Definition: RTPSink.hh:200
u_int32_t fTotalOctetCount_lo
Definition: RTPSink.hh:246
unsigned fDiffSR_RRTime
Definition: RTPSink.hh:239
u_int8_t packetLossRatio() const
Definition: RTPSink.hh:215
unsigned diffSR_RRTime() const
Definition: RTPSink.hh:204
unsigned fTotNumPacketsLost
Definition: RTPSink.hh:236
Boolean fAtLeastTwoRRsHaveBeenReceived
Definition: RTPSink.hh:241
u_int8_t fPacketLossRatio
Definition: RTPSink.hh:235