1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
|
/**********
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the
Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
**********/
// "liveMedia"
// Copyright (c) 1996-2005 Live Networks, Inc. All rights reserved.
// RTP Sinks
// Implementation
#include "RTPSink.hh"
#include "GroupsockHelper.hh"
////////// RTPSink //////////
Boolean RTPSink::lookupByName(UsageEnvironment& env, char const* sinkName,
RTPSink*& resultSink) {
resultSink = NULL; // unless we succeed
MediaSink* sink;
if (!MediaSink::lookupByName(env, sinkName, sink)) return False;
if (!sink->isRTPSink()) {
env.setResultMsg(sinkName, " is not a RTP sink");
return False;
}
resultSink = (RTPSink*)sink;
return True;
}
Boolean RTPSink::isRTPSink() const {
return True;
}
RTPSink::RTPSink(UsageEnvironment& env,
Groupsock* rtpGS, unsigned char rtpPayloadType,
unsigned rtpTimestampFrequency,
char const* rtpPayloadFormatName,
unsigned numChannels)
: MediaSink(env), fRTPInterface(this, rtpGS),
fRTPPayloadType(rtpPayloadType),
fPacketCount(0), fOctetCount(0), fTotalOctetCount(0),
fTimestampFrequency(rtpTimestampFrequency), fHaveComputedFirstTimestamp(False),
fNumChannels(numChannels) {
fRTPPayloadFormatName
= strDup(rtpPayloadFormatName == NULL ? "???" : rtpPayloadFormatName);
gettimeofday(&fCreationTime, NULL);
fTotalOctetCountStartTime = fCreationTime;
fSeqNo = (u_int16_t)our_random();
fSSRC = our_random32();
fTimestampBase = our_random32();
fCurrentTimestamp = fTimestampBase;
fTransmissionStatsDB = new RTPTransmissionStatsDB(*this);
}
RTPSink::~RTPSink() {
delete fTransmissionStatsDB;
delete[] (char*)fRTPPayloadFormatName;
}
u_int32_t RTPSink::convertToRTPTimestamp(struct timeval tv) {
u_int32_t rtpTimestampIncrement = timevalToTimestamp(tv);
if (!fHaveComputedFirstTimestamp) {
// Make the first timestamp the same as the current "fTimestampBase", so that
// timestamps begin with the value we promised when this "RTPSink" was created:
fTimestampBase -= rtpTimestampIncrement;
fHaveComputedFirstTimestamp = True;
}
u_int32_t const rtpTimestamp = fTimestampBase + rtpTimestampIncrement;
#ifdef DEBUG_TIMESTAMPS
fprintf(stderr, "fTimestampBase: 0x%08x, tv: %lu.%06ld\n\t=> RTP timestamp: 0x%08x\n",
fTimestampBase, tv.tv_sec, tv.tv_usec, rtpTimestamp);
fflush(stderr);
#endif
return rtpTimestamp;
}
u_int32_t RTPSink::timevalToTimestamp(struct timeval tv) const {
u_int32_t timestamp = (fTimestampFrequency*tv.tv_sec);
timestamp += (u_int32_t)((2.0*fTimestampFrequency*tv.tv_usec + 1000000.0)/2000000);
// note: rounding
return timestamp;
}
void RTPSink::getTotalBitrate(unsigned& outNumBytes, double& outElapsedTime) {
struct timeval timeNow;
gettimeofday(&timeNow, NULL);
outNumBytes = fTotalOctetCount;
outElapsedTime = (double)(timeNow.tv_sec-fTotalOctetCountStartTime.tv_sec)
+ (timeNow.tv_usec-fTotalOctetCountStartTime.tv_usec)/1000000.0;
fTotalOctetCount = 0;
fTotalOctetCountStartTime = timeNow;
}
char const* RTPSink::sdpMediaType() const {
return "data";
// default SDP media (m=) type, unless redefined by subclasses
}
char* RTPSink::rtpmapLine() const {
if (rtpPayloadType() >= 96) { // the payload format type is dynamic
char* encodingParamsPart;
if (numChannels() != 1) {
encodingParamsPart = new char[1 + 20 /* max int len */];
sprintf(encodingParamsPart, "/%d", numChannels());
} else {
encodingParamsPart = strDup("");
}
char const* const rtpmapFmt = "a=rtpmap:%d %s/%d%s\r\n";
unsigned rtpmapFmtSize = strlen(rtpmapFmt)
+ 3 /* max char len */ + strlen(rtpPayloadFormatName())
+ 20 /* max int len */ + strlen(encodingParamsPart);
char* rtpmapLine = new char[rtpmapFmtSize];
sprintf(rtpmapLine, rtpmapFmt,
rtpPayloadType(), rtpPayloadFormatName(),
rtpTimestampFrequency(), encodingParamsPart);
delete[] encodingParamsPart;
return rtpmapLine;
} else {
// The payload format is staic, so there's no "a=rtpmap:" line:
return strDup("");
}
}
char const* RTPSink::auxSDPLine() {
return NULL; // by default
}
////////// RTPTransmissionStatsDB //////////
RTPTransmissionStatsDB::RTPTransmissionStatsDB(RTPSink& rtpSink)
: fOurRTPSink(rtpSink),
fTable(HashTable::create(ONE_WORD_HASH_KEYS)) {
fNumReceivers=0;
}
RTPTransmissionStatsDB::~RTPTransmissionStatsDB() {
// First, remove and delete all stats records from the table:
RTPTransmissionStats* stats;
while ((stats = (RTPTransmissionStats*)fTable->RemoveNext()) != NULL) {
delete stats;
}
// Then, delete the table itself:
delete fTable;
}
void RTPTransmissionStatsDB
::noteIncomingRR(u_int32_t SSRC, struct sockaddr_in const& lastFromAddress,
unsigned lossStats, unsigned lastPacketNumReceived,
unsigned jitter, unsigned lastSRTime, unsigned diffSR_RRTime) {
RTPTransmissionStats* stats = lookup(SSRC);
if (stats == NULL) {
// This is the first time we've heard of this SSRC.
// Create a new record for it:
stats = new RTPTransmissionStats(fOurRTPSink, SSRC);
if (stats == NULL) return;
add(SSRC, stats);
#ifdef DEBUG_RR
fprintf(stderr, "Adding new entry for SSRC %x in RTPTransmissionStatsDB\n", SSRC);
#endif
}
stats->noteIncomingRR(lastFromAddress,
lossStats, lastPacketNumReceived, jitter,
lastSRTime, diffSR_RRTime);
}
void RTPTransmissionStatsDB::removeRecord(u_int32_t SSRC) {
RTPTransmissionStats* stats = lookup(SSRC);
if (stats != NULL) {
long SSRC_long = (long)SSRC;
fTable->Remove((char const*)SSRC_long);
--fNumReceivers;
delete stats;
}
}
RTPTransmissionStatsDB::Iterator
::Iterator(RTPTransmissionStatsDB& receptionStatsDB)
: fIter(HashTable::Iterator::create(*(receptionStatsDB.fTable))) {
}
RTPTransmissionStatsDB::Iterator::~Iterator() {
delete fIter;
}
RTPTransmissionStats*
RTPTransmissionStatsDB::Iterator::next() {
char const* key; // dummy
return (RTPTransmissionStats*)(fIter->next(key));
}
RTPTransmissionStats* RTPTransmissionStatsDB::lookup(u_int32_t SSRC) const {
long SSRC_long = (long)SSRC;
return (RTPTransmissionStats*)(fTable->Lookup((char const*)SSRC_long));
}
void RTPTransmissionStatsDB::add(u_int32_t SSRC, RTPTransmissionStats* stats) {
long SSRC_long = (long)SSRC;
fTable->Add((char const*)SSRC_long, stats);
++fNumReceivers;
}
////////// RTPTransmissionStats //////////
RTPTransmissionStats::RTPTransmissionStats(RTPSink& rtpSink, u_int32_t SSRC)
: fOurRTPSink(rtpSink), fSSRC(SSRC), fLastPacketNumReceived(0),
fPacketLossRatio(0), fTotNumPacketsLost(0), fJitter(0),
fLastSRTime(0), fDiffSR_RRTime(0), fFirstPacket(True),
fTotalOctetCount_hi(0), fTotalOctetCount_lo(0),
fTotalPacketCount_hi(0), fTotalPacketCount_lo(0) {
gettimeofday(&fTimeCreated, NULL);
fLastOctetCount = rtpSink.octetCount();
fLastPacketCount = rtpSink.packetCount();
}
RTPTransmissionStats::~RTPTransmissionStats() {}
void RTPTransmissionStats
::noteIncomingRR(struct sockaddr_in const& lastFromAddress,
unsigned lossStats, unsigned lastPacketNumReceived,
unsigned jitter, unsigned lastSRTime,
unsigned diffSR_RRTime) {
if (fFirstPacket) {
fFirstPacket = False;
fFirstPacketNumReported = lastPacketNumReceived;
} else {
fOldValid = True;
fOldLastPacketNumReceived = fLastPacketNumReceived;
fOldTotNumPacketsLost = fTotNumPacketsLost;
}
gettimeofday(&fTimeReceived, NULL);
fLastFromAddress = lastFromAddress;
fPacketLossRatio = lossStats>>24;
fTotNumPacketsLost = lossStats&0xFFFFFF;
fLastPacketNumReceived = lastPacketNumReceived;
fJitter = jitter;
fLastSRTime = lastSRTime;
fDiffSR_RRTime = diffSR_RRTime;
#ifdef DEBUG_RR
fprintf(stderr, "RTCP RR data (received at %lu.%06ld): lossStats 0x%08x, lastPacketNumReceived 0x%08x, jitter 0x%08x, lastSRTime 0x%08x, diffSR_RRTime 0x%08x\n",
fTimeReceived.tv_sec, fTimeReceived.tv_usec, lossStats, lastPacketNumReceived, jitter, lastSRTime, diffSR_RRTime);
unsigned rtd = roundTripDelay();
fprintf(stderr, "=> round-trip delay: 0x%04x (== %f seconds)\n", rtd, rtd/65536.0);
#endif
// Update our counts of the total number of octets and packets sent towards
// this receiver:
u_int32_t newOctetCount = fOurRTPSink.octetCount();
u_int32_t octetCountDiff = newOctetCount - fLastOctetCount;
fLastOctetCount = newOctetCount;
u_int32_t prevTotalOctetCount_lo = fTotalOctetCount_lo;
fTotalOctetCount_lo += octetCountDiff;
if (fTotalOctetCount_lo < prevTotalOctetCount_lo) { // wrap around
++fTotalOctetCount_hi;
}
u_int32_t newPacketCount = fOurRTPSink.packetCount();
u_int32_t packetCountDiff = newPacketCount - fLastPacketCount;
fLastPacketCount = newPacketCount;
u_int32_t prevTotalPacketCount_lo = fTotalPacketCount_lo;
fTotalPacketCount_lo += packetCountDiff;
if (fTotalPacketCount_lo < prevTotalPacketCount_lo) { // wrap around
++fTotalPacketCount_hi;
}
}
unsigned RTPTransmissionStats::roundTripDelay() const {
// Compute the round-trip delay that was indicated by the most recently-received
// RTCP RR packet. Use the method noted in the RTP/RTCP specification (RFC 3350).
if (fLastSRTime == 0) {
// Either no RTCP RR packet has been received yet, or else the
// reporting receiver has not yet received any RTCP SR packets from us:
return 0;
}
// First, convert the time that we received the last RTCP RR packet to NTP format,
// in units of 1/65536 (2^-16) seconds:
unsigned lastReceivedTimeNTP_high
= fTimeReceived.tv_sec + 0x83AA7E80; // 1970 epoch -> 1900 epoch
double fractionalPart = (fTimeReceived.tv_usec*0x0400)/15625.0; // 2^16/10^6
unsigned lastReceivedTimeNTP
= (unsigned)((lastReceivedTimeNTP_high<<16) + fractionalPart + 0.5);
int rawResult = lastReceivedTimeNTP - fLastSRTime - fDiffSR_RRTime;
if (rawResult < 0) {
// This can happen if there's clock drift between the sender and receiver,
// and if the round-trip time was very small.
rawResult = 0;
}
return (unsigned)rawResult;
}
void RTPTransmissionStats::getTotalOctetCount(u_int32_t& hi, u_int32_t& lo) {
hi = fTotalOctetCount_hi;
lo = fTotalOctetCount_lo;
}
void RTPTransmissionStats::getTotalPacketCount(u_int32_t& hi, u_int32_t& lo) {
hi = fTotalPacketCount_hi;
lo = fTotalPacketCount_lo;
}
unsigned RTPTransmissionStats::packetsReceivedSinceLastRR() const {
if (!fOldValid) return 0;
return fLastPacketNumReceived-fOldLastPacketNumReceived;
}
int RTPTransmissionStats::packetsLostBetweenRR() const {
if (!fOldValid) return 0;
return fTotNumPacketsLost - fOldTotNumPacketsLost;
}
|