blob: c84a63ee4eab41a8357e9c4b078e592523551504 [file] [log] [blame]
Stefan Holmer1acbd682017-09-01 15:29:28 +02001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "call/rtp_config.h"
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020012
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <cstdint>
14
Henrik Boströmf45ca372020-03-24 13:30:50 +010015#include "absl/algorithm/container.h"
Yves Gerey988cc082018-10-23 12:03:01 +020016#include "api/array_view.h"
Henrik Boströmf45ca372020-03-24 13:30:50 +010017#include "rtc_base/checks.h"
Jonas Olsson0a713b62018-04-04 15:49:32 +020018#include "rtc_base/strings/string_builder.h"
Stefan Holmer1acbd682017-09-01 15:29:28 +020019
20namespace webrtc {
21
Henrik Boströmf45ca372020-03-24 13:30:50 +010022namespace {
23
24uint32_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_NOTREACHED();
33 return 0;
34}
35
36} // namespace
37
Elad Alonfadb1812019-05-24 13:40:02 +020038std::string LntfConfig::ToString() const {
39 return enabled ? "{enabled: true}" : "{enabled: false}";
40}
41
Stefan Holmer1acbd682017-09-01 15:29:28 +020042std::string NackConfig::ToString() const {
Jonas Olsson0a713b62018-04-04 15:49:32 +020043 char buf[1024];
44 rtc::SimpleStringBuilder ss(buf);
Stefan Holmer1acbd682017-09-01 15:29:28 +020045 ss << "{rtp_history_ms: " << rtp_history_ms;
46 ss << '}';
47 return ss.str();
48}
49
50std::string UlpfecConfig::ToString() const {
Jonas Olsson0a713b62018-04-04 15:49:32 +020051 char buf[1024];
52 rtc::SimpleStringBuilder ss(buf);
Stefan Holmer1acbd682017-09-01 15:29:28 +020053 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
60bool 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}
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020065
66RtpConfig::RtpConfig() = default;
67RtpConfig::RtpConfig(const RtpConfig&) = default;
68RtpConfig::~RtpConfig() = default;
69
70RtpConfig::Flexfec::Flexfec() = default;
71RtpConfig::Flexfec::Flexfec(const Flexfec&) = default;
72RtpConfig::Flexfec::~Flexfec() = default;
73
74std::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 }
Rasmus Brandt9df698c2020-03-09 13:53:26 +010083 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 << "'";
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020090 ss << ", rtcp_mode: "
91 << (rtcp_mode == RtcpMode::kCompound ? "RtcpMode::kCompound"
92 : "RtcpMode::kReducedSize");
93 ss << ", max_packet_size: " << max_packet_size;
Johannes Kron9190b822018-10-29 11:22:05 +010094 ss << ", extmap-allow-mixed: " << (extmap_allow_mixed ? "true" : "false");
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020095 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
Elad Alonfadb1812019-05-24 13:40:02 +0200103 ss << ", lntf: " << lntf.ToString();
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200104 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;
Mirta Dvornicicfe68daa2019-05-23 13:21:12 +0200108 ss << ", raw_payload: " << (raw_payload ? "true" : "false");
Stefan Holmerdbdb3a02018-07-17 16:03:46 +0200109
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
126RtpConfig::Rtx::Rtx() = default;
127RtpConfig::Rtx::Rtx(const Rtx&) = default;
128RtpConfig::Rtx::~Rtx() = default;
129
130std::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}
Henrik Boströmf45ca372020-03-24 13:30:50 +0100145
146bool RtpConfig::IsMediaSsrc(uint32_t ssrc) const {
147 return absl::c_linear_search(ssrcs, ssrc);
148}
149
150bool RtpConfig::IsRtxSsrc(uint32_t ssrc) const {
151 return absl::c_linear_search(rtx.ssrcs, ssrc);
152}
153
154bool RtpConfig::IsFlexfecSsrc(uint32_t ssrc) const {
155 return flexfec.payload_type != -1 && ssrc == flexfec.ssrc;
156}
157
158absl::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
169uint32_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
176uint32_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
Henrik Boströma0ff50c2020-05-05 15:54:46 +0200192absl::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
Stefan Holmer1acbd682017-09-01 15:29:28 +0200203} // namespace webrtc