This source file includes following definitions.
- OnReceivedDelaySinceLastReport
- cast_environment_
- OnReceivedSenderReport
- OnReceiverReferenceTimeReport
- OnReceivedSendReportRequest
- OnReceivedReceiverLog
- OnReceivedSenderLog
- number_of_rtt_in_avg_
- IsRtcpPacket
- GetSsrcOfSender
- TimeToSendNextRtcpReport
- IncomingRtcpPacket
- SendRtcpFromRtpReceiver
- SendRtcpFromRtpSender
- OnReceivedNtp
- OnReceivedLipSyncInfo
- OnReceivedSendReportRequest
- RtpTimestampInSenderTime
- SetCastReceiverEventHistorySize
- SetTargetDelay
- OnReceivedDelaySinceLastReport
- SaveLastSentNtpTime
- UpdateRtt
- Rtt
- CheckForWrapAround
- UpdateNextTimeToSendRtcp
#include "media/cast/rtcp/rtcp.h"
#include "base/big_endian.h"
#include "base/rand_util.h"
#include "media/cast/cast_config.h"
#include "media/cast/cast_defines.h"
#include "media/cast/cast_environment.h"
#include "media/cast/rtcp/rtcp_defines.h"
#include "media/cast/rtcp/rtcp_receiver.h"
#include "media/cast/rtcp/rtcp_sender.h"
#include "media/cast/rtcp/rtcp_utility.h"
#include "media/cast/transport/cast_transport_defines.h"
namespace media {
namespace cast {
static const int kMaxRttMs = 10000;
static const uint16 kMaxDelay = 2000;
static const int64 kMaxDiffSinceReceivedRtcpMs = 100000;
class LocalRtcpRttFeedback : public RtcpRttFeedback {
public:
explicit LocalRtcpRttFeedback(Rtcp* rtcp) : rtcp_(rtcp) {}
virtual void OnReceivedDelaySinceLastReport(
uint32 receivers_ssrc, uint32 last_report,
uint32 delay_since_last_report) OVERRIDE {
rtcp_->OnReceivedDelaySinceLastReport(receivers_ssrc, last_report,
delay_since_last_report);
}
private:
Rtcp* rtcp_;
};
class LocalRtcpReceiverFeedback : public RtcpReceiverFeedback {
public:
LocalRtcpReceiverFeedback(Rtcp* rtcp,
scoped_refptr<CastEnvironment> cast_environment)
: rtcp_(rtcp), cast_environment_(cast_environment) {}
virtual void OnReceivedSenderReport(
const transport::RtcpSenderInfo& remote_sender_info) OVERRIDE {
rtcp_->OnReceivedNtp(remote_sender_info.ntp_seconds,
remote_sender_info.ntp_fraction);
if (remote_sender_info.send_packet_count != 0) {
rtcp_->OnReceivedLipSyncInfo(remote_sender_info.rtp_timestamp,
remote_sender_info.ntp_seconds,
remote_sender_info.ntp_fraction);
}
}
virtual void OnReceiverReferenceTimeReport(
const RtcpReceiverReferenceTimeReport& remote_time_report) OVERRIDE {
rtcp_->OnReceivedNtp(remote_time_report.ntp_seconds,
remote_time_report.ntp_fraction);
}
virtual void OnReceivedSendReportRequest() OVERRIDE {
rtcp_->OnReceivedSendReportRequest();
}
virtual void OnReceivedReceiverLog(const RtcpReceiverLogMessage& receiver_log)
OVERRIDE {
RtcpReceiverLogMessage::const_iterator it = receiver_log.begin();
for (; it != receiver_log.end(); ++it) {
uint32 rtp_timestamp = it->rtp_timestamp_;
RtcpReceiverEventLogMessages::const_iterator event_it =
it->event_log_messages_.begin();
for (; event_it != it->event_log_messages_.end(); ++event_it) {
switch (event_it->type) {
case kAudioPacketReceived:
case kVideoPacketReceived:
case kDuplicateAudioPacketReceived:
case kDuplicateVideoPacketReceived:
cast_environment_->Logging()->InsertPacketEvent(
event_it->event_timestamp, event_it->type, rtp_timestamp,
kFrameIdUnknown, event_it->packet_id, 0, 0);
break;
case kAudioAckSent:
case kVideoAckSent:
case kAudioFrameDecoded:
case kVideoFrameDecoded:
cast_environment_->Logging()->InsertFrameEvent(
event_it->event_timestamp, event_it->type, rtp_timestamp,
kFrameIdUnknown);
break;
case kAudioPlayoutDelay:
case kVideoRenderDelay:
cast_environment_->Logging()->InsertFrameEventWithDelay(
event_it->event_timestamp, event_it->type, rtp_timestamp,
kFrameIdUnknown, event_it->delay_delta);
break;
default:
VLOG(2) << "Received log message via RTCP that we did not expect: "
<< static_cast<int>(event_it->type);
break;
}
}
}
}
virtual void OnReceivedSenderLog(
const transport::RtcpSenderLogMessage& sender_log) OVERRIDE {
transport::RtcpSenderLogMessage::const_iterator it = sender_log.begin();
for (; it != sender_log.end(); ++it) {
uint32 rtp_timestamp = it->rtp_timestamp;
CastLoggingEvent log_event = kUnknown;
switch (it->frame_status) {
case transport::kRtcpSenderFrameStatusDroppedByFlowControl:
log_event = kVideoFrameCaptured;
break;
case transport::kRtcpSenderFrameStatusDroppedByEncoder:
log_event = kVideoFrameSentToEncoder;
break;
case transport::kRtcpSenderFrameStatusSentToNetwork:
log_event = kVideoFrameEncoded;
break;
default:
continue;
}
base::TimeTicks now = cast_environment_->Clock()->NowTicks();
cast_environment_->Logging()->InsertFrameEvent(
now, log_event, rtp_timestamp, kFrameIdUnknown);
}
}
private:
Rtcp* rtcp_;
scoped_refptr<CastEnvironment> cast_environment_;
};
Rtcp::Rtcp(scoped_refptr<CastEnvironment> cast_environment,
RtcpSenderFeedback* sender_feedback,
transport::CastTransportSender* const transport_sender,
transport::PacedPacketSender* paced_packet_sender,
RtpReceiverStatistics* rtp_receiver_statistics, RtcpMode rtcp_mode,
const base::TimeDelta& rtcp_interval, uint32 local_ssrc,
uint32 remote_ssrc, const std::string& c_name)
: cast_environment_(cast_environment),
transport_sender_(transport_sender),
rtcp_interval_(rtcp_interval),
rtcp_mode_(rtcp_mode),
local_ssrc_(local_ssrc),
remote_ssrc_(remote_ssrc),
c_name_(c_name),
rtp_receiver_statistics_(rtp_receiver_statistics),
rtt_feedback_(new LocalRtcpRttFeedback(this)),
receiver_feedback_(new LocalRtcpReceiverFeedback(this, cast_environment)),
rtcp_sender_(new RtcpSender(cast_environment, paced_packet_sender,
local_ssrc, c_name)),
last_report_received_(0),
last_received_rtp_timestamp_(0),
last_received_ntp_seconds_(0),
last_received_ntp_fraction_(0),
min_rtt_(base::TimeDelta::FromMilliseconds(kMaxRttMs)),
number_of_rtt_in_avg_(0) {
rtcp_receiver_.reset(new RtcpReceiver(cast_environment, sender_feedback,
receiver_feedback_.get(),
rtt_feedback_.get(), local_ssrc));
rtcp_receiver_->SetRemoteSSRC(remote_ssrc);
}
Rtcp::~Rtcp() {}
bool Rtcp::IsRtcpPacket(const uint8* packet, size_t length) {
DCHECK_GE(length, kMinLengthOfRtcp) << "Invalid RTCP packet";
if (length < kMinLengthOfRtcp) return false;
uint8 packet_type = packet[1];
if (packet_type >= transport::kPacketTypeLow &&
packet_type <= transport::kPacketTypeHigh) {
return true;
}
return false;
}
uint32 Rtcp::GetSsrcOfSender(const uint8* rtcp_buffer, size_t length) {
DCHECK_GE(length, kMinLengthOfRtcp) << "Invalid RTCP packet";
uint32 ssrc_of_sender;
base::BigEndianReader big_endian_reader(
reinterpret_cast<const char*>(rtcp_buffer), length);
big_endian_reader.Skip(4);
big_endian_reader.ReadU32(&ssrc_of_sender);
return ssrc_of_sender;
}
base::TimeTicks Rtcp::TimeToSendNextRtcpReport() {
if (next_time_to_send_rtcp_.is_null()) {
UpdateNextTimeToSendRtcp();
}
return next_time_to_send_rtcp_;
}
void Rtcp::IncomingRtcpPacket(const uint8* rtcp_buffer, size_t length) {
RtcpParser rtcp_parser(rtcp_buffer, length);
if (!rtcp_parser.IsValid()) {
DLOG(ERROR) << "Received invalid RTCP packet";
return;
}
rtcp_receiver_->IncomingRtcpPacket(&rtcp_parser);
}
void Rtcp::SendRtcpFromRtpReceiver(
const RtcpCastMessage* cast_message,
const ReceiverRtcpEventSubscriber::RtcpEventMultiMap* rtcp_events) {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
uint32 packet_type_flags = 0;
base::TimeTicks now = cast_environment_->Clock()->NowTicks();
transport::RtcpReportBlock report_block;
RtcpReceiverReferenceTimeReport rrtr;
packet_type_flags |= transport::kRtcpRrtr;
ConvertTimeTicksToNtp(now, &rrtr.ntp_seconds, &rrtr.ntp_fraction);
SaveLastSentNtpTime(now, rrtr.ntp_seconds, rrtr.ntp_fraction);
if (cast_message) {
packet_type_flags |= transport::kRtcpCast;
}
if (rtcp_events) {
packet_type_flags |= transport::kRtcpReceiverLog;
}
if (rtcp_mode_ == kRtcpCompound || now >= next_time_to_send_rtcp_) {
packet_type_flags |= transport::kRtcpRr;
report_block.remote_ssrc = 0;
report_block.media_ssrc = remote_ssrc_;
if (rtp_receiver_statistics_) {
rtp_receiver_statistics_->GetStatistics(
&report_block.fraction_lost, &report_block.cumulative_lost,
&report_block.extended_high_sequence_number, &report_block.jitter);
cast_environment_->Logging()->InsertGenericEvent(now, kJitterMs,
report_block.jitter);
cast_environment_->Logging()->InsertGenericEvent(
now, kPacketLoss, report_block.fraction_lost);
}
report_block.last_sr = last_report_received_;
if (!time_last_report_received_.is_null()) {
uint32 delay_seconds = 0;
uint32 delay_fraction = 0;
base::TimeDelta delta = now - time_last_report_received_;
ConvertTimeToFractions(delta.InMicroseconds(), &delay_seconds,
&delay_fraction);
report_block.delay_since_last_sr =
ConvertToNtpDiff(delay_seconds, delay_fraction);
} else {
report_block.delay_since_last_sr = 0;
}
UpdateNextTimeToSendRtcp();
}
rtcp_sender_->SendRtcpFromRtpReceiver(packet_type_flags,
&report_block,
&rrtr,
cast_message,
rtcp_events,
target_delay_ms_);
}
void Rtcp::SendRtcpFromRtpSender(
const transport::RtcpSenderLogMessage& sender_log_message,
transport::RtcpSenderInfo sender_info) {
DCHECK(transport_sender_);
uint32 packet_type_flags = transport::kRtcpSr;
base::TimeTicks now = cast_environment_->Clock()->NowTicks();
if (sender_log_message.size()) {
packet_type_flags |= transport::kRtcpSenderLog;
}
SaveLastSentNtpTime(now, sender_info.ntp_seconds, sender_info.ntp_fraction);
transport::RtcpDlrrReportBlock dlrr;
if (!time_last_report_received_.is_null()) {
packet_type_flags |= transport::kRtcpDlrr;
dlrr.last_rr = last_report_received_;
uint32 delay_seconds = 0;
uint32 delay_fraction = 0;
base::TimeDelta delta = now - time_last_report_received_;
ConvertTimeToFractions(delta.InMicroseconds(), &delay_seconds,
&delay_fraction);
dlrr.delay_since_last_rr = ConvertToNtpDiff(delay_seconds, delay_fraction);
}
transport_sender_->SendRtcpFromRtpSender(
packet_type_flags, sender_info, dlrr, sender_log_message, local_ssrc_,
c_name_);
UpdateNextTimeToSendRtcp();
}
void Rtcp::OnReceivedNtp(uint32 ntp_seconds, uint32 ntp_fraction) {
last_report_received_ = (ntp_seconds << 16) + (ntp_fraction >> 16);
base::TimeTicks now = cast_environment_->Clock()->NowTicks();
time_last_report_received_ = now;
}
void Rtcp::OnReceivedLipSyncInfo(uint32 rtp_timestamp, uint32 ntp_seconds,
uint32 ntp_fraction) {
last_received_rtp_timestamp_ = rtp_timestamp;
last_received_ntp_seconds_ = ntp_seconds;
last_received_ntp_fraction_ = ntp_fraction;
}
void Rtcp::OnReceivedSendReportRequest() {
base::TimeTicks now = cast_environment_->Clock()->NowTicks();
next_time_to_send_rtcp_ = now;
}
bool Rtcp::RtpTimestampInSenderTime(int frequency, uint32 rtp_timestamp,
base::TimeTicks* rtp_timestamp_in_ticks)
const {
if (last_received_ntp_seconds_ == 0)
return false;
int wrap = CheckForWrapAround(rtp_timestamp, last_received_rtp_timestamp_);
int64 rtp_timestamp_int64 = rtp_timestamp;
int64 last_received_rtp_timestamp_int64 = last_received_rtp_timestamp_;
if (wrap == 1) {
rtp_timestamp_int64 += (1LL << 32);
} else if (wrap == -1) {
last_received_rtp_timestamp_int64 += (1LL << 32);
}
int64 rtp_timestamp_diff =
rtp_timestamp_int64 - last_received_rtp_timestamp_int64;
int frequency_khz = frequency / 1000;
int64 rtp_time_diff_ms = rtp_timestamp_diff / frequency_khz;
if (std::abs(rtp_time_diff_ms) > kMaxDiffSinceReceivedRtcpMs)
return false;
*rtp_timestamp_in_ticks = ConvertNtpToTimeTicks(last_received_ntp_seconds_,
last_received_ntp_fraction_) +
base::TimeDelta::FromMilliseconds(rtp_time_diff_ms);
return true;
}
void Rtcp::SetCastReceiverEventHistorySize(size_t size) {
rtcp_receiver_->SetCastReceiverEventHistorySize(size);
}
void Rtcp::SetTargetDelay(base::TimeDelta target_delay) {
target_delay_ms_ = static_cast<uint16>(target_delay.InMilliseconds());
DCHECK(target_delay_ms_ < kMaxDelay);
}
void Rtcp::OnReceivedDelaySinceLastReport(uint32 receivers_ssrc,
uint32 last_report,
uint32 delay_since_last_report) {
RtcpSendTimeMap::iterator it = last_reports_sent_map_.find(last_report);
if (it == last_reports_sent_map_.end()) {
return;
}
base::TimeDelta sender_delay =
cast_environment_->Clock()->NowTicks() - it->second;
UpdateRtt(sender_delay, ConvertFromNtpDiff(delay_since_last_report));
}
void Rtcp::SaveLastSentNtpTime(const base::TimeTicks& now,
uint32 last_ntp_seconds,
uint32 last_ntp_fraction) {
if (!last_reports_sent_queue_.empty())
DCHECK(now >= last_reports_sent_queue_.back().second);
uint32 last_report = ConvertToNtpDiff(last_ntp_seconds, last_ntp_fraction);
last_reports_sent_map_[last_report] = now;
last_reports_sent_queue_.push(std::make_pair(last_report, now));
base::TimeTicks timeout = now - base::TimeDelta::FromMilliseconds(kMaxRttMs);
while (!last_reports_sent_queue_.empty()) {
RtcpSendTimePair oldest_report = last_reports_sent_queue_.front();
if (oldest_report.second < timeout) {
last_reports_sent_map_.erase(oldest_report.first);
last_reports_sent_queue_.pop();
} else {
break;
}
}
}
void Rtcp::UpdateRtt(const base::TimeDelta& sender_delay,
const base::TimeDelta& receiver_delay) {
base::TimeDelta rtt = sender_delay - receiver_delay;
rtt = std::max(rtt, base::TimeDelta::FromMilliseconds(1));
rtt_ = rtt;
min_rtt_ = std::min(min_rtt_, rtt);
max_rtt_ = std::max(max_rtt_, rtt);
if (number_of_rtt_in_avg_ != 0) {
float ac = static_cast<float>(number_of_rtt_in_avg_);
avg_rtt_ms_ = ((ac / (ac + 1.0)) * avg_rtt_ms_) +
((1.0 / (ac + 1.0)) * rtt.InMilliseconds());
} else {
avg_rtt_ms_ = rtt.InMilliseconds();
}
number_of_rtt_in_avg_++;
}
bool Rtcp::Rtt(base::TimeDelta* rtt, base::TimeDelta* avg_rtt,
base::TimeDelta* min_rtt, base::TimeDelta* max_rtt) const {
DCHECK(rtt) << "Invalid argument";
DCHECK(avg_rtt) << "Invalid argument";
DCHECK(min_rtt) << "Invalid argument";
DCHECK(max_rtt) << "Invalid argument";
if (number_of_rtt_in_avg_ == 0) return false;
base::TimeTicks now = cast_environment_->Clock()->NowTicks();
cast_environment_->Logging()->InsertGenericEvent(now, kRttMs,
rtt->InMilliseconds());
*rtt = rtt_;
*avg_rtt = base::TimeDelta::FromMilliseconds(avg_rtt_ms_);
*min_rtt = min_rtt_;
*max_rtt = max_rtt_;
return true;
}
int Rtcp::CheckForWrapAround(uint32 new_timestamp, uint32 old_timestamp) const {
if (new_timestamp < old_timestamp) {
if (static_cast<int32>(new_timestamp - old_timestamp) > 0) {
return 1;
}
} else if (static_cast<int32>(old_timestamp - new_timestamp) > 0) {
return -1;
}
return 0;
}
void Rtcp::UpdateNextTimeToSendRtcp() {
int random = base::RandInt(0, 999);
base::TimeDelta time_to_next =
(rtcp_interval_ / 2) + (rtcp_interval_ * random / 1000);
base::TimeTicks now = cast_environment_->Clock()->NowTicks();
next_time_to_send_rtcp_ = now + time_to_next;
}
}
}