 Chromium Code Reviews
 Chromium Code Reviews Issue 1247293002:
  Add support for transport wide sequence numbers  (Closed) 
  Base URL: https://chromium.googlesource.com/external/webrtc.git@master
    
  
    Issue 1247293002:
  Add support for transport wide sequence numbers  (Closed) 
  Base URL: https://chromium.googlesource.com/external/webrtc.git@master| 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(&modules_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(&modules_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::AcquireLoad(&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 rtc::CritScope cs(&modules_lock_); | 
| 44 for (auto* rtp_module : rtp_modules_) { | 75 UpdateModuleMap(); | 
| 45 if (rtp_module->SendingMedia() && ssrc == rtp_module->SSRC()) { | 76 auto it = rtp_modules_.find(ssrc); | 
| 46 return rtp_module->TimeToSendPacket(ssrc, sequence_number, | 77 if (it == rtp_modules_.end()) | 
| 47 capture_timestamp, retransmission); | 78 return true; | 
| 79 RtpRtcp* rtp_module = it->second; | |
| 80 | |
| 81 if (!rtp_module || !rtp_module->SendingMedia()) | |
| 82 return true; | |
| 83 | |
| 84 return rtp_module->TimeToSendPacket(ssrc, sequence_number, capture_timestamp, | |
| 85 retransmission); | |
| 86 } | |
| 87 | |
| 88 size_t PacketRouter::TimeToSendPadding(size_t bytes_to_send) { | |
| 89 size_t total_bytes_sent = 0; | |
| 90 rtc::CritScope cs(&modules_lock_); | |
| 91 for (auto it : rtp_modules_) { | |
| 92 if (it.second->SendingMedia()) { | |
| 93 size_t bytes_sent = | |
| 94 it.second->TimeToSendPadding(bytes_to_send - total_bytes_sent); | |
| 95 total_bytes_sent += bytes_sent; | |
| 96 if (total_bytes_sent >= bytes_to_send) | |
| 97 break; | |
| 48 } | 98 } | 
| 49 } | 99 } | 
| 50 return true; | 100 return total_bytes_sent; | 
| 51 } | 101 } | 
| 52 | 102 | 
| 53 size_t PacketRouter::TimeToSendPadding(size_t bytes) { | 103 void PacketRouter::EnableTransportWideFeedback() { | 
| 54 CriticalSectionScoped cs(crit_.get()); | 104 if (transport_wide_seq_enabled_) | 
| 55 for (auto* rtp_module : rtp_modules_) { | 105 return; | 
| 56 if (rtp_module->SendingMedia()) | 106 | 
| 57 return rtp_module->TimeToSendPadding(bytes); | 107 rtc::CritScope cs(&history_lock_); | 
| 108 send_time_history_.reset(new SendTimeHistory(kSendTimeHistoryWindowMs)); | |
| 109 transport_wide_seq_enabled_ = true; | |
| 110 } | |
| 111 | |
| 112 void PacketRouter::SetTransportWideSequenceNumber(uint16_t sequence_number) { | |
| 113 if (!transport_wide_seq_enabled_) | |
| 114 return; | |
| 115 | |
| 116 rtc::AtomicOps::ReleaseStore(&transport_seq_, sequence_number); | |
| 117 } | |
| 118 | |
| 119 size_t PacketRouter::PopulateSendTimes(std::vector<PacketInfo>* packet_info) { | |
| 120 if (!transport_wide_seq_enabled_) | |
| 121 return 0; | |
| 122 | |
| 123 size_t successful_lookups = 0; | |
| 124 rtc::CritScope cs(&history_lock_); | |
| 125 for (PacketInfo& info : *packet_info) { | |
| 126 if (send_time_history_->GetSendTime(info.sequence_number, | |
| 127 &info.send_time_ms, true)) { | |
| 128 ++successful_lookups; | |
| 129 } | |
| 58 } | 130 } | 
| 59 return 0; | 131 return successful_lookups; | 
| 60 } | 132 } | 
| 133 | |
| 134 uint16_t PacketRouter::AllocateSequenceNumber() { | |
| 135 if (!transport_wide_seq_enabled_) | |
| 136 return 0; | |
| 137 | |
| 138 int prev_seq; | |
| 139 int new_seq; | |
| 140 do { | |
| 
stefan-webrtc
2015/07/27 12:13:49
I think we should comment on this code. I have rea
 
sprang_webrtc
2015/07/28 13:29:27
Added comment and also reformulated code a bit.
 | |
| 141 prev_seq = rtc::AtomicOps::AcquireLoad(&transport_seq_); | |
| 142 new_seq = (prev_seq + 1) & 0xFFFF; | |
| 143 } while (rtc::AtomicOps::CompareAndSwap(&transport_seq_, prev_seq, new_seq) != | |
| 144 prev_seq); | |
| 145 | |
| 146 return new_seq; | |
| 147 } | |
| 148 | |
| 149 void PacketRouter::OnPacketSent(uint16_t sequence_number, int64_t send_time) { | |
| 150 if (!transport_wide_seq_enabled_) | |
| 151 return; | |
| 152 | |
| 153 rtc::CritScope cs(&history_lock_); | |
| 154 send_time_history_->AddAndRemoveOldSendTimes(sequence_number, send_time); | |
| 155 } | |
| 156 | |
| 61 } // namespace webrtc | 157 } // namespace webrtc | 
| OLD | NEW |