blob: 16ef65dff539c61972286525cb943ae51003692c [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander1afca732016-02-07 20:46:45 -08002 * Copyright (c) 2004 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander1afca732016-02-07 20:46:45 -08004 * 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.org28e20752013-07-10 00:45:36 +00009 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "media/base/codec.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
Steve Anton2c9ebef2019-01-28 17:27:58 -080013#include "absl/algorithm/container.h"
Niels Möller3c7d5992018-10-19 15:29:54 +020014#include "absl/strings/match.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "media/base/h264_profile_level_id.h"
Emircan Uysaler98badbc2018-06-28 10:59:02 -070016#include "media/base/vp9_profile.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/checks.h"
18#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080019#include "rtc_base/string_encode.h"
Jonas Olsson88c99562018-05-03 11:45:33 +020020#include "rtc_base/strings/string_builder.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021
22namespace cricket {
23
Magnus Jedvert244ad802017-09-28 21:19:18 +020024FeedbackParams::FeedbackParams() = default;
Paulina Hensmana680a6a2018-04-05 11:42:24 +020025FeedbackParams::~FeedbackParams() = default;
Magnus Jedvert244ad802017-09-28 21:19:18 +020026
henrike@webrtc.org28e20752013-07-10 00:45:36 +000027bool FeedbackParam::operator==(const FeedbackParam& other) const {
Niels Möller3c7d5992018-10-19 15:29:54 +020028 return absl::EqualsIgnoreCase(other.id(), id()) &&
29 absl::EqualsIgnoreCase(other.param(), param());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000030}
31
32bool FeedbackParams::operator==(const FeedbackParams& other) const {
33 return params_ == other.params_;
34}
35
36bool FeedbackParams::Has(const FeedbackParam& param) const {
Steve Anton2c9ebef2019-01-28 17:27:58 -080037 return absl::c_linear_search(params_, param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000038}
39
40void FeedbackParams::Add(const FeedbackParam& param) {
41 if (param.id().empty()) {
42 return;
43 }
44 if (Has(param)) {
45 // Param already in |this|.
46 return;
47 }
48 params_.push_back(param);
magjed0928a3c2016-11-25 00:40:18 -080049 RTC_CHECK(!HasDuplicateEntries());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000050}
51
52void FeedbackParams::Intersect(const FeedbackParams& from) {
53 std::vector<FeedbackParam>::iterator iter_to = params_.begin();
54 while (iter_to != params_.end()) {
55 if (!from.Has(*iter_to)) {
56 iter_to = params_.erase(iter_to);
57 } else {
58 ++iter_to;
59 }
60 }
61}
62
63bool FeedbackParams::HasDuplicateEntries() const {
64 for (std::vector<FeedbackParam>::const_iterator iter = params_.begin();
65 iter != params_.end(); ++iter) {
66 for (std::vector<FeedbackParam>::const_iterator found = iter + 1;
67 found != params_.end(); ++found) {
68 if (*found == *iter) {
69 return true;
70 }
71 }
72 }
73 return false;
74}
75
deadbeef67cf2c12016-04-13 10:07:16 -070076Codec::Codec(int id, const std::string& name, int clockrate)
77 : id(id), name(name), clockrate(clockrate) {}
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +000078
deadbeef67cf2c12016-04-13 10:07:16 -070079Codec::Codec() : id(0), clockrate(0) {}
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +000080
81Codec::Codec(const Codec& c) = default;
magjed3663c522016-11-07 10:14:36 -080082Codec::Codec(Codec&& c) = default;
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +000083Codec::~Codec() = default;
magjed3663c522016-11-07 10:14:36 -080084Codec& Codec::operator=(const Codec& c) = default;
85Codec& Codec::operator=(Codec&& c) = default;
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +000086
87bool Codec::operator==(const Codec& c) const {
88 return this->id == c.id && // id is reserved in objective-c
deadbeef67cf2c12016-04-13 10:07:16 -070089 name == c.name && clockrate == c.clockrate && params == c.params &&
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +000090 feedback_params == c.feedback_params;
91}
92
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093bool Codec::Matches(const Codec& codec) const {
94 // Match the codec id/name based on the typical static/dynamic name rules.
95 // Matching is case-insensitive.
pkasting@chromium.orgd3245462015-02-23 21:28:22 +000096 const int kMaxStaticPayloadId = 95;
magjed3663c522016-11-07 10:14:36 -080097 return (id <= kMaxStaticPayloadId || codec.id <= kMaxStaticPayloadId)
98 ? (id == codec.id)
Niels Möller3c7d5992018-10-19 15:29:54 +020099 : (absl::EqualsIgnoreCase(name, codec.name));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100}
101
102bool Codec::GetParam(const std::string& name, std::string* out) const {
103 CodecParameterMap::const_iterator iter = params.find(name);
104 if (iter == params.end())
105 return false;
106 *out = iter->second;
107 return true;
108}
109
110bool Codec::GetParam(const std::string& name, int* out) const {
111 CodecParameterMap::const_iterator iter = params.find(name);
112 if (iter == params.end())
113 return false;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000114 return rtc::FromString(iter->second, out);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115}
116
117void Codec::SetParam(const std::string& name, const std::string& value) {
118 params[name] = value;
119}
120
Yves Gerey665174f2018-06-19 15:03:05 +0200121void Codec::SetParam(const std::string& name, int value) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000122 params[name] = rtc::ToString(value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123}
124
buildbot@webrtc.orgfbd13282014-06-19 19:50:55 +0000125bool Codec::RemoveParam(const std::string& name) {
126 return params.erase(name) == 1;
127}
128
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129void Codec::AddFeedbackParam(const FeedbackParam& param) {
130 feedback_params.Add(param);
131}
132
133bool Codec::HasFeedbackParam(const FeedbackParam& param) const {
134 return feedback_params.Has(param);
135}
136
137void Codec::IntersectFeedbackParams(const Codec& other) {
138 feedback_params.Intersect(other.feedback_params);
139}
140
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -0700141webrtc::RtpCodecParameters Codec::ToCodecParameters() const {
142 webrtc::RtpCodecParameters codec_params;
143 codec_params.payload_type = id;
deadbeefe702b302017-02-04 12:09:01 -0800144 codec_params.name = name;
Oskar Sundbom78807582017-11-16 11:09:55 +0100145 codec_params.clock_rate = clockrate;
Florent Castellib7d9d832018-05-15 18:14:14 +0200146 codec_params.parameters.insert(params.begin(), params.end());
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -0700147 return codec_params;
148}
149
pkasting25702cb2016-01-08 13:50:27 -0800150AudioCodec::AudioCodec(int id,
151 const std::string& name,
152 int clockrate,
153 int bitrate,
deadbeef67cf2c12016-04-13 10:07:16 -0700154 size_t channels)
155 : Codec(id, name, clockrate), bitrate(bitrate), channels(channels) {}
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000156
Yves Gerey665174f2018-06-19 15:03:05 +0200157AudioCodec::AudioCodec() : Codec(), bitrate(0), channels(0) {}
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000158
159AudioCodec::AudioCodec(const AudioCodec& c) = default;
magjed3663c522016-11-07 10:14:36 -0800160AudioCodec::AudioCodec(AudioCodec&& c) = default;
161AudioCodec& AudioCodec::operator=(const AudioCodec& c) = default;
162AudioCodec& AudioCodec::operator=(AudioCodec&& c) = default;
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000163
164bool AudioCodec::operator==(const AudioCodec& c) const {
guoweis@webrtc.orgcce874b2015-02-19 18:14:36 +0000165 return bitrate == c.bitrate && channels == c.channels && Codec::operator==(c);
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000166}
167
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000168bool AudioCodec::Matches(const AudioCodec& codec) const {
169 // If a nonzero clockrate is specified, it must match the actual clockrate.
170 // If a nonzero bitrate is specified, it must match the actual bitrate,
171 // unless the codec is VBR (0), where we just force the supplied value.
172 // The number of channels must match exactly, with the exception
173 // that channels=0 is treated synonymously as channels=1, per RFC
174 // 4566 section 6: " [The channels] parameter is OPTIONAL and may be
175 // omitted if the number of channels is one."
176 // Preference is ignored.
177 // TODO(juberti): Treat a zero clockrate as 8000Hz, the RTP default clockrate.
178 return Codec::Matches(codec) &&
Yves Gerey665174f2018-06-19 15:03:05 +0200179 ((codec.clockrate == 0 /*&& clockrate == 8000*/) ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000180 clockrate == codec.clockrate) &&
Yves Gerey665174f2018-06-19 15:03:05 +0200181 (codec.bitrate == 0 || bitrate <= 0 || bitrate == codec.bitrate) &&
182 ((codec.channels < 2 && channels < 2) || channels == codec.channels);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183}
184
185std::string AudioCodec::ToString() const {
Jonas Olsson88c99562018-05-03 11:45:33 +0200186 char buf[256];
187 rtc::SimpleStringBuilder sb(buf);
188 sb << "AudioCodec[" << id << ":" << name << ":" << clockrate << ":" << bitrate
deadbeef67cf2c12016-04-13 10:07:16 -0700189 << ":" << channels << "]";
Jonas Olsson88c99562018-05-03 11:45:33 +0200190 return sb.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000191}
192
deadbeefe702b302017-02-04 12:09:01 -0800193webrtc::RtpCodecParameters AudioCodec::ToCodecParameters() const {
194 webrtc::RtpCodecParameters codec_params = Codec::ToCodecParameters();
Oskar Sundbom78807582017-11-16 11:09:55 +0100195 codec_params.num_channels = static_cast<int>(channels);
deadbeefe702b302017-02-04 12:09:01 -0800196 codec_params.kind = MEDIA_TYPE_AUDIO;
197 return codec_params;
198}
199
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200std::string VideoCodec::ToString() const {
Jonas Olsson88c99562018-05-03 11:45:33 +0200201 char buf[256];
202 rtc::SimpleStringBuilder sb(buf);
203 sb << "VideoCodec[" << id << ":" << name << "]";
204 return sb.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000205}
206
deadbeefe702b302017-02-04 12:09:01 -0800207webrtc::RtpCodecParameters VideoCodec::ToCodecParameters() const {
208 webrtc::RtpCodecParameters codec_params = Codec::ToCodecParameters();
209 codec_params.kind = MEDIA_TYPE_VIDEO;
210 return codec_params;
211}
212
pkasting25702cb2016-01-08 13:50:27 -0800213VideoCodec::VideoCodec(int id, const std::string& name)
hta9aa96882016-12-06 05:36:03 -0800214 : Codec(id, name, kVideoCodecClockrate) {
215 SetDefaultParameters();
216}
Shao Changbine62202f2015-04-21 20:24:50 +0800217
hta9aa96882016-12-06 05:36:03 -0800218VideoCodec::VideoCodec(const std::string& name) : VideoCodec(0 /* id */, name) {
219 SetDefaultParameters();
220}
magjed1e45cc62016-10-28 07:43:45 -0700221
perkj26752742016-10-24 01:21:16 -0700222VideoCodec::VideoCodec() : Codec() {
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000223 clockrate = kVideoCodecClockrate;
224}
225
Magnus Jedvert024d8972017-09-29 15:00:29 +0200226VideoCodec::VideoCodec(const webrtc::SdpVideoFormat& c)
227 : Codec(0 /* id */, c.name, kVideoCodecClockrate) {
228 params = c.parameters;
229}
230
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000231VideoCodec::VideoCodec(const VideoCodec& c) = default;
magjed3663c522016-11-07 10:14:36 -0800232VideoCodec::VideoCodec(VideoCodec&& c) = default;
233VideoCodec& VideoCodec::operator=(const VideoCodec& c) = default;
234VideoCodec& VideoCodec::operator=(VideoCodec&& c) = default;
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000235
hta9aa96882016-12-06 05:36:03 -0800236void VideoCodec::SetDefaultParameters() {
Niels Möller3c7d5992018-10-19 15:29:54 +0200237 if (absl::EqualsIgnoreCase(kH264CodecName, name)) {
hta9aa96882016-12-06 05:36:03 -0800238 // This default is set for all H.264 codecs created because
239 // that was the default before packetization mode support was added.
240 // TODO(hta): Move this to the places that create VideoCodecs from
241 // SDP or from knowledge of implementation capabilities.
242 SetParam(kH264FmtpPacketizationMode, "1");
243 }
244}
245
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000246bool VideoCodec::operator==(const VideoCodec& c) const {
perkj26752742016-10-24 01:21:16 -0700247 return Codec::operator==(c);
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000248}
249
Steve Anton9c1fb1e2018-02-26 15:09:41 -0800250static bool IsSameH264PacketizationMode(const CodecParameterMap& ours,
251 const CodecParameterMap& theirs) {
252 // If packetization-mode is not present, default to "0".
253 // https://tools.ietf.org/html/rfc6184#section-6.2
254 std::string our_packetization_mode = "0";
255 std::string their_packetization_mode = "0";
256 auto ours_it = ours.find(kH264FmtpPacketizationMode);
257 if (ours_it != ours.end()) {
258 our_packetization_mode = ours_it->second;
259 }
260 auto theirs_it = theirs.find(kH264FmtpPacketizationMode);
261 if (theirs_it != theirs.end()) {
262 their_packetization_mode = theirs_it->second;
263 }
264 return our_packetization_mode == their_packetization_mode;
265}
266
magjedf823ede2016-11-12 09:53:04 -0800267bool VideoCodec::Matches(const VideoCodec& other) const {
268 if (!Codec::Matches(other))
269 return false;
Niels Möller039743e2018-10-23 10:07:25 +0200270 if (absl::EqualsIgnoreCase(name, kH264CodecName))
Steve Anton9c1fb1e2018-02-26 15:09:41 -0800271 return webrtc::H264::IsSameH264Profile(params, other.params) &&
272 IsSameH264PacketizationMode(params, other.params);
Niels Möller039743e2018-10-23 10:07:25 +0200273 if (absl::EqualsIgnoreCase(name, kVp9CodecName))
Emircan Uysaler98badbc2018-06-28 10:59:02 -0700274 return webrtc::IsSameVP9Profile(params, other.params);
magjedf823ede2016-11-12 09:53:04 -0800275 return true;
276}
277
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000278VideoCodec VideoCodec::CreateRtxCodec(int rtx_payload_type,
279 int associated_payload_type) {
perkj26752742016-10-24 01:21:16 -0700280 VideoCodec rtx_codec(rtx_payload_type, kRtxCodecName);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000281 rtx_codec.SetParam(kCodecParamAssociatedPayloadType, associated_payload_type);
282 return rtx_codec;
283}
284
285VideoCodec::CodecType VideoCodec::GetCodecType() const {
286 const char* payload_name = name.c_str();
Niels Möller3c7d5992018-10-19 15:29:54 +0200287 if (absl::EqualsIgnoreCase(payload_name, kRedCodecName)) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000288 return CODEC_RED;
289 }
Niels Möller3c7d5992018-10-19 15:29:54 +0200290 if (absl::EqualsIgnoreCase(payload_name, kUlpfecCodecName)) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000291 return CODEC_ULPFEC;
292 }
Niels Möller3c7d5992018-10-19 15:29:54 +0200293 if (absl::EqualsIgnoreCase(payload_name, kFlexfecCodecName)) {
brandtr87d7d772016-11-07 03:03:41 -0800294 return CODEC_FLEXFEC;
295 }
Niels Möller3c7d5992018-10-19 15:29:54 +0200296 if (absl::EqualsIgnoreCase(payload_name, kRtxCodecName)) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000297 return CODEC_RTX;
298 }
299
300 return CODEC_VIDEO;
301}
302
303bool VideoCodec::ValidateCodecFormat() const {
304 if (id < 0 || id > 127) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100305 RTC_LOG(LS_ERROR) << "Codec with invalid payload type: " << ToString();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000306 return false;
307 }
308 if (GetCodecType() != CODEC_VIDEO) {
309 return true;
310 }
311
312 // Video validation from here on.
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000313 int min_bitrate = -1;
314 int max_bitrate = -1;
315 if (GetParam(kCodecParamMinBitrate, &min_bitrate) &&
316 GetParam(kCodecParamMaxBitrate, &max_bitrate)) {
317 if (max_bitrate < min_bitrate) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100318 RTC_LOG(LS_ERROR) << "Codec with max < min bitrate: " << ToString();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000319 return false;
320 }
321 }
322 return true;
323}
324
deadbeef67cf2c12016-04-13 10:07:16 -0700325DataCodec::DataCodec(int id, const std::string& name)
326 : Codec(id, name, kDataCodecClockrate) {}
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000327
328DataCodec::DataCodec() : Codec() {
329 clockrate = kDataCodecClockrate;
330}
331
332DataCodec::DataCodec(const DataCodec& c) = default;
magjed3663c522016-11-07 10:14:36 -0800333DataCodec::DataCodec(DataCodec&& c) = default;
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000334DataCodec& DataCodec::operator=(const DataCodec& c) = default;
magjed3663c522016-11-07 10:14:36 -0800335DataCodec& DataCodec::operator=(DataCodec&& c) = default;
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000336
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000337std::string DataCodec::ToString() const {
Jonas Olsson88c99562018-05-03 11:45:33 +0200338 char buf[256];
339 rtc::SimpleStringBuilder sb(buf);
340 sb << "DataCodec[" << id << ":" << name << "]";
341 return sb.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000342}
343
stefanba4c0e42016-02-04 04:12:24 -0800344bool HasNack(const Codec& codec) {
Shao Changbine62202f2015-04-21 20:24:50 +0800345 return codec.HasFeedbackParam(
346 FeedbackParam(kRtcpFbParamNack, kParamValueEmpty));
347}
348
stefanba4c0e42016-02-04 04:12:24 -0800349bool HasRemb(const Codec& codec) {
Shao Changbine62202f2015-04-21 20:24:50 +0800350 return codec.HasFeedbackParam(
351 FeedbackParam(kRtcpFbParamRemb, kParamValueEmpty));
352}
353
Ilya Nikolaevskiy634a7772018-04-04 16:33:49 +0200354bool HasRrtr(const Codec& codec) {
355 return codec.HasFeedbackParam(
356 FeedbackParam(kRtcpFbParamRrtr, kParamValueEmpty));
357}
358
stefanba4c0e42016-02-04 04:12:24 -0800359bool HasTransportCc(const Codec& codec) {
stefan43edf0f2015-11-20 18:05:48 -0800360 return codec.HasFeedbackParam(
361 FeedbackParam(kRtcpFbParamTransportCc, kParamValueEmpty));
362}
363
magjedf823ede2016-11-12 09:53:04 -0800364const VideoCodec* FindMatchingCodec(
365 const std::vector<VideoCodec>& supported_codecs,
366 const VideoCodec& codec) {
367 for (const VideoCodec& supported_codec : supported_codecs) {
Magnus Jedvert523589d2017-11-23 13:24:53 +0100368 if (IsSameCodec(codec.name, codec.params, supported_codec.name,
369 supported_codec.params)) {
370 return &supported_codec;
magjedf823ede2016-11-12 09:53:04 -0800371 }
magjedf823ede2016-11-12 09:53:04 -0800372 }
373 return nullptr;
Shao Changbine62202f2015-04-21 20:24:50 +0800374}
375
Magnus Jedvert523589d2017-11-23 13:24:53 +0100376bool IsSameCodec(const std::string& name1,
377 const CodecParameterMap& params1,
378 const std::string& name2,
379 const CodecParameterMap& params2) {
380 // If different names (case insensitive), then not same formats.
Niels Möller039743e2018-10-23 10:07:25 +0200381 if (!absl::EqualsIgnoreCase(name1, name2))
Magnus Jedvert523589d2017-11-23 13:24:53 +0100382 return false;
Emircan Uysaler98badbc2018-06-28 10:59:02 -0700383 // For every format besides H264 and VP9, comparing names is enough.
Niels Möller039743e2018-10-23 10:07:25 +0200384 if (absl::EqualsIgnoreCase(name1, kH264CodecName))
Emircan Uysaler98badbc2018-06-28 10:59:02 -0700385 return webrtc::H264::IsSameH264Profile(params1, params2);
Niels Möller039743e2018-10-23 10:07:25 +0200386 if (absl::EqualsIgnoreCase(name1, kVp9CodecName))
Emircan Uysaler98badbc2018-06-28 10:59:02 -0700387 return webrtc::IsSameVP9Profile(params1, params2);
388 return true;
Magnus Jedvert523589d2017-11-23 13:24:53 +0100389}
390
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000391} // namespace cricket