| 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/big_endian.h" |
| 7 #include "base/bind.h" | 8 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 9 | 10 |
| 10 namespace media { | 11 namespace media { |
| 11 namespace cast { | 12 namespace cast { |
| 12 namespace transport { | 13 namespace transport { |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 15 static const int64 kPacingIntervalMs = 10; | 16 static const int64 kPacingIntervalMs = 10; |
| 16 // Each frame will be split into no more than kPacingMaxBurstsPerFrame | 17 // Each frame will be split into no more than kPacingMaxBurstsPerFrame |
| 17 // bursts of packets. | 18 // bursts of packets. |
| 18 static const size_t kPacingMaxBurstsPerFrame = 3; | 19 static const size_t kPacingMaxBurstsPerFrame = 3; |
| 19 | 20 |
| 21 using media::cast::CastLoggingEvent; |
| 22 |
| 23 CastLoggingEvent GetLoggingEvent(bool is_audio, bool retransmit) { |
| 24 if (retransmit) { |
| 25 return is_audio ? media::cast::kAudioPacketRetransmitted |
| 26 : media::cast::kVideoPacketRetransmitted; |
| 27 } else { |
| 28 return is_audio ? media::cast::kAudioPacketSentToNetwork |
| 29 : media::cast::kVideoPacketSentToNetwork; |
| 30 } |
| 31 } |
| 32 |
| 20 } // namespace | 33 } // namespace |
| 21 | 34 |
| 22 PacedSender::PacedSender( | 35 PacedSender::PacedSender( |
| 23 base::TickClock* clock, | 36 base::TickClock* clock, |
| 37 LoggingImpl* logging, |
| 24 PacketSender* transport, | 38 PacketSender* transport, |
| 25 const scoped_refptr<base::SingleThreadTaskRunner>& transport_task_runner) | 39 const scoped_refptr<base::SingleThreadTaskRunner>& transport_task_runner) |
| 26 : clock_(clock), | 40 : clock_(clock), |
| 41 logging_(logging), |
| 27 transport_(transport), | 42 transport_(transport), |
| 28 transport_task_runner_(transport_task_runner), | 43 transport_task_runner_(transport_task_runner), |
| 44 audio_ssrc_(0), |
| 45 video_ssrc_(0), |
| 29 burst_size_(1), | 46 burst_size_(1), |
| 30 packets_sent_in_burst_(0), | 47 packets_sent_in_burst_(0), |
| 31 weak_factory_(this) { | 48 weak_factory_(this) { |
| 32 ScheduleNextSend(); | 49 ScheduleNextSend(); |
| 33 } | 50 } |
| 34 | 51 |
| 35 PacedSender::~PacedSender() {} | 52 PacedSender::~PacedSender() {} |
| 36 | 53 |
| 54 void PacedSender::InitializeAudio(const CastTransportAudioConfig& config) { |
| 55 audio_ssrc_ = config.base.ssrc; |
| 56 } |
| 57 |
| 58 void PacedSender::InitializeVideo(const CastTransportVideoConfig& config) { |
| 59 video_ssrc_ = config.base.ssrc; |
| 60 } |
| 61 |
| 37 bool PacedSender::SendPackets(const PacketList& packets) { | 62 bool PacedSender::SendPackets(const PacketList& packets) { |
| 38 return SendPacketsToTransport(packets, &packet_list_); | 63 return SendPacketsToTransport(packets, &packet_list_, false); |
| 39 } | 64 } |
| 40 | 65 |
| 41 bool PacedSender::ResendPackets(const PacketList& packets) { | 66 bool PacedSender::ResendPackets(const PacketList& packets) { |
| 42 return SendPacketsToTransport(packets, &resend_packet_list_); | 67 return SendPacketsToTransport(packets, &resend_packet_list_, true); |
| 43 } | 68 } |
| 44 | 69 |
| 45 bool PacedSender::SendPacketsToTransport(const PacketList& packets, | 70 bool PacedSender::SendPacketsToTransport(const PacketList& packets, |
| 46 PacketList* packets_not_sent) { | 71 PacketList* packets_not_sent, |
| 72 bool retransmit) { |
| 47 UpdateBurstSize(packets.size()); | 73 UpdateBurstSize(packets.size()); |
| 48 | 74 |
| 49 if (!packets_not_sent->empty()) { | 75 if (!packets_not_sent->empty()) { |
| 50 packets_not_sent->insert( | 76 packets_not_sent->insert( |
| 51 packets_not_sent->end(), packets.begin(), packets.end()); | 77 packets_not_sent->end(), packets.begin(), packets.end()); |
| 52 return true; | 78 return true; |
| 53 } | 79 } |
| 54 | 80 |
| 55 PacketList packets_to_send; | 81 PacketList packets_to_send; |
| 56 PacketList::const_iterator first_to_store_it = packets.begin(); | 82 PacketList::const_iterator first_to_store_it = packets.begin(); |
| 57 | 83 |
| 58 size_t max_packets_to_send_now = burst_size_ - packets_sent_in_burst_; | 84 size_t max_packets_to_send_now = burst_size_ - packets_sent_in_burst_; |
| 59 | 85 |
| 60 if (max_packets_to_send_now > 0) { | 86 if (max_packets_to_send_now > 0) { |
| 61 size_t packets_to_send_now = | 87 size_t packets_to_send_now = |
| 62 std::min(max_packets_to_send_now, packets.size()); | 88 std::min(max_packets_to_send_now, packets.size()); |
| 63 | 89 |
| 64 std::advance(first_to_store_it, packets_to_send_now); | 90 std::advance(first_to_store_it, packets_to_send_now); |
| 65 packets_to_send.insert( | 91 packets_to_send.insert( |
| 66 packets_to_send.begin(), packets.begin(), first_to_store_it); | 92 packets_to_send.begin(), packets.begin(), first_to_store_it); |
| 67 } | 93 } |
| 68 packets_not_sent->insert( | 94 packets_not_sent->insert( |
| 69 packets_not_sent->end(), first_to_store_it, packets.end()); | 95 packets_not_sent->end(), first_to_store_it, packets.end()); |
| 70 packets_sent_in_burst_ = packets_to_send.size(); | 96 packets_sent_in_burst_ = packets_to_send.size(); |
| 71 if (packets_to_send.empty()) | 97 if (packets_to_send.empty()) |
| 72 return true; | 98 return true; |
| 73 | 99 |
| 100 // All packets are either audio or video; check first packet only. |
| 101 bool is_audio = IsAudioPacket(packets_to_send.front()); |
| 102 CastLoggingEvent event = GetLoggingEvent(is_audio, retransmit); |
| 103 LogPacketEvents(packets_to_send, event); |
| 104 |
| 74 return TransmitPackets(packets_to_send); | 105 return TransmitPackets(packets_to_send); |
| 75 } | 106 } |
| 76 | 107 |
| 77 bool PacedSender::SendRtcpPacket(const Packet& packet) { | 108 bool PacedSender::SendRtcpPacket(const Packet& packet) { |
| 78 // We pass the RTCP packets straight through. | 109 // We pass the RTCP packets straight through. |
| 79 return transport_->SendPacket(packet); | 110 return transport_->SendPacket(packet); |
| 80 } | 111 } |
| 81 | 112 |
| 82 void PacedSender::ScheduleNextSend() { | 113 void PacedSender::ScheduleNextSend() { |
| 83 base::TimeDelta time_to_next = | 114 base::TimeDelta time_to_next = |
| (...skipping 20 matching lines...) Expand all Loading... |
| 104 | 135 |
| 105 size_t packets_to_send = burst_size_; | 136 size_t packets_to_send = burst_size_; |
| 106 PacketList packets_to_resend; | 137 PacketList packets_to_resend; |
| 107 | 138 |
| 108 // Send our re-send packets first. | 139 // Send our re-send packets first. |
| 109 if (!resend_packet_list_.empty()) { | 140 if (!resend_packet_list_.empty()) { |
| 110 PacketList::iterator it = resend_packet_list_.begin(); | 141 PacketList::iterator it = resend_packet_list_.begin(); |
| 111 size_t packets_to_send_now = | 142 size_t packets_to_send_now = |
| 112 std::min(packets_to_send, resend_packet_list_.size()); | 143 std::min(packets_to_send, resend_packet_list_.size()); |
| 113 std::advance(it, packets_to_send_now); | 144 std::advance(it, packets_to_send_now); |
| 145 |
| 146 for (PacketList::iterator log_it = resend_packet_list_.begin(); |
| 147 log_it != it; |
| 148 ++log_it) { |
| 149 LogPacketEvent(*it, true); |
| 150 } |
| 114 packets_to_resend.insert( | 151 packets_to_resend.insert( |
| 115 packets_to_resend.begin(), resend_packet_list_.begin(), it); | 152 packets_to_resend.begin(), resend_packet_list_.begin(), it); |
| 116 resend_packet_list_.erase(resend_packet_list_.begin(), it); | 153 resend_packet_list_.erase(resend_packet_list_.begin(), it); |
| 117 packets_to_send -= packets_to_resend.size(); | 154 packets_to_send -= packets_to_resend.size(); |
| 118 } | 155 } |
| 119 if (!packet_list_.empty() && packets_to_send > 0) { | 156 if (!packet_list_.empty() && packets_to_send > 0) { |
| 120 PacketList::iterator it = packet_list_.begin(); | 157 PacketList::iterator it = packet_list_.begin(); |
| 121 size_t packets_to_send_now = std::min(packets_to_send, packet_list_.size()); | 158 size_t packets_to_send_now = std::min(packets_to_send, packet_list_.size()); |
| 159 std::advance(it, packets_to_send_now); |
| 122 | 160 |
| 123 std::advance(it, packets_to_send_now); | 161 for (PacketList::iterator log_it = packet_list_.begin(); log_it != it; |
| 162 ++log_it) { |
| 163 LogPacketEvent(*it, true); |
| 164 } |
| 165 |
| 124 packets_to_resend.insert(packets_to_resend.end(), packet_list_.begin(), it); | 166 packets_to_resend.insert(packets_to_resend.end(), packet_list_.begin(), it); |
| 125 packet_list_.erase(packet_list_.begin(), it); | 167 packet_list_.erase(packet_list_.begin(), it); |
| 126 | 168 |
| 127 if (packet_list_.empty()) { | 169 if (packet_list_.empty()) { |
| 128 burst_size_ = 1; // Reset burst size after we sent the last stored packet | 170 burst_size_ = 1; // Reset burst size after we sent the last stored packet |
| 129 packets_sent_in_burst_ = 0; | 171 packets_sent_in_burst_ = 0; |
| 130 } else { | 172 } else { |
| 131 packets_sent_in_burst_ = packets_to_resend.size(); | 173 packets_sent_in_burst_ = packets_to_resend.size(); |
| 132 } | 174 } |
| 133 } | 175 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 144 | 186 |
| 145 void PacedSender::UpdateBurstSize(size_t packets_to_send) { | 187 void PacedSender::UpdateBurstSize(size_t packets_to_send) { |
| 146 packets_to_send = std::max(packets_to_send, | 188 packets_to_send = std::max(packets_to_send, |
| 147 resend_packet_list_.size() + packet_list_.size()); | 189 resend_packet_list_.size() + packet_list_.size()); |
| 148 | 190 |
| 149 packets_to_send += (kPacingMaxBurstsPerFrame - 1); // Round up. | 191 packets_to_send += (kPacingMaxBurstsPerFrame - 1); // Round up. |
| 150 burst_size_ = | 192 burst_size_ = |
| 151 std::max(packets_to_send / kPacingMaxBurstsPerFrame, burst_size_); | 193 std::max(packets_to_send / kPacingMaxBurstsPerFrame, burst_size_); |
| 152 } | 194 } |
| 153 | 195 |
| 196 void PacedSender::LogPacketEvents(const PacketList& packet_list, |
| 197 CastLoggingEvent event) { |
| 198 logging_->InsertPacketListEvent(clock_->NowTicks(), event, packet_list); |
| 199 } |
| 200 |
| 201 void PacedSender::LogPacketEvent(const Packet& packet, bool retransmit) { |
| 202 CastLoggingEvent event = GetLoggingEvent(IsAudioPacket(packet), retransmit); |
| 203 |
| 204 logging_->InsertSinglePacketEvent(clock_->NowTicks(), event, packet); |
| 205 } |
| 206 |
| 207 bool PacedSender::IsAudioPacket(const Packet& packet) { |
| 208 // Get SSRC from packet and compare with the audio_ssrc / video_ssrc to see |
| 209 // if the packet is audio or video. |
| 210 base::BigEndianReader reader(reinterpret_cast<const char*>(packet[0]), 10); |
| 211 reader.Skip(8); |
| 212 uint32 ssrc; |
| 213 bool success = reader.ReadU32(&ssrc); |
| 214 DCHECK(success); |
| 215 if (ssrc == audio_ssrc_) { |
| 216 return true; |
| 217 } else if (ssrc == video_ssrc_) { |
| 218 return false; |
| 219 } else { |
| 220 NOTREACHED() << "Got unknown ssrc " << ssrc << " when logging packet event"; |
| 221 return false; |
| 222 } |
| 223 } |
| 224 |
| 154 } // namespace transport | 225 } // namespace transport |
| 155 } // namespace cast | 226 } // namespace cast |
| 156 } // namespace media | 227 } // namespace media |
| OLD | NEW |