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() : 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 DCHECK(std::find(rtp_modules_.begin(), rtp_modules_.end(), rtp_module) == |
30 rtp_modules_.end()); | 30 rtp_modules_.end()); |
31 rtp_modules_.push_back(rtp_module); | 31 rtp_modules_.push_back(rtp_module); |
32 } | 32 } |
33 | 33 |
34 void PacketRouter::RemoveRtpModule(RtpRtcp* rtp_module) { | 34 void PacketRouter::RemoveRtpModule(RtpRtcp* rtp_module) { |
35 CriticalSectionScoped cs(crit_.get()); | 35 rtc::CritScope cs(&modules_lock_); |
36 rtp_modules_.remove(rtp_module); | 36 auto it = std::find(rtp_modules_.begin(), rtp_modules_.end(), rtp_module); |
37 DCHECK(it != rtp_modules_.end()); | |
38 rtp_modules_.erase(it); | |
stefan-webrtc
2015/07/29 12:19:52
rtp_modules_.remove(rtp_module); as before?
sprang_webrtc
2015/07/29 12:27:27
erase() will directly remove that element from the
| |
37 } | 39 } |
38 | 40 |
39 bool PacketRouter::TimeToSendPacket(uint32_t ssrc, | 41 bool PacketRouter::TimeToSendPacket(uint32_t ssrc, |
40 uint16_t sequence_number, | 42 uint16_t sequence_number, |
41 int64_t capture_timestamp, | 43 int64_t capture_timestamp, |
42 bool retransmission) { | 44 bool retransmission) { |
43 CriticalSectionScoped cs(crit_.get()); | 45 rtc::CritScope cs(&modules_lock_); |
44 for (auto* rtp_module : rtp_modules_) { | 46 for (auto* rtp_module : rtp_modules_) { |
45 if (rtp_module->SendingMedia() && ssrc == rtp_module->SSRC()) { | 47 if (rtp_module->SendingMedia() && ssrc == rtp_module->SSRC()) { |
46 return rtp_module->TimeToSendPacket(ssrc, sequence_number, | 48 return rtp_module->TimeToSendPacket(ssrc, sequence_number, |
47 capture_timestamp, retransmission); | 49 capture_timestamp, retransmission); |
48 } | 50 } |
49 } | 51 } |
50 return true; | 52 return true; |
51 } | 53 } |
52 | 54 |
53 size_t PacketRouter::TimeToSendPadding(size_t bytes) { | 55 size_t PacketRouter::TimeToSendPadding(size_t bytes_to_send) { |
54 CriticalSectionScoped cs(crit_.get()); | 56 size_t total_bytes_sent = 0; |
55 for (auto* rtp_module : rtp_modules_) { | 57 rtc::CritScope cs(&modules_lock_); |
56 if (rtp_module->SendingMedia()) | 58 for (RtpRtcp* module : rtp_modules_) { |
57 return rtp_module->TimeToSendPadding(bytes); | 59 if (module->SendingMedia()) { |
60 size_t bytes_sent = | |
61 module->TimeToSendPadding(bytes_to_send - total_bytes_sent); | |
62 total_bytes_sent += bytes_sent; | |
63 if (total_bytes_sent >= bytes_to_send) | |
64 break; | |
65 } | |
58 } | 66 } |
59 return 0; | 67 return total_bytes_sent; |
60 } | 68 } |
69 | |
70 void PacketRouter::SetTransportWideSequenceNumber(uint16_t sequence_number) { | |
71 rtc::AtomicOps::ReleaseStore(&transport_seq_, sequence_number); | |
72 } | |
73 | |
74 uint16_t PacketRouter::AllocateSequenceNumber() { | |
75 int prev_seq = rtc::AtomicOps::AcquireLoad(&transport_seq_); | |
76 int desired_prev_seq; | |
77 int new_seq; | |
78 do { | |
79 desired_prev_seq = prev_seq; | |
80 new_seq = (desired_prev_seq + 1) & 0xFFFF; | |
81 // Note: CompareAndSwap returns the actual value of transport_seq at the | |
82 // time the CAS operation was executed. Thus, if prev_seq is returned, the | |
83 // operation was successful - otherwise we need to retry. Saving the | |
84 // return value saves us a load on retry. | |
85 prev_seq = rtc::AtomicOps::CompareAndSwap(&transport_seq_, desired_prev_seq, | |
86 new_seq); | |
87 } while (prev_seq != desired_prev_seq); | |
88 | |
89 return new_seq; | |
90 } | |
91 | |
61 } // namespace webrtc | 92 } // namespace webrtc |
OLD | NEW |