blob: a94940227bd0967677e82d03258a0a241d38ec6e [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
kjellandera96e2d72016-02-04 23:52:28 -080011#include "webrtc/media/base/codec.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
13#include <algorithm>
14#include <sstream>
15
zhihuang38ede132017-06-15 12:52:32 -070016#include "webrtc/media/base/h264_profile_level_id.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020017#include "webrtc/rtc_base/checks.h"
18#include "webrtc/rtc_base/logging.h"
19#include "webrtc/rtc_base/stringencode.h"
20#include "webrtc/rtc_base/stringutils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021
22namespace cricket {
23
magjedf823ede2016-11-12 09:53:04 -080024static bool IsSameH264Profile(const CodecParameterMap& params1,
25 const CodecParameterMap& params2) {
26 const rtc::Optional<webrtc::H264::ProfileLevelId> profile_level_id =
27 webrtc::H264::ParseSdpProfileLevelId(params1);
28 const rtc::Optional<webrtc::H264::ProfileLevelId> other_profile_level_id =
29 webrtc::H264::ParseSdpProfileLevelId(params2);
30 // Compare H264 profiles, but not levels.
31 return profile_level_id && other_profile_level_id &&
32 profile_level_id->profile == other_profile_level_id->profile;
33}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034
35bool FeedbackParam::operator==(const FeedbackParam& other) const {
36 return _stricmp(other.id().c_str(), id().c_str()) == 0 &&
37 _stricmp(other.param().c_str(), param().c_str()) == 0;
38}
39
40bool FeedbackParams::operator==(const FeedbackParams& other) const {
41 return params_ == other.params_;
42}
43
44bool FeedbackParams::Has(const FeedbackParam& param) const {
45 return std::find(params_.begin(), params_.end(), param) != params_.end();
46}
47
48void FeedbackParams::Add(const FeedbackParam& param) {
49 if (param.id().empty()) {
50 return;
51 }
52 if (Has(param)) {
53 // Param already in |this|.
54 return;
55 }
56 params_.push_back(param);
magjed0928a3c2016-11-25 00:40:18 -080057 RTC_CHECK(!HasDuplicateEntries());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058}
59
60void FeedbackParams::Intersect(const FeedbackParams& from) {
61 std::vector<FeedbackParam>::iterator iter_to = params_.begin();
62 while (iter_to != params_.end()) {
63 if (!from.Has(*iter_to)) {
64 iter_to = params_.erase(iter_to);
65 } else {
66 ++iter_to;
67 }
68 }
69}
70
71bool FeedbackParams::HasDuplicateEntries() const {
72 for (std::vector<FeedbackParam>::const_iterator iter = params_.begin();
73 iter != params_.end(); ++iter) {
74 for (std::vector<FeedbackParam>::const_iterator found = iter + 1;
75 found != params_.end(); ++found) {
76 if (*found == *iter) {
77 return true;
78 }
79 }
80 }
81 return false;
82}
83
deadbeef67cf2c12016-04-13 10:07:16 -070084Codec::Codec(int id, const std::string& name, int clockrate)
85 : id(id), name(name), clockrate(clockrate) {}
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +000086
deadbeef67cf2c12016-04-13 10:07:16 -070087Codec::Codec() : id(0), clockrate(0) {}
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +000088
89Codec::Codec(const Codec& c) = default;
magjed3663c522016-11-07 10:14:36 -080090Codec::Codec(Codec&& c) = default;
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +000091Codec::~Codec() = default;
magjed3663c522016-11-07 10:14:36 -080092Codec& Codec::operator=(const Codec& c) = default;
93Codec& Codec::operator=(Codec&& c) = default;
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +000094
95bool Codec::operator==(const Codec& c) const {
96 return this->id == c.id && // id is reserved in objective-c
deadbeef67cf2c12016-04-13 10:07:16 -070097 name == c.name && clockrate == c.clockrate && params == c.params &&
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +000098 feedback_params == c.feedback_params;
99}
100
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101bool Codec::Matches(const Codec& codec) const {
102 // Match the codec id/name based on the typical static/dynamic name rules.
103 // Matching is case-insensitive.
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000104 const int kMaxStaticPayloadId = 95;
magjed3663c522016-11-07 10:14:36 -0800105 return (id <= kMaxStaticPayloadId || codec.id <= kMaxStaticPayloadId)
106 ? (id == codec.id)
107 : (_stricmp(name.c_str(), codec.name.c_str()) == 0);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000108}
109
110bool Codec::GetParam(const std::string& name, std::string* out) const {
111 CodecParameterMap::const_iterator iter = params.find(name);
112 if (iter == params.end())
113 return false;
114 *out = iter->second;
115 return true;
116}
117
118bool Codec::GetParam(const std::string& name, int* out) const {
119 CodecParameterMap::const_iterator iter = params.find(name);
120 if (iter == params.end())
121 return false;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000122 return rtc::FromString(iter->second, out);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123}
124
125void Codec::SetParam(const std::string& name, const std::string& value) {
126 params[name] = value;
127}
128
129void Codec::SetParam(const std::string& name, int value) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000130 params[name] = rtc::ToString(value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131}
132
buildbot@webrtc.orgfbd13282014-06-19 19:50:55 +0000133bool Codec::RemoveParam(const std::string& name) {
134 return params.erase(name) == 1;
135}
136
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137void Codec::AddFeedbackParam(const FeedbackParam& param) {
138 feedback_params.Add(param);
139}
140
141bool Codec::HasFeedbackParam(const FeedbackParam& param) const {
142 return feedback_params.Has(param);
143}
144
145void Codec::IntersectFeedbackParams(const Codec& other) {
146 feedback_params.Intersect(other.feedback_params);
147}
148
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -0700149webrtc::RtpCodecParameters Codec::ToCodecParameters() const {
150 webrtc::RtpCodecParameters codec_params;
151 codec_params.payload_type = id;
deadbeefe702b302017-02-04 12:09:01 -0800152 codec_params.name = name;
153 codec_params.clock_rate = rtc::Optional<int>(clockrate);
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -0700154 return codec_params;
155}
156
pkasting25702cb2016-01-08 13:50:27 -0800157AudioCodec::AudioCodec(int id,
158 const std::string& name,
159 int clockrate,
160 int bitrate,
deadbeef67cf2c12016-04-13 10:07:16 -0700161 size_t channels)
162 : Codec(id, name, clockrate), bitrate(bitrate), channels(channels) {}
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000163
guoweis@webrtc.orgcce874b2015-02-19 18:14:36 +0000164AudioCodec::AudioCodec() : Codec(), bitrate(0), channels(0) {
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000165}
166
167AudioCodec::AudioCodec(const AudioCodec& c) = default;
magjed3663c522016-11-07 10:14:36 -0800168AudioCodec::AudioCodec(AudioCodec&& c) = default;
169AudioCodec& AudioCodec::operator=(const AudioCodec& c) = default;
170AudioCodec& AudioCodec::operator=(AudioCodec&& c) = default;
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000171
172bool AudioCodec::operator==(const AudioCodec& c) const {
guoweis@webrtc.orgcce874b2015-02-19 18:14:36 +0000173 return bitrate == c.bitrate && channels == c.channels && Codec::operator==(c);
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000174}
175
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176bool AudioCodec::Matches(const AudioCodec& codec) const {
177 // If a nonzero clockrate is specified, it must match the actual clockrate.
178 // If a nonzero bitrate is specified, it must match the actual bitrate,
179 // unless the codec is VBR (0), where we just force the supplied value.
180 // The number of channels must match exactly, with the exception
181 // that channels=0 is treated synonymously as channels=1, per RFC
182 // 4566 section 6: " [The channels] parameter is OPTIONAL and may be
183 // omitted if the number of channels is one."
184 // Preference is ignored.
185 // TODO(juberti): Treat a zero clockrate as 8000Hz, the RTP default clockrate.
186 return Codec::Matches(codec) &&
187 ((codec.clockrate == 0 /*&& clockrate == 8000*/) ||
188 clockrate == codec.clockrate) &&
189 (codec.bitrate == 0 || bitrate <= 0 || bitrate == codec.bitrate) &&
190 ((codec.channels < 2 && channels < 2) || channels == codec.channels);
191}
192
193std::string AudioCodec::ToString() const {
194 std::ostringstream os;
195 os << "AudioCodec[" << id << ":" << name << ":" << clockrate << ":" << bitrate
deadbeef67cf2c12016-04-13 10:07:16 -0700196 << ":" << channels << "]";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197 return os.str();
198}
199
deadbeefe702b302017-02-04 12:09:01 -0800200webrtc::RtpCodecParameters AudioCodec::ToCodecParameters() const {
201 webrtc::RtpCodecParameters codec_params = Codec::ToCodecParameters();
202 codec_params.num_channels = rtc::Optional<int>(static_cast<int>(channels));
203 codec_params.kind = MEDIA_TYPE_AUDIO;
204 return codec_params;
205}
206
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000207std::string VideoCodec::ToString() const {
208 std::ostringstream os;
perkj26752742016-10-24 01:21:16 -0700209 os << "VideoCodec[" << id << ":" << name << "]";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210 return os.str();
211}
212
deadbeefe702b302017-02-04 12:09:01 -0800213webrtc::RtpCodecParameters VideoCodec::ToCodecParameters() const {
214 webrtc::RtpCodecParameters codec_params = Codec::ToCodecParameters();
215 codec_params.kind = MEDIA_TYPE_VIDEO;
216 return codec_params;
217}
218
pkasting25702cb2016-01-08 13:50:27 -0800219VideoCodec::VideoCodec(int id, const std::string& name)
hta9aa96882016-12-06 05:36:03 -0800220 : Codec(id, name, kVideoCodecClockrate) {
221 SetDefaultParameters();
222}
Shao Changbine62202f2015-04-21 20:24:50 +0800223
hta9aa96882016-12-06 05:36:03 -0800224VideoCodec::VideoCodec(const std::string& name) : VideoCodec(0 /* id */, name) {
225 SetDefaultParameters();
226}
magjed1e45cc62016-10-28 07:43:45 -0700227
perkj26752742016-10-24 01:21:16 -0700228VideoCodec::VideoCodec() : Codec() {
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000229 clockrate = kVideoCodecClockrate;
230}
231
232VideoCodec::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() {
238 if (_stricmp(kH264CodecName, name.c_str()) == 0) {
239 // 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
magjedf823ede2016-11-12 09:53:04 -0800251bool VideoCodec::Matches(const VideoCodec& other) const {
252 if (!Codec::Matches(other))
253 return false;
254 if (CodecNamesEq(name.c_str(), kH264CodecName))
255 return IsSameH264Profile(params, other.params);
256 return true;
257}
258
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000259VideoCodec VideoCodec::CreateRtxCodec(int rtx_payload_type,
260 int associated_payload_type) {
perkj26752742016-10-24 01:21:16 -0700261 VideoCodec rtx_codec(rtx_payload_type, kRtxCodecName);
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000262 rtx_codec.SetParam(kCodecParamAssociatedPayloadType, associated_payload_type);
263 return rtx_codec;
264}
265
266VideoCodec::CodecType VideoCodec::GetCodecType() const {
267 const char* payload_name = name.c_str();
268 if (_stricmp(payload_name, kRedCodecName) == 0) {
269 return CODEC_RED;
270 }
271 if (_stricmp(payload_name, kUlpfecCodecName) == 0) {
272 return CODEC_ULPFEC;
273 }
brandtr87d7d772016-11-07 03:03:41 -0800274 if (_stricmp(payload_name, kFlexfecCodecName) == 0) {
275 return CODEC_FLEXFEC;
276 }
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000277 if (_stricmp(payload_name, kRtxCodecName) == 0) {
278 return CODEC_RTX;
279 }
280
281 return CODEC_VIDEO;
282}
283
284bool VideoCodec::ValidateCodecFormat() const {
285 if (id < 0 || id > 127) {
286 LOG(LS_ERROR) << "Codec with invalid payload type: " << ToString();
287 return false;
288 }
289 if (GetCodecType() != CODEC_VIDEO) {
290 return true;
291 }
292
293 // Video validation from here on.
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000294 int min_bitrate = -1;
295 int max_bitrate = -1;
296 if (GetParam(kCodecParamMinBitrate, &min_bitrate) &&
297 GetParam(kCodecParamMaxBitrate, &max_bitrate)) {
298 if (max_bitrate < min_bitrate) {
299 LOG(LS_ERROR) << "Codec with max < min bitrate: " << ToString();
300 return false;
301 }
302 }
303 return true;
304}
305
deadbeef67cf2c12016-04-13 10:07:16 -0700306DataCodec::DataCodec(int id, const std::string& name)
307 : Codec(id, name, kDataCodecClockrate) {}
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000308
309DataCodec::DataCodec() : Codec() {
310 clockrate = kDataCodecClockrate;
311}
312
313DataCodec::DataCodec(const DataCodec& c) = default;
magjed3663c522016-11-07 10:14:36 -0800314DataCodec::DataCodec(DataCodec&& c) = default;
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000315DataCodec& DataCodec::operator=(const DataCodec& c) = default;
magjed3663c522016-11-07 10:14:36 -0800316DataCodec& DataCodec::operator=(DataCodec&& c) = default;
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000317
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000318std::string DataCodec::ToString() const {
319 std::ostringstream os;
320 os << "DataCodec[" << id << ":" << name << "]";
321 return os.str();
322}
323
stefanba4c0e42016-02-04 04:12:24 -0800324bool HasNack(const Codec& codec) {
Shao Changbine62202f2015-04-21 20:24:50 +0800325 return codec.HasFeedbackParam(
326 FeedbackParam(kRtcpFbParamNack, kParamValueEmpty));
327}
328
stefanba4c0e42016-02-04 04:12:24 -0800329bool HasRemb(const Codec& codec) {
Shao Changbine62202f2015-04-21 20:24:50 +0800330 return codec.HasFeedbackParam(
331 FeedbackParam(kRtcpFbParamRemb, kParamValueEmpty));
332}
333
stefanba4c0e42016-02-04 04:12:24 -0800334bool HasTransportCc(const Codec& codec) {
stefan43edf0f2015-11-20 18:05:48 -0800335 return codec.HasFeedbackParam(
336 FeedbackParam(kRtcpFbParamTransportCc, kParamValueEmpty));
337}
338
Shao Changbine62202f2015-04-21 20:24:50 +0800339bool CodecNamesEq(const std::string& name1, const std::string& name2) {
magjed1e45cc62016-10-28 07:43:45 -0700340 return CodecNamesEq(name1.c_str(), name2.c_str());
341}
342
343bool CodecNamesEq(const char* name1, const char* name2) {
344 return _stricmp(name1, name2) == 0;
345}
346
magjedf823ede2016-11-12 09:53:04 -0800347const VideoCodec* FindMatchingCodec(
348 const std::vector<VideoCodec>& supported_codecs,
349 const VideoCodec& codec) {
350 for (const VideoCodec& supported_codec : supported_codecs) {
351 if (!CodecNamesEq(codec.name, supported_codec.name))
352 continue;
353 if (CodecNamesEq(codec.name.c_str(), kH264CodecName) &&
354 !IsSameH264Profile(codec.params, supported_codec.params)) {
355 continue;
356 }
357 return &supported_codec;
358 }
359 return nullptr;
Shao Changbine62202f2015-04-21 20:24:50 +0800360}
361
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000362} // namespace cricket