henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 2 | * Copyright (c) 2004 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 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. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "media/base/codec.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
Steve Anton | 2c9ebef | 2019-01-28 17:27:58 -0800 | [diff] [blame] | 13 | #include "absl/algorithm/container.h" |
Niels Möller | 3c7d599 | 2018-10-19 15:29:54 +0200 | [diff] [blame] | 14 | #include "absl/strings/match.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "media/base/h264_profile_level_id.h" |
Emircan Uysaler | 98badbc | 2018-06-28 10:59:02 -0700 | [diff] [blame] | 16 | #include "media/base/vp9_profile.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "rtc_base/checks.h" |
| 18 | #include "rtc_base/logging.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 19 | #include "rtc_base/string_encode.h" |
Jonas Olsson | 88c9956 | 2018-05-03 11:45:33 +0200 | [diff] [blame] | 20 | #include "rtc_base/strings/string_builder.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 21 | |
| 22 | namespace cricket { |
Steve Anton | 2dbc627 | 2019-05-31 13:08:58 -0700 | [diff] [blame] | 23 | namespace { |
| 24 | |
| 25 | std::string GetH264PacketizationModeOrDefault(const CodecParameterMap& params) { |
| 26 | auto it = params.find(kH264FmtpPacketizationMode); |
| 27 | if (it != params.end()) { |
| 28 | return it->second; |
| 29 | } |
| 30 | // If packetization-mode is not present, default to "0". |
| 31 | // https://tools.ietf.org/html/rfc6184#section-6.2 |
| 32 | return "0"; |
| 33 | } |
| 34 | |
| 35 | bool IsSameH264PacketizationMode(const CodecParameterMap& left, |
| 36 | const CodecParameterMap& right) { |
| 37 | return GetH264PacketizationModeOrDefault(left) == |
| 38 | GetH264PacketizationModeOrDefault(right); |
| 39 | } |
| 40 | |
| 41 | // Some (video) codecs are actually families of codecs and rely on parameters |
| 42 | // to distinguish different incompatible family members. |
| 43 | bool IsSameCodecSpecific(const std::string& name1, |
| 44 | const CodecParameterMap& params1, |
| 45 | const std::string& name2, |
| 46 | const CodecParameterMap& params2) { |
| 47 | // The names might not necessarily match, so check both. |
| 48 | auto either_name_matches = [&](const std::string name) { |
| 49 | return absl::EqualsIgnoreCase(name, name1) || |
| 50 | absl::EqualsIgnoreCase(name, name2); |
| 51 | }; |
| 52 | if (either_name_matches(kH264CodecName)) |
| 53 | return webrtc::H264::IsSameH264Profile(params1, params2) && |
| 54 | IsSameH264PacketizationMode(params1, params2); |
| 55 | if (either_name_matches(kVp9CodecName)) |
| 56 | return webrtc::IsSameVP9Profile(params1, params2); |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | } // namespace |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 61 | |
Magnus Jedvert | 244ad80 | 2017-09-28 21:19:18 +0200 | [diff] [blame] | 62 | FeedbackParams::FeedbackParams() = default; |
Paulina Hensman | a680a6a | 2018-04-05 11:42:24 +0200 | [diff] [blame] | 63 | FeedbackParams::~FeedbackParams() = default; |
Magnus Jedvert | 244ad80 | 2017-09-28 21:19:18 +0200 | [diff] [blame] | 64 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 65 | bool FeedbackParam::operator==(const FeedbackParam& other) const { |
Niels Möller | 3c7d599 | 2018-10-19 15:29:54 +0200 | [diff] [blame] | 66 | return absl::EqualsIgnoreCase(other.id(), id()) && |
| 67 | absl::EqualsIgnoreCase(other.param(), param()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | bool FeedbackParams::operator==(const FeedbackParams& other) const { |
| 71 | return params_ == other.params_; |
| 72 | } |
| 73 | |
| 74 | bool FeedbackParams::Has(const FeedbackParam& param) const { |
Steve Anton | 2c9ebef | 2019-01-28 17:27:58 -0800 | [diff] [blame] | 75 | return absl::c_linear_search(params_, param); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void FeedbackParams::Add(const FeedbackParam& param) { |
| 79 | if (param.id().empty()) { |
| 80 | return; |
| 81 | } |
| 82 | if (Has(param)) { |
| 83 | // Param already in |this|. |
| 84 | return; |
| 85 | } |
| 86 | params_.push_back(param); |
magjed | 0928a3c | 2016-11-25 00:40:18 -0800 | [diff] [blame] | 87 | RTC_CHECK(!HasDuplicateEntries()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void FeedbackParams::Intersect(const FeedbackParams& from) { |
| 91 | std::vector<FeedbackParam>::iterator iter_to = params_.begin(); |
| 92 | while (iter_to != params_.end()) { |
| 93 | if (!from.Has(*iter_to)) { |
| 94 | iter_to = params_.erase(iter_to); |
| 95 | } else { |
| 96 | ++iter_to; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | bool FeedbackParams::HasDuplicateEntries() const { |
| 102 | for (std::vector<FeedbackParam>::const_iterator iter = params_.begin(); |
| 103 | iter != params_.end(); ++iter) { |
| 104 | for (std::vector<FeedbackParam>::const_iterator found = iter + 1; |
| 105 | found != params_.end(); ++found) { |
| 106 | if (*found == *iter) { |
| 107 | return true; |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | return false; |
| 112 | } |
| 113 | |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 114 | Codec::Codec(int id, const std::string& name, int clockrate) |
| 115 | : id(id), name(name), clockrate(clockrate) {} |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 116 | |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 117 | Codec::Codec() : id(0), clockrate(0) {} |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 118 | |
| 119 | Codec::Codec(const Codec& c) = default; |
magjed | 3663c52 | 2016-11-07 10:14:36 -0800 | [diff] [blame] | 120 | Codec::Codec(Codec&& c) = default; |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 121 | Codec::~Codec() = default; |
magjed | 3663c52 | 2016-11-07 10:14:36 -0800 | [diff] [blame] | 122 | Codec& Codec::operator=(const Codec& c) = default; |
| 123 | Codec& Codec::operator=(Codec&& c) = default; |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 124 | |
| 125 | bool Codec::operator==(const Codec& c) const { |
| 126 | return this->id == c.id && // id is reserved in objective-c |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 127 | name == c.name && clockrate == c.clockrate && params == c.params && |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 128 | feedback_params == c.feedback_params; |
| 129 | } |
| 130 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 131 | bool Codec::Matches(const Codec& codec) const { |
| 132 | // Match the codec id/name based on the typical static/dynamic name rules. |
| 133 | // Matching is case-insensitive. |
pkasting@chromium.org | d324546 | 2015-02-23 21:28:22 +0000 | [diff] [blame] | 134 | const int kMaxStaticPayloadId = 95; |
magjed | 3663c52 | 2016-11-07 10:14:36 -0800 | [diff] [blame] | 135 | return (id <= kMaxStaticPayloadId || codec.id <= kMaxStaticPayloadId) |
| 136 | ? (id == codec.id) |
Niels Möller | 3c7d599 | 2018-10-19 15:29:54 +0200 | [diff] [blame] | 137 | : (absl::EqualsIgnoreCase(name, codec.name)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 140 | bool Codec::MatchesCapability( |
| 141 | const webrtc::RtpCodecCapability& codec_capability) const { |
| 142 | webrtc::RtpCodecParameters codec_parameters = ToCodecParameters(); |
| 143 | |
| 144 | return codec_parameters.name == codec_capability.name && |
| 145 | codec_parameters.kind == codec_capability.kind && |
| 146 | (codec_parameters.name == cricket::kRtxCodecName || |
| 147 | (codec_parameters.num_channels == codec_capability.num_channels && |
| 148 | codec_parameters.clock_rate == codec_capability.clock_rate && |
| 149 | codec_parameters.parameters == codec_capability.parameters)); |
| 150 | } |
| 151 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 152 | bool Codec::GetParam(const std::string& name, std::string* out) const { |
| 153 | CodecParameterMap::const_iterator iter = params.find(name); |
| 154 | if (iter == params.end()) |
| 155 | return false; |
| 156 | *out = iter->second; |
| 157 | return true; |
| 158 | } |
| 159 | |
| 160 | bool Codec::GetParam(const std::string& name, int* out) const { |
| 161 | CodecParameterMap::const_iterator iter = params.find(name); |
| 162 | if (iter == params.end()) |
| 163 | return false; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 164 | return rtc::FromString(iter->second, out); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | void Codec::SetParam(const std::string& name, const std::string& value) { |
| 168 | params[name] = value; |
| 169 | } |
| 170 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 171 | void Codec::SetParam(const std::string& name, int value) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 172 | params[name] = rtc::ToString(value); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 173 | } |
| 174 | |
buildbot@webrtc.org | fbd1328 | 2014-06-19 19:50:55 +0000 | [diff] [blame] | 175 | bool Codec::RemoveParam(const std::string& name) { |
| 176 | return params.erase(name) == 1; |
| 177 | } |
| 178 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 179 | void Codec::AddFeedbackParam(const FeedbackParam& param) { |
| 180 | feedback_params.Add(param); |
| 181 | } |
| 182 | |
| 183 | bool Codec::HasFeedbackParam(const FeedbackParam& param) const { |
| 184 | return feedback_params.Has(param); |
| 185 | } |
| 186 | |
| 187 | void Codec::IntersectFeedbackParams(const Codec& other) { |
| 188 | feedback_params.Intersect(other.feedback_params); |
| 189 | } |
| 190 | |
Taylor Brandstetter | 0cd086b | 2016-04-20 16:23:10 -0700 | [diff] [blame] | 191 | webrtc::RtpCodecParameters Codec::ToCodecParameters() const { |
| 192 | webrtc::RtpCodecParameters codec_params; |
| 193 | codec_params.payload_type = id; |
deadbeef | e702b30 | 2017-02-04 12:09:01 -0800 | [diff] [blame] | 194 | codec_params.name = name; |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 195 | codec_params.clock_rate = clockrate; |
Florent Castelli | b7d9d83 | 2018-05-15 18:14:14 +0200 | [diff] [blame] | 196 | codec_params.parameters.insert(params.begin(), params.end()); |
Taylor Brandstetter | 0cd086b | 2016-04-20 16:23:10 -0700 | [diff] [blame] | 197 | return codec_params; |
| 198 | } |
| 199 | |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 200 | AudioCodec::AudioCodec(int id, |
| 201 | const std::string& name, |
| 202 | int clockrate, |
| 203 | int bitrate, |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 204 | size_t channels) |
| 205 | : Codec(id, name, clockrate), bitrate(bitrate), channels(channels) {} |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 206 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 207 | AudioCodec::AudioCodec() : Codec(), bitrate(0), channels(0) {} |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 208 | |
| 209 | AudioCodec::AudioCodec(const AudioCodec& c) = default; |
magjed | 3663c52 | 2016-11-07 10:14:36 -0800 | [diff] [blame] | 210 | AudioCodec::AudioCodec(AudioCodec&& c) = default; |
| 211 | AudioCodec& AudioCodec::operator=(const AudioCodec& c) = default; |
| 212 | AudioCodec& AudioCodec::operator=(AudioCodec&& c) = default; |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 213 | |
| 214 | bool AudioCodec::operator==(const AudioCodec& c) const { |
guoweis@webrtc.org | cce874b | 2015-02-19 18:14:36 +0000 | [diff] [blame] | 215 | return bitrate == c.bitrate && channels == c.channels && Codec::operator==(c); |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 216 | } |
| 217 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 218 | bool AudioCodec::Matches(const AudioCodec& codec) const { |
| 219 | // If a nonzero clockrate is specified, it must match the actual clockrate. |
| 220 | // If a nonzero bitrate is specified, it must match the actual bitrate, |
| 221 | // unless the codec is VBR (0), where we just force the supplied value. |
| 222 | // The number of channels must match exactly, with the exception |
| 223 | // that channels=0 is treated synonymously as channels=1, per RFC |
| 224 | // 4566 section 6: " [The channels] parameter is OPTIONAL and may be |
| 225 | // omitted if the number of channels is one." |
| 226 | // Preference is ignored. |
| 227 | // TODO(juberti): Treat a zero clockrate as 8000Hz, the RTP default clockrate. |
| 228 | return Codec::Matches(codec) && |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 229 | ((codec.clockrate == 0 /*&& clockrate == 8000*/) || |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 230 | clockrate == codec.clockrate) && |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 231 | (codec.bitrate == 0 || bitrate <= 0 || bitrate == codec.bitrate) && |
| 232 | ((codec.channels < 2 && channels < 2) || channels == codec.channels); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | std::string AudioCodec::ToString() const { |
Jonas Olsson | 88c9956 | 2018-05-03 11:45:33 +0200 | [diff] [blame] | 236 | char buf[256]; |
| 237 | rtc::SimpleStringBuilder sb(buf); |
| 238 | sb << "AudioCodec[" << id << ":" << name << ":" << clockrate << ":" << bitrate |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 239 | << ":" << channels << "]"; |
Jonas Olsson | 88c9956 | 2018-05-03 11:45:33 +0200 | [diff] [blame] | 240 | return sb.str(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 241 | } |
| 242 | |
deadbeef | e702b30 | 2017-02-04 12:09:01 -0800 | [diff] [blame] | 243 | webrtc::RtpCodecParameters AudioCodec::ToCodecParameters() const { |
| 244 | webrtc::RtpCodecParameters codec_params = Codec::ToCodecParameters(); |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 245 | codec_params.num_channels = static_cast<int>(channels); |
deadbeef | e702b30 | 2017-02-04 12:09:01 -0800 | [diff] [blame] | 246 | codec_params.kind = MEDIA_TYPE_AUDIO; |
| 247 | return codec_params; |
| 248 | } |
| 249 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 250 | std::string VideoCodec::ToString() const { |
Jonas Olsson | 88c9956 | 2018-05-03 11:45:33 +0200 | [diff] [blame] | 251 | char buf[256]; |
| 252 | rtc::SimpleStringBuilder sb(buf); |
| 253 | sb << "VideoCodec[" << id << ":" << name << "]"; |
| 254 | return sb.str(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 255 | } |
| 256 | |
deadbeef | e702b30 | 2017-02-04 12:09:01 -0800 | [diff] [blame] | 257 | webrtc::RtpCodecParameters VideoCodec::ToCodecParameters() const { |
| 258 | webrtc::RtpCodecParameters codec_params = Codec::ToCodecParameters(); |
| 259 | codec_params.kind = MEDIA_TYPE_VIDEO; |
| 260 | return codec_params; |
| 261 | } |
| 262 | |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 263 | VideoCodec::VideoCodec(int id, const std::string& name) |
hta | 9aa9688 | 2016-12-06 05:36:03 -0800 | [diff] [blame] | 264 | : Codec(id, name, kVideoCodecClockrate) { |
| 265 | SetDefaultParameters(); |
| 266 | } |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 267 | |
hta | 9aa9688 | 2016-12-06 05:36:03 -0800 | [diff] [blame] | 268 | VideoCodec::VideoCodec(const std::string& name) : VideoCodec(0 /* id */, name) { |
| 269 | SetDefaultParameters(); |
| 270 | } |
magjed | 1e45cc6 | 2016-10-28 07:43:45 -0700 | [diff] [blame] | 271 | |
perkj | 2675274 | 2016-10-24 01:21:16 -0700 | [diff] [blame] | 272 | VideoCodec::VideoCodec() : Codec() { |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 273 | clockrate = kVideoCodecClockrate; |
| 274 | } |
| 275 | |
Magnus Jedvert | 024d897 | 2017-09-29 15:00:29 +0200 | [diff] [blame] | 276 | VideoCodec::VideoCodec(const webrtc::SdpVideoFormat& c) |
| 277 | : Codec(0 /* id */, c.name, kVideoCodecClockrate) { |
| 278 | params = c.parameters; |
| 279 | } |
| 280 | |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 281 | VideoCodec::VideoCodec(const VideoCodec& c) = default; |
magjed | 3663c52 | 2016-11-07 10:14:36 -0800 | [diff] [blame] | 282 | VideoCodec::VideoCodec(VideoCodec&& c) = default; |
| 283 | VideoCodec& VideoCodec::operator=(const VideoCodec& c) = default; |
| 284 | VideoCodec& VideoCodec::operator=(VideoCodec&& c) = default; |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 285 | |
hta | 9aa9688 | 2016-12-06 05:36:03 -0800 | [diff] [blame] | 286 | void VideoCodec::SetDefaultParameters() { |
Niels Möller | 3c7d599 | 2018-10-19 15:29:54 +0200 | [diff] [blame] | 287 | if (absl::EqualsIgnoreCase(kH264CodecName, name)) { |
hta | 9aa9688 | 2016-12-06 05:36:03 -0800 | [diff] [blame] | 288 | // This default is set for all H.264 codecs created because |
| 289 | // that was the default before packetization mode support was added. |
| 290 | // TODO(hta): Move this to the places that create VideoCodecs from |
| 291 | // SDP or from knowledge of implementation capabilities. |
| 292 | SetParam(kH264FmtpPacketizationMode, "1"); |
| 293 | } |
| 294 | } |
| 295 | |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 296 | bool VideoCodec::operator==(const VideoCodec& c) const { |
Mirta Dvornicic | 479a3c0 | 2019-06-04 15:38:50 +0200 | [diff] [blame] | 297 | return Codec::operator==(c) && packetization == c.packetization; |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 298 | } |
| 299 | |
magjed | f823ede | 2016-11-12 09:53:04 -0800 | [diff] [blame] | 300 | bool VideoCodec::Matches(const VideoCodec& other) const { |
Steve Anton | 2dbc627 | 2019-05-31 13:08:58 -0700 | [diff] [blame] | 301 | return Codec::Matches(other) && |
| 302 | IsSameCodecSpecific(name, params, other.name, other.params); |
magjed | f823ede | 2016-11-12 09:53:04 -0800 | [diff] [blame] | 303 | } |
| 304 | |
Mirta Dvornicic | 479a3c0 | 2019-06-04 15:38:50 +0200 | [diff] [blame] | 305 | absl::optional<std::string> VideoCodec::IntersectPacketization( |
| 306 | const VideoCodec& local_codec, |
| 307 | const VideoCodec& remote_codec) { |
| 308 | if (local_codec.packetization == remote_codec.packetization) { |
| 309 | return local_codec.packetization; |
| 310 | } |
| 311 | return absl::nullopt; |
| 312 | } |
| 313 | |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 314 | VideoCodec VideoCodec::CreateRtxCodec(int rtx_payload_type, |
| 315 | int associated_payload_type) { |
perkj | 2675274 | 2016-10-24 01:21:16 -0700 | [diff] [blame] | 316 | VideoCodec rtx_codec(rtx_payload_type, kRtxCodecName); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 317 | rtx_codec.SetParam(kCodecParamAssociatedPayloadType, associated_payload_type); |
| 318 | return rtx_codec; |
| 319 | } |
| 320 | |
| 321 | VideoCodec::CodecType VideoCodec::GetCodecType() const { |
Steve Anton | 2dbc627 | 2019-05-31 13:08:58 -0700 | [diff] [blame] | 322 | if (absl::EqualsIgnoreCase(name, kRedCodecName)) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 323 | return CODEC_RED; |
| 324 | } |
Steve Anton | 2dbc627 | 2019-05-31 13:08:58 -0700 | [diff] [blame] | 325 | if (absl::EqualsIgnoreCase(name, kUlpfecCodecName)) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 326 | return CODEC_ULPFEC; |
| 327 | } |
Steve Anton | 2dbc627 | 2019-05-31 13:08:58 -0700 | [diff] [blame] | 328 | if (absl::EqualsIgnoreCase(name, kFlexfecCodecName)) { |
brandtr | 87d7d77 | 2016-11-07 03:03:41 -0800 | [diff] [blame] | 329 | return CODEC_FLEXFEC; |
| 330 | } |
Steve Anton | 2dbc627 | 2019-05-31 13:08:58 -0700 | [diff] [blame] | 331 | if (absl::EqualsIgnoreCase(name, kRtxCodecName)) { |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 332 | return CODEC_RTX; |
| 333 | } |
| 334 | |
| 335 | return CODEC_VIDEO; |
| 336 | } |
| 337 | |
| 338 | bool VideoCodec::ValidateCodecFormat() const { |
| 339 | if (id < 0 || id > 127) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 340 | RTC_LOG(LS_ERROR) << "Codec with invalid payload type: " << ToString(); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 341 | return false; |
| 342 | } |
| 343 | if (GetCodecType() != CODEC_VIDEO) { |
| 344 | return true; |
| 345 | } |
| 346 | |
| 347 | // Video validation from here on. |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 348 | int min_bitrate = -1; |
| 349 | int max_bitrate = -1; |
| 350 | if (GetParam(kCodecParamMinBitrate, &min_bitrate) && |
| 351 | GetParam(kCodecParamMaxBitrate, &max_bitrate)) { |
| 352 | if (max_bitrate < min_bitrate) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 353 | RTC_LOG(LS_ERROR) << "Codec with max < min bitrate: " << ToString(); |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 354 | return false; |
| 355 | } |
| 356 | } |
| 357 | return true; |
| 358 | } |
| 359 | |
Harald Alvestrand | 5fc28b1 | 2019-05-13 13:36:16 +0200 | [diff] [blame] | 360 | RtpDataCodec::RtpDataCodec(int id, const std::string& name) |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 361 | : Codec(id, name, kDataCodecClockrate) {} |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 362 | |
Harald Alvestrand | 5fc28b1 | 2019-05-13 13:36:16 +0200 | [diff] [blame] | 363 | RtpDataCodec::RtpDataCodec() : Codec() { |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 364 | clockrate = kDataCodecClockrate; |
| 365 | } |
| 366 | |
Harald Alvestrand | 5fc28b1 | 2019-05-13 13:36:16 +0200 | [diff] [blame] | 367 | RtpDataCodec::RtpDataCodec(const RtpDataCodec& c) = default; |
| 368 | RtpDataCodec::RtpDataCodec(RtpDataCodec&& c) = default; |
| 369 | RtpDataCodec& RtpDataCodec::operator=(const RtpDataCodec& c) = default; |
| 370 | RtpDataCodec& RtpDataCodec::operator=(RtpDataCodec&& c) = default; |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 371 | |
Harald Alvestrand | 5fc28b1 | 2019-05-13 13:36:16 +0200 | [diff] [blame] | 372 | std::string RtpDataCodec::ToString() const { |
Jonas Olsson | 88c9956 | 2018-05-03 11:45:33 +0200 | [diff] [blame] | 373 | char buf[256]; |
| 374 | rtc::SimpleStringBuilder sb(buf); |
Harald Alvestrand | 5fc28b1 | 2019-05-13 13:36:16 +0200 | [diff] [blame] | 375 | sb << "RtpDataCodec[" << id << ":" << name << "]"; |
Jonas Olsson | 88c9956 | 2018-05-03 11:45:33 +0200 | [diff] [blame] | 376 | return sb.str(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Elad Alon | fadb181 | 2019-05-24 13:40:02 +0200 | [diff] [blame] | 379 | bool HasLntf(const Codec& codec) { |
| 380 | return codec.HasFeedbackParam( |
| 381 | FeedbackParam(kRtcpFbParamLntf, kParamValueEmpty)); |
| 382 | } |
| 383 | |
stefan | ba4c0e4 | 2016-02-04 04:12:24 -0800 | [diff] [blame] | 384 | bool HasNack(const Codec& codec) { |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 385 | return codec.HasFeedbackParam( |
| 386 | FeedbackParam(kRtcpFbParamNack, kParamValueEmpty)); |
| 387 | } |
| 388 | |
stefan | ba4c0e4 | 2016-02-04 04:12:24 -0800 | [diff] [blame] | 389 | bool HasRemb(const Codec& codec) { |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 390 | return codec.HasFeedbackParam( |
| 391 | FeedbackParam(kRtcpFbParamRemb, kParamValueEmpty)); |
| 392 | } |
| 393 | |
Ilya Nikolaevskiy | 634a777 | 2018-04-04 16:33:49 +0200 | [diff] [blame] | 394 | bool HasRrtr(const Codec& codec) { |
| 395 | return codec.HasFeedbackParam( |
| 396 | FeedbackParam(kRtcpFbParamRrtr, kParamValueEmpty)); |
| 397 | } |
| 398 | |
stefan | ba4c0e4 | 2016-02-04 04:12:24 -0800 | [diff] [blame] | 399 | bool HasTransportCc(const Codec& codec) { |
stefan | 43edf0f | 2015-11-20 18:05:48 -0800 | [diff] [blame] | 400 | return codec.HasFeedbackParam( |
| 401 | FeedbackParam(kRtcpFbParamTransportCc, kParamValueEmpty)); |
| 402 | } |
| 403 | |
magjed | f823ede | 2016-11-12 09:53:04 -0800 | [diff] [blame] | 404 | const VideoCodec* FindMatchingCodec( |
| 405 | const std::vector<VideoCodec>& supported_codecs, |
| 406 | const VideoCodec& codec) { |
| 407 | for (const VideoCodec& supported_codec : supported_codecs) { |
Magnus Jedvert | 523589d | 2017-11-23 13:24:53 +0100 | [diff] [blame] | 408 | if (IsSameCodec(codec.name, codec.params, supported_codec.name, |
| 409 | supported_codec.params)) { |
| 410 | return &supported_codec; |
magjed | f823ede | 2016-11-12 09:53:04 -0800 | [diff] [blame] | 411 | } |
magjed | f823ede | 2016-11-12 09:53:04 -0800 | [diff] [blame] | 412 | } |
| 413 | return nullptr; |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 414 | } |
| 415 | |
Magnus Jedvert | 523589d | 2017-11-23 13:24:53 +0100 | [diff] [blame] | 416 | bool IsSameCodec(const std::string& name1, |
| 417 | const CodecParameterMap& params1, |
| 418 | const std::string& name2, |
| 419 | const CodecParameterMap& params2) { |
Steve Anton | 2dbc627 | 2019-05-31 13:08:58 -0700 | [diff] [blame] | 420 | // Two codecs are considered the same if the name matches (case insensitive) |
| 421 | // and certain codec-specific parameters match. |
| 422 | return absl::EqualsIgnoreCase(name1, name2) && |
| 423 | IsSameCodecSpecific(name1, params1, name2, params2); |
Magnus Jedvert | 523589d | 2017-11-23 13:24:53 +0100 | [diff] [blame] | 424 | } |
| 425 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 426 | } // namespace cricket |