OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/cast/transport/pacing/paced_sender.h" | 5 #include "media/cast/transport/pacing/paced_sender.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 | 9 |
10 namespace media { | 10 namespace media { |
11 namespace cast { | 11 namespace cast { |
12 namespace transport { | 12 namespace transport { |
13 | 13 |
14 static const int64 kPacingIntervalMs = 10; | 14 static const int64 kPacingIntervalMs = 10; |
15 // Each frame will be split into no more than kPacingMaxBurstsPerFrame | 15 // Each frame will be split into no more than kPacingMaxBurstsPerFrame |
16 // bursts of packets. | 16 // bursts of packets. |
17 static const size_t kPacingMaxBurstsPerFrame = 3; | 17 static const size_t kPacingMaxBurstsPerFrame = 3; |
18 | 18 |
19 PacedSender::PacedSender(scoped_refptr<CastEnvironment> cast_environment, | 19 PacedSender::PacedSender(base::TickClock* clock, |
20 PacketSender* transport) | 20 PacketSender* transport, |
21 : cast_environment_(cast_environment), | 21 scoped_refptr<base::TaskRunner> transport_task_runner) |
| 22 : clock_(clock), |
22 transport_(transport), | 23 transport_(transport), |
| 24 transport_task_runner_(transport_task_runner), |
23 burst_size_(1), | 25 burst_size_(1), |
24 packets_sent_in_burst_(0), | 26 packets_sent_in_burst_(0), |
25 weak_factory_(this) { | 27 weak_factory_(this) { |
26 ScheduleNextSend(); | 28 ScheduleNextSend(); |
27 } | 29 } |
28 | 30 |
29 PacedSender::~PacedSender() {} | 31 PacedSender::~PacedSender() {} |
30 | 32 |
31 bool PacedSender::SendPackets(const PacketList& packets) { | 33 bool PacedSender::SendPackets(const PacketList& packets) { |
32 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | |
33 base::TimeTicks now = cast_environment_->Clock()->NowTicks(); | |
34 cast_environment_->Logging()->InsertPacketListEvent(now, kPacketSentToPacer, | |
35 packets); | |
36 return SendPacketsToTransport(packets, &packet_list_); | 34 return SendPacketsToTransport(packets, &packet_list_); |
37 } | 35 } |
38 | 36 |
39 bool PacedSender::ResendPackets(const PacketList& packets) { | 37 bool PacedSender::ResendPackets(const PacketList& packets) { |
40 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | |
41 base::TimeTicks now = cast_environment_->Clock()->NowTicks(); | |
42 cast_environment_->Logging()->InsertPacketListEvent(now, kPacketRetransmited, | |
43 packets); | |
44 return SendPacketsToTransport(packets, &resend_packet_list_); | 38 return SendPacketsToTransport(packets, &resend_packet_list_); |
45 } | 39 } |
46 | 40 |
47 bool PacedSender::SendPacketsToTransport(const PacketList& packets, | 41 bool PacedSender::SendPacketsToTransport(const PacketList& packets, |
48 PacketList* packets_not_sent) { | 42 PacketList* packets_not_sent) { |
49 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | |
50 UpdateBurstSize(packets.size()); | 43 UpdateBurstSize(packets.size()); |
51 | 44 |
52 if (!packets_not_sent->empty()) { | 45 if (!packets_not_sent->empty()) { |
53 packets_not_sent->insert(packets_not_sent->end(), | 46 packets_not_sent->insert(packets_not_sent->end(), |
54 packets.begin(), packets.end()); | 47 packets.begin(), packets.end()); |
55 return true; | 48 return true; |
56 } | 49 } |
57 PacketList packets_to_send; | 50 PacketList packets_to_send; |
58 PacketList::const_iterator first_to_store_it = packets.begin(); | 51 PacketList::const_iterator first_to_store_it = packets.begin(); |
59 | 52 |
60 size_t max_packets_to_send_now = burst_size_ - packets_sent_in_burst_; | 53 size_t max_packets_to_send_now = burst_size_ - packets_sent_in_burst_; |
61 if (max_packets_to_send_now > 0) { | 54 if (max_packets_to_send_now > 0) { |
62 size_t packets_to_send_now = std::min(max_packets_to_send_now, | 55 size_t packets_to_send_now = std::min(max_packets_to_send_now, |
63 packets.size()); | 56 packets.size()); |
64 | 57 |
65 std::advance(first_to_store_it, packets_to_send_now); | 58 std::advance(first_to_store_it, packets_to_send_now); |
66 packets_to_send.insert(packets_to_send.begin(), | 59 packets_to_send.insert(packets_to_send.begin(), |
67 packets.begin(), first_to_store_it); | 60 packets.begin(), first_to_store_it); |
68 } | 61 } |
69 packets_not_sent->insert(packets_not_sent->end(), | 62 packets_not_sent->insert(packets_not_sent->end(), |
70 first_to_store_it, packets.end()); | 63 first_to_store_it, packets.end()); |
71 packets_sent_in_burst_ += packets_to_send.size(); | 64 packets_sent_in_burst_ += packets_to_send.size(); |
72 if (packets_to_send.empty()) return true; | 65 if (packets_to_send.empty()) return true; |
73 | 66 |
74 base::TimeTicks now = cast_environment_->Clock()->NowTicks(); | |
75 cast_environment_->Logging()->InsertPacketListEvent(now, kPacketSentToNetwork, | |
76 packets); | |
77 return transport_->SendPackets(packets_to_send); | 67 return transport_->SendPackets(packets_to_send); |
78 } | 68 } |
79 | 69 |
80 bool PacedSender::SendRtcpPacket(const Packet& packet) { | 70 bool PacedSender::SendRtcpPacket(const Packet& packet) { |
81 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | |
82 // We pass the RTCP packets straight through. | 71 // We pass the RTCP packets straight through. |
83 return transport_->SendPacket(packet); | 72 return transport_->SendPacket(packet); |
84 } | 73 } |
85 | 74 |
86 void PacedSender::ScheduleNextSend() { | 75 void PacedSender::ScheduleNextSend() { |
87 base::TimeDelta time_to_next = time_last_process_ - | 76 base::TimeDelta time_to_next = time_last_process_ - |
88 cast_environment_->Clock()->NowTicks() + | 77 clock_->NowTicks() + base::TimeDelta::FromMilliseconds(kPacingIntervalMs); |
89 base::TimeDelta::FromMilliseconds(kPacingIntervalMs); | |
90 | 78 |
91 time_to_next = std::max(time_to_next, base::TimeDelta()); | 79 time_to_next = std::max(time_to_next, base::TimeDelta()); |
92 | 80 |
93 cast_environment_->PostDelayedTask(CastEnvironment::MAIN, FROM_HERE, | 81 transport_task_runner_->PostDelayedTask(FROM_HERE, |
94 base::Bind(&PacedSender::SendNextPacketBurst, weak_factory_.GetWeakPtr()), | 82 base::Bind(&PacedSender::SendNextPacketBurst, weak_factory_.GetWeakPtr()), |
95 time_to_next); | 83 time_to_next); |
96 } | 84 } |
97 | 85 |
98 void PacedSender::SendNextPacketBurst() { | 86 void PacedSender::SendNextPacketBurst() { |
99 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | |
100 SendStoredPackets(); | 87 SendStoredPackets(); |
101 time_last_process_ = cast_environment_->Clock()->NowTicks(); | 88 time_last_process_ = clock_->NowTicks(); |
102 ScheduleNextSend(); | 89 ScheduleNextSend(); |
103 } | 90 } |
104 | 91 |
105 void PacedSender::SendStoredPackets() { | 92 void PacedSender::SendStoredPackets() { |
106 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | |
107 if (packet_list_.empty() && resend_packet_list_.empty()) return; | 93 if (packet_list_.empty() && resend_packet_list_.empty()) return; |
108 | 94 |
109 size_t packets_to_send = burst_size_; | 95 size_t packets_to_send = burst_size_; |
110 PacketList packets_to_resend; | 96 PacketList packets_to_resend; |
111 | 97 |
112 // Send our re-send packets first. | 98 // Send our re-send packets first. |
113 if (!resend_packet_list_.empty()) { | 99 if (!resend_packet_list_.empty()) { |
114 PacketList::iterator it = resend_packet_list_.begin(); | 100 PacketList::iterator it = resend_packet_list_.begin(); |
115 size_t packets_to_send_now = std::min(packets_to_send, | 101 size_t packets_to_send_now = std::min(packets_to_send, |
116 resend_packet_list_.size()); | 102 resend_packet_list_.size()); |
(...skipping 15 matching lines...) Expand all Loading... |
132 | 118 |
133 if (packet_list_.empty()) { | 119 if (packet_list_.empty()) { |
134 burst_size_ = 1; // Reset burst size after we sent the last stored packet | 120 burst_size_ = 1; // Reset burst size after we sent the last stored packet |
135 packets_sent_in_burst_ = 0; | 121 packets_sent_in_burst_ = 0; |
136 } | 122 } |
137 } | 123 } |
138 transport_->SendPackets(packets_to_resend); | 124 transport_->SendPackets(packets_to_resend); |
139 } | 125 } |
140 | 126 |
141 void PacedSender::UpdateBurstSize(size_t packets_to_send) { | 127 void PacedSender::UpdateBurstSize(size_t packets_to_send) { |
142 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | |
143 packets_to_send = std::max(packets_to_send, | 128 packets_to_send = std::max(packets_to_send, |
144 resend_packet_list_.size() + packet_list_.size()); | 129 resend_packet_list_.size() + packet_list_.size()); |
145 | 130 |
146 packets_to_send += (kPacingMaxBurstsPerFrame - 1); // Round up. | 131 packets_to_send += (kPacingMaxBurstsPerFrame - 1); // Round up. |
147 burst_size_ = std::max(packets_to_send / kPacingMaxBurstsPerFrame, | 132 burst_size_ = std::max(packets_to_send / kPacingMaxBurstsPerFrame, |
148 burst_size_); | 133 burst_size_); |
149 } | 134 } |
150 | 135 |
151 } // namespace transport | 136 } // namespace transport |
152 } // namespace cast | 137 } // namespace cast |
153 } // namespace media | 138 } // namespace media |
OLD | NEW |