Line data Source code
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/packet_router.h"
12 :
13 : #include "webrtc/base/atomicops.h"
14 : #include "webrtc/base/checks.h"
15 : #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h"
16 : #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
17 : #include "webrtc/modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
18 :
19 : namespace webrtc {
20 :
21 0 : PacketRouter::PacketRouter() : transport_seq_(0) {
22 0 : pacer_thread_checker_.DetachFromThread();
23 0 : }
24 :
25 0 : PacketRouter::~PacketRouter() {
26 0 : RTC_DCHECK(rtp_modules_.empty());
27 0 : }
28 :
29 0 : void PacketRouter::AddRtpModule(RtpRtcp* rtp_module) {
30 0 : rtc::CritScope cs(&modules_crit_);
31 0 : RTC_DCHECK(std::find(rtp_modules_.begin(), rtp_modules_.end(), rtp_module) ==
32 0 : rtp_modules_.end());
33 0 : rtp_modules_.push_back(rtp_module);
34 0 : }
35 :
36 0 : void PacketRouter::RemoveRtpModule(RtpRtcp* rtp_module) {
37 0 : rtc::CritScope cs(&modules_crit_);
38 0 : RTC_DCHECK(std::find(rtp_modules_.begin(), rtp_modules_.end(), rtp_module) !=
39 0 : rtp_modules_.end());
40 0 : rtp_modules_.remove(rtp_module);
41 0 : }
42 :
43 0 : bool PacketRouter::TimeToSendPacket(uint32_t ssrc,
44 : uint16_t sequence_number,
45 : int64_t capture_timestamp,
46 : bool retransmission,
47 : int probe_cluster_id) {
48 0 : RTC_DCHECK(pacer_thread_checker_.CalledOnValidThread());
49 0 : rtc::CritScope cs(&modules_crit_);
50 0 : for (auto* rtp_module : rtp_modules_) {
51 0 : if (!rtp_module->SendingMedia())
52 0 : continue;
53 0 : if (ssrc == rtp_module->SSRC() || ssrc == rtp_module->FlexfecSsrc()) {
54 0 : return rtp_module->TimeToSendPacket(ssrc, sequence_number,
55 : capture_timestamp, retransmission,
56 0 : probe_cluster_id);
57 : }
58 : }
59 0 : return true;
60 : }
61 :
62 0 : size_t PacketRouter::TimeToSendPadding(size_t bytes_to_send,
63 : int probe_cluster_id) {
64 0 : RTC_DCHECK(pacer_thread_checker_.CalledOnValidThread());
65 0 : size_t total_bytes_sent = 0;
66 0 : rtc::CritScope cs(&modules_crit_);
67 0 : for (RtpRtcp* module : rtp_modules_) {
68 0 : if (module->SendingMedia()) {
69 0 : size_t bytes_sent = module->TimeToSendPadding(
70 0 : bytes_to_send - total_bytes_sent, probe_cluster_id);
71 0 : total_bytes_sent += bytes_sent;
72 0 : if (total_bytes_sent >= bytes_to_send)
73 0 : break;
74 : }
75 : }
76 0 : return total_bytes_sent;
77 : }
78 :
79 0 : void PacketRouter::SetTransportWideSequenceNumber(uint16_t sequence_number) {
80 0 : rtc::AtomicOps::ReleaseStore(&transport_seq_, sequence_number);
81 0 : }
82 :
83 0 : uint16_t PacketRouter::AllocateSequenceNumber() {
84 0 : int prev_seq = rtc::AtomicOps::AcquireLoad(&transport_seq_);
85 : int desired_prev_seq;
86 : int new_seq;
87 0 : do {
88 0 : desired_prev_seq = prev_seq;
89 0 : new_seq = (desired_prev_seq + 1) & 0xFFFF;
90 : // Note: CompareAndSwap returns the actual value of transport_seq at the
91 : // time the CAS operation was executed. Thus, if prev_seq is returned, the
92 : // operation was successful - otherwise we need to retry. Saving the
93 : // return value saves us a load on retry.
94 0 : prev_seq = rtc::AtomicOps::CompareAndSwap(&transport_seq_, desired_prev_seq,
95 0 : new_seq);
96 0 : } while (prev_seq != desired_prev_seq);
97 :
98 0 : return new_seq;
99 : }
100 :
101 0 : bool PacketRouter::SendFeedback(rtcp::TransportFeedback* packet) {
102 0 : rtc::CritScope cs(&modules_crit_);
103 0 : for (auto* rtp_module : rtp_modules_) {
104 0 : packet->SetSenderSsrc(rtp_module->SSRC());
105 0 : if (rtp_module->SendFeedbackPacket(*packet))
106 0 : return true;
107 : }
108 0 : return false;
109 : }
110 :
111 : } // namespace webrtc
|