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 "webrtc/base/common.h"
34 #include "webrtc/base/logging.h"
35 #include "webrtc/base/stringencode.h"
36 #include "webrtc/base/stringutils.h"
37
38 namespace cricket {
39
40 static const int kMaxStaticPayloadId = 95;
41 const int kMaxPayloadId = 127;
42
operator ==(const FeedbackParam & other) const43 bool FeedbackParam::operator==(const FeedbackParam& other) const {
44 return _stricmp(other.id().c_str(), id().c_str()) == 0 &&
45 _stricmp(other.param().c_str(), param().c_str()) == 0;
46 }
47
operator ==(const FeedbackParams & other) const48 bool FeedbackParams::operator==(const FeedbackParams& other) const {
49 return params_ == other.params_;
50 }
51
Has(const FeedbackParam & param) const52 bool FeedbackParams::Has(const FeedbackParam& param) const {
53 return std::find(params_.begin(), params_.end(), param) != params_.end();
54 }
55
Add(const FeedbackParam & param)56 void FeedbackParams::Add(const FeedbackParam& param) {
57 if (param.id().empty()) {
58 return;
59 }
60 if (Has(param)) {
61 // Param already in |this|.
62 return;
63 }
64 params_.push_back(param);
65 ASSERT(!HasDuplicateEntries());
66 }
67
Intersect(const FeedbackParams & from)68 void FeedbackParams::Intersect(const FeedbackParams& from) {
69 std::vector<FeedbackParam>::iterator iter_to = params_.begin();
70 while (iter_to != params_.end()) {
71 if (!from.Has(*iter_to)) {
72 iter_to = params_.erase(iter_to);
73 } else {
74 ++iter_to;
75 }
76 }
77 }
78
HasDuplicateEntries() const79 bool FeedbackParams::HasDuplicateEntries() const {
80 for (std::vector<FeedbackParam>::const_iterator iter = params_.begin();
81 iter != params_.end(); ++iter) {
82 for (std::vector<FeedbackParam>::const_iterator found = iter + 1;
83 found != params_.end(); ++found) {
84 if (*found == *iter) {
85 return true;
86 }
87 }
88 }
89 return false;
90 }
91
Matches(const Codec & codec) const92 bool Codec::Matches(const Codec& codec) const {
93 // Match the codec id/name based on the typical static/dynamic name rules.
94 // Matching is case-insensitive.
95 return (codec.id <= kMaxStaticPayloadId) ?
96 (id == codec.id) : (_stricmp(name.c_str(), codec.name.c_str()) == 0);
97 }
98
GetParam(const std::string & name,std::string * out) const99 bool Codec::GetParam(const std::string& name, std::string* out) const {
100 CodecParameterMap::const_iterator iter = params.find(name);
101 if (iter == params.end())
102 return false;
103 *out = iter->second;
104 return true;
105 }
106
GetParam(const std::string & name,int * out) const107 bool Codec::GetParam(const std::string& name, int* out) const {
108 CodecParameterMap::const_iterator iter = params.find(name);
109 if (iter == params.end())
110 return false;
111 return rtc::FromString(iter->second, out);
112 }
113
SetParam(const std::string & name,const std::string & value)114 void Codec::SetParam(const std::string& name, const std::string& value) {
115 params[name] = value;
116 }
117
SetParam(const std::string & name,int value)118 void Codec::SetParam(const std::string& name, int value) {
119 params[name] = rtc::ToString(value);
120 }
121
RemoveParam(const std::string & name)122 bool Codec::RemoveParam(const std::string& name) {
123 return params.erase(name) == 1;
124 }
125
AddFeedbackParam(const FeedbackParam & param)126 void Codec::AddFeedbackParam(const FeedbackParam& param) {
127 feedback_params.Add(param);
128 }
129
HasFeedbackParam(const FeedbackParam & param) const130 bool Codec::HasFeedbackParam(const FeedbackParam& param) const {
131 return feedback_params.Has(param);
132 }
133
IntersectFeedbackParams(const Codec & other)134 void Codec::IntersectFeedbackParams(const Codec& other) {
135 feedback_params.Intersect(other.feedback_params);
136 }
137
Matches(const AudioCodec & codec) const138 bool AudioCodec::Matches(const AudioCodec& codec) const {
139 // If a nonzero clockrate is specified, it must match the actual clockrate.
140 // If a nonzero bitrate is specified, it must match the actual bitrate,
141 // unless the codec is VBR (0), where we just force the supplied value.
142 // The number of channels must match exactly, with the exception
143 // that channels=0 is treated synonymously as channels=1, per RFC
144 // 4566 section 6: " [The channels] parameter is OPTIONAL and may be
145 // omitted if the number of channels is one."
146 // Preference is ignored.
147 // TODO(juberti): Treat a zero clockrate as 8000Hz, the RTP default clockrate.
148 return Codec::Matches(codec) &&
149 ((codec.clockrate == 0 /*&& clockrate == 8000*/) ||
150 clockrate == codec.clockrate) &&
151 (codec.bitrate == 0 || bitrate <= 0 || bitrate == codec.bitrate) &&
152 ((codec.channels < 2 && channels < 2) || channels == codec.channels);
153 }
154
ToString() const155 std::string AudioCodec::ToString() const {
156 std::ostringstream os;
157 os << "AudioCodec[" << id << ":" << name << ":" << clockrate << ":" << bitrate
158 << ":" << channels << ":" << preference << "]";
159 return os.str();
160 }
161
ToString() const162 std::string VideoCodec::ToString() const {
163 std::ostringstream os;
164 os << "VideoCodec[" << id << ":" << name << ":" << width << ":" << height
165 << ":" << framerate << ":" << preference << "]";
166 return os.str();
167 }
168
CreateRtxCodec(int rtx_payload_type,int associated_payload_type)169 VideoCodec VideoCodec::CreateRtxCodec(int rtx_payload_type,
170 int associated_payload_type) {
171 VideoCodec rtx_codec(rtx_payload_type, kRtxCodecName, 0, 0, 0, 0);
172 rtx_codec.SetParam(kCodecParamAssociatedPayloadType, associated_payload_type);
173 return rtx_codec;
174 }
175
GetCodecType() const176 VideoCodec::CodecType VideoCodec::GetCodecType() const {
177 const char* payload_name = name.c_str();
178 if (_stricmp(payload_name, kRedCodecName) == 0) {
179 return CODEC_RED;
180 }
181 if (_stricmp(payload_name, kUlpfecCodecName) == 0) {
182 return CODEC_ULPFEC;
183 }
184 if (_stricmp(payload_name, kRtxCodecName) == 0) {
185 return CODEC_RTX;
186 }
187
188 return CODEC_VIDEO;
189 }
190
ValidateCodecFormat() const191 bool VideoCodec::ValidateCodecFormat() const {
192 if (id < 0 || id > 127) {
193 LOG(LS_ERROR) << "Codec with invalid payload type: " << ToString();
194 return false;
195 }
196 if (GetCodecType() != CODEC_VIDEO) {
197 return true;
198 }
199
200 // Video validation from here on.
201
202 if (width <= 0 || height <= 0) {
203 LOG(LS_ERROR) << "Codec with invalid dimensions: " << ToString();
204 return false;
205 }
206 int min_bitrate = -1;
207 int max_bitrate = -1;
208 if (GetParam(kCodecParamMinBitrate, &min_bitrate) &&
209 GetParam(kCodecParamMaxBitrate, &max_bitrate)) {
210 if (max_bitrate < min_bitrate) {
211 LOG(LS_ERROR) << "Codec with max < min bitrate: " << ToString();
212 return false;
213 }
214 }
215 return true;
216 }
217
ToString() const218 std::string DataCodec::ToString() const {
219 std::ostringstream os;
220 os << "DataCodec[" << id << ":" << name << "]";
221 return os.str();
222 }
223
224 } // namespace cricket
225