blob: e4b6b7899bf924596a39c6736c00410b6e2b4f30 [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>
14#include <sstream>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "media/base/h264_profile_level_id.h"
17#include "rtc_base/checks.h"
18#include "rtc_base/logging.h"
19#include "rtc_base/stringencode.h"
20#include "rtc_base/stringutils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021
22namespace cricket {
23
Magnus Jedvert244ad802017-09-28 21:19:18 +020024FeedbackParams::FeedbackParams() = default;
25
henrike@webrtc.org28e20752013-07-10 00:45:36 +000026bool FeedbackParam::operator==(const FeedbackParam& other) const {
27 return _stricmp(other.id().c_str(), id().c_str()) == 0 &&
28 _stricmp(other.param().c_str(), param().c_str()) == 0;
29}
30
31bool FeedbackParams::operator==(const FeedbackParams& other) const {
32 return params_ == other.params_;
33}
34
35bool FeedbackParams::Has(const FeedbackParam& param) const {
36 return std::find(params_.begin(), params_.end(), param) != params_.end();
37}
38
39void FeedbackParams::Add(const FeedbackParam& param) {
40 if (param.id().empty()) {
41 return;
42 }
43 if (Has(param)) {
44 // Param already in |this|.
45 return;
46 }
47 params_.push_back(param);
magjed0928a3c2016-11-25 00:40:18 -080048 RTC_CHECK(!HasDuplicateEntries());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049}
50
51void FeedbackParams::Intersect(const FeedbackParams& from) {
52 std::vector<FeedbackParam>::iterator iter_to = params_.begin();
53 while (iter_to != params_.end()) {
54 if (!from.Has(*iter_to)) {
55 iter_to = params_.erase(iter_to);
56 } else {
57 ++iter_to;
58 }
59 }
60}
61
62bool FeedbackParams::HasDuplicateEntries() const {
63 for (std::vector<FeedbackParam>::const_iterator iter = params_.begin();
64 iter != params_.end(); ++iter) {
65 for (std::vector<FeedbackParam>::const_iterator found = iter + 1;
66 found != params_.end(); ++found) {
67 if (*found == *iter) {
68 return true;
69 }
70 }
71 }
72 return false;
73}
74
deadbeef67cf2c12016-04-13 10:07:16 -070075Codec::Codec(int id, const std::string& name, int clockrate)
76 : id(id), name(name), clockrate(clockrate) {}
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +000077
deadbeef67cf2c12016-04-13 10:07:16 -070078Codec::Codec() : id(0), clockrate(0) {}
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +000079
80Codec::Codec(const Codec& c) = default;
magjed3663c522016-11-07 10:14:36 -080081Codec::Codec(Codec&& c) = default;
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +000082Codec::~Codec() = default;
magjed3663c522016-11-07 10:14:36 -080083Codec& Codec::operator=(const Codec& c) = default;
84Codec& Codec::operator=(Codec&& c) = default;
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +000085
86bool Codec::operator==(const Codec& c) const {
87 return this->id == c.id && // id is reserved in objective-c
deadbeef67cf2c12016-04-13 10:07:16 -070088 name == c.name && clockrate == c.clockrate && params == c.params &&
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +000089 feedback_params == c.feedback_params;
90}
91
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092bool Codec::Matches(const Codec& codec) const {
93 // Match the codec id/name based on the typical static/dynamic name rules.
94 // Matching is case-insensitive.
pkasting@chromium.orgd3245462015-02-23 21:28:22 +000095 const int kMaxStaticPayloadId = 95;
magjed3663c522016-11-07 10:14:36 -080096 return (id <= kMaxStaticPayloadId || codec.id <= kMaxStaticPayloadId)
97 ? (id == codec.id)
98 : (_stricmp(name.c_str(), codec.name.c_str()) == 0);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099}
100
101bool Codec::GetParam(const std::string& name, std::string* out) const {
102 CodecParameterMap::const_iterator iter = params.find(name);
103 if (iter == params.end())
104 return false;
105 *out = iter->second;
106 return true;
107}
108
109bool Codec::GetParam(const std::string& name, int* out) const {
110 CodecParameterMap::const_iterator iter = params.find(name);
111 if (iter == params.end())
112 return false;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000113 return rtc::FromString(iter->second, out);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114}
115
116void Codec::SetParam(const std::string& name, const std::string& value) {
117 params[name] = value;
118}
119
120void Codec::SetParam(const std::string& name, int value) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000121 params[name] = rtc::ToString(value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122}
123
buildbot@webrtc.orgfbd13282014-06-19 19:50:55 +0000124bool Codec::RemoveParam(const std::string& name) {
125 return params.erase(name) == 1;
126}
127
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128void Codec::AddFeedbackParam(const FeedbackParam& param) {
129 feedback_params.Add(param);
130}
131
132bool Codec::HasFeedbackParam(const FeedbackParam& param) const {
133 return feedback_params.Has(param);
134}
135
136void Codec::IntersectFeedbackParams(const Codec& other) {
137 feedback_params.Intersect(other.feedback_params);
138}
139
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -0700140webrtc::RtpCodecParameters Codec::ToCodecParameters() const {
141 webrtc::RtpCodecParameters codec_params;
142 codec_params.payload_type = id;
deadbeefe702b302017-02-04 12:09:01 -0800143 codec_params.name = name;
Oskar Sundbom78807582017-11-16 11:09:55 +0100144 codec_params.clock_rate = clockrate;
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -0700145 return codec_params;
146}
147
pkasting25702cb2016-01-08 13:50:27 -0800148AudioCodec::AudioCodec(int id,
149 const std::string& name,
150 int clockrate,
151 int bitrate,
deadbeef67cf2c12016-04-13 10:07:16 -0700152 size_t channels)
153 : Codec(id, name, clockrate), bitrate(bitrate), channels(channels) {}
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000154
guoweis@webrtc.orgcce874b2015-02-19 18:14:36 +0000155AudioCodec::AudioCodec() : Codec(), bitrate(0), channels(0) {
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000156}
157
158AudioCodec::AudioCodec(const AudioCodec& c) = default;
magjed3663c522016-11-07 10:14:36 -0800159AudioCodec::AudioCodec(AudioCodec&& c) = default;
160AudioCodec& AudioCodec::operator=(const AudioCodec& c) = default;
161AudioCodec& AudioCodec::operator=(AudioCodec&& c) = default;
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000162
163bool AudioCodec::operator==(const AudioCodec& c) const {
guoweis@webrtc.orgcce874b2015-02-19 18:14:36 +0000164 return bitrate == c.bitrate && channels == c.channels && Codec::operator==(c);
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000165}
166
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167bool AudioCodec::Matches(const AudioCodec& codec) const {
168 // If a nonzero clockrate is specified, it must match the actual clockrate.
169 // If a nonzero bitrate is specified, it must match the actual bitrate,
170 // unless the codec is VBR (0), where we just force the supplied value.
171 // The number of channels must match exactly, with the exception
172 // that channels=0 is treated synonymously as channels=1, per RFC
173 // 4566 section 6: " [The channels] parameter is OPTIONAL and may be
174 // omitted if the number of channels is one."
175 // Preference is ignored.
176 // TODO(juberti): Treat a zero clockrate as 8000Hz, the RTP default clockrate.
177 return Codec::Matches(codec) &&
178 ((codec.clockrate == 0 /*&& clockrate == 8000*/) ||
179 clockrate == codec.clockrate) &&
180 (codec.bitrate == 0 || bitrate <= 0 || bitrate == codec.bitrate) &&
181 ((codec.channels < 2 && channels < 2) || channels == codec.channels);
182}
183
184std::string AudioCodec::ToString() const {
185 std::ostringstream os;
186 os << "AudioCodec[" << id << ":" << name << ":" << clockrate << ":" << bitrate
deadbeef67cf2c12016-04-13 10:07:16 -0700187 << ":" << channels << "]";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000188 return os.str();
189}
190
deadbeefe702b302017-02-04 12:09:01 -0800191webrtc::RtpCodecParameters AudioCodec::ToCodecParameters() const {
192 webrtc::RtpCodecParameters codec_params = Codec::ToCodecParameters();
Oskar Sundbom78807582017-11-16 11:09:55 +0100193 codec_params.num_channels = static_cast<int>(channels);
deadbeefe702b302017-02-04 12:09:01 -0800194 codec_params.kind = MEDIA_TYPE_AUDIO;
195 return codec_params;
196}
197
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198std::string VideoCodec::ToString() const {
199 std::ostringstream os;
perkj26752742016-10-24 01:21:16 -0700200 os << "VideoCodec[" << id << ":" << name << "]";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201 return os.str();
202}
203
deadbeefe702b302017-02-04 12:09:01 -0800204webrtc::RtpCodecParameters VideoCodec::ToCodecParameters() const {
205 webrtc::RtpCodecParameters codec_params = Codec::ToCodecParameters();
206 codec_params.kind = MEDIA_TYPE_VIDEO;
207 return codec_params;
208}
209
pkasting25702cb2016-01-08 13:50:27 -0800210VideoCodec::VideoCodec(int id, const std::string& name)
hta9aa96882016-12-06 05:36:03 -0800211 : Codec(id, name, kVideoCodecClockrate) {
212 SetDefaultParameters();
213}
Shao Changbine62202f2015-04-21 20:24:50 +0800214
hta9aa96882016-12-06 05:36:03 -0800215VideoCodec::VideoCodec(const std::string& name) : VideoCodec(0 /* id */, name) {
216 SetDefaultParameters();
217}
magjed1e45cc62016-10-28 07:43:45 -0700218
perkj26752742016-10-24 01:21:16 -0700219VideoCodec::VideoCodec() : Codec() {
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000220 clockrate = kVideoCodecClockrate;
221}
222
Magnus Jedvert024d8972017-09-29 15:00:29 +0200223VideoCodec::VideoCodec(const webrtc::SdpVideoFormat& c)
224 : Codec(0 /* id */, c.name, kVideoCodecClockrate) {
225 params = c.parameters;
226}
227
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000228VideoCodec::VideoCodec(const VideoCodec& c) = default;
magjed3663c522016-11-07 10:14:36 -0800229VideoCodec::VideoCodec(VideoCodec&& c) = default;
230VideoCodec& VideoCodec::operator=(const VideoCodec& c) = default;
231VideoCodec& VideoCodec::operator=(VideoCodec&& c) = default;
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000232
hta9aa96882016-12-06 05:36:03 -0800233void VideoCodec::SetDefaultParameters() {
234 if (_stricmp(kH264CodecName, name.c_str()) == 0) {
235 // This default is set for all H.264 codecs created because
236 // that was the default before packetization mode support was added.
237 // TODO(hta): Move this to the places that create VideoCodecs from
238 // SDP or from knowledge of implementation capabilities.
239 SetParam(kH264FmtpPacketizationMode, "1");
240 }
241}
242
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000243bool VideoCodec::operator==(const VideoCodec& c) const {
perkj26752742016-10-24 01:21:16 -0700244 return Codec::operator==(c);
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000245}
246
Steve Anton9c1fb1e2018-02-26 15:09:41 -0800247static bool IsSameH264PacketizationMode(const CodecParameterMap& ours,
248 const CodecParameterMap& theirs) {
249 // If packetization-mode is not present, default to "0".
250 // https://tools.ietf.org/html/rfc6184#section-6.2
251 std::string our_packetization_mode = "0";
252 std::string their_packetization_mode = "0";
253 auto ours_it = ours.find(kH264FmtpPacketizationMode);
254 if (ours_it != ours.end()) {
255 our_packetization_mode = ours_it->second;
256 }
257 auto theirs_it = theirs.find(kH264FmtpPacketizationMode);
258 if (theirs_it != theirs.end()) {
259 their_packetization_mode = theirs_it->second;
260 }
261 return our_packetization_mode == their_packetization_mode;
262}
263
magjedf823ede2016-11-12 09:53:04 -0800264bool VideoCodec::Matches(const VideoCodec& other) const {
265 if (!Codec::Matches(other))
266 return false;
267 if (CodecNamesEq(name.c_str(), kH264CodecName))
Steve Anton9c1fb1e2018-02-26 15:09:41 -0800268 return webrtc::H264::IsSameH264Profile(params, other.params) &&
269 IsSameH264PacketizationMode(params, other.params);
magjedf823ede2016-11-12 09:53:04 -0800270 return true;
271}
272
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000273VideoCodec VideoCodec::CreateRtxCodec(int rtx_payload_type,
274 int associated_payload_type) {
perkj26752742016-10-24 01:21:16 -0700275 VideoCodec rtx_codec(rtx_payload_type, kRtxCodecName);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000276 rtx_codec.SetParam(kCodecParamAssociatedPayloadType, associated_payload_type);
277 return rtx_codec;
278}
279
280VideoCodec::CodecType VideoCodec::GetCodecType() const {
281 const char* payload_name = name.c_str();
282 if (_stricmp(payload_name, kRedCodecName) == 0) {
283 return CODEC_RED;
284 }
285 if (_stricmp(payload_name, kUlpfecCodecName) == 0) {
286 return CODEC_ULPFEC;
287 }
brandtr87d7d772016-11-07 03:03:41 -0800288 if (_stricmp(payload_name, kFlexfecCodecName) == 0) {
289 return CODEC_FLEXFEC;
290 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000291 if (_stricmp(payload_name, kRtxCodecName) == 0) {
292 return CODEC_RTX;
293 }
294
295 return CODEC_VIDEO;
296}
297
298bool VideoCodec::ValidateCodecFormat() const {
299 if (id < 0 || id > 127) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100300 RTC_LOG(LS_ERROR) << "Codec with invalid payload type: " << ToString();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000301 return false;
302 }
303 if (GetCodecType() != CODEC_VIDEO) {
304 return true;
305 }
306
307 // Video validation from here on.
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000308 int min_bitrate = -1;
309 int max_bitrate = -1;
310 if (GetParam(kCodecParamMinBitrate, &min_bitrate) &&
311 GetParam(kCodecParamMaxBitrate, &max_bitrate)) {
312 if (max_bitrate < min_bitrate) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100313 RTC_LOG(LS_ERROR) << "Codec with max < min bitrate: " << ToString();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000314 return false;
315 }
316 }
317 return true;
318}
319
deadbeef67cf2c12016-04-13 10:07:16 -0700320DataCodec::DataCodec(int id, const std::string& name)
321 : Codec(id, name, kDataCodecClockrate) {}
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000322
323DataCodec::DataCodec() : Codec() {
324 clockrate = kDataCodecClockrate;
325}
326
327DataCodec::DataCodec(const DataCodec& c) = default;
magjed3663c522016-11-07 10:14:36 -0800328DataCodec::DataCodec(DataCodec&& c) = default;
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000329DataCodec& DataCodec::operator=(const DataCodec& c) = default;
magjed3663c522016-11-07 10:14:36 -0800330DataCodec& DataCodec::operator=(DataCodec&& c) = default;
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000331
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000332std::string DataCodec::ToString() const {
333 std::ostringstream os;
334 os << "DataCodec[" << id << ":" << name << "]";
335 return os.str();
336}
337
stefanba4c0e42016-02-04 04:12:24 -0800338bool HasNack(const Codec& codec) {
Shao Changbine62202f2015-04-21 20:24:50 +0800339 return codec.HasFeedbackParam(
340 FeedbackParam(kRtcpFbParamNack, kParamValueEmpty));
341}
342
stefanba4c0e42016-02-04 04:12:24 -0800343bool HasRemb(const Codec& codec) {
Shao Changbine62202f2015-04-21 20:24:50 +0800344 return codec.HasFeedbackParam(
345 FeedbackParam(kRtcpFbParamRemb, kParamValueEmpty));
346}
347
stefanba4c0e42016-02-04 04:12:24 -0800348bool HasTransportCc(const Codec& codec) {
stefan43edf0f2015-11-20 18:05:48 -0800349 return codec.HasFeedbackParam(
350 FeedbackParam(kRtcpFbParamTransportCc, kParamValueEmpty));
351}
352
Shao Changbine62202f2015-04-21 20:24:50 +0800353bool CodecNamesEq(const std::string& name1, const std::string& name2) {
magjed1e45cc62016-10-28 07:43:45 -0700354 return CodecNamesEq(name1.c_str(), name2.c_str());
355}
356
357bool CodecNamesEq(const char* name1, const char* name2) {
358 return _stricmp(name1, name2) == 0;
359}
360
magjedf823ede2016-11-12 09:53:04 -0800361const VideoCodec* FindMatchingCodec(
362 const std::vector<VideoCodec>& supported_codecs,
363 const VideoCodec& codec) {
364 for (const VideoCodec& supported_codec : supported_codecs) {
Magnus Jedvert523589d2017-11-23 13:24:53 +0100365 if (IsSameCodec(codec.name, codec.params, supported_codec.name,
366 supported_codec.params)) {
367 return &supported_codec;
magjedf823ede2016-11-12 09:53:04 -0800368 }
magjedf823ede2016-11-12 09:53:04 -0800369 }
370 return nullptr;
Shao Changbine62202f2015-04-21 20:24:50 +0800371}
372
Magnus Jedvert523589d2017-11-23 13:24:53 +0100373bool IsSameCodec(const std::string& name1,
374 const CodecParameterMap& params1,
375 const std::string& name2,
376 const CodecParameterMap& params2) {
377 // If different names (case insensitive), then not same formats.
378 if (!CodecNamesEq(name1, name2))
379 return false;
380 // For every format besides H264, comparing names is enough.
381 return !CodecNamesEq(name1.c_str(), kH264CodecName) ||
382 webrtc::H264::IsSameH264Profile(params1, params2);
383}
384
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000385} // namespace cricket