1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/cast/transport/pacing/paced_sender.h"
7 #include "base/big_endian.h"
9 #include "base/message_loop/message_loop.h"
17 static const int64 kPacingIntervalMs = 10;
18 // Each frame will be split into no more than kPacingMaxBurstsPerFrame
20 static const size_t kPacingMaxBurstsPerFrame = 3;
21 static const size_t kTargetBurstSize = 10;
22 static const size_t kMaxBurstSize = 20;
24 using media::cast::CastLoggingEvent;
26 CastLoggingEvent GetLoggingEvent(bool is_audio, bool retransmit) {
28 return is_audio ? media::cast::kAudioPacketRetransmitted
29 : media::cast::kVideoPacketRetransmitted;
31 return is_audio ? media::cast::kAudioPacketSentToNetwork
32 : media::cast::kVideoPacketSentToNetwork;
39 PacketKey PacedPacketSender::MakePacketKey(const base::TimeTicks& ticks,
42 return std::make_pair(ticks, std::make_pair(ssrc, packet_id));
45 PacedSender::PacedSender(
46 base::TickClock* clock,
48 PacketSender* transport,
49 const scoped_refptr<base::SingleThreadTaskRunner>& transport_task_runner)
52 transport_(transport),
53 transport_task_runner_(transport_task_runner),
56 max_burst_size_(kTargetBurstSize),
57 next_max_burst_size_(kTargetBurstSize),
58 next_next_max_burst_size_(kTargetBurstSize),
59 current_burst_size_(0),
60 state_(State_Unblocked),
64 PacedSender::~PacedSender() {}
66 void PacedSender::RegisterAudioSsrc(uint32 audio_ssrc) {
67 audio_ssrc_ = audio_ssrc;
70 void PacedSender::RegisterVideoSsrc(uint32 video_ssrc) {
71 video_ssrc_ = video_ssrc;
74 bool PacedSender::SendPackets(const SendPacketVector& packets) {
75 if (packets.empty()) {
78 for (size_t i = 0; i < packets.size(); i++) {
79 packet_list_[packets[i].first] =
80 make_pair(PacketType_Normal, packets[i].second);
82 if (state_ == State_Unblocked) {
88 bool PacedSender::ResendPackets(const SendPacketVector& packets) {
89 if (packets.empty()) {
92 for (size_t i = 0; i < packets.size(); i++) {
93 packet_list_[packets[i].first] =
94 make_pair(PacketType_Resend, packets[i].second);
96 if (state_ == State_Unblocked) {
102 bool PacedSender::SendRtcpPacket(uint32 ssrc, PacketRef packet) {
103 if (state_ == State_TransportBlocked) {
104 packet_list_[PacedPacketSender::MakePacketKey(base::TimeTicks(), ssrc, 0)] =
105 make_pair(PacketType_RTCP, packet);
107 // We pass the RTCP packets straight through.
108 if (!transport_->SendPacket(
110 base::Bind(&PacedSender::SendStoredPackets,
111 weak_factory_.GetWeakPtr()))) {
112 state_ = State_TransportBlocked;
119 PacketRef PacedSender::GetNextPacket(PacketType* packet_type) {
120 std::map<PacketKey, std::pair<PacketType, PacketRef> >::iterator i;
121 i = packet_list_.begin();
122 DCHECK(i != packet_list_.end());
123 *packet_type = i->second.first;
124 PacketRef ret = i->second.second;
125 packet_list_.erase(i);
129 bool PacedSender::empty() const {
130 return packet_list_.empty();
133 size_t PacedSender::size() const {
134 return packet_list_.size();
137 // This function can be called from three places:
138 // 1. User called one of the Send* functions and we were in an unblocked state.
139 // 2. state_ == State_TransportBlocked and the transport is calling us to
140 // let us know that it's ok to send again.
141 // 3. state_ == State_BurstFull and there are still packets to send. In this
142 // case we called PostDelayedTask on this function to start a new burst.
143 void PacedSender::SendStoredPackets() {
144 State previous_state = state_;
145 state_ = State_Unblocked;
150 base::TimeTicks now = clock_->NowTicks();
151 // I don't actually trust that PostDelayTask(x - now) will mean that
152 // now >= x when the call happens, so check if the previous state was
153 // State_BurstFull too.
154 if (now >= burst_end_ || previous_state == State_BurstFull) {
155 // Start a new burst.
156 current_burst_size_ = 0;
157 burst_end_ = now + base::TimeDelta::FromMilliseconds(kPacingIntervalMs);
159 // The goal here is to try to send out the queued packets over the next
160 // three bursts, while trying to keep the burst size below 10 if possible.
161 // We have some evidence that sending more than 12 packets in a row doesn't
162 // work very well, but we don't actually know why yet. Sending out packets
163 // sooner is better than sending out packets later as that gives us more
164 // time to re-send them if needed. So if we have less than 30 packets, just
165 // send 10 at a time. If we have less than 60 packets, send n / 3 at a time.
166 // if we have more than 60, we send 20 at a time. 20 packets is ~24Mbit/s
167 // which is more bandwidth than the cast library should need, and sending
168 // out more data per second is unlikely to be helpful.
169 size_t max_burst_size = std::min(
171 std::max(kTargetBurstSize, size() / kPacingMaxBurstsPerFrame));
173 // If the queue is long, issue a warning. Try to limit the number of
174 // warnings issued by only issuing the warning when the burst size
175 // grows. Otherwise we might get 100 warnings per second.
176 if (max_burst_size > next_next_max_burst_size_ && size() > 100) {
177 LOG(WARNING) << "Packet queue is very long:" << size();
180 max_burst_size_ = std::max(next_max_burst_size_, max_burst_size);
181 next_max_burst_size_ = std::max(next_next_max_burst_size_, max_burst_size);
182 next_next_max_burst_size_ = max_burst_size;
185 base::Closure cb = base::Bind(&PacedSender::SendStoredPackets,
186 weak_factory_.GetWeakPtr());
188 if (current_burst_size_ >= max_burst_size_) {
189 transport_task_runner_->PostDelayedTask(FROM_HERE,
192 state_ = State_BurstFull;
195 PacketType packet_type;
196 PacketRef packet = GetNextPacket(&packet_type);
198 switch (packet_type) {
199 case PacketType_Resend:
200 LogPacketEvent(packet->data, true);
202 case PacketType_Normal:
203 LogPacketEvent(packet->data, false);
205 case PacketType_RTCP:
208 if (!transport_->SendPacket(packet, cb)) {
209 state_ = State_TransportBlocked;
212 current_burst_size_++;
214 state_ = State_Unblocked;
217 void PacedSender::LogPacketEvent(const Packet& packet, bool retransmit) {
218 // Get SSRC from packet and compare with the audio_ssrc / video_ssrc to see
219 // if the packet is audio or video.
220 DCHECK_GE(packet.size(), 12u);
221 base::BigEndianReader reader(reinterpret_cast<const char*>(&packet[8]), 4);
223 bool success = reader.ReadU32(&ssrc);
226 if (ssrc == audio_ssrc_) {
228 } else if (ssrc == video_ssrc_) {
231 DVLOG(3) << "Got unknown ssrc " << ssrc << " when logging packet event";
235 CastLoggingEvent event = GetLoggingEvent(is_audio, retransmit);
237 logging_->InsertSinglePacketEvent(clock_->NowTicks(), event, packet);
240 } // namespace transport