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 PacketRouter::PacketRouter() | 20 PacketRouter::PacketRouter() : dirty_map_(0), transport_seq_(0) { |
21 : crit_(CriticalSectionWrapper::CreateCriticalSection()) { | |
22 } | 21 } |
23 | 22 |
24 PacketRouter::~PacketRouter() { | 23 PacketRouter::~PacketRouter() { |
24 DCHECK(rtp_modules_.empty()); | |
25 } | 25 } |
26 | 26 |
27 void PacketRouter::AddRtpModule(RtpRtcp* rtp_module) { | 27 void PacketRouter::AddRtpModule(RtpRtcp* rtp_module) { |
28 CriticalSectionScoped cs(crit_.get()); | 28 rtc::CritScope cs(&modules_lock_); |
29 DCHECK(std::find(rtp_modules_.begin(), rtp_modules_.end(), rtp_module) == | 29 UpdateModuleMap(); |
30 rtp_modules_.end()); | 30 uint32_t ssrc = rtp_module->SSRC(); |
31 rtp_modules_.push_back(rtp_module); | 31 DCHECK(rtp_modules_.find(ssrc) == rtp_modules_.end()); |
32 rtp_modules_[ssrc] = rtp_module; | |
32 } | 33 } |
33 | 34 |
34 void PacketRouter::RemoveRtpModule(RtpRtcp* rtp_module) { | 35 void PacketRouter::RemoveRtpModule(RtpRtcp* rtp_module) { |
35 CriticalSectionScoped cs(crit_.get()); | 36 rtc::CritScope cs(&modules_lock_); |
36 rtp_modules_.remove(rtp_module); | 37 UpdateModuleMap(); |
38 auto it = rtp_modules_.find(rtp_module->SSRC()); | |
39 DCHECK(it != rtp_modules_.end()); | |
40 rtp_modules_.erase(it); | |
41 } | |
42 | |
43 void PacketRouter::OnSsrcChanged() { | |
44 // Just flag module map as dirty, to avoid taking the ssrc_lookup_lock and | |
45 // cause potential lock order inversions. | |
46 rtc::AtomicOps::Increment(&dirty_map_); | |
47 } | |
48 | |
49 void PacketRouter::UpdateModuleMap() { | |
50 int dirty; | |
51 do { | |
52 // Load atomic flag and return immediately if not dirty. | |
53 dirty = rtc::AtomicOps::AcquireLoad(&dirty_map_); | |
54 if (dirty <= 0) | |
55 return; | |
56 | |
57 // Map was dirty, re-map all modules. | |
58 std::map<uint32_t, RtpRtcp*> updated_map; | |
59 for (auto it : rtp_modules_) | |
60 updated_map[it.second->SSRC()] = it.second; | |
61 rtp_modules_ = updated_map; | |
62 | |
63 // If dirty-flag was concurrently set again, we need to make another loop. | |
64 } while (!rtc::AtomicOps::CompareAndSwap(&dirty_map_, dirty, 0)); | |
37 } | 65 } |
38 | 66 |
39 bool PacketRouter::TimeToSendPacket(uint32_t ssrc, | 67 bool PacketRouter::TimeToSendPacket(uint32_t ssrc, |
40 uint16_t sequence_number, | 68 uint16_t sequence_number, |
41 int64_t capture_timestamp, | 69 int64_t capture_timestamp, |
42 bool retransmission) { | 70 bool retransmission) { |
43 CriticalSectionScoped cs(crit_.get()); | 71 rtc::CritScope cs(&modules_lock_); |
44 for (auto* rtp_module : rtp_modules_) { | 72 UpdateModuleMap(); |
45 if (rtp_module->SendingMedia() && ssrc == rtp_module->SSRC()) { | 73 auto it = rtp_modules_.find(ssrc); |
46 return rtp_module->TimeToSendPacket(ssrc, sequence_number, | 74 if (it == rtp_modules_.end()) |
47 capture_timestamp, retransmission); | 75 return true; |
76 RtpRtcp* rtp_module = it->second; | |
77 | |
78 if (!rtp_module || !rtp_module->SendingMedia()) | |
79 return true; | |
80 | |
81 return rtp_module->TimeToSendPacket(ssrc, sequence_number, capture_timestamp, | |
82 retransmission); | |
83 } | |
stefan-webrtc
2015/07/29 09:04:11
I'd say you could argue whether the code actually
sprang_webrtc
2015/07/29 10:03:25
Especially after it turned out that we actually ne
| |
84 | |
85 size_t PacketRouter::TimeToSendPadding(size_t bytes_to_send) { | |
86 size_t total_bytes_sent = 0; | |
87 rtc::CritScope cs(&modules_lock_); | |
88 for (auto it : rtp_modules_) { | |
89 if (it.second->SendingMedia()) { | |
90 size_t bytes_sent = | |
91 it.second->TimeToSendPadding(bytes_to_send - total_bytes_sent); | |
92 total_bytes_sent += bytes_sent; | |
93 if (total_bytes_sent >= bytes_to_send) | |
94 break; | |
48 } | 95 } |
49 } | 96 } |
50 return true; | 97 return total_bytes_sent; |
51 } | 98 } |
52 | 99 |
53 size_t PacketRouter::TimeToSendPadding(size_t bytes) { | 100 void PacketRouter::SetTransportWideSequenceNumber(uint16_t sequence_number) { |
54 CriticalSectionScoped cs(crit_.get()); | 101 rtc::AtomicOps::ReleaseStore(&transport_seq_, sequence_number); |
55 for (auto* rtp_module : rtp_modules_) { | |
56 if (rtp_module->SendingMedia()) | |
57 return rtp_module->TimeToSendPadding(bytes); | |
58 } | |
59 return 0; | |
60 } | 102 } |
103 | |
104 uint16_t PacketRouter::AllocateSequenceNumber() { | |
105 int prev_seq = rtc::AtomicOps::AcquireLoad(&transport_seq_); | |
106 int desired_prev_seq; | |
107 int new_seq; | |
108 do { | |
109 desired_prev_seq = prev_seq; | |
110 new_seq = (desired_prev_seq + 1) & 0xFFFF; | |
111 // Note: CompareAndSwap returns the actual value of transport_seq at the | |
112 // time the CAS operation was executed. Thus, if prev_seq is returned, the | |
113 // operation was successful - otherwise we need to retry. Saving the | |
114 // return value saves us a load on retry. | |
115 prev_seq = rtc::AtomicOps::CompareAndSwap(&transport_seq_, desired_prev_seq, | |
116 new_seq); | |
117 } while (prev_seq != desired_prev_seq); | |
118 | |
119 return new_seq; | |
120 } | |
121 | |
61 } // namespace webrtc | 122 } // namespace webrtc |
OLD | NEW |