1 /*
2  *  Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "webrtc/modules/remote_bitrate_estimator/test/estimators/send_side.h"
12 
13 #include "webrtc/base/logging.h"
14 #include "webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_abs_send_time.h"
15 #include "webrtc/modules/remote_bitrate_estimator/test/bwe_test_logging.h"
16 
17 namespace webrtc {
18 namespace testing {
19 namespace bwe {
20 
21 const int kFeedbackIntervalMs = 50;
22 
FullBweSender(int kbps,BitrateObserver * observer,Clock * clock)23 FullBweSender::FullBweSender(int kbps, BitrateObserver* observer, Clock* clock)
24     : bitrate_controller_(
25           BitrateController::CreateBitrateController(clock, observer)),
26       rbe_(new RemoteBitrateEstimatorAbsSendTime(this, clock)),
27       feedback_observer_(bitrate_controller_->CreateRtcpBandwidthObserver()),
28       clock_(clock),
29       send_time_history_(clock_, 10000),
30       has_received_ack_(false),
31       last_acked_seq_num_(0) {
32   assert(kbps >= kMinBitrateKbps);
33   assert(kbps <= kMaxBitrateKbps);
34   bitrate_controller_->SetStartBitrate(1000 * kbps);
35   bitrate_controller_->SetMinMaxBitrate(1000 * kMinBitrateKbps,
36                                         1000 * kMaxBitrateKbps);
37   rbe_->SetMinBitrate(1000 * kMinBitrateKbps);
38 }
39 
~FullBweSender()40 FullBweSender::~FullBweSender() {
41 }
42 
GetFeedbackIntervalMs() const43 int FullBweSender::GetFeedbackIntervalMs() const {
44   return kFeedbackIntervalMs;
45 }
46 
GiveFeedback(const FeedbackPacket & feedback)47 void FullBweSender::GiveFeedback(const FeedbackPacket& feedback) {
48   const SendSideBweFeedback& fb =
49       static_cast<const SendSideBweFeedback&>(feedback);
50   if (fb.packet_feedback_vector().empty())
51     return;
52   std::vector<PacketInfo> packet_feedback_vector(fb.packet_feedback_vector());
53   for (PacketInfo& packet_info : packet_feedback_vector) {
54     if (!send_time_history_.GetInfo(&packet_info, true)) {
55       LOG(LS_WARNING) << "Ack arrived too late.";
56     }
57   }
58 
59   int64_t rtt_ms =
60       clock_->TimeInMilliseconds() - feedback.latest_send_time_ms();
61   rbe_->OnRttUpdate(rtt_ms, rtt_ms);
62   BWE_TEST_LOGGING_PLOT(1, "RTT", clock_->TimeInMilliseconds(), rtt_ms);
63 
64   rbe_->IncomingPacketFeedbackVector(packet_feedback_vector);
65   if (has_received_ack_) {
66     int expected_packets = fb.packet_feedback_vector().back().sequence_number -
67                            last_acked_seq_num_;
68     // Assuming no reordering for now.
69     if (expected_packets > 0) {
70       int lost_packets = expected_packets -
71                          static_cast<int>(fb.packet_feedback_vector().size());
72       report_block_.fractionLost = (lost_packets << 8) / expected_packets;
73       report_block_.cumulativeLost += lost_packets;
74       report_block_.extendedHighSeqNum =
75           packet_feedback_vector.back().sequence_number;
76       ReportBlockList report_blocks;
77       report_blocks.push_back(report_block_);
78       feedback_observer_->OnReceivedRtcpReceiverReport(
79           report_blocks, rtt_ms, clock_->TimeInMilliseconds());
80     }
81     bitrate_controller_->Process();
82 
83     last_acked_seq_num_ = LatestSequenceNumber(
84         packet_feedback_vector.back().sequence_number, last_acked_seq_num_);
85   } else {
86     last_acked_seq_num_ = packet_feedback_vector.back().sequence_number;
87     has_received_ack_ = true;
88   }
89 }
90 
OnPacketsSent(const Packets & packets)91 void FullBweSender::OnPacketsSent(const Packets& packets) {
92   for (Packet* packet : packets) {
93     if (packet->GetPacketType() == Packet::kMedia) {
94       MediaPacket* media_packet = static_cast<MediaPacket*>(packet);
95       send_time_history_.AddAndRemoveOld(media_packet->header().sequenceNumber,
96                                          media_packet->payload_size(),
97                                          packet->paced());
98       send_time_history_.OnSentPacket(media_packet->header().sequenceNumber,
99                                       media_packet->sender_timestamp_ms());
100     }
101   }
102 }
103 
OnReceiveBitrateChanged(const std::vector<unsigned int> & ssrcs,unsigned int bitrate)104 void FullBweSender::OnReceiveBitrateChanged(
105     const std::vector<unsigned int>& ssrcs,
106     unsigned int bitrate) {
107   feedback_observer_->OnReceivedEstimatedBitrate(bitrate);
108 }
109 
TimeUntilNextProcess()110 int64_t FullBweSender::TimeUntilNextProcess() {
111   return bitrate_controller_->TimeUntilNextProcess();
112 }
113 
Process()114 int FullBweSender::Process() {
115   rbe_->Process();
116   return bitrate_controller_->Process();
117 }
118 
SendSideBweReceiver(int flow_id)119 SendSideBweReceiver::SendSideBweReceiver(int flow_id)
120     : BweReceiver(flow_id), last_feedback_ms_(0) {
121 }
122 
~SendSideBweReceiver()123 SendSideBweReceiver::~SendSideBweReceiver() {
124 }
125 
ReceivePacket(int64_t arrival_time_ms,const MediaPacket & media_packet)126 void SendSideBweReceiver::ReceivePacket(int64_t arrival_time_ms,
127                                         const MediaPacket& media_packet) {
128   packet_feedback_vector_.push_back(PacketInfo(
129       -1, arrival_time_ms, media_packet.sender_timestamp_ms(),
130       media_packet.header().sequenceNumber, media_packet.payload_size(), true));
131 
132   // Log received packet information.
133   BweReceiver::ReceivePacket(arrival_time_ms, media_packet);
134 }
135 
GetFeedback(int64_t now_ms)136 FeedbackPacket* SendSideBweReceiver::GetFeedback(int64_t now_ms) {
137   if (now_ms - last_feedback_ms_ < kFeedbackIntervalMs)
138     return NULL;
139   last_feedback_ms_ = now_ms;
140   int64_t corrected_send_time_ms =
141       packet_feedback_vector_.back().send_time_ms + now_ms -
142       packet_feedback_vector_.back().arrival_time_ms;
143   FeedbackPacket* fb = new SendSideBweFeedback(
144       flow_id_, now_ms * 1000, corrected_send_time_ms, packet_feedback_vector_);
145   packet_feedback_vector_.clear();
146   return fb;
147 }
148 
149 }  // namespace bwe
150 }  // namespace testing
151 }  // namespace webrtc
152