• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
33 #include "talk/base/common.h"
34 #include "talk/base/stringencode.h"
35 #include "talk/base/stringutils.h"
36 
37 namespace cricket {
38 
39 static const int kMaxStaticPayloadId = 95;
40 
operator ==(const FeedbackParam & other) const41 bool FeedbackParam::operator==(const FeedbackParam& other) const {
42   return _stricmp(other.id().c_str(), id().c_str()) == 0 &&
43       _stricmp(other.param().c_str(), param().c_str()) == 0;
44 }
45 
operator ==(const FeedbackParams & other) const46 bool FeedbackParams::operator==(const FeedbackParams& other) const {
47   return params_ == other.params_;
48 }
49 
Has(const FeedbackParam & param) const50 bool FeedbackParams::Has(const FeedbackParam& param) const {
51   return std::find(params_.begin(), params_.end(), param) != params_.end();
52 }
53 
Add(const FeedbackParam & param)54 void FeedbackParams::Add(const FeedbackParam& param) {
55   if (param.id().empty()) {
56     return;
57   }
58   if (Has(param)) {
59     // Param already in |this|.
60     return;
61   }
62   params_.push_back(param);
63   ASSERT(!HasDuplicateEntries());
64 }
65 
Intersect(const FeedbackParams & from)66 void FeedbackParams::Intersect(const FeedbackParams& from) {
67   std::vector<FeedbackParam>::iterator iter_to = params_.begin();
68   while (iter_to != params_.end()) {
69     if (!from.Has(*iter_to)) {
70       iter_to = params_.erase(iter_to);
71     } else {
72       ++iter_to;
73     }
74   }
75 }
76 
HasDuplicateEntries() const77 bool FeedbackParams::HasDuplicateEntries() const {
78   for (std::vector<FeedbackParam>::const_iterator iter = params_.begin();
79        iter != params_.end(); ++iter) {
80     for (std::vector<FeedbackParam>::const_iterator found = iter + 1;
81          found != params_.end(); ++found) {
82       if (*found == *iter) {
83         return true;
84       }
85     }
86   }
87   return false;
88 }
89 
Matches(const Codec & codec) const90 bool Codec::Matches(const Codec& codec) const {
91   // Match the codec id/name based on the typical static/dynamic name rules.
92   // Matching is case-insensitive.
93   return (codec.id <= kMaxStaticPayloadId) ?
94       (id == codec.id) : (_stricmp(name.c_str(), codec.name.c_str()) == 0);
95 }
96 
GetParam(const std::string & name,std::string * out) const97 bool Codec::GetParam(const std::string& name, std::string* out) const {
98   CodecParameterMap::const_iterator iter = params.find(name);
99   if (iter == params.end())
100     return false;
101   *out = iter->second;
102   return true;
103 }
104 
GetParam(const std::string & name,int * out) const105 bool Codec::GetParam(const std::string& name, int* out) const {
106   CodecParameterMap::const_iterator iter = params.find(name);
107   if (iter == params.end())
108     return false;
109   return talk_base::FromString(iter->second, out);
110 }
111 
SetParam(const std::string & name,const std::string & value)112 void Codec::SetParam(const std::string& name, const std::string& value) {
113   params[name] = value;
114 }
115 
SetParam(const std::string & name,int value)116 void Codec::SetParam(const std::string& name, int value)  {
117   params[name] = talk_base::ToString(value);
118 }
119 
AddFeedbackParam(const FeedbackParam & param)120 void Codec::AddFeedbackParam(const FeedbackParam& param) {
121   feedback_params.Add(param);
122 }
123 
HasFeedbackParam(const FeedbackParam & param) const124 bool Codec::HasFeedbackParam(const FeedbackParam& param) const {
125   return feedback_params.Has(param);
126 }
127 
IntersectFeedbackParams(const Codec & other)128 void Codec::IntersectFeedbackParams(const Codec& other) {
129   feedback_params.Intersect(other.feedback_params);
130 }
131 
Matches(const AudioCodec & codec) const132 bool AudioCodec::Matches(const AudioCodec& codec) const {
133   // If a nonzero clockrate is specified, it must match the actual clockrate.
134   // If a nonzero bitrate is specified, it must match the actual bitrate,
135   // unless the codec is VBR (0), where we just force the supplied value.
136   // The number of channels must match exactly, with the exception
137   // that channels=0 is treated synonymously as channels=1, per RFC
138   // 4566 section 6: " [The channels] parameter is OPTIONAL and may be
139   // omitted if the number of channels is one."
140   // Preference is ignored.
141   // TODO(juberti): Treat a zero clockrate as 8000Hz, the RTP default clockrate.
142   return Codec::Matches(codec) &&
143       ((codec.clockrate == 0 /*&& clockrate == 8000*/) ||
144           clockrate == codec.clockrate) &&
145       (codec.bitrate == 0 || bitrate <= 0 || bitrate == codec.bitrate) &&
146       ((codec.channels < 2 && channels < 2) || channels == codec.channels);
147 }
148 
ToString() const149 std::string AudioCodec::ToString() const {
150   std::ostringstream os;
151   os << "AudioCodec[" << id << ":" << name << ":" << clockrate << ":" << bitrate
152      << ":" << channels << ":" << preference << "]";
153   return os.str();
154 }
155 
ToString() const156 std::string VideoCodec::ToString() const {
157   std::ostringstream os;
158   os << "VideoCodec[" << id << ":" << name << ":" << width << ":" << height
159      << ":" << framerate << ":" << preference << "]";
160   return os.str();
161 }
162 
ToString() const163 std::string DataCodec::ToString() const {
164   std::ostringstream os;
165   os << "DataCodec[" << id << ":" << name << "]";
166   return os.str();
167 }
168 
169 }  // namespace cricket
170