blob: 56a918d399d327708f96b89b9ac159550a75d039 [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
13#include <algorithm>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014
Niels Möller3c7d5992018-10-19 15:29:54 +020015#include "absl/strings/match.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "media/base/h264_profile_level_id.h"
Emircan Uysaler98badbc2018-06-28 10:59:02 -070017#include "media/base/vp9_profile.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/checks.h"
19#include "rtc_base/logging.h"
20#include "rtc_base/stringencode.h"
Jonas Olsson88c99562018-05-03 11:45:33 +020021#include "rtc_base/strings/string_builder.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000022
23namespace cricket {
24
Magnus Jedvert244ad802017-09-28 21:19:18 +020025FeedbackParams::FeedbackParams() = default;
Paulina Hensmana680a6a2018-04-05 11:42:24 +020026FeedbackParams::~FeedbackParams() = default;
Magnus Jedvert244ad802017-09-28 21:19:18 +020027
henrike@webrtc.org28e20752013-07-10 00:45:36 +000028bool FeedbackParam::operator==(const FeedbackParam& other) const {
Niels Möller3c7d5992018-10-19 15:29:54 +020029 return absl::EqualsIgnoreCase(other.id(), id()) &&
30 absl::EqualsIgnoreCase(other.param(), param());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000031}
32
33bool FeedbackParams::operator==(const FeedbackParams& other) const {
34 return params_ == other.params_;
35}
36
37bool FeedbackParams::Has(const FeedbackParam& param) const {
38 return std::find(params_.begin(), params_.end(), param) != params_.end();
39}
40
41void FeedbackParams::Add(const FeedbackParam& param) {
42 if (param.id().empty()) {
43 return;
44 }
45 if (Has(param)) {
46 // Param already in |this|.
47 return;
48 }
49 params_.push_back(param);
magjed0928a3c2016-11-25 00:40:18 -080050 RTC_CHECK(!HasDuplicateEntries());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051}
52
53void FeedbackParams::Intersect(const FeedbackParams& from) {
54 std::vector<FeedbackParam>::iterator iter_to = params_.begin();
55 while (iter_to != params_.end()) {
56 if (!from.Has(*iter_to)) {
57 iter_to = params_.erase(iter_to);
58 } else {
59 ++iter_to;
60 }
61 }
62}
63
64bool FeedbackParams::HasDuplicateEntries() const {
65 for (std::vector<FeedbackParam>::const_iterator iter = params_.begin();
66 iter != params_.end(); ++iter) {
67 for (std::vector<FeedbackParam>::const_iterator found = iter + 1;
68 found != params_.end(); ++found) {
69 if (*found == *iter) {
70 return true;
71 }
72 }
73 }
74 return false;
75}
76
deadbeef67cf2c12016-04-13 10:07:16 -070077Codec::Codec(int id, const std::string& name, int clockrate)
78 : id(id), name(name), clockrate(clockrate) {}
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +000079
deadbeef67cf2c12016-04-13 10:07:16 -070080Codec::Codec() : id(0), clockrate(0) {}
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +000081
82Codec::Codec(const Codec& c) = default;
magjed3663c522016-11-07 10:14:36 -080083Codec::Codec(Codec&& c) = default;
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +000084Codec::~Codec() = default;
magjed3663c522016-11-07 10:14:36 -080085Codec& Codec::operator=(const Codec& c) = default;
86Codec& Codec::operator=(Codec&& c) = default;
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +000087
88bool Codec::operator==(const Codec& c) const {
89 return this->id == c.id && // id is reserved in objective-c
deadbeef67cf2c12016-04-13 10:07:16 -070090 name == c.name && clockrate == c.clockrate && params == c.params &&
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +000091 feedback_params == c.feedback_params;
92}
93
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094bool Codec::Matches(const Codec& codec) const {
95 // Match the codec id/name based on the typical static/dynamic name rules.
96 // Matching is case-insensitive.
pkasting@chromium.orgd3245462015-02-23 21:28:22 +000097 const int kMaxStaticPayloadId = 95;
magjed3663c522016-11-07 10:14:36 -080098 return (id <= kMaxStaticPayloadId || codec.id <= kMaxStaticPayloadId)
99 ? (id == codec.id)
Niels Möller3c7d5992018-10-19 15:29:54 +0200100 : (absl::EqualsIgnoreCase(name, codec.name));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101}
102
103bool Codec::GetParam(const std::string& name, std::string* out) const {
104 CodecParameterMap::const_iterator iter = params.find(name);
105 if (iter == params.end())
106 return false;
107 *out = iter->second;
108 return true;
109}
110
111bool Codec::GetParam(const std::string& name, int* out) const {
112 CodecParameterMap::const_iterator iter = params.find(name);
113 if (iter == params.end())
114 return false;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000115 return rtc::FromString(iter->second, out);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116}
117
118void Codec::SetParam(const std::string& name, const std::string& value) {
119 params[name] = value;
120}
121
Yves Gerey665174f2018-06-19 15:03:05 +0200122void Codec::SetParam(const std::string& name, int value) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000123 params[name] = rtc::ToString(value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124}
125
buildbot@webrtc.orgfbd13282014-06-19 19:50:55 +0000126bool Codec::RemoveParam(const std::string& name) {
127 return params.erase(name) == 1;
128}
129
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130void Codec::AddFeedbackParam(const FeedbackParam& param) {
131 feedback_params.Add(param);
132}
133
134bool Codec::HasFeedbackParam(const FeedbackParam& param) const {
135 return feedback_params.Has(param);
136}
137
138void Codec::IntersectFeedbackParams(const Codec& other) {
139 feedback_params.Intersect(other.feedback_params);
140}
141
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -0700142webrtc::RtpCodecParameters Codec::ToCodecParameters() const {
143 webrtc::RtpCodecParameters codec_params;
144 codec_params.payload_type = id;
deadbeefe702b302017-02-04 12:09:01 -0800145 codec_params.name = name;
Oskar Sundbom78807582017-11-16 11:09:55 +0100146 codec_params.clock_rate = clockrate;
Florent Castellib7d9d832018-05-15 18:14:14 +0200147 codec_params.parameters.insert(params.begin(), params.end());
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -0700148 return codec_params;
149}
150
pkasting25702cb2016-01-08 13:50:27 -0800151AudioCodec::AudioCodec(int id,
152 const std::string& name,
153 int clockrate,
154 int bitrate,
deadbeef67cf2c12016-04-13 10:07:16 -0700155 size_t channels)
156 : Codec(id, name, clockrate), bitrate(bitrate), channels(channels) {}
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000157
Yves Gerey665174f2018-06-19 15:03:05 +0200158AudioCodec::AudioCodec() : Codec(), bitrate(0), channels(0) {}
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000159
160AudioCodec::AudioCodec(const AudioCodec& c) = default;
magjed3663c522016-11-07 10:14:36 -0800161AudioCodec::AudioCodec(AudioCodec&& c) = default;
162AudioCodec& AudioCodec::operator=(const AudioCodec& c) = default;
163AudioCodec& AudioCodec::operator=(AudioCodec&& c) = default;
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000164
165bool AudioCodec::operator==(const AudioCodec& c) const {
guoweis@webrtc.orgcce874b2015-02-19 18:14:36 +0000166 return bitrate == c.bitrate && channels == c.channels && Codec::operator==(c);
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000167}
168
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169bool AudioCodec::Matches(const AudioCodec& codec) const {
170 // If a nonzero clockrate is specified, it must match the actual clockrate.
171 // If a nonzero bitrate is specified, it must match the actual bitrate,
172 // unless the codec is VBR (0), where we just force the supplied value.
173 // The number of channels must match exactly, with the exception
174 // that channels=0 is treated synonymously as channels=1, per RFC
175 // 4566 section 6: " [The channels] parameter is OPTIONAL and may be
176 // omitted if the number of channels is one."
177 // Preference is ignored.
178 // TODO(juberti): Treat a zero clockrate as 8000Hz, the RTP default clockrate.
179 return Codec::Matches(codec) &&
Yves Gerey665174f2018-06-19 15:03:05 +0200180 ((codec.clockrate == 0 /*&& clockrate == 8000*/) ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181 clockrate == codec.clockrate) &&
Yves Gerey665174f2018-06-19 15:03:05 +0200182 (codec.bitrate == 0 || bitrate <= 0 || bitrate == codec.bitrate) &&
183 ((codec.channels < 2 && channels < 2) || channels == codec.channels);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000184}
185
186std::string AudioCodec::ToString() const {
Jonas Olsson88c99562018-05-03 11:45:33 +0200187 char buf[256];
188 rtc::SimpleStringBuilder sb(buf);
189 sb << "AudioCodec[" << id << ":" << name << ":" << clockrate << ":" << bitrate
deadbeef67cf2c12016-04-13 10:07:16 -0700190 << ":" << channels << "]";
Jonas Olsson88c99562018-05-03 11:45:33 +0200191 return sb.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192}
193
deadbeefe702b302017-02-04 12:09:01 -0800194webrtc::RtpCodecParameters AudioCodec::ToCodecParameters() const {
195 webrtc::RtpCodecParameters codec_params = Codec::ToCodecParameters();
Oskar Sundbom78807582017-11-16 11:09:55 +0100196 codec_params.num_channels = static_cast<int>(channels);
deadbeefe702b302017-02-04 12:09:01 -0800197 codec_params.kind = MEDIA_TYPE_AUDIO;
198 return codec_params;
199}
200
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201std::string VideoCodec::ToString() const {
Jonas Olsson88c99562018-05-03 11:45:33 +0200202 char buf[256];
203 rtc::SimpleStringBuilder sb(buf);
204 sb << "VideoCodec[" << id << ":" << name << "]";
205 return sb.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206}
207
deadbeefe702b302017-02-04 12:09:01 -0800208webrtc::RtpCodecParameters VideoCodec::ToCodecParameters() const {
209 webrtc::RtpCodecParameters codec_params = Codec::ToCodecParameters();
210 codec_params.kind = MEDIA_TYPE_VIDEO;
211 return codec_params;
212}
213
pkasting25702cb2016-01-08 13:50:27 -0800214VideoCodec::VideoCodec(int id, const std::string& name)
hta9aa96882016-12-06 05:36:03 -0800215 : Codec(id, name, kVideoCodecClockrate) {
216 SetDefaultParameters();
217}
Shao Changbine62202f2015-04-21 20:24:50 +0800218
hta9aa96882016-12-06 05:36:03 -0800219VideoCodec::VideoCodec(const std::string& name) : VideoCodec(0 /* id */, name) {
220 SetDefaultParameters();
221}
magjed1e45cc62016-10-28 07:43:45 -0700222
perkj26752742016-10-24 01:21:16 -0700223VideoCodec::VideoCodec() : Codec() {
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000224 clockrate = kVideoCodecClockrate;
225}
226
Magnus Jedvert024d8972017-09-29 15:00:29 +0200227VideoCodec::VideoCodec(const webrtc::SdpVideoFormat& c)
228 : Codec(0 /* id */, c.name, kVideoCodecClockrate) {
229 params = c.parameters;
230}
231
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000232VideoCodec::VideoCodec(const VideoCodec& c) = default;
magjed3663c522016-11-07 10:14:36 -0800233VideoCodec::VideoCodec(VideoCodec&& c) = default;
234VideoCodec& VideoCodec::operator=(const VideoCodec& c) = default;
235VideoCodec& VideoCodec::operator=(VideoCodec&& c) = default;
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000236
hta9aa96882016-12-06 05:36:03 -0800237void VideoCodec::SetDefaultParameters() {
Niels Möller3c7d5992018-10-19 15:29:54 +0200238 if (absl::EqualsIgnoreCase(kH264CodecName, name)) {
hta9aa96882016-12-06 05:36:03 -0800239 // This default is set for all H.264 codecs created because
240 // that was the default before packetization mode support was added.
241 // TODO(hta): Move this to the places that create VideoCodecs from
242 // SDP or from knowledge of implementation capabilities.
243 SetParam(kH264FmtpPacketizationMode, "1");
244 }
245}
246
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000247bool VideoCodec::operator==(const VideoCodec& c) const {
perkj26752742016-10-24 01:21:16 -0700248 return Codec::operator==(c);
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000249}
250
Steve Anton9c1fb1e2018-02-26 15:09:41 -0800251static bool IsSameH264PacketizationMode(const CodecParameterMap& ours,
252 const CodecParameterMap& theirs) {
253 // If packetization-mode is not present, default to "0".
254 // https://tools.ietf.org/html/rfc6184#section-6.2
255 std::string our_packetization_mode = "0";
256 std::string their_packetization_mode = "0";
257 auto ours_it = ours.find(kH264FmtpPacketizationMode);
258 if (ours_it != ours.end()) {
259 our_packetization_mode = ours_it->second;
260 }
261 auto theirs_it = theirs.find(kH264FmtpPacketizationMode);
262 if (theirs_it != theirs.end()) {
263 their_packetization_mode = theirs_it->second;
264 }
265 return our_packetization_mode == their_packetization_mode;
266}
267
magjedf823ede2016-11-12 09:53:04 -0800268bool VideoCodec::Matches(const VideoCodec& other) const {
269 if (!Codec::Matches(other))
270 return false;
271 if (CodecNamesEq(name.c_str(), kH264CodecName))
Steve Anton9c1fb1e2018-02-26 15:09:41 -0800272 return webrtc::H264::IsSameH264Profile(params, other.params) &&
273 IsSameH264PacketizationMode(params, other.params);
Emircan Uysaler98badbc2018-06-28 10:59:02 -0700274 if (CodecNamesEq(name.c_str(), kVp9CodecName))
275 return webrtc::IsSameVP9Profile(params, other.params);
magjedf823ede2016-11-12 09:53:04 -0800276 return true;
277}
278
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000279VideoCodec VideoCodec::CreateRtxCodec(int rtx_payload_type,
280 int associated_payload_type) {
perkj26752742016-10-24 01:21:16 -0700281 VideoCodec rtx_codec(rtx_payload_type, kRtxCodecName);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000282 rtx_codec.SetParam(kCodecParamAssociatedPayloadType, associated_payload_type);
283 return rtx_codec;
284}
285
286VideoCodec::CodecType VideoCodec::GetCodecType() const {
287 const char* payload_name = name.c_str();
Niels Möller3c7d5992018-10-19 15:29:54 +0200288 if (absl::EqualsIgnoreCase(payload_name, kRedCodecName)) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000289 return CODEC_RED;
290 }
Niels Möller3c7d5992018-10-19 15:29:54 +0200291 if (absl::EqualsIgnoreCase(payload_name, kUlpfecCodecName)) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000292 return CODEC_ULPFEC;
293 }
Niels Möller3c7d5992018-10-19 15:29:54 +0200294 if (absl::EqualsIgnoreCase(payload_name, kFlexfecCodecName)) {
brandtr87d7d772016-11-07 03:03:41 -0800295 return CODEC_FLEXFEC;
296 }
Niels Möller3c7d5992018-10-19 15:29:54 +0200297 if (absl::EqualsIgnoreCase(payload_name, kRtxCodecName)) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000298 return CODEC_RTX;
299 }
300
301 return CODEC_VIDEO;
302}
303
304bool VideoCodec::ValidateCodecFormat() const {
305 if (id < 0 || id > 127) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100306 RTC_LOG(LS_ERROR) << "Codec with invalid payload type: " << ToString();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000307 return false;
308 }
309 if (GetCodecType() != CODEC_VIDEO) {
310 return true;
311 }
312
313 // Video validation from here on.
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000314 int min_bitrate = -1;
315 int max_bitrate = -1;
316 if (GetParam(kCodecParamMinBitrate, &min_bitrate) &&
317 GetParam(kCodecParamMaxBitrate, &max_bitrate)) {
318 if (max_bitrate < min_bitrate) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100319 RTC_LOG(LS_ERROR) << "Codec with max < min bitrate: " << ToString();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000320 return false;
321 }
322 }
323 return true;
324}
325
deadbeef67cf2c12016-04-13 10:07:16 -0700326DataCodec::DataCodec(int id, const std::string& name)
327 : Codec(id, name, kDataCodecClockrate) {}
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000328
329DataCodec::DataCodec() : Codec() {
330 clockrate = kDataCodecClockrate;
331}
332
333DataCodec::DataCodec(const DataCodec& c) = default;
magjed3663c522016-11-07 10:14:36 -0800334DataCodec::DataCodec(DataCodec&& c) = default;
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000335DataCodec& DataCodec::operator=(const DataCodec& c) = default;
magjed3663c522016-11-07 10:14:36 -0800336DataCodec& DataCodec::operator=(DataCodec&& c) = default;
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000337
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338std::string DataCodec::ToString() const {
Jonas Olsson88c99562018-05-03 11:45:33 +0200339 char buf[256];
340 rtc::SimpleStringBuilder sb(buf);
341 sb << "DataCodec[" << id << ":" << name << "]";
342 return sb.str();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000343}
344
stefanba4c0e42016-02-04 04:12:24 -0800345bool HasNack(const Codec& codec) {
Shao Changbine62202f2015-04-21 20:24:50 +0800346 return codec.HasFeedbackParam(
347 FeedbackParam(kRtcpFbParamNack, kParamValueEmpty));
348}
349
stefanba4c0e42016-02-04 04:12:24 -0800350bool HasRemb(const Codec& codec) {
Shao Changbine62202f2015-04-21 20:24:50 +0800351 return codec.HasFeedbackParam(
352 FeedbackParam(kRtcpFbParamRemb, kParamValueEmpty));
353}
354
Ilya Nikolaevskiy634a7772018-04-04 16:33:49 +0200355bool HasRrtr(const Codec& codec) {
356 return codec.HasFeedbackParam(
357 FeedbackParam(kRtcpFbParamRrtr, kParamValueEmpty));
358}
359
stefanba4c0e42016-02-04 04:12:24 -0800360bool HasTransportCc(const Codec& codec) {
stefan43edf0f2015-11-20 18:05:48 -0800361 return codec.HasFeedbackParam(
362 FeedbackParam(kRtcpFbParamTransportCc, kParamValueEmpty));
363}
364
Niels Möller3c7d5992018-10-19 15:29:54 +0200365// TODO(nisse): Delete these wrappers.
Shao Changbine62202f2015-04-21 20:24:50 +0800366bool CodecNamesEq(const std::string& name1, const std::string& name2) {
magjed1e45cc62016-10-28 07:43:45 -0700367 return CodecNamesEq(name1.c_str(), name2.c_str());
368}
369
370bool CodecNamesEq(const char* name1, const char* name2) {
Niels Möller3c7d5992018-10-19 15:29:54 +0200371 return absl::EqualsIgnoreCase(name1, name2);
magjed1e45cc62016-10-28 07:43:45 -0700372}
373
magjedf823ede2016-11-12 09:53:04 -0800374const VideoCodec* FindMatchingCodec(
375 const std::vector<VideoCodec>& supported_codecs,
376 const VideoCodec& codec) {
377 for (const VideoCodec& supported_codec : supported_codecs) {
Magnus Jedvert523589d2017-11-23 13:24:53 +0100378 if (IsSameCodec(codec.name, codec.params, supported_codec.name,
379 supported_codec.params)) {
380 return &supported_codec;
magjedf823ede2016-11-12 09:53:04 -0800381 }
magjedf823ede2016-11-12 09:53:04 -0800382 }
383 return nullptr;
Shao Changbine62202f2015-04-21 20:24:50 +0800384}
385
Magnus Jedvert523589d2017-11-23 13:24:53 +0100386bool IsSameCodec(const std::string& name1,
387 const CodecParameterMap& params1,
388 const std::string& name2,
389 const CodecParameterMap& params2) {
390 // If different names (case insensitive), then not same formats.
391 if (!CodecNamesEq(name1, name2))
392 return false;
Emircan Uysaler98badbc2018-06-28 10:59:02 -0700393 // For every format besides H264 and VP9, comparing names is enough.
394 if (CodecNamesEq(name1.c_str(), kH264CodecName))
395 return webrtc::H264::IsSameH264Profile(params1, params2);
396 if (CodecNamesEq(name1.c_str(), kVp9CodecName))
397 return webrtc::IsSameVP9Profile(params1, params2);
398 return true;
Magnus Jedvert523589d2017-11-23 13:24:53 +0100399}
400
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000401} // namespace cricket