Home | History | Annotate | Download | only in base
      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/logging.h"
     35 #include "talk/base/stringencode.h"
     36 #include "talk/base/stringutils.h"
     37 
     38 namespace cricket {
     39 
     40 static const int kMaxStaticPayloadId = 95;
     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 
     91 bool Codec::Matches(const Codec& codec) const {
     92   // Match the codec id/name based on the typical static/dynamic name rules.
     93   // Matching is case-insensitive.
     94   return (codec.id <= kMaxStaticPayloadId) ?
     95       (id == codec.id) : (_stricmp(name.c_str(), codec.name.c_str()) == 0);
     96 }
     97 
     98 bool Codec::GetParam(const std::string& name, std::string* out) const {
     99   CodecParameterMap::const_iterator iter = params.find(name);
    100   if (iter == params.end())
    101     return false;
    102   *out = iter->second;
    103   return true;
    104 }
    105 
    106 bool Codec::GetParam(const std::string& name, int* out) const {
    107   CodecParameterMap::const_iterator iter = params.find(name);
    108   if (iter == params.end())
    109     return false;
    110   return talk_base::FromString(iter->second, out);
    111 }
    112 
    113 void Codec::SetParam(const std::string& name, const std::string& value) {
    114   params[name] = value;
    115 }
    116 
    117 void Codec::SetParam(const std::string& name, int value)  {
    118   params[name] = talk_base::ToString(value);
    119 }
    120 
    121 void Codec::AddFeedbackParam(const FeedbackParam& param) {
    122   feedback_params.Add(param);
    123 }
    124 
    125 bool Codec::HasFeedbackParam(const FeedbackParam& param) const {
    126   return feedback_params.Has(param);
    127 }
    128 
    129 void Codec::IntersectFeedbackParams(const Codec& other) {
    130   feedback_params.Intersect(other.feedback_params);
    131 }
    132 
    133 bool AudioCodec::Matches(const AudioCodec& codec) const {
    134   // If a nonzero clockrate is specified, it must match the actual clockrate.
    135   // If a nonzero bitrate is specified, it must match the actual bitrate,
    136   // unless the codec is VBR (0), where we just force the supplied value.
    137   // The number of channels must match exactly, with the exception
    138   // that channels=0 is treated synonymously as channels=1, per RFC
    139   // 4566 section 6: " [The channels] parameter is OPTIONAL and may be
    140   // omitted if the number of channels is one."
    141   // Preference is ignored.
    142   // TODO(juberti): Treat a zero clockrate as 8000Hz, the RTP default clockrate.
    143   return Codec::Matches(codec) &&
    144       ((codec.clockrate == 0 /*&& clockrate == 8000*/) ||
    145           clockrate == codec.clockrate) &&
    146       (codec.bitrate == 0 || bitrate <= 0 || bitrate == codec.bitrate) &&
    147       ((codec.channels < 2 && channels < 2) || channels == codec.channels);
    148 }
    149 
    150 std::string AudioCodec::ToString() const {
    151   std::ostringstream os;
    152   os << "AudioCodec[" << id << ":" << name << ":" << clockrate << ":" << bitrate
    153      << ":" << channels << ":" << preference << "]";
    154   return os.str();
    155 }
    156 
    157 std::string VideoCodec::ToString() const {
    158   std::ostringstream os;
    159   os << "VideoCodec[" << id << ":" << name << ":" << width << ":" << height
    160      << ":" << framerate << ":" << preference << "]";
    161   return os.str();
    162 }
    163 
    164 VideoCodec VideoCodec::CreateRtxCodec(int rtx_payload_type,
    165                                       int associated_payload_type) {
    166   VideoCodec rtx_codec(rtx_payload_type, kRtxCodecName, 0, 0, 0, 0);
    167   rtx_codec.SetParam(kCodecParamAssociatedPayloadType, associated_payload_type);
    168   return rtx_codec;
    169 }
    170 
    171 VideoCodec::CodecType VideoCodec::GetCodecType() const {
    172   const char* payload_name = name.c_str();
    173   if (_stricmp(payload_name, kRedCodecName) == 0) {
    174     return CODEC_RED;
    175   }
    176   if (_stricmp(payload_name, kUlpfecCodecName) == 0) {
    177     return CODEC_ULPFEC;
    178   }
    179   if (_stricmp(payload_name, kRtxCodecName) == 0) {
    180     return CODEC_RTX;
    181   }
    182 
    183   return CODEC_VIDEO;
    184 }
    185 
    186 bool VideoCodec::ValidateCodecFormat() const {
    187   if (id < 0 || id > 127) {
    188     LOG(LS_ERROR) << "Codec with invalid payload type: " << ToString();
    189     return false;
    190   }
    191   if (GetCodecType() != CODEC_VIDEO) {
    192     return true;
    193   }
    194 
    195   // Video validation from here on.
    196 
    197   if (width <= 0 || height <= 0) {
    198     LOG(LS_ERROR) << "Codec with invalid dimensions: " << ToString();
    199     return false;
    200   }
    201   int min_bitrate = -1;
    202   int max_bitrate = -1;
    203   if (GetParam(kCodecParamMinBitrate, &min_bitrate) &&
    204       GetParam(kCodecParamMaxBitrate, &max_bitrate)) {
    205     if (max_bitrate < min_bitrate) {
    206       LOG(LS_ERROR) << "Codec with max < min bitrate: " << ToString();
    207       return false;
    208     }
    209   }
    210   return true;
    211 }
    212 
    213 std::string DataCodec::ToString() const {
    214   std::ostringstream os;
    215   os << "DataCodec[" << id << ":" << name << "]";
    216   return os.str();
    217 }
    218 
    219 }  // namespace cricket
    220