blob: 98e52d6848900ede2b9f47be2b469a4d6f6c188f [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
magjedf823ede2016-11-12 09:53:04 -0800247bool VideoCodec::Matches(const VideoCodec& other) const {
248 if (!Codec::Matches(other))
249 return false;
250 if (CodecNamesEq(name.c_str(), kH264CodecName))
Magnus Jedvert523589d2017-11-23 13:24:53 +0100251 return webrtc::H264::IsSameH264Profile(params, other.params);
magjedf823ede2016-11-12 09:53:04 -0800252 return true;
253}
254
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000255VideoCodec VideoCodec::CreateRtxCodec(int rtx_payload_type,
256 int associated_payload_type) {
perkj26752742016-10-24 01:21:16 -0700257 VideoCodec rtx_codec(rtx_payload_type, kRtxCodecName);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000258 rtx_codec.SetParam(kCodecParamAssociatedPayloadType, associated_payload_type);
259 return rtx_codec;
260}
261
262VideoCodec::CodecType VideoCodec::GetCodecType() const {
263 const char* payload_name = name.c_str();
264 if (_stricmp(payload_name, kRedCodecName) == 0) {
265 return CODEC_RED;
266 }
267 if (_stricmp(payload_name, kUlpfecCodecName) == 0) {
268 return CODEC_ULPFEC;
269 }
brandtr87d7d772016-11-07 03:03:41 -0800270 if (_stricmp(payload_name, kFlexfecCodecName) == 0) {
271 return CODEC_FLEXFEC;
272 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000273 if (_stricmp(payload_name, kRtxCodecName) == 0) {
274 return CODEC_RTX;
275 }
276
277 return CODEC_VIDEO;
278}
279
280bool VideoCodec::ValidateCodecFormat() const {
281 if (id < 0 || id > 127) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100282 RTC_LOG(LS_ERROR) << "Codec with invalid payload type: " << ToString();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000283 return false;
284 }
285 if (GetCodecType() != CODEC_VIDEO) {
286 return true;
287 }
288
289 // Video validation from here on.
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000290 int min_bitrate = -1;
291 int max_bitrate = -1;
292 if (GetParam(kCodecParamMinBitrate, &min_bitrate) &&
293 GetParam(kCodecParamMaxBitrate, &max_bitrate)) {
294 if (max_bitrate < min_bitrate) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100295 RTC_LOG(LS_ERROR) << "Codec with max < min bitrate: " << ToString();
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000296 return false;
297 }
298 }
299 return true;
300}
301
deadbeef67cf2c12016-04-13 10:07:16 -0700302DataCodec::DataCodec(int id, const std::string& name)
303 : Codec(id, name, kDataCodecClockrate) {}
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000304
305DataCodec::DataCodec() : Codec() {
306 clockrate = kDataCodecClockrate;
307}
308
309DataCodec::DataCodec(const DataCodec& c) = default;
magjed3663c522016-11-07 10:14:36 -0800310DataCodec::DataCodec(DataCodec&& c) = default;
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000311DataCodec& DataCodec::operator=(const DataCodec& c) = default;
magjed3663c522016-11-07 10:14:36 -0800312DataCodec& DataCodec::operator=(DataCodec&& c) = default;
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000313
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000314std::string DataCodec::ToString() const {
315 std::ostringstream os;
316 os << "DataCodec[" << id << ":" << name << "]";
317 return os.str();
318}
319
stefanba4c0e42016-02-04 04:12:24 -0800320bool HasNack(const Codec& codec) {
Shao Changbine62202f2015-04-21 20:24:50 +0800321 return codec.HasFeedbackParam(
322 FeedbackParam(kRtcpFbParamNack, kParamValueEmpty));
323}
324
stefanba4c0e42016-02-04 04:12:24 -0800325bool HasRemb(const Codec& codec) {
Shao Changbine62202f2015-04-21 20:24:50 +0800326 return codec.HasFeedbackParam(
327 FeedbackParam(kRtcpFbParamRemb, kParamValueEmpty));
328}
329
stefanba4c0e42016-02-04 04:12:24 -0800330bool HasTransportCc(const Codec& codec) {
stefan43edf0f2015-11-20 18:05:48 -0800331 return codec.HasFeedbackParam(
332 FeedbackParam(kRtcpFbParamTransportCc, kParamValueEmpty));
333}
334
Shao Changbine62202f2015-04-21 20:24:50 +0800335bool CodecNamesEq(const std::string& name1, const std::string& name2) {
magjed1e45cc62016-10-28 07:43:45 -0700336 return CodecNamesEq(name1.c_str(), name2.c_str());
337}
338
339bool CodecNamesEq(const char* name1, const char* name2) {
340 return _stricmp(name1, name2) == 0;
341}
342
magjedf823ede2016-11-12 09:53:04 -0800343const VideoCodec* FindMatchingCodec(
344 const std::vector<VideoCodec>& supported_codecs,
345 const VideoCodec& codec) {
346 for (const VideoCodec& supported_codec : supported_codecs) {
Magnus Jedvert523589d2017-11-23 13:24:53 +0100347 if (IsSameCodec(codec.name, codec.params, supported_codec.name,
348 supported_codec.params)) {
349 return &supported_codec;
magjedf823ede2016-11-12 09:53:04 -0800350 }
magjedf823ede2016-11-12 09:53:04 -0800351 }
352 return nullptr;
Shao Changbine62202f2015-04-21 20:24:50 +0800353}
354
Magnus Jedvert523589d2017-11-23 13:24:53 +0100355bool IsSameCodec(const std::string& name1,
356 const CodecParameterMap& params1,
357 const std::string& name2,
358 const CodecParameterMap& params2) {
359 // If different names (case insensitive), then not same formats.
360 if (!CodecNamesEq(name1, name2))
361 return false;
362 // For every format besides H264, comparing names is enough.
363 return !CodecNamesEq(name1.c_str(), kH264CodecName) ||
364 webrtc::H264::IsSameH264Profile(params1, params2);
365}
366
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000367} // namespace cricket