| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/net/pacing/paced_sender.h" | 5 #include "media/cast/net/pacing/paced_sender.h" |
| 6 | 6 |
| 7 #include "base/big_endian.h" | 7 #include "base/big_endian.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "media/cast/logging/logging_impl.h" | 10 #include "media/cast/logging/logging_impl.h" |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 PacedSender::~PacedSender() {} | 53 PacedSender::~PacedSender() {} |
| 54 | 54 |
| 55 void PacedSender::RegisterAudioSsrc(uint32 audio_ssrc) { | 55 void PacedSender::RegisterAudioSsrc(uint32 audio_ssrc) { |
| 56 audio_ssrc_ = audio_ssrc; | 56 audio_ssrc_ = audio_ssrc; |
| 57 } | 57 } |
| 58 | 58 |
| 59 void PacedSender::RegisterVideoSsrc(uint32 video_ssrc) { | 59 void PacedSender::RegisterVideoSsrc(uint32 video_ssrc) { |
| 60 video_ssrc_ = video_ssrc; | 60 video_ssrc_ = video_ssrc; |
| 61 } | 61 } |
| 62 | 62 |
| 63 void PacedSender::RegisterPrioritySsrc(uint32 ssrc) { |
| 64 priority_ssrcs_.push_back(ssrc); |
| 65 } |
| 66 |
| 63 bool PacedSender::SendPackets(const SendPacketVector& packets) { | 67 bool PacedSender::SendPackets(const SendPacketVector& packets) { |
| 64 if (packets.empty()) { | 68 if (packets.empty()) { |
| 65 return true; | 69 return true; |
| 66 } | 70 } |
| 71 const bool high_priority = IsHighPriority(packets.begin()->first); |
| 67 for (size_t i = 0; i < packets.size(); i++) { | 72 for (size_t i = 0; i < packets.size(); i++) { |
| 68 packet_list_[packets[i].first] = | 73 DCHECK(IsHighPriority(packets[i].first) == high_priority); |
| 69 make_pair(PacketType_Normal, packets[i].second); | 74 if (high_priority) { |
| 75 priority_packet_list_[packets[i].first] = |
| 76 make_pair(PacketType_Normal, packets[i].second); |
| 77 } else { |
| 78 packet_list_[packets[i].first] = |
| 79 make_pair(PacketType_Normal, packets[i].second); |
| 80 } |
| 70 } | 81 } |
| 71 if (state_ == State_Unblocked) { | 82 if (state_ == State_Unblocked) { |
| 72 SendStoredPackets(); | 83 SendStoredPackets(); |
| 73 } | 84 } |
| 74 return true; | 85 return true; |
| 75 } | 86 } |
| 76 | 87 |
| 77 bool PacedSender::ResendPackets(const SendPacketVector& packets, | 88 bool PacedSender::ResendPackets(const SendPacketVector& packets, |
| 78 base::TimeDelta dedupe_window) { | 89 base::TimeDelta dedupe_window) { |
| 79 if (packets.empty()) { | 90 if (packets.empty()) { |
| 80 return true; | 91 return true; |
| 81 } | 92 } |
| 93 const bool high_priority = IsHighPriority(packets.begin()->first); |
| 82 base::TimeTicks now = clock_->NowTicks(); | 94 base::TimeTicks now = clock_->NowTicks(); |
| 83 for (size_t i = 0; i < packets.size(); i++) { | 95 for (size_t i = 0; i < packets.size(); i++) { |
| 84 std::map<PacketKey, base::TimeTicks>::const_iterator j = | 96 std::map<PacketKey, base::TimeTicks>::const_iterator j = |
| 85 sent_time_.find(packets[i].first); | 97 sent_time_.find(packets[i].first); |
| 86 | 98 |
| 87 if (j != sent_time_.end() && now - j->second < dedupe_window) { | 99 if (j != sent_time_.end() && now - j->second < dedupe_window) { |
| 88 LogPacketEvent(packets[i].second->data, PACKET_RTX_REJECTED); | 100 LogPacketEvent(packets[i].second->data, PACKET_RTX_REJECTED); |
| 89 continue; | 101 continue; |
| 90 } | 102 } |
| 91 | 103 |
| 92 packet_list_[packets[i].first] = | 104 DCHECK(IsHighPriority(packets[i].first) == high_priority); |
| 93 make_pair(PacketType_Resend, packets[i].second); | 105 if (high_priority) { |
| 106 priority_packet_list_[packets[i].first] = |
| 107 make_pair(PacketType_Resend, packets[i].second); |
| 108 } else { |
| 109 packet_list_[packets[i].first] = |
| 110 make_pair(PacketType_Resend, packets[i].second); |
| 111 } |
| 94 } | 112 } |
| 95 if (state_ == State_Unblocked) { | 113 if (state_ == State_Unblocked) { |
| 96 SendStoredPackets(); | 114 SendStoredPackets(); |
| 97 } | 115 } |
| 98 return true; | 116 return true; |
| 99 } | 117 } |
| 100 | 118 |
| 101 bool PacedSender::SendRtcpPacket(uint32 ssrc, PacketRef packet) { | 119 bool PacedSender::SendRtcpPacket(uint32 ssrc, PacketRef packet) { |
| 102 if (state_ == State_TransportBlocked) { | 120 if (state_ == State_TransportBlocked) { |
| 103 packet_list_[PacedPacketSender::MakePacketKey(base::TimeTicks(), ssrc, 0)] = | 121 priority_packet_list_[ |
| 122 PacedPacketSender::MakePacketKey(base::TimeTicks(), ssrc, 0)] = |
| 104 make_pair(PacketType_RTCP, packet); | 123 make_pair(PacketType_RTCP, packet); |
| 105 } else { | 124 } else { |
| 106 // We pass the RTCP packets straight through. | 125 // We pass the RTCP packets straight through. |
| 107 if (!transport_->SendPacket( | 126 if (!transport_->SendPacket( |
| 108 packet, | 127 packet, |
| 109 base::Bind(&PacedSender::SendStoredPackets, | 128 base::Bind(&PacedSender::SendStoredPackets, |
| 110 weak_factory_.GetWeakPtr()))) { | 129 weak_factory_.GetWeakPtr()))) { |
| 111 state_ = State_TransportBlocked; | 130 state_ = State_TransportBlocked; |
| 112 } | 131 } |
| 113 | |
| 114 } | 132 } |
| 115 return true; | 133 return true; |
| 116 } | 134 } |
| 117 | 135 |
| 118 void PacedSender::CancelSendingPacket(const PacketKey& packet_key) { | 136 void PacedSender::CancelSendingPacket(const PacketKey& packet_key) { |
| 119 packet_list_.erase(packet_key); | 137 packet_list_.erase(packet_key); |
| 138 priority_packet_list_.erase(packet_key); |
| 120 } | 139 } |
| 121 | 140 |
| 122 PacketRef PacedSender::GetNextPacket(PacketType* packet_type, | 141 PacketRef PacedSender::PopNextPacket(PacketType* packet_type, |
| 123 PacketKey* packet_key) { | 142 PacketKey* packet_key) { |
| 124 std::map<PacketKey, std::pair<PacketType, PacketRef> >::iterator i; | 143 PacketList* list = !priority_packet_list_.empty() ? |
| 125 i = packet_list_.begin(); | 144 &priority_packet_list_ : &packet_list_; |
| 126 DCHECK(i != packet_list_.end()); | 145 DCHECK(!list->empty()); |
| 146 PacketList::iterator i = list->begin(); |
| 127 *packet_type = i->second.first; | 147 *packet_type = i->second.first; |
| 128 *packet_key = i->first; | 148 *packet_key = i->first; |
| 129 PacketRef ret = i->second.second; | 149 PacketRef ret = i->second.second; |
| 130 packet_list_.erase(i); | 150 list->erase(i); |
| 131 return ret; | 151 return ret; |
| 132 } | 152 } |
| 133 | 153 |
| 154 bool PacedSender::IsHighPriority(const PacketKey& packet_key) const { |
| 155 return std::find(priority_ssrcs_.begin(), priority_ssrcs_.end(), |
| 156 packet_key.second.first) != priority_ssrcs_.end(); |
| 157 } |
| 158 |
| 134 bool PacedSender::empty() const { | 159 bool PacedSender::empty() const { |
| 135 return packet_list_.empty(); | 160 return packet_list_.empty() && priority_packet_list_.empty(); |
| 136 } | 161 } |
| 137 | 162 |
| 138 size_t PacedSender::size() const { | 163 size_t PacedSender::size() const { |
| 139 return packet_list_.size(); | 164 return packet_list_.size() + priority_packet_list_.size(); |
| 140 } | 165 } |
| 141 | 166 |
| 142 // This function can be called from three places: | 167 // This function can be called from three places: |
| 143 // 1. User called one of the Send* functions and we were in an unblocked state. | 168 // 1. User called one of the Send* functions and we were in an unblocked state. |
| 144 // 2. state_ == State_TransportBlocked and the transport is calling us to | 169 // 2. state_ == State_TransportBlocked and the transport is calling us to |
| 145 // let us know that it's ok to send again. | 170 // let us know that it's ok to send again. |
| 146 // 3. state_ == State_BurstFull and there are still packets to send. In this | 171 // 3. state_ == State_BurstFull and there are still packets to send. In this |
| 147 // case we called PostDelayedTask on this function to start a new burst. | 172 // case we called PostDelayedTask on this function to start a new burst. |
| 148 void PacedSender::SendStoredPackets() { | 173 void PacedSender::SendStoredPackets() { |
| 149 State previous_state = state_; | 174 State previous_state = state_; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 while (!empty()) { | 217 while (!empty()) { |
| 193 if (current_burst_size_ >= max_burst_size_) { | 218 if (current_burst_size_ >= max_burst_size_) { |
| 194 transport_task_runner_->PostDelayedTask(FROM_HERE, | 219 transport_task_runner_->PostDelayedTask(FROM_HERE, |
| 195 cb, | 220 cb, |
| 196 burst_end_ - now); | 221 burst_end_ - now); |
| 197 state_ = State_BurstFull; | 222 state_ = State_BurstFull; |
| 198 return; | 223 return; |
| 199 } | 224 } |
| 200 PacketType packet_type; | 225 PacketType packet_type; |
| 201 PacketKey packet_key; | 226 PacketKey packet_key; |
| 202 PacketRef packet = GetNextPacket(&packet_type, &packet_key); | 227 PacketRef packet = PopNextPacket(&packet_type, &packet_key); |
| 203 sent_time_[packet_key] = now; | 228 sent_time_[packet_key] = now; |
| 204 sent_time_buffer_[packet_key] = now; | 229 sent_time_buffer_[packet_key] = now; |
| 205 | 230 |
| 206 switch (packet_type) { | 231 switch (packet_type) { |
| 207 case PacketType_Resend: | 232 case PacketType_Resend: |
| 208 LogPacketEvent(packet->data, PACKET_RETRANSMITTED); | 233 LogPacketEvent(packet->data, PACKET_RETRANSMITTED); |
| 209 break; | 234 break; |
| 210 case PacketType_Normal: | 235 case PacketType_Normal: |
| 211 LogPacketEvent(packet->data, PACKET_SENT_TO_NETWORK); | 236 LogPacketEvent(packet->data, PACKET_SENT_TO_NETWORK); |
| 212 break; | 237 break; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 return; | 275 return; |
| 251 } | 276 } |
| 252 | 277 |
| 253 EventMediaType media_type = is_audio ? AUDIO_EVENT : VIDEO_EVENT; | 278 EventMediaType media_type = is_audio ? AUDIO_EVENT : VIDEO_EVENT; |
| 254 logging_->InsertSinglePacketEvent(clock_->NowTicks(), event, media_type, | 279 logging_->InsertSinglePacketEvent(clock_->NowTicks(), event, media_type, |
| 255 packet); | 280 packet); |
| 256 } | 281 } |
| 257 | 282 |
| 258 } // namespace cast | 283 } // namespace cast |
| 259 } // namespace media | 284 } // namespace media |
| OLD | NEW |