henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 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.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 33 | #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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 37 | |
| 38 | namespace cricket { |
| 39 | |
wu@webrtc.org | ff1b1bf | 2014-06-20 20:57:42 +0000 | [diff] [blame] | 40 | const int kMaxPayloadId = 127; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 41 | |
| 42 | bool 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 | |
| 47 | bool FeedbackParams::operator==(const FeedbackParams& other) const { |
| 48 | return params_ == other.params_; |
| 49 | } |
| 50 | |
| 51 | bool FeedbackParams::Has(const FeedbackParam& param) const { |
| 52 | return std::find(params_.begin(), params_.end(), param) != params_.end(); |
| 53 | } |
| 54 | |
| 55 | void 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 | |
| 67 | void 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 | |
| 78 | bool 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.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 91 | Codec::Codec(int id, const std::string& name, int clockrate, int preference) |
guoweis@webrtc.org | cce874b | 2015-02-19 18:14:36 +0000 | [diff] [blame] | 92 | : id(id), name(name), clockrate(clockrate), preference(preference) { |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | Codec::Codec() : id(0), clockrate(0), preference(0) { |
| 96 | } |
| 97 | |
| 98 | Codec::Codec(const Codec& c) = default; |
| 99 | |
| 100 | Codec::~Codec() = default; |
| 101 | |
| 102 | Codec& 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 | |
| 112 | bool Codec::operator==(const Codec& c) const { |
| 113 | return this->id == c.id && // id is reserved in objective-c |
guoweis@webrtc.org | cce874b | 2015-02-19 18:14:36 +0000 | [diff] [blame] | 114 | name == c.name && clockrate == c.clockrate && |
| 115 | preference == c.preference && params == c.params && |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 116 | feedback_params == c.feedback_params; |
| 117 | } |
| 118 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 119 | bool 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.org | d324546 | 2015-02-23 21:28:22 +0000 | [diff] [blame] | 122 | const int kMaxStaticPayloadId = 95; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 123 | return (codec.id <= kMaxStaticPayloadId) ? |
| 124 | (id == codec.id) : (_stricmp(name.c_str(), codec.name.c_str()) == 0); |
| 125 | } |
| 126 | |
| 127 | bool 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 | |
| 135 | bool 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.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 139 | return rtc::FromString(iter->second, out); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void Codec::SetParam(const std::string& name, const std::string& value) { |
| 143 | params[name] = value; |
| 144 | } |
| 145 | |
| 146 | void Codec::SetParam(const std::string& name, int value) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 147 | params[name] = rtc::ToString(value); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 148 | } |
| 149 | |
buildbot@webrtc.org | fbd1328 | 2014-06-19 19:50:55 +0000 | [diff] [blame] | 150 | bool Codec::RemoveParam(const std::string& name) { |
| 151 | return params.erase(name) == 1; |
| 152 | } |
| 153 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 154 | void Codec::AddFeedbackParam(const FeedbackParam& param) { |
| 155 | feedback_params.Add(param); |
| 156 | } |
| 157 | |
| 158 | bool Codec::HasFeedbackParam(const FeedbackParam& param) const { |
| 159 | return feedback_params.Has(param); |
| 160 | } |
| 161 | |
| 162 | void Codec::IntersectFeedbackParams(const Codec& other) { |
| 163 | feedback_params.Intersect(other.feedback_params); |
| 164 | } |
| 165 | |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 166 | AudioCodec::AudioCodec(int pt, |
| 167 | const std::string& nm, |
| 168 | int cr, |
| 169 | int br, |
| 170 | int cs, |
| 171 | int pr) |
guoweis@webrtc.org | cce874b | 2015-02-19 18:14:36 +0000 | [diff] [blame] | 172 | : Codec(pt, nm, cr, pr), bitrate(br), channels(cs) { |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 173 | } |
| 174 | |
guoweis@webrtc.org | cce874b | 2015-02-19 18:14:36 +0000 | [diff] [blame] | 175 | AudioCodec::AudioCodec() : Codec(), bitrate(0), channels(0) { |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | AudioCodec::AudioCodec(const AudioCodec& c) = default; |
| 179 | |
| 180 | AudioCodec& AudioCodec::operator=(const AudioCodec& c) { |
| 181 | Codec::operator=(c); |
| 182 | bitrate = c.bitrate; |
| 183 | channels = c.channels; |
| 184 | return *this; |
| 185 | } |
| 186 | |
| 187 | bool AudioCodec::operator==(const AudioCodec& c) const { |
guoweis@webrtc.org | cce874b | 2015-02-19 18:14:36 +0000 | [diff] [blame] | 188 | return bitrate == c.bitrate && channels == c.channels && Codec::operator==(c); |
guoweis@webrtc.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 189 | } |
| 190 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 191 | bool 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 | |
| 208 | std::string AudioCodec::ToString() const { |
| 209 | std::ostringstream os; |
| 210 | os << "AudioCodec[" << id << ":" << name << ":" << clockrate << ":" << bitrate |
| 211 | << ":" << channels << ":" << preference << "]"; |
| 212 | return os.str(); |
| 213 | } |
| 214 | |
| 215 | std::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.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 222 | VideoCodec::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 Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 234 | VideoCodec::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.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 241 | VideoCodec::VideoCodec() : Codec(), width(0), height(0), framerate(0) { |
| 242 | clockrate = kVideoCodecClockrate; |
| 243 | } |
| 244 | |
| 245 | VideoCodec::VideoCodec(const VideoCodec& c) = default; |
| 246 | |
| 247 | VideoCodec& 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 | |
| 255 | bool 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.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 260 | VideoCodec 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 | |
| 267 | VideoCodec::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 | |
| 282 | bool 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.org | bc6961f | 2015-02-19 17:55:18 +0000 | [diff] [blame] | 309 | DataCodec::DataCodec(int id, const std::string& name, int preference) |
| 310 | : Codec(id, name, kDataCodecClockrate, preference) { |
| 311 | } |
| 312 | |
| 313 | DataCodec::DataCodec() : Codec() { |
| 314 | clockrate = kDataCodecClockrate; |
| 315 | } |
| 316 | |
| 317 | DataCodec::DataCodec(const DataCodec& c) = default; |
| 318 | |
| 319 | DataCodec& DataCodec::operator=(const DataCodec& c) = default; |
| 320 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 321 | std::string DataCodec::ToString() const { |
| 322 | std::ostringstream os; |
| 323 | os << "DataCodec[" << id << ":" << name << "]"; |
| 324 | return os.str(); |
| 325 | } |
| 326 | |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 327 | bool HasNack(const VideoCodec& codec) { |
| 328 | return codec.HasFeedbackParam( |
| 329 | FeedbackParam(kRtcpFbParamNack, kParamValueEmpty)); |
| 330 | } |
| 331 | |
| 332 | bool HasRemb(const VideoCodec& codec) { |
| 333 | return codec.HasFeedbackParam( |
| 334 | FeedbackParam(kRtcpFbParamRemb, kParamValueEmpty)); |
| 335 | } |
| 336 | |
stefan | 43edf0f | 2015-11-20 18:05:48 -0800 | [diff] [blame^] | 337 | bool HasTransportCc(const VideoCodec& codec) { |
| 338 | return codec.HasFeedbackParam( |
| 339 | FeedbackParam(kRtcpFbParamTransportCc, kParamValueEmpty)); |
| 340 | } |
| 341 | |
Shao Changbin | e62202f | 2015-04-21 20:24:50 +0800 | [diff] [blame] | 342 | bool CodecNamesEq(const std::string& name1, const std::string& name2) { |
| 343 | return _stricmp(name1.c_str(), name2.c_str()) == 0; |
| 344 | } |
| 345 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 346 | } // namespace cricket |