void RtcpProcessor::analyzeFeedback(char *buf, int len) { RtcpHeader *chead = reinterpret_cast<RtcpHeader*>(buf); if (chead->isFeedback()) { uint32_t sourceSsrc = chead->getSourceSSRC(); // We try to add it just in case it is not there yet (otherwise its noop) this->addSourceSsrc(sourceSsrc); boost::mutex::scoped_lock mlock(mapLock_); boost::shared_ptr<RtcpData> theData = rtcpData_[sourceSsrc]; boost::mutex::scoped_lock lock(theData->dataLock); struct timeval now; gettimeofday(&now, NULL); char* movingBuf = buf; int rtcpLength = 0; int totalLength = 0; int partNum = 0; uint32_t calculatedlsr, delay, calculateLastSr; do { movingBuf+=rtcpLength; chead = reinterpret_cast<RtcpHeader*>(movingBuf); rtcpLength = (ntohs(chead->length)+1) * 4; totalLength += rtcpLength; switch(chead->packettype){ case RTCP_SDES_PT: ELOG_DEBUG("SDES"); break; case RTCP_BYE: ELOG_DEBUG("BYE"); break; case RTCP_Receiver_PT: theData->rrsReceivedInPeriod++; if (chead->getSourceSSRC() == rtcpSource_->getVideoSourceSSRC()){ ELOG_DEBUG("Analyzing Video RR: PacketLost %u, Ratio %u, partNum %d, blocks %d, sourceSSRC %u, ssrc %u", chead->getLostPackets(), chead->getFractionLost(), partNum, chead->getBlockCount(), chead->getSourceSSRC(), chead->getSSRC()); }else{ ELOG_DEBUG("Analyzing Audio RR: PacketLost %u, Ratio %u, partNum %d, blocks %d, sourceSSRC %u, ssrc %u", chead->getLostPackets(), chead->getFractionLost(), partNum, chead->getBlockCount(), chead->getSourceSSRC(), chead->getSSRC()); } theData->ratioLost = theData->ratioLost > chead->getFractionLost()? theData->ratioLost: chead->getFractionLost(); theData->totalPacketsLost = theData->totalPacketsLost > chead->getLostPackets()? theData->totalPacketsLost : chead->getLostPackets(); theData->highestSeqNumReceived = theData->highestSeqNumReceived > chead->getHighestSeqnum()? theData->highestSeqNumReceived : chead->getHighestSeqnum(); theData->seqNumCycles = theData->seqNumCycles > chead->getSeqnumCycles()? theData->seqNumCycles : chead->getSeqnumCycles(); theData->jitter = theData->jitter > chead->getJitter()? theData->jitter: chead->getJitter(); calculateLastSr = chead->getLastSr(); calculatedlsr = (chead->getDelaySinceLastSr()*1000)/65536; for (std::list<boost::shared_ptr<SrData>>::iterator it=theData->senderReports.begin(); it != theData->senderReports.end(); ++it){ if ((*it)->srNtp == calculateLastSr){ uint64_t nowms = (now.tv_sec * 1000) + (now.tv_usec / 1000); uint64_t sentts = ((*it)->timestamp.tv_sec * 1000) + ((*it)->timestamp.tv_usec / 1000); delay = nowms - sentts - calculatedlsr; } } if (theData->lastSr==0||theData->lastDelay < delay){ ELOG_DEBUG("Recording DLSR %u, lastSR %u last delay %u, calculated delay %u for SSRC %u", chead->getDelaySinceLastSr(), chead->getLastSr(), theData->lastDelay, delay, sourceSsrc); theData->lastSr = chead->getLastSr(); theData->delaySinceLastSr = chead->getDelaySinceLastSr(); theData->lastSrUpdated = now; theData->lastDelay = delay; }else{ // ELOG_DEBUG("Not recording delay %u, lastDelay %u", delay, theData->lastDelay); } break; case RTCP_RTP_Feedback_PT: ELOG_DEBUG("RTP FB: Usually NACKs: %u, partNum %d", chead->getBlockCount(), partNum); ELOG_DEBUG("PID %u BLP %u", chead->getNackPid(), chead->getNackBlp()); theData->shouldSendNACK = true; theData->nackSeqnum = chead->getNackPid(); theData->nackBlp = chead->getNackBlp(); theData->requestRr = true; break; case RTCP_PS_Feedback_PT: // ELOG_DEBUG("RTCP PS FB TYPE: %u", chead->getBlockCount() ); switch(chead->getBlockCount()){ case RTCP_PLI_FMT: ELOG_DEBUG("PLI Message, partNum %d", partNum); // 1: PLI, 4: FIR theData->shouldSendPli = true; break; case RTCP_SLI_FMT: ELOG_DEBUG("SLI Message"); break; case RTCP_FIR_FMT: ELOG_DEBUG("FIR Message"); break; case RTCP_AFB: { char *uniqueId = (char*)&chead->report.rembPacket.uniqueid; if (!strncmp(uniqueId,"REMB", 4)){ uint64_t bitrate = chead->getBrMantis() << chead->getBrExp(); ELOG_DEBUG("Received REMBO %lu", bitrate); if ((bitrate < theData->reportedBandwidth) || theData->reportedBandwidth==0){ ELOG_DEBUG("Should send Packet REMB, before BR %lu, will send with Br %lu", theData->reportedBandwidth, bitrate); theData->reportedBandwidth = bitrate; theData->shouldSendREMB = true; } } else{ ELOG_DEBUG("Unsupported AFB Packet not REMB") } break; } default: ELOG_WARN("Unsupported RTCP_PS FB TYPE %u",chead->getBlockCount()); break; } break; default: ELOG_DEBUG("Unknown RTCP Packet, %d", chead->packettype); break; } partNum++; } while (totalLength < len); }
int RtcpAggregator::analyzeFeedback(char *buf, int len) { RtcpHeader *chead = reinterpret_cast<RtcpHeader*>(buf); if (chead->isFeedback()) { if (chead->getBlockCount() == 0 && (chead->getLength()+1) * 4 == len) { ELOG_DEBUG("Ignoring empty RR"); return 0; } uint32_t sourceSsrc = chead->getSourceSSRC(); // We try to add it just in case it is not there yet (otherwise its noop) this->addSourceSsrc(sourceSsrc); boost::mutex::scoped_lock mlock(mapLock_); boost::shared_ptr<RtcpData> theData = rtcpData_[sourceSsrc]; boost::mutex::scoped_lock lock(theData->dataLock); uint64_t nowms = ClockUtils::timePointToMs(clock::now()); char* movingBuf = buf; int rtcpLength = 0; int totalLength = 0; int partNum = 0; uint16_t currentNackPos = 0; uint16_t blp = 0; uint32_t lostPacketSeq = 0; uint32_t delay = 0; uint32_t calculatedlsr, calculateLastSr, extendedSeqNo; do { movingBuf += rtcpLength; chead = reinterpret_cast<RtcpHeader*>(movingBuf); rtcpLength = (ntohs(chead->length) + 1) * 4; totalLength += rtcpLength; switch (chead->packettype) { case RTCP_SDES_PT: ELOG_DEBUG("SDES"); break; case RTCP_BYE: ELOG_DEBUG("BYE"); break; case RTCP_Receiver_PT: theData->rrsReceivedInPeriod++; if (rtcpSource_->isVideoSourceSSRC(chead->getSourceSSRC())) { ELOG_DEBUG("Analyzing Video RR: PacketLost %u, Ratio %u, partNum %d, blocks %d, sourceSSRC %u, ssrc %u", chead->getLostPackets(), chead->getFractionLost(), partNum, chead->getBlockCount(), chead->getSourceSSRC(), chead->getSSRC()); } else { ELOG_DEBUG("Analyzing Audio RR: PacketLost %u, Ratio %u, partNum %d, blocks %d, sourceSSRC %u, ssrc %u", chead->getLostPackets(), chead->getFractionLost(), partNum, chead->getBlockCount(), chead->getSourceSSRC(), chead->getSSRC()); } theData->ratioLost = theData->ratioLost > chead->getFractionLost() ? theData->ratioLost : chead->getFractionLost(); theData->totalPacketsLost = theData->totalPacketsLost > chead->getLostPackets() ? theData->totalPacketsLost : chead->getLostPackets(); extendedSeqNo = chead->getSeqnumCycles(); extendedSeqNo = (extendedSeqNo << 16) + chead->getHighestSeqnum(); if (extendedSeqNo > theData->extendedSeqNo) { theData->extendedSeqNo = extendedSeqNo; theData->highestSeqNumReceived = chead->getHighestSeqnum(); theData->seqNumCycles = chead->getSeqnumCycles(); } theData->jitter = theData->jitter > chead->getJitter()? theData->jitter: chead->getJitter(); calculateLastSr = chead->getLastSr(); calculatedlsr = (chead->getDelaySinceLastSr() * 1000) / 65536; for (std::list<boost::shared_ptr<SrDelayData>>::iterator it = theData->senderReports.begin(); it != theData->senderReports.end(); ++it) { if ((*it)->sr_ntp == calculateLastSr) { uint64_t sentts = (*it)->sr_send_time; delay = nowms - sentts - calculatedlsr; } } if (theData->lastSr == 0 || theData->lastDelay < delay) { ELOG_DEBUG("Recording DLSR %u, lastSR %u last delay %u, calculated delay %u for SSRC %u", chead->getDelaySinceLastSr(), chead->getLastSr(), theData->lastDelay, delay, sourceSsrc); theData->lastSr = chead->getLastSr(); theData->delaySinceLastSr = chead->getDelaySinceLastSr(); theData->last_sr_updated = nowms; theData->lastDelay = delay; } else { // ELOG_DEBUG("Not recording delay %u, lastDelay %u", delay, theData->lastDelay); } break; case RTCP_RTP_Feedback_PT: { ELOG_DEBUG("RTP FB: Usually NACKs: %u, partNum %d", chead->getBlockCount(), partNum); ELOG_DEBUG("NACK PID %u BLP %u", chead->getNackPid(), chead->getNackBlp()); // We analyze NACK to avoid sending repeated NACKs blp = chead->getNackBlp(); theData->shouldSendNACK = false; std::pair<std::set<uint32_t>::iterator, bool> ret; ret = theData->nackedPackets_.insert(chead->getNackPid()); if (ret.second) { ELOG_DEBUG("We received PID NACK for unacked packet %u", chead->getNackPid()); theData->shouldSendNACK = true; } else { if (theData->nackedPackets_.size() >= MAP_NACK_SIZE) { while (theData->nackedPackets_.size() >= MAP_NACK_SIZE) { theData->nackedPackets_.erase(theData->nackedPackets_.begin()); } } ELOG_DEBUG("We received PID NACK for ALREADY acked packet %u", chead->getNackPid()); } if (blp != 0) { for (int i = 0; i < 16; i++) { currentNackPos = blp & 0x0001; blp = blp >> 1; if (currentNackPos == 1) { lostPacketSeq = chead->getNackPid() + 1 + i; ret = theData->nackedPackets_.insert(lostPacketSeq); if (ret.second) { ELOG_DEBUG("We received NACK for unacked packet %u", lostPacketSeq); } else { ELOG_DEBUG("We received NACK for ALREADY acked packet %u", lostPacketSeq); } theData->shouldSendNACK |=ret.second; } } } if (theData->shouldSendNACK) { ELOG_DEBUG("Will send NACK"); theData->nackSeqnum = chead->getNackPid(); theData->nackBlp = chead->getNackBlp(); theData->requestRr = true; } else { ELOG_DEBUG("I'm ignoring a NACK"); } } break; case RTCP_PS_Feedback_PT: // ELOG_DEBUG("RTCP PS FB TYPE: %u", chead->getBlockCount() ); switch (chead->getBlockCount()) { case RTCP_PLI_FMT: ELOG_DEBUG("PLI Message, partNum %d", partNum); // 1: PLI, 4: FIR theData->shouldSendPli = true; break; case RTCP_SLI_FMT: ELOG_DEBUG("SLI Message"); break; case RTCP_FIR_FMT: ELOG_DEBUG("FIR Message"); break; case RTCP_AFB: { char *uniqueId = reinterpret_cast<char*>(&chead->report.rembPacket.uniqueid); if (!strncmp(uniqueId, "REMB", 4)) { uint64_t bitrate = chead->getBrMantis() << chead->getBrExp(); ELOG_DEBUG("Received REMB %lu", bitrate); if (bitrate < defaultVideoBw_) { theData->reportedBandwidth = bitrate; theData->shouldSendREMB = true; } else { theData->reportedBandwidth = defaultVideoBw_; } } else { ELOG_DEBUG("Unsupported AFB Packet not REMB") } break; } default: ELOG_WARN("Unsupported RTCP_PS FB TYPE %u", chead->getBlockCount()); break; } break; default: ELOG_DEBUG("Unknown RTCP Packet, %d", chead->packettype); break; } partNum++; } while (totalLength < len); } return 0; }