128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org/*
228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org * libjingle
328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org * Copyright 2004 Google Inc.
428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org *
528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org * Redistribution and use in source and binary forms, with or without
628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org * modification, are permitted provided that the following conditions are met:
728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org *
828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org *  1. Redistributions of source code must retain the above copyright notice,
928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org *     this list of conditions and the following disclaimer.
1028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org *  2. Redistributions in binary form must reproduce the above copyright notice,
1128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org *     this list of conditions and the following disclaimer in the documentation
1228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org *     and/or other materials provided with the distribution.
1328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org *  3. The name of the author may not be used to endorse or promote products
1428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org *     derived from this software without specific prior written permission.
1528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org *
1628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
1728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
1928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
2228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
2528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org */
2728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
2828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org#include "talk/media/base/codec.h"
2928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
3028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org#include <algorithm>
3128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org#include <sstream>
3228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
33d4e598d57aed714a599444a7eab5e8fdde52a950buildbot@webrtc.org#include "webrtc/base/common.h"
34d4e598d57aed714a599444a7eab5e8fdde52a950buildbot@webrtc.org#include "webrtc/base/logging.h"
35d4e598d57aed714a599444a7eab5e8fdde52a950buildbot@webrtc.org#include "webrtc/base/stringencode.h"
36d4e598d57aed714a599444a7eab5e8fdde52a950buildbot@webrtc.org#include "webrtc/base/stringutils.h"
3728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
3828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.orgnamespace cricket {
3928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
40ff1b1bf0944d42700edadae68bd774835a7a13f0wu@webrtc.orgconst int kMaxPayloadId = 127;
4128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
4228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.orgbool FeedbackParam::operator==(const FeedbackParam& other) const {
4328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  return _stricmp(other.id().c_str(), id().c_str()) == 0 &&
4428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org      _stricmp(other.param().c_str(), param().c_str()) == 0;
4528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org}
4628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
4728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.orgbool FeedbackParams::operator==(const FeedbackParams& other) const {
4828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  return params_ == other.params_;
4928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org}
5028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
5128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.orgbool FeedbackParams::Has(const FeedbackParam& param) const {
5228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  return std::find(params_.begin(), params_.end(), param) != params_.end();
5328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org}
5428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
5528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.orgvoid FeedbackParams::Add(const FeedbackParam& param) {
5628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  if (param.id().empty()) {
5728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org    return;
5828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  }
5928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  if (Has(param)) {
6028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org    // Param already in |this|.
6128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org    return;
6228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  }
6328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  params_.push_back(param);
6428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  ASSERT(!HasDuplicateEntries());
6528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org}
6628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
6728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.orgvoid FeedbackParams::Intersect(const FeedbackParams& from) {
6828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  std::vector<FeedbackParam>::iterator iter_to = params_.begin();
6928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  while (iter_to != params_.end()) {
7028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org    if (!from.Has(*iter_to)) {
7128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org      iter_to = params_.erase(iter_to);
7228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org    } else {
7328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org      ++iter_to;
7428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org    }
7528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  }
7628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org}
7728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
7828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.orgbool FeedbackParams::HasDuplicateEntries() const {
7928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  for (std::vector<FeedbackParam>::const_iterator iter = params_.begin();
8028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org       iter != params_.end(); ++iter) {
8128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org    for (std::vector<FeedbackParam>::const_iterator found = iter + 1;
8228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org         found != params_.end(); ++found) {
8328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org      if (*found == *iter) {
8428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org        return true;
8528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org      }
8628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org    }
8728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  }
8828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  return false;
8928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org}
9028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
91bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.orgCodec::Codec(int id, const std::string& name, int clockrate, int preference)
92cce874b8d2f448a800317ae375b77a7935336564guoweis@webrtc.org    : id(id), name(name), clockrate(clockrate), preference(preference) {
93bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org}
94bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org
95bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.orgCodec::Codec() : id(0), clockrate(0), preference(0) {
96bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org}
97bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org
98bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.orgCodec::Codec(const Codec& c) = default;
99bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org
100bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.orgCodec::~Codec() = default;
101bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org
102bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.orgCodec& Codec::operator=(const Codec& c) {
103bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org  this->id = c.id;  // id is reserved in objective-c
104bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org  name = c.name;
105bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org  clockrate = c.clockrate;
106bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org  preference = c.preference;
107bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org  params = c.params;
108bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org  feedback_params = c.feedback_params;
109bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org  return *this;
110bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org}
111bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org
112bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.orgbool Codec::operator==(const Codec& c) const {
113bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org  return this->id == c.id &&  // id is reserved in objective-c
114cce874b8d2f448a800317ae375b77a7935336564guoweis@webrtc.org         name == c.name && clockrate == c.clockrate &&
115cce874b8d2f448a800317ae375b77a7935336564guoweis@webrtc.org         preference == c.preference && params == c.params &&
116bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org         feedback_params == c.feedback_params;
117bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org}
118bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org
11928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.orgbool Codec::Matches(const Codec& codec) const {
12028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  // Match the codec id/name based on the typical static/dynamic name rules.
12128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  // Matching is case-insensitive.
122d324546ced76d4e792338af4f7d02a5cd8819f92pkasting@chromium.org  const int kMaxStaticPayloadId = 95;
12328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  return (codec.id <= kMaxStaticPayloadId) ?
12428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org      (id == codec.id) : (_stricmp(name.c_str(), codec.name.c_str()) == 0);
12528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org}
12628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
12728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.orgbool Codec::GetParam(const std::string& name, std::string* out) const {
12828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  CodecParameterMap::const_iterator iter = params.find(name);
12928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  if (iter == params.end())
13028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org    return false;
13128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  *out = iter->second;
13228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  return true;
13328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org}
13428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
13528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.orgbool Codec::GetParam(const std::string& name, int* out) const {
13628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  CodecParameterMap::const_iterator iter = params.find(name);
13728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  if (iter == params.end())
13828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org    return false;
139d4e598d57aed714a599444a7eab5e8fdde52a950buildbot@webrtc.org  return rtc::FromString(iter->second, out);
14028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org}
14128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
14228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.orgvoid Codec::SetParam(const std::string& name, const std::string& value) {
14328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  params[name] = value;
14428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org}
14528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
14628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.orgvoid Codec::SetParam(const std::string& name, int value)  {
147d4e598d57aed714a599444a7eab5e8fdde52a950buildbot@webrtc.org  params[name] = rtc::ToString(value);
14828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org}
14928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
150fbd13286dc280eaa69c562e20e11a38cb393da3dbuildbot@webrtc.orgbool Codec::RemoveParam(const std::string& name) {
151fbd13286dc280eaa69c562e20e11a38cb393da3dbuildbot@webrtc.org  return params.erase(name) == 1;
152fbd13286dc280eaa69c562e20e11a38cb393da3dbuildbot@webrtc.org}
153fbd13286dc280eaa69c562e20e11a38cb393da3dbuildbot@webrtc.org
15428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.orgvoid Codec::AddFeedbackParam(const FeedbackParam& param) {
15528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  feedback_params.Add(param);
15628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org}
15728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
15828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.orgbool Codec::HasFeedbackParam(const FeedbackParam& param) const {
15928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  return feedback_params.Has(param);
16028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org}
16128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
16228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.orgvoid Codec::IntersectFeedbackParams(const Codec& other) {
16328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  feedback_params.Intersect(other.feedback_params);
16428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org}
16528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
16625702cb1628941427fa55e528f53483f239ae011pkastingAudioCodec::AudioCodec(int id,
16725702cb1628941427fa55e528f53483f239ae011pkasting                       const std::string& name,
16825702cb1628941427fa55e528f53483f239ae011pkasting                       int clockrate,
16925702cb1628941427fa55e528f53483f239ae011pkasting                       int bitrate,
1706955870806624479723addfae6dcf5d13968796cPeter Kasting                       size_t channels,
17125702cb1628941427fa55e528f53483f239ae011pkasting                       int preference)
17225702cb1628941427fa55e528f53483f239ae011pkasting    : Codec(id, name, clockrate, preference),
17325702cb1628941427fa55e528f53483f239ae011pkasting      bitrate(bitrate),
17425702cb1628941427fa55e528f53483f239ae011pkasting      channels(channels) {
175bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org}
176bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org
177cce874b8d2f448a800317ae375b77a7935336564guoweis@webrtc.orgAudioCodec::AudioCodec() : Codec(), bitrate(0), channels(0) {
178bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org}
179bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org
180bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.orgAudioCodec::AudioCodec(const AudioCodec& c) = default;
181bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org
182bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.orgAudioCodec& AudioCodec::operator=(const AudioCodec& c) {
183bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org  Codec::operator=(c);
184bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org  bitrate = c.bitrate;
185bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org  channels = c.channels;
186bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org  return *this;
187bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org}
188bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org
189bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.orgbool AudioCodec::operator==(const AudioCodec& c) const {
190cce874b8d2f448a800317ae375b77a7935336564guoweis@webrtc.org  return bitrate == c.bitrate && channels == c.channels && Codec::operator==(c);
191bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org}
192bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org
19328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.orgbool AudioCodec::Matches(const AudioCodec& codec) const {
19428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  // If a nonzero clockrate is specified, it must match the actual clockrate.
19528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  // If a nonzero bitrate is specified, it must match the actual bitrate,
19628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  // unless the codec is VBR (0), where we just force the supplied value.
19728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  // The number of channels must match exactly, with the exception
19828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  // that channels=0 is treated synonymously as channels=1, per RFC
19928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  // 4566 section 6: " [The channels] parameter is OPTIONAL and may be
20028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  // omitted if the number of channels is one."
20128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  // Preference is ignored.
20228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  // TODO(juberti): Treat a zero clockrate as 8000Hz, the RTP default clockrate.
20328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  return Codec::Matches(codec) &&
20428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org      ((codec.clockrate == 0 /*&& clockrate == 8000*/) ||
20528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org          clockrate == codec.clockrate) &&
20628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org      (codec.bitrate == 0 || bitrate <= 0 || bitrate == codec.bitrate) &&
20728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org      ((codec.channels < 2 && channels < 2) || channels == codec.channels);
20828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org}
20928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
21028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.orgstd::string AudioCodec::ToString() const {
21128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  std::ostringstream os;
21228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  os << "AudioCodec[" << id << ":" << name << ":" << clockrate << ":" << bitrate
21328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org     << ":" << channels << ":" << preference << "]";
21428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  return os.str();
21528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org}
21628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
21728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.orgstd::string VideoCodec::ToString() const {
21828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  std::ostringstream os;
21928e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  os << "VideoCodec[" << id << ":" << name << ":" << width << ":" << height
22028e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org     << ":" << framerate << ":" << preference << "]";
22128e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  return os.str();
22228e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org}
22328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
22425702cb1628941427fa55e528f53483f239ae011pkastingVideoCodec::VideoCodec(int id,
22525702cb1628941427fa55e528f53483f239ae011pkasting                       const std::string& name,
22625702cb1628941427fa55e528f53483f239ae011pkasting                       int width,
22725702cb1628941427fa55e528f53483f239ae011pkasting                       int height,
22825702cb1628941427fa55e528f53483f239ae011pkasting                       int framerate,
22925702cb1628941427fa55e528f53483f239ae011pkasting                       int preference)
23025702cb1628941427fa55e528f53483f239ae011pkasting    : Codec(id, name, kVideoCodecClockrate, preference),
23125702cb1628941427fa55e528f53483f239ae011pkasting      width(width),
23225702cb1628941427fa55e528f53483f239ae011pkasting      height(height),
23325702cb1628941427fa55e528f53483f239ae011pkasting      framerate(framerate) {
234bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org}
235bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org
23625702cb1628941427fa55e528f53483f239ae011pkastingVideoCodec::VideoCodec(int id, const std::string& name)
23725702cb1628941427fa55e528f53483f239ae011pkasting    : Codec(id, name, kVideoCodecClockrate, 0),
238e62202fedf57b74cc263246c0586ee353978caf8Shao Changbin      width(0),
239e62202fedf57b74cc263246c0586ee353978caf8Shao Changbin      height(0),
240e62202fedf57b74cc263246c0586ee353978caf8Shao Changbin      framerate(0) {
241e62202fedf57b74cc263246c0586ee353978caf8Shao Changbin}
242e62202fedf57b74cc263246c0586ee353978caf8Shao Changbin
243bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.orgVideoCodec::VideoCodec() : Codec(), width(0), height(0), framerate(0) {
244bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org  clockrate = kVideoCodecClockrate;
245bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org}
246bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org
247bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.orgVideoCodec::VideoCodec(const VideoCodec& c) = default;
248bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org
249bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.orgVideoCodec& VideoCodec::operator=(const VideoCodec& c) {
250bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org  Codec::operator=(c);
251bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org  width = c.width;
252bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org  height = c.height;
253bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org  framerate = c.framerate;
254bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org  return *this;
255bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org}
256bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org
257bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.orgbool VideoCodec::operator==(const VideoCodec& c) const {
258bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org  return width == c.width && height == c.height && framerate == c.framerate &&
259bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org         Codec::operator==(c);
260bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org}
261bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org
262b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.orgVideoCodec VideoCodec::CreateRtxCodec(int rtx_payload_type,
263b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org                                      int associated_payload_type) {
264b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org  VideoCodec rtx_codec(rtx_payload_type, kRtxCodecName, 0, 0, 0, 0);
265b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org  rtx_codec.SetParam(kCodecParamAssociatedPayloadType, associated_payload_type);
266b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org  return rtx_codec;
267b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org}
268b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org
269b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.orgVideoCodec::CodecType VideoCodec::GetCodecType() const {
270b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org  const char* payload_name = name.c_str();
271b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org  if (_stricmp(payload_name, kRedCodecName) == 0) {
272b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org    return CODEC_RED;
273b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org  }
274b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org  if (_stricmp(payload_name, kUlpfecCodecName) == 0) {
275b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org    return CODEC_ULPFEC;
276b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org  }
277b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org  if (_stricmp(payload_name, kRtxCodecName) == 0) {
278b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org    return CODEC_RTX;
279b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org  }
280b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org
281b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org  return CODEC_VIDEO;
282b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org}
283b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org
284b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.orgbool VideoCodec::ValidateCodecFormat() const {
285b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org  if (id < 0 || id > 127) {
286b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org    LOG(LS_ERROR) << "Codec with invalid payload type: " << ToString();
287b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org    return false;
288b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org  }
289b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org  if (GetCodecType() != CODEC_VIDEO) {
290b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org    return true;
291b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org  }
292b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org
293b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org  // Video validation from here on.
294b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org
295b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org  if (width <= 0 || height <= 0) {
296b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org    LOG(LS_ERROR) << "Codec with invalid dimensions: " << ToString();
297b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org    return false;
298b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org  }
299b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org  int min_bitrate = -1;
300b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org  int max_bitrate = -1;
301b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org  if (GetParam(kCodecParamMinBitrate, &min_bitrate) &&
302b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org      GetParam(kCodecParamMaxBitrate, &max_bitrate)) {
303b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org    if (max_bitrate < min_bitrate) {
304b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org      LOG(LS_ERROR) << "Codec with max < min bitrate: " << ToString();
305b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org      return false;
306b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org    }
307b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org  }
308b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org  return true;
309b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org}
310b5a22b14648c53874b4b76368a1a2271d985e875pbos@webrtc.org
311bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.orgDataCodec::DataCodec(int id, const std::string& name, int preference)
312bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org    : Codec(id, name, kDataCodecClockrate, preference) {
313bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org}
314bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org
315bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.orgDataCodec::DataCodec() : Codec() {
316bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org  clockrate = kDataCodecClockrate;
317bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org}
318bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org
319bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.orgDataCodec::DataCodec(const DataCodec& c) = default;
320bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org
321bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.orgDataCodec& DataCodec::operator=(const DataCodec& c) = default;
322bc6961fe323bf60ee9fa5f6b6569f0f64a80276dguoweis@webrtc.org
32328e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.orgstd::string DataCodec::ToString() const {
32428e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  std::ostringstream os;
32528e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  os << "DataCodec[" << id << ":" << name << "]";
32628e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org  return os.str();
32728e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org}
32828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org
329e62202fedf57b74cc263246c0586ee353978caf8Shao Changbinbool HasNack(const VideoCodec& codec) {
330e62202fedf57b74cc263246c0586ee353978caf8Shao Changbin  return codec.HasFeedbackParam(
331e62202fedf57b74cc263246c0586ee353978caf8Shao Changbin      FeedbackParam(kRtcpFbParamNack, kParamValueEmpty));
332e62202fedf57b74cc263246c0586ee353978caf8Shao Changbin}
333e62202fedf57b74cc263246c0586ee353978caf8Shao Changbin
334e62202fedf57b74cc263246c0586ee353978caf8Shao Changbinbool HasRemb(const VideoCodec& codec) {
335e62202fedf57b74cc263246c0586ee353978caf8Shao Changbin  return codec.HasFeedbackParam(
336e62202fedf57b74cc263246c0586ee353978caf8Shao Changbin      FeedbackParam(kRtcpFbParamRemb, kParamValueEmpty));
337e62202fedf57b74cc263246c0586ee353978caf8Shao Changbin}
338e62202fedf57b74cc263246c0586ee353978caf8Shao Changbin
33943edf0ffb91a50e2efa01c7befe4d188a7e30ea2stefanbool HasTransportCc(const VideoCodec& codec) {
34043edf0ffb91a50e2efa01c7befe4d188a7e30ea2stefan  return codec.HasFeedbackParam(
34143edf0ffb91a50e2efa01c7befe4d188a7e30ea2stefan      FeedbackParam(kRtcpFbParamTransportCc, kParamValueEmpty));
34243edf0ffb91a50e2efa01c7befe4d188a7e30ea2stefan}
34343edf0ffb91a50e2efa01c7befe4d188a7e30ea2stefan
344e62202fedf57b74cc263246c0586ee353978caf8Shao Changbinbool CodecNamesEq(const std::string& name1, const std::string& name2) {
345e62202fedf57b74cc263246c0586ee353978caf8Shao Changbin  return _stricmp(name1.c_str(), name2.c_str()) == 0;
346e62202fedf57b74cc263246c0586ee353978caf8Shao Changbin}
347e62202fedf57b74cc263246c0586ee353978caf8Shao Changbin
34828e20752806a492f5a6a5d343c02f9556f39b1cdhenrike@webrtc.org}  // namespace cricket
349