Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/modules/pacing/include/packet_router.h" | 11 #include "webrtc/modules/pacing/include/packet_router.h" |
| 12 | 12 |
| 13 #include "webrtc/base/atomicops.h" | |
| 13 #include "webrtc/base/checks.h" | 14 #include "webrtc/base/checks.h" |
| 14 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h" | 15 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h" |
| 15 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h" | 16 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h" |
| 16 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" | |
| 17 | 17 |
| 18 namespace webrtc { | 18 namespace webrtc { |
| 19 | 19 |
| 20 static const int64_t kSendTimeHistoryWindowMs = 2000; | |
| 21 | |
| 20 PacketRouter::PacketRouter() | 22 PacketRouter::PacketRouter() |
| 21 : crit_(CriticalSectionWrapper::CreateCriticalSection()) { | 23 : dirty_map_(0), transport_wide_seq_enabled_(false), transport_seq_(0) { |
| 22 } | 24 } |
| 23 | 25 |
| 24 PacketRouter::~PacketRouter() { | 26 PacketRouter::~PacketRouter() { |
| 27 DCHECK(rtp_modules_.empty()); | |
| 25 } | 28 } |
| 26 | 29 |
| 27 void PacketRouter::AddRtpModule(RtpRtcp* rtp_module) { | 30 void PacketRouter::AddRtpModule(RtpRtcp* rtp_module) { |
| 28 CriticalSectionScoped cs(crit_.get()); | 31 rtc::CritScope cs(&ssrc_lookup_lock_); |
| 29 DCHECK(std::find(rtp_modules_.begin(), rtp_modules_.end(), rtp_module) == | 32 UpdateModuleMap(); |
| 30 rtp_modules_.end()); | 33 uint32_t ssrc = rtp_module->SSRC(); |
| 31 rtp_modules_.push_back(rtp_module); | 34 DCHECK(rtp_modules_.find(ssrc) == rtp_modules_.end()); |
| 35 rtp_modules_[ssrc] = rtp_module; | |
| 32 } | 36 } |
| 33 | 37 |
| 34 void PacketRouter::RemoveRtpModule(RtpRtcp* rtp_module) { | 38 void PacketRouter::RemoveRtpModule(RtpRtcp* rtp_module) { |
| 35 CriticalSectionScoped cs(crit_.get()); | 39 rtc::CritScope cs(&ssrc_lookup_lock_); |
| 36 rtp_modules_.remove(rtp_module); | 40 UpdateModuleMap(); |
| 41 auto it = rtp_modules_.find(rtp_module->SSRC()); | |
| 42 DCHECK(it != rtp_modules_.end()); | |
| 43 rtp_modules_.erase(it); | |
| 44 } | |
| 45 | |
| 46 void PacketRouter::OnSsrcChanged() { | |
| 47 // Just flag module map as dirty, to avoid taking the ssrc_lookup_lock and | |
| 48 // cause potential lock order inversions. | |
| 49 rtc::AtomicOps::Increment(&dirty_map_); | |
| 50 } | |
| 51 | |
| 52 void PacketRouter::UpdateModuleMap() { | |
| 53 int dirty; | |
| 54 do { | |
| 55 // Load atomic flag and return immediately if not dirty. | |
| 56 dirty = rtc::AtomicOps::Load(&dirty_map_); | |
| 57 if (dirty <= 0) | |
| 58 return; | |
| 59 | |
| 60 // Map was dirty, re-map all modules. | |
| 61 std::map<uint32_t, RtpRtcp*> updated_map; | |
| 62 for (auto it : rtp_modules_) | |
| 63 updated_map[it.second->SSRC()] = it.second; | |
| 64 rtp_modules_ = updated_map; | |
| 65 | |
| 66 // If dirty-flag was concurrently set again, we need to make another loop. | |
| 67 } while (!rtc::AtomicOps::CompareAndSwap(&dirty_map_, dirty, 0)); | |
| 37 } | 68 } |
| 38 | 69 |
| 39 bool PacketRouter::TimeToSendPacket(uint32_t ssrc, | 70 bool PacketRouter::TimeToSendPacket(uint32_t ssrc, |
| 40 uint16_t sequence_number, | 71 uint16_t sequence_number, |
| 41 int64_t capture_timestamp, | 72 int64_t capture_timestamp, |
| 42 bool retransmission) { | 73 bool retransmission) { |
| 43 CriticalSectionScoped cs(crit_.get()); | 74 RtpRtcp* rtp_module = nullptr; |
| 44 for (auto* rtp_module : rtp_modules_) { | 75 { |
| 45 if (rtp_module->SendingMedia() && ssrc == rtp_module->SSRC()) { | 76 rtc::CritScope cs(&ssrc_lookup_lock_); |
| 46 return rtp_module->TimeToSendPacket(ssrc, sequence_number, | 77 UpdateModuleMap(); |
| 47 capture_timestamp, retransmission); | 78 auto it = rtp_modules_.find(ssrc); |
| 79 if (it == rtp_modules_.end()) | |
| 80 return true; | |
| 81 rtp_module = it->second; | |
| 82 } | |
| 83 | |
| 84 if (!rtp_module || !rtp_module->SendingMedia()) | |
| 85 return true; | |
| 86 | |
| 87 return rtp_module->TimeToSendPacket(ssrc, sequence_number, capture_timestamp, | |
| 88 retransmission); | |
| 89 } | |
| 90 | |
| 91 size_t PacketRouter::TimeToSendPadding(size_t bytes_to_send) { | |
| 92 size_t total_bytes_sent = 0; | |
| 93 std::vector<RtpRtcp*> modules; | |
| 94 { | |
| 95 rtc::CritScope cs(&ssrc_lookup_lock_); | |
| 96 for (auto it : rtp_modules_) | |
| 97 modules.push_back(it.second); | |
| 98 } | |
| 99 for (auto it : modules) { | |
| 100 if (it->SendingMedia()) { | |
| 101 size_t bytes_sent = | |
| 102 it->TimeToSendPadding(bytes_to_send - total_bytes_sent); | |
| 103 if (bytes_sent > 0) | |
| 104 ++transport_seq_; | |
|
stefan-webrtc
2015/07/22 10:49:00
This isn't being used anywhere? Seems like we shou
sprang_webrtc
2015/07/22 15:11:32
That's a bug, that my test case evidently didn't c
stefan-webrtc
2015/07/27 12:13:49
Acknowledged.
| |
| 105 total_bytes_sent += bytes_sent; | |
| 106 if (total_bytes_sent >= bytes_to_send) | |
| 107 break; | |
| 48 } | 108 } |
| 49 } | 109 } |
| 50 return true; | 110 return total_bytes_sent; |
| 51 } | 111 } |
| 52 | 112 |
| 53 size_t PacketRouter::TimeToSendPadding(size_t bytes) { | 113 void PacketRouter::EnableTransportWideFeedback() { |
| 54 CriticalSectionScoped cs(crit_.get()); | 114 if (transport_wide_seq_enabled_) |
| 55 for (auto* rtp_module : rtp_modules_) { | 115 return; |
| 56 if (rtp_module->SendingMedia()) | 116 |
| 57 return rtp_module->TimeToSendPadding(bytes); | 117 rtc::CritScope cs(&history_lock_); |
| 118 send_time_history_.reset(new SendTimeHistory(kSendTimeHistoryWindowMs)); | |
| 119 transport_wide_seq_enabled_ = true; | |
| 120 } | |
| 121 | |
| 122 void PacketRouter::SetTransportWideSequenceNumber(uint16_t sequence_number) { | |
| 123 if (!transport_wide_seq_enabled_) | |
| 124 return; | |
| 125 | |
| 126 rtc::AtomicOps::Store(&transport_seq_, sequence_number); | |
| 127 } | |
| 128 | |
| 129 size_t PacketRouter::PopulateSendTimes(std::vector<PacketInfo>* packet_info) { | |
| 130 if (!transport_wide_seq_enabled_) | |
| 131 return 0; | |
| 132 | |
| 133 size_t successful_lookups = 0; | |
| 134 rtc::CritScope cs(&history_lock_); | |
| 135 for (PacketInfo& info : *packet_info) { | |
| 136 if (send_time_history_->GetSendTime(info.sequence_number, | |
| 137 &info.send_time_ms, true)) { | |
| 138 ++successful_lookups; | |
| 139 } | |
| 58 } | 140 } |
| 59 return 0; | 141 return successful_lookups; |
| 60 } | 142 } |
| 143 | |
| 144 uint16_t PacketRouter::AllocateSequenceNumber() { | |
| 145 if (!transport_wide_seq_enabled_) | |
| 146 return 0; | |
| 147 | |
| 148 return static_cast<uint16_t>(rtc::AtomicOps::Increment(&transport_seq_) & | |
| 149 0xFFFF); | |
|
stefan-webrtc
2015/07/22 10:49:00
Isn't this implicit from the cast?
sprang_webrtc
2015/07/22 15:11:32
The static_cast, yes. This was a remnant from when
| |
| 150 } | |
| 151 | |
| 152 void PacketRouter::OnPacketSent(uint16_t sequence_number, int64_t send_time) { | |
| 153 if (!transport_wide_seq_enabled_) | |
| 154 return; | |
| 155 | |
| 156 rtc::CritScope cs(&history_lock_); | |
| 157 send_time_history_->AddAndRemoveOldSendTimes(sequence_number, send_time); | |
| 158 } | |
| 159 | |
| 61 } // namespace webrtc | 160 } // namespace webrtc |
| OLD | NEW |