blob: 5fbae2c45e68990f2762a7f20bf32c633ab25505 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/media/base/codec.h"
29
30#include <algorithm>
31#include <sstream>
32
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000033#include "webrtc/base/common.h"
34#include "webrtc/base/logging.h"
35#include "webrtc/base/stringencode.h"
36#include "webrtc/base/stringutils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037
38namespace cricket {
39
wu@webrtc.orgff1b1bf2014-06-20 20:57:42 +000040const int kMaxPayloadId = 127;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041
42bool FeedbackParam::operator==(const FeedbackParam& other) const {
43 return _stricmp(other.id().c_str(), id().c_str()) == 0 &&
44 _stricmp(other.param().c_str(), param().c_str()) == 0;
45}
46
47bool FeedbackParams::operator==(const FeedbackParams& other) const {
48 return params_ == other.params_;
49}
50
51bool FeedbackParams::Has(const FeedbackParam& param) const {
52 return std::find(params_.begin(), params_.end(), param) != params_.end();
53}
54
55void FeedbackParams::Add(const FeedbackParam& param) {
56 if (param.id().empty()) {
57 return;
58 }
59 if (Has(param)) {
60 // Param already in |this|.
61 return;
62 }
63 params_.push_back(param);
64 ASSERT(!HasDuplicateEntries());
65}
66
67void FeedbackParams::Intersect(const FeedbackParams& from) {
68 std::vector<FeedbackParam>::iterator iter_to = params_.begin();
69 while (iter_to != params_.end()) {
70 if (!from.Has(*iter_to)) {
71 iter_to = params_.erase(iter_to);
72 } else {
73 ++iter_to;
74 }
75 }
76}
77
78bool FeedbackParams::HasDuplicateEntries() const {
79 for (std::vector<FeedbackParam>::const_iterator iter = params_.begin();
80 iter != params_.end(); ++iter) {
81 for (std::vector<FeedbackParam>::const_iterator found = iter + 1;
82 found != params_.end(); ++found) {
83 if (*found == *iter) {
84 return true;
85 }
86 }
87 }
88 return false;
89}
90
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +000091Codec::Codec(int id, const std::string& name, int clockrate, int preference)
guoweis@webrtc.orgcce874b2015-02-19 18:14:36 +000092 : id(id), name(name), clockrate(clockrate), preference(preference) {
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +000093}
94
95Codec::Codec() : id(0), clockrate(0), preference(0) {
96}
97
98Codec::Codec(const Codec& c) = default;
99
100Codec::~Codec() = default;
101
102Codec& Codec::operator=(const Codec& c) {
103 this->id = c.id; // id is reserved in objective-c
104 name = c.name;
105 clockrate = c.clockrate;
106 preference = c.preference;
107 params = c.params;
108 feedback_params = c.feedback_params;
109 return *this;
110}
111
112bool Codec::operator==(const Codec& c) const {
113 return this->id == c.id && // id is reserved in objective-c
guoweis@webrtc.orgcce874b2015-02-19 18:14:36 +0000114 name == c.name && clockrate == c.clockrate &&
115 preference == c.preference && params == c.params &&
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000116 feedback_params == c.feedback_params;
117}
118
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119bool Codec::Matches(const Codec& codec) const {
120 // Match the codec id/name based on the typical static/dynamic name rules.
121 // Matching is case-insensitive.
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000122 const int kMaxStaticPayloadId = 95;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123 return (codec.id <= kMaxStaticPayloadId) ?
124 (id == codec.id) : (_stricmp(name.c_str(), codec.name.c_str()) == 0);
125}
126
127bool Codec::GetParam(const std::string& name, std::string* out) const {
128 CodecParameterMap::const_iterator iter = params.find(name);
129 if (iter == params.end())
130 return false;
131 *out = iter->second;
132 return true;
133}
134
135bool Codec::GetParam(const std::string& name, int* out) const {
136 CodecParameterMap::const_iterator iter = params.find(name);
137 if (iter == params.end())
138 return false;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000139 return rtc::FromString(iter->second, out);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140}
141
142void Codec::SetParam(const std::string& name, const std::string& value) {
143 params[name] = value;
144}
145
146void Codec::SetParam(const std::string& name, int value) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000147 params[name] = rtc::ToString(value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148}
149
buildbot@webrtc.orgfbd13282014-06-19 19:50:55 +0000150bool Codec::RemoveParam(const std::string& name) {
151 return params.erase(name) == 1;
152}
153
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154void Codec::AddFeedbackParam(const FeedbackParam& param) {
155 feedback_params.Add(param);
156}
157
158bool Codec::HasFeedbackParam(const FeedbackParam& param) const {
159 return feedback_params.Has(param);
160}
161
162void Codec::IntersectFeedbackParams(const Codec& other) {
163 feedback_params.Intersect(other.feedback_params);
164}
165
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000166AudioCodec::AudioCodec(int pt,
167 const std::string& nm,
168 int cr,
169 int br,
170 int cs,
171 int pr)
guoweis@webrtc.orgcce874b2015-02-19 18:14:36 +0000172 : Codec(pt, nm, cr, pr), bitrate(br), channels(cs) {
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000173}
174
guoweis@webrtc.orgcce874b2015-02-19 18:14:36 +0000175AudioCodec::AudioCodec() : Codec(), bitrate(0), channels(0) {
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000176}
177
178AudioCodec::AudioCodec(const AudioCodec& c) = default;
179
180AudioCodec& AudioCodec::operator=(const AudioCodec& c) {
181 Codec::operator=(c);
182 bitrate = c.bitrate;
183 channels = c.channels;
184 return *this;
185}
186
187bool AudioCodec::operator==(const AudioCodec& c) const {
guoweis@webrtc.orgcce874b2015-02-19 18:14:36 +0000188 return bitrate == c.bitrate && channels == c.channels && Codec::operator==(c);
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000189}
190
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000191bool AudioCodec::Matches(const AudioCodec& codec) const {
192 // If a nonzero clockrate is specified, it must match the actual clockrate.
193 // If a nonzero bitrate is specified, it must match the actual bitrate,
194 // unless the codec is VBR (0), where we just force the supplied value.
195 // The number of channels must match exactly, with the exception
196 // that channels=0 is treated synonymously as channels=1, per RFC
197 // 4566 section 6: " [The channels] parameter is OPTIONAL and may be
198 // omitted if the number of channels is one."
199 // Preference is ignored.
200 // TODO(juberti): Treat a zero clockrate as 8000Hz, the RTP default clockrate.
201 return Codec::Matches(codec) &&
202 ((codec.clockrate == 0 /*&& clockrate == 8000*/) ||
203 clockrate == codec.clockrate) &&
204 (codec.bitrate == 0 || bitrate <= 0 || bitrate == codec.bitrate) &&
205 ((codec.channels < 2 && channels < 2) || channels == codec.channels);
206}
207
208std::string AudioCodec::ToString() const {
209 std::ostringstream os;
210 os << "AudioCodec[" << id << ":" << name << ":" << clockrate << ":" << bitrate
211 << ":" << channels << ":" << preference << "]";
212 return os.str();
213}
214
215std::string VideoCodec::ToString() const {
216 std::ostringstream os;
217 os << "VideoCodec[" << id << ":" << name << ":" << width << ":" << height
218 << ":" << framerate << ":" << preference << "]";
219 return os.str();
220}
221
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000222VideoCodec::VideoCodec(int pt,
223 const std::string& nm,
224 int w,
225 int h,
226 int fr,
227 int pr)
228 : Codec(pt, nm, kVideoCodecClockrate, pr),
229 width(w),
230 height(h),
231 framerate(fr) {
232}
233
Shao Changbine62202f2015-04-21 20:24:50 +0800234VideoCodec::VideoCodec(int pt, const std::string& nm)
235 : Codec(pt, nm, kVideoCodecClockrate, 0),
236 width(0),
237 height(0),
238 framerate(0) {
239}
240
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000241VideoCodec::VideoCodec() : Codec(), width(0), height(0), framerate(0) {
242 clockrate = kVideoCodecClockrate;
243}
244
245VideoCodec::VideoCodec(const VideoCodec& c) = default;
246
247VideoCodec& VideoCodec::operator=(const VideoCodec& c) {
248 Codec::operator=(c);
249 width = c.width;
250 height = c.height;
251 framerate = c.framerate;
252 return *this;
253}
254
255bool VideoCodec::operator==(const VideoCodec& c) const {
256 return width == c.width && height == c.height && framerate == c.framerate &&
257 Codec::operator==(c);
258}
259
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000260VideoCodec VideoCodec::CreateRtxCodec(int rtx_payload_type,
261 int associated_payload_type) {
262 VideoCodec rtx_codec(rtx_payload_type, kRtxCodecName, 0, 0, 0, 0);
263 rtx_codec.SetParam(kCodecParamAssociatedPayloadType, associated_payload_type);
264 return rtx_codec;
265}
266
267VideoCodec::CodecType VideoCodec::GetCodecType() const {
268 const char* payload_name = name.c_str();
269 if (_stricmp(payload_name, kRedCodecName) == 0) {
270 return CODEC_RED;
271 }
272 if (_stricmp(payload_name, kUlpfecCodecName) == 0) {
273 return CODEC_ULPFEC;
274 }
275 if (_stricmp(payload_name, kRtxCodecName) == 0) {
276 return CODEC_RTX;
277 }
278
279 return CODEC_VIDEO;
280}
281
282bool VideoCodec::ValidateCodecFormat() const {
283 if (id < 0 || id > 127) {
284 LOG(LS_ERROR) << "Codec with invalid payload type: " << ToString();
285 return false;
286 }
287 if (GetCodecType() != CODEC_VIDEO) {
288 return true;
289 }
290
291 // Video validation from here on.
292
293 if (width <= 0 || height <= 0) {
294 LOG(LS_ERROR) << "Codec with invalid dimensions: " << ToString();
295 return false;
296 }
297 int min_bitrate = -1;
298 int max_bitrate = -1;
299 if (GetParam(kCodecParamMinBitrate, &min_bitrate) &&
300 GetParam(kCodecParamMaxBitrate, &max_bitrate)) {
301 if (max_bitrate < min_bitrate) {
302 LOG(LS_ERROR) << "Codec with max < min bitrate: " << ToString();
303 return false;
304 }
305 }
306 return true;
307}
308
guoweis@webrtc.orgbc6961f2015-02-19 17:55:18 +0000309DataCodec::DataCodec(int id, const std::string& name, int preference)
310 : Codec(id, name, kDataCodecClockrate, preference) {
311}
312
313DataCodec::DataCodec() : Codec() {
314 clockrate = kDataCodecClockrate;
315}
316
317DataCodec::DataCodec(const DataCodec& c) = default;
318
319DataCodec& DataCodec::operator=(const DataCodec& c) = default;
320
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000321std::string DataCodec::ToString() const {
322 std::ostringstream os;
323 os << "DataCodec[" << id << ":" << name << "]";
324 return os.str();
325}
326
Shao Changbine62202f2015-04-21 20:24:50 +0800327bool HasNack(const VideoCodec& codec) {
328 return codec.HasFeedbackParam(
329 FeedbackParam(kRtcpFbParamNack, kParamValueEmpty));
330}
331
332bool HasRemb(const VideoCodec& codec) {
333 return codec.HasFeedbackParam(
334 FeedbackParam(kRtcpFbParamRemb, kParamValueEmpty));
335}
336
stefan43edf0f2015-11-20 18:05:48 -0800337bool HasTransportCc(const VideoCodec& codec) {
338 return codec.HasFeedbackParam(
339 FeedbackParam(kRtcpFbParamTransportCc, kParamValueEmpty));
340}
341
Shao Changbine62202f2015-04-21 20:24:50 +0800342bool CodecNamesEq(const std::string& name1, const std::string& name2) {
343 return _stricmp(name1.c_str(), name2.c_str()) == 0;
344}
345
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000346} // namespace cricket