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