1 /*
2 * Copyright (c) 2017 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 "call/rtp_config.h"
12
13 #include <cstdint>
14
15 #include "absl/algorithm/container.h"
16 #include "api/array_view.h"
17 #include "rtc_base/checks.h"
18 #include "rtc_base/strings/string_builder.h"
19
20 namespace webrtc {
21
22 namespace {
23
FindAssociatedSsrc(uint32_t ssrc,const std::vector<uint32_t> & ssrcs,const std::vector<uint32_t> & associated_ssrcs)24 uint32_t FindAssociatedSsrc(uint32_t ssrc,
25 const std::vector<uint32_t>& ssrcs,
26 const std::vector<uint32_t>& associated_ssrcs) {
27 RTC_DCHECK_EQ(ssrcs.size(), associated_ssrcs.size());
28 for (size_t i = 0; i < ssrcs.size(); ++i) {
29 if (ssrcs[i] == ssrc)
30 return associated_ssrcs[i];
31 }
32 RTC_DCHECK_NOTREACHED();
33 return 0;
34 }
35
36 } // namespace
37
ToString() const38 std::string LntfConfig::ToString() const {
39 return enabled ? "{enabled: true}" : "{enabled: false}";
40 }
41
ToString() const42 std::string NackConfig::ToString() const {
43 char buf[1024];
44 rtc::SimpleStringBuilder ss(buf);
45 ss << "{rtp_history_ms: " << rtp_history_ms;
46 ss << '}';
47 return ss.str();
48 }
49
ToString() const50 std::string UlpfecConfig::ToString() const {
51 char buf[1024];
52 rtc::SimpleStringBuilder ss(buf);
53 ss << "{ulpfec_payload_type: " << ulpfec_payload_type;
54 ss << ", red_payload_type: " << red_payload_type;
55 ss << ", red_rtx_payload_type: " << red_rtx_payload_type;
56 ss << '}';
57 return ss.str();
58 }
59
operator ==(const UlpfecConfig & other) const60 bool UlpfecConfig::operator==(const UlpfecConfig& other) const {
61 return ulpfec_payload_type == other.ulpfec_payload_type &&
62 red_payload_type == other.red_payload_type &&
63 red_rtx_payload_type == other.red_rtx_payload_type;
64 }
65
66 RtpConfig::RtpConfig() = default;
67 RtpConfig::RtpConfig(const RtpConfig&) = default;
68 RtpConfig::~RtpConfig() = default;
69
70 RtpConfig::Flexfec::Flexfec() = default;
71 RtpConfig::Flexfec::Flexfec(const Flexfec&) = default;
72 RtpConfig::Flexfec::~Flexfec() = default;
73
ToString() const74 std::string RtpConfig::ToString() const {
75 char buf[2 * 1024];
76 rtc::SimpleStringBuilder ss(buf);
77 ss << "{ssrcs: [";
78 for (size_t i = 0; i < ssrcs.size(); ++i) {
79 ss << ssrcs[i];
80 if (i != ssrcs.size() - 1)
81 ss << ", ";
82 }
83 ss << "], rids: [";
84 for (size_t i = 0; i < rids.size(); ++i) {
85 ss << rids[i];
86 if (i != rids.size() - 1)
87 ss << ", ";
88 }
89 ss << "], mid: '" << mid << "'";
90 ss << ", rtcp_mode: "
91 << (rtcp_mode == RtcpMode::kCompound ? "RtcpMode::kCompound"
92 : "RtcpMode::kReducedSize");
93 ss << ", max_packet_size: " << max_packet_size;
94 ss << ", extmap-allow-mixed: " << (extmap_allow_mixed ? "true" : "false");
95 ss << ", extensions: [";
96 for (size_t i = 0; i < extensions.size(); ++i) {
97 ss << extensions[i].ToString();
98 if (i != extensions.size() - 1)
99 ss << ", ";
100 }
101 ss << ']';
102
103 ss << ", lntf: " << lntf.ToString();
104 ss << ", nack: {rtp_history_ms: " << nack.rtp_history_ms << '}';
105 ss << ", ulpfec: " << ulpfec.ToString();
106 ss << ", payload_name: " << payload_name;
107 ss << ", payload_type: " << payload_type;
108 ss << ", raw_payload: " << (raw_payload ? "true" : "false");
109
110 ss << ", flexfec: {payload_type: " << flexfec.payload_type;
111 ss << ", ssrc: " << flexfec.ssrc;
112 ss << ", protected_media_ssrcs: [";
113 for (size_t i = 0; i < flexfec.protected_media_ssrcs.size(); ++i) {
114 ss << flexfec.protected_media_ssrcs[i];
115 if (i != flexfec.protected_media_ssrcs.size() - 1)
116 ss << ", ";
117 }
118 ss << "]}";
119
120 ss << ", rtx: " << rtx.ToString();
121 ss << ", c_name: " << c_name;
122 ss << '}';
123 return ss.str();
124 }
125
126 RtpConfig::Rtx::Rtx() = default;
127 RtpConfig::Rtx::Rtx(const Rtx&) = default;
128 RtpConfig::Rtx::~Rtx() = default;
129
ToString() const130 std::string RtpConfig::Rtx::ToString() const {
131 char buf[1024];
132 rtc::SimpleStringBuilder ss(buf);
133 ss << "{ssrcs: [";
134 for (size_t i = 0; i < ssrcs.size(); ++i) {
135 ss << ssrcs[i];
136 if (i != ssrcs.size() - 1)
137 ss << ", ";
138 }
139 ss << ']';
140
141 ss << ", payload_type: " << payload_type;
142 ss << '}';
143 return ss.str();
144 }
145
IsMediaSsrc(uint32_t ssrc) const146 bool RtpConfig::IsMediaSsrc(uint32_t ssrc) const {
147 return absl::c_linear_search(ssrcs, ssrc);
148 }
149
IsRtxSsrc(uint32_t ssrc) const150 bool RtpConfig::IsRtxSsrc(uint32_t ssrc) const {
151 return absl::c_linear_search(rtx.ssrcs, ssrc);
152 }
153
IsFlexfecSsrc(uint32_t ssrc) const154 bool RtpConfig::IsFlexfecSsrc(uint32_t ssrc) const {
155 return flexfec.payload_type != -1 && ssrc == flexfec.ssrc;
156 }
157
GetRtxSsrcAssociatedWithMediaSsrc(uint32_t media_ssrc) const158 absl::optional<uint32_t> RtpConfig::GetRtxSsrcAssociatedWithMediaSsrc(
159 uint32_t media_ssrc) const {
160 RTC_DCHECK(IsMediaSsrc(media_ssrc));
161 // If we don't use RTX there is no association.
162 if (rtx.ssrcs.empty())
163 return absl::nullopt;
164 // If we use RTX there MUST be an association ssrcs[i] <-> rtx.ssrcs[i].
165 RTC_DCHECK_EQ(ssrcs.size(), rtx.ssrcs.size());
166 return FindAssociatedSsrc(media_ssrc, ssrcs, rtx.ssrcs);
167 }
168
GetMediaSsrcAssociatedWithRtxSsrc(uint32_t rtx_ssrc) const169 uint32_t RtpConfig::GetMediaSsrcAssociatedWithRtxSsrc(uint32_t rtx_ssrc) const {
170 RTC_DCHECK(IsRtxSsrc(rtx_ssrc));
171 // If we use RTX there MUST be an association ssrcs[i] <-> rtx.ssrcs[i].
172 RTC_DCHECK_EQ(ssrcs.size(), rtx.ssrcs.size());
173 return FindAssociatedSsrc(rtx_ssrc, rtx.ssrcs, ssrcs);
174 }
175
GetMediaSsrcAssociatedWithFlexfecSsrc(uint32_t flexfec_ssrc) const176 uint32_t RtpConfig::GetMediaSsrcAssociatedWithFlexfecSsrc(
177 uint32_t flexfec_ssrc) const {
178 RTC_DCHECK(IsFlexfecSsrc(flexfec_ssrc));
179 // If we use FlexFEC there MUST be an associated media ssrc.
180 //
181 // TODO(brandtr/hbos): The current implementation only supports an association
182 // with a single media ssrc. If multiple ssrcs are to be supported in the
183 // future, in order not to break GetStats()'s packet and byte counters, we
184 // must be able to tell how many packets and bytes have contributed to which
185 // SSRC.
186 RTC_DCHECK_EQ(1u, flexfec.protected_media_ssrcs.size());
187 uint32_t media_ssrc = flexfec.protected_media_ssrcs[0];
188 RTC_DCHECK(IsMediaSsrc(media_ssrc));
189 return media_ssrc;
190 }
191
GetRidForSsrc(uint32_t ssrc) const192 absl::optional<std::string> RtpConfig::GetRidForSsrc(uint32_t ssrc) const {
193 auto it = std::find(ssrcs.begin(), ssrcs.end(), ssrc);
194 if (it != ssrcs.end()) {
195 size_t ssrc_index = std::distance(ssrcs.begin(), it);
196 if (ssrc_index < rids.size()) {
197 return rids[ssrc_index];
198 }
199 }
200 return absl::nullopt;
201 }
202
203 } // namespace webrtc
204