Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "webrtc/modules/pacing/include/packet_router.h" |
| 12 | |
sprang | 867fb52 | 2015-08-03 04:38:41 -0700 | [diff] [blame] | 13 | #include "webrtc/base/atomicops.h" |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 14 | #include "webrtc/base/checks.h" |
| 15 | #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h" |
| 16 | #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h" |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | |
sprang | 867fb52 | 2015-08-03 04:38:41 -0700 | [diff] [blame] | 20 | PacketRouter::PacketRouter() : transport_seq_(0) { |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | PacketRouter::~PacketRouter() { |
sprang | 867fb52 | 2015-08-03 04:38:41 -0700 | [diff] [blame] | 24 | DCHECK(rtp_modules_.empty()); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | void PacketRouter::AddRtpModule(RtpRtcp* rtp_module) { |
sprang | 867fb52 | 2015-08-03 04:38:41 -0700 | [diff] [blame] | 28 | rtc::CritScope cs(&modules_lock_); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 29 | DCHECK(std::find(rtp_modules_.begin(), rtp_modules_.end(), rtp_module) == |
| 30 | rtp_modules_.end()); |
| 31 | rtp_modules_.push_back(rtp_module); |
| 32 | } |
| 33 | |
| 34 | void PacketRouter::RemoveRtpModule(RtpRtcp* rtp_module) { |
sprang | 867fb52 | 2015-08-03 04:38:41 -0700 | [diff] [blame] | 35 | rtc::CritScope cs(&modules_lock_); |
| 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 Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | bool PacketRouter::TimeToSendPacket(uint32_t ssrc, |
| 42 | uint16_t sequence_number, |
| 43 | int64_t capture_timestamp, |
| 44 | bool retransmission) { |
sprang | 867fb52 | 2015-08-03 04:38:41 -0700 | [diff] [blame] | 45 | rtc::CritScope cs(&modules_lock_); |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 46 | for (auto* rtp_module : rtp_modules_) { |
| 47 | if (rtp_module->SendingMedia() && ssrc == rtp_module->SSRC()) { |
| 48 | return rtp_module->TimeToSendPacket(ssrc, sequence_number, |
| 49 | capture_timestamp, retransmission); |
| 50 | } |
| 51 | } |
| 52 | return true; |
| 53 | } |
| 54 | |
sprang | 867fb52 | 2015-08-03 04:38:41 -0700 | [diff] [blame] | 55 | size_t PacketRouter::TimeToSendPadding(size_t bytes_to_send) { |
| 56 | size_t total_bytes_sent = 0; |
| 57 | rtc::CritScope cs(&modules_lock_); |
| 58 | for (RtpRtcp* module : rtp_modules_) { |
| 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 | } |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 66 | } |
sprang | 867fb52 | 2015-08-03 04:38:41 -0700 | [diff] [blame] | 67 | return total_bytes_sent; |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 68 | } |
sprang | 867fb52 | 2015-08-03 04:38:41 -0700 | [diff] [blame] | 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 | |
Stefan Holmer | e590416 | 2015-03-26 11:11:06 +0100 | [diff] [blame] | 92 | } // namespace webrtc |