You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

426 lines
14 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2004 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "media/base/codec.h"
  11. #include "absl/algorithm/container.h"
  12. #include "absl/strings/match.h"
  13. #include "media/base/h264_profile_level_id.h"
  14. #include "media/base/vp9_profile.h"
  15. #include "rtc_base/checks.h"
  16. #include "rtc_base/logging.h"
  17. #include "rtc_base/string_encode.h"
  18. #include "rtc_base/strings/string_builder.h"
  19. namespace cricket {
  20. namespace {
  21. std::string GetH264PacketizationModeOrDefault(const CodecParameterMap& params) {
  22. auto it = params.find(kH264FmtpPacketizationMode);
  23. if (it != params.end()) {
  24. return it->second;
  25. }
  26. // If packetization-mode is not present, default to "0".
  27. // https://tools.ietf.org/html/rfc6184#section-6.2
  28. return "0";
  29. }
  30. bool IsSameH264PacketizationMode(const CodecParameterMap& left,
  31. const CodecParameterMap& right) {
  32. return GetH264PacketizationModeOrDefault(left) ==
  33. GetH264PacketizationModeOrDefault(right);
  34. }
  35. // Some (video) codecs are actually families of codecs and rely on parameters
  36. // to distinguish different incompatible family members.
  37. bool IsSameCodecSpecific(const std::string& name1,
  38. const CodecParameterMap& params1,
  39. const std::string& name2,
  40. const CodecParameterMap& params2) {
  41. // The names might not necessarily match, so check both.
  42. auto either_name_matches = [&](const std::string name) {
  43. return absl::EqualsIgnoreCase(name, name1) ||
  44. absl::EqualsIgnoreCase(name, name2);
  45. };
  46. if (either_name_matches(kH264CodecName))
  47. return webrtc::H264::IsSameH264Profile(params1, params2) &&
  48. IsSameH264PacketizationMode(params1, params2);
  49. if (either_name_matches(kVp9CodecName))
  50. return webrtc::IsSameVP9Profile(params1, params2);
  51. return true;
  52. }
  53. } // namespace
  54. FeedbackParams::FeedbackParams() = default;
  55. FeedbackParams::~FeedbackParams() = default;
  56. bool FeedbackParam::operator==(const FeedbackParam& other) const {
  57. return absl::EqualsIgnoreCase(other.id(), id()) &&
  58. absl::EqualsIgnoreCase(other.param(), param());
  59. }
  60. bool FeedbackParams::operator==(const FeedbackParams& other) const {
  61. return params_ == other.params_;
  62. }
  63. bool FeedbackParams::Has(const FeedbackParam& param) const {
  64. return absl::c_linear_search(params_, param);
  65. }
  66. void FeedbackParams::Add(const FeedbackParam& param) {
  67. if (param.id().empty()) {
  68. return;
  69. }
  70. if (Has(param)) {
  71. // Param already in |this|.
  72. return;
  73. }
  74. params_.push_back(param);
  75. RTC_CHECK(!HasDuplicateEntries());
  76. }
  77. void FeedbackParams::Intersect(const FeedbackParams& from) {
  78. std::vector<FeedbackParam>::iterator iter_to = params_.begin();
  79. while (iter_to != params_.end()) {
  80. if (!from.Has(*iter_to)) {
  81. iter_to = params_.erase(iter_to);
  82. } else {
  83. ++iter_to;
  84. }
  85. }
  86. }
  87. bool FeedbackParams::HasDuplicateEntries() const {
  88. for (std::vector<FeedbackParam>::const_iterator iter = params_.begin();
  89. iter != params_.end(); ++iter) {
  90. for (std::vector<FeedbackParam>::const_iterator found = iter + 1;
  91. found != params_.end(); ++found) {
  92. if (*found == *iter) {
  93. return true;
  94. }
  95. }
  96. }
  97. return false;
  98. }
  99. Codec::Codec(int id, const std::string& name, int clockrate)
  100. : id(id), name(name), clockrate(clockrate) {}
  101. Codec::Codec() : id(0), clockrate(0) {}
  102. Codec::Codec(const Codec& c) = default;
  103. Codec::Codec(Codec&& c) = default;
  104. Codec::~Codec() = default;
  105. Codec& Codec::operator=(const Codec& c) = default;
  106. Codec& Codec::operator=(Codec&& c) = default;
  107. bool Codec::operator==(const Codec& c) const {
  108. return this->id == c.id && // id is reserved in objective-c
  109. name == c.name && clockrate == c.clockrate && params == c.params &&
  110. feedback_params == c.feedback_params;
  111. }
  112. bool Codec::Matches(const Codec& codec) const {
  113. // Match the codec id/name based on the typical static/dynamic name rules.
  114. // Matching is case-insensitive.
  115. const int kMaxStaticPayloadId = 95;
  116. return (id <= kMaxStaticPayloadId || codec.id <= kMaxStaticPayloadId)
  117. ? (id == codec.id)
  118. : (absl::EqualsIgnoreCase(name, codec.name));
  119. }
  120. bool Codec::MatchesCapability(
  121. const webrtc::RtpCodecCapability& codec_capability) const {
  122. webrtc::RtpCodecParameters codec_parameters = ToCodecParameters();
  123. return codec_parameters.name == codec_capability.name &&
  124. codec_parameters.kind == codec_capability.kind &&
  125. (codec_parameters.name == cricket::kRtxCodecName ||
  126. (codec_parameters.num_channels == codec_capability.num_channels &&
  127. codec_parameters.clock_rate == codec_capability.clock_rate &&
  128. codec_parameters.parameters == codec_capability.parameters));
  129. }
  130. bool Codec::GetParam(const std::string& name, std::string* out) const {
  131. CodecParameterMap::const_iterator iter = params.find(name);
  132. if (iter == params.end())
  133. return false;
  134. *out = iter->second;
  135. return true;
  136. }
  137. bool Codec::GetParam(const std::string& name, int* out) const {
  138. CodecParameterMap::const_iterator iter = params.find(name);
  139. if (iter == params.end())
  140. return false;
  141. return rtc::FromString(iter->second, out);
  142. }
  143. void Codec::SetParam(const std::string& name, const std::string& value) {
  144. params[name] = value;
  145. }
  146. void Codec::SetParam(const std::string& name, int value) {
  147. params[name] = rtc::ToString(value);
  148. }
  149. bool Codec::RemoveParam(const std::string& name) {
  150. return params.erase(name) == 1;
  151. }
  152. void Codec::AddFeedbackParam(const FeedbackParam& param) {
  153. feedback_params.Add(param);
  154. }
  155. bool Codec::HasFeedbackParam(const FeedbackParam& param) const {
  156. return feedback_params.Has(param);
  157. }
  158. void Codec::IntersectFeedbackParams(const Codec& other) {
  159. feedback_params.Intersect(other.feedback_params);
  160. }
  161. webrtc::RtpCodecParameters Codec::ToCodecParameters() const {
  162. webrtc::RtpCodecParameters codec_params;
  163. codec_params.payload_type = id;
  164. codec_params.name = name;
  165. codec_params.clock_rate = clockrate;
  166. codec_params.parameters.insert(params.begin(), params.end());
  167. return codec_params;
  168. }
  169. AudioCodec::AudioCodec(int id,
  170. const std::string& name,
  171. int clockrate,
  172. int bitrate,
  173. size_t channels)
  174. : Codec(id, name, clockrate), bitrate(bitrate), channels(channels) {}
  175. AudioCodec::AudioCodec() : Codec(), bitrate(0), channels(0) {}
  176. AudioCodec::AudioCodec(const AudioCodec& c) = default;
  177. AudioCodec::AudioCodec(AudioCodec&& c) = default;
  178. AudioCodec& AudioCodec::operator=(const AudioCodec& c) = default;
  179. AudioCodec& AudioCodec::operator=(AudioCodec&& c) = default;
  180. bool AudioCodec::operator==(const AudioCodec& c) const {
  181. return bitrate == c.bitrate && channels == c.channels && Codec::operator==(c);
  182. }
  183. bool AudioCodec::Matches(const AudioCodec& codec) const {
  184. // If a nonzero clockrate is specified, it must match the actual clockrate.
  185. // If a nonzero bitrate is specified, it must match the actual bitrate,
  186. // unless the codec is VBR (0), where we just force the supplied value.
  187. // The number of channels must match exactly, with the exception
  188. // that channels=0 is treated synonymously as channels=1, per RFC
  189. // 4566 section 6: " [The channels] parameter is OPTIONAL and may be
  190. // omitted if the number of channels is one."
  191. // Preference is ignored.
  192. // TODO(juberti): Treat a zero clockrate as 8000Hz, the RTP default clockrate.
  193. return Codec::Matches(codec) &&
  194. ((codec.clockrate == 0 /*&& clockrate == 8000*/) ||
  195. clockrate == codec.clockrate) &&
  196. (codec.bitrate == 0 || bitrate <= 0 || bitrate == codec.bitrate) &&
  197. ((codec.channels < 2 && channels < 2) || channels == codec.channels);
  198. }
  199. std::string AudioCodec::ToString() const {
  200. char buf[256];
  201. rtc::SimpleStringBuilder sb(buf);
  202. sb << "AudioCodec[" << id << ":" << name << ":" << clockrate << ":" << bitrate
  203. << ":" << channels << "]";
  204. return sb.str();
  205. }
  206. webrtc::RtpCodecParameters AudioCodec::ToCodecParameters() const {
  207. webrtc::RtpCodecParameters codec_params = Codec::ToCodecParameters();
  208. codec_params.num_channels = static_cast<int>(channels);
  209. codec_params.kind = MEDIA_TYPE_AUDIO;
  210. return codec_params;
  211. }
  212. std::string VideoCodec::ToString() const {
  213. char buf[256];
  214. rtc::SimpleStringBuilder sb(buf);
  215. sb << "VideoCodec[" << id << ":" << name << "]";
  216. return sb.str();
  217. }
  218. webrtc::RtpCodecParameters VideoCodec::ToCodecParameters() const {
  219. webrtc::RtpCodecParameters codec_params = Codec::ToCodecParameters();
  220. codec_params.kind = MEDIA_TYPE_VIDEO;
  221. return codec_params;
  222. }
  223. VideoCodec::VideoCodec(int id, const std::string& name)
  224. : Codec(id, name, kVideoCodecClockrate) {
  225. SetDefaultParameters();
  226. }
  227. VideoCodec::VideoCodec(const std::string& name) : VideoCodec(0 /* id */, name) {
  228. SetDefaultParameters();
  229. }
  230. VideoCodec::VideoCodec() : Codec() {
  231. clockrate = kVideoCodecClockrate;
  232. }
  233. VideoCodec::VideoCodec(const webrtc::SdpVideoFormat& c)
  234. : Codec(0 /* id */, c.name, kVideoCodecClockrate) {
  235. params = c.parameters;
  236. }
  237. VideoCodec::VideoCodec(const VideoCodec& c) = default;
  238. VideoCodec::VideoCodec(VideoCodec&& c) = default;
  239. VideoCodec& VideoCodec::operator=(const VideoCodec& c) = default;
  240. VideoCodec& VideoCodec::operator=(VideoCodec&& c) = default;
  241. void VideoCodec::SetDefaultParameters() {
  242. if (absl::EqualsIgnoreCase(kH264CodecName, name)) {
  243. // This default is set for all H.264 codecs created because
  244. // that was the default before packetization mode support was added.
  245. // TODO(hta): Move this to the places that create VideoCodecs from
  246. // SDP or from knowledge of implementation capabilities.
  247. SetParam(kH264FmtpPacketizationMode, "1");
  248. }
  249. }
  250. bool VideoCodec::operator==(const VideoCodec& c) const {
  251. return Codec::operator==(c) && packetization == c.packetization;
  252. }
  253. bool VideoCodec::Matches(const VideoCodec& other) const {
  254. return Codec::Matches(other) &&
  255. IsSameCodecSpecific(name, params, other.name, other.params);
  256. }
  257. absl::optional<std::string> VideoCodec::IntersectPacketization(
  258. const VideoCodec& local_codec,
  259. const VideoCodec& remote_codec) {
  260. if (local_codec.packetization == remote_codec.packetization) {
  261. return local_codec.packetization;
  262. }
  263. return absl::nullopt;
  264. }
  265. VideoCodec VideoCodec::CreateRtxCodec(int rtx_payload_type,
  266. int associated_payload_type) {
  267. VideoCodec rtx_codec(rtx_payload_type, kRtxCodecName);
  268. rtx_codec.SetParam(kCodecParamAssociatedPayloadType, associated_payload_type);
  269. return rtx_codec;
  270. }
  271. VideoCodec::CodecType VideoCodec::GetCodecType() const {
  272. if (absl::EqualsIgnoreCase(name, kRedCodecName)) {
  273. return CODEC_RED;
  274. }
  275. if (absl::EqualsIgnoreCase(name, kUlpfecCodecName)) {
  276. return CODEC_ULPFEC;
  277. }
  278. if (absl::EqualsIgnoreCase(name, kFlexfecCodecName)) {
  279. return CODEC_FLEXFEC;
  280. }
  281. if (absl::EqualsIgnoreCase(name, kRtxCodecName)) {
  282. return CODEC_RTX;
  283. }
  284. return CODEC_VIDEO;
  285. }
  286. bool VideoCodec::ValidateCodecFormat() const {
  287. if (id < 0 || id > 127) {
  288. RTC_LOG(LS_ERROR) << "Codec with invalid payload type: " << ToString();
  289. return false;
  290. }
  291. if (GetCodecType() != CODEC_VIDEO) {
  292. return true;
  293. }
  294. // Video validation from here on.
  295. int min_bitrate = -1;
  296. int max_bitrate = -1;
  297. if (GetParam(kCodecParamMinBitrate, &min_bitrate) &&
  298. GetParam(kCodecParamMaxBitrate, &max_bitrate)) {
  299. if (max_bitrate < min_bitrate) {
  300. RTC_LOG(LS_ERROR) << "Codec with max < min bitrate: " << ToString();
  301. return false;
  302. }
  303. }
  304. return true;
  305. }
  306. RtpDataCodec::RtpDataCodec(int id, const std::string& name)
  307. : Codec(id, name, kDataCodecClockrate) {}
  308. RtpDataCodec::RtpDataCodec() : Codec() {
  309. clockrate = kDataCodecClockrate;
  310. }
  311. RtpDataCodec::RtpDataCodec(const RtpDataCodec& c) = default;
  312. RtpDataCodec::RtpDataCodec(RtpDataCodec&& c) = default;
  313. RtpDataCodec& RtpDataCodec::operator=(const RtpDataCodec& c) = default;
  314. RtpDataCodec& RtpDataCodec::operator=(RtpDataCodec&& c) = default;
  315. std::string RtpDataCodec::ToString() const {
  316. char buf[256];
  317. rtc::SimpleStringBuilder sb(buf);
  318. sb << "RtpDataCodec[" << id << ":" << name << "]";
  319. return sb.str();
  320. }
  321. bool HasLntf(const Codec& codec) {
  322. return codec.HasFeedbackParam(
  323. FeedbackParam(kRtcpFbParamLntf, kParamValueEmpty));
  324. }
  325. bool HasNack(const Codec& codec) {
  326. return codec.HasFeedbackParam(
  327. FeedbackParam(kRtcpFbParamNack, kParamValueEmpty));
  328. }
  329. bool HasRemb(const Codec& codec) {
  330. return codec.HasFeedbackParam(
  331. FeedbackParam(kRtcpFbParamRemb, kParamValueEmpty));
  332. }
  333. bool HasRrtr(const Codec& codec) {
  334. return codec.HasFeedbackParam(
  335. FeedbackParam(kRtcpFbParamRrtr, kParamValueEmpty));
  336. }
  337. bool HasTransportCc(const Codec& codec) {
  338. return codec.HasFeedbackParam(
  339. FeedbackParam(kRtcpFbParamTransportCc, kParamValueEmpty));
  340. }
  341. const VideoCodec* FindMatchingCodec(
  342. const std::vector<VideoCodec>& supported_codecs,
  343. const VideoCodec& codec) {
  344. for (const VideoCodec& supported_codec : supported_codecs) {
  345. if (IsSameCodec(codec.name, codec.params, supported_codec.name,
  346. supported_codec.params)) {
  347. return &supported_codec;
  348. }
  349. }
  350. return nullptr;
  351. }
  352. bool IsSameCodec(const std::string& name1,
  353. const CodecParameterMap& params1,
  354. const std::string& name2,
  355. const CodecParameterMap& params2) {
  356. // Two codecs are considered the same if the name matches (case insensitive)
  357. // and certain codec-specific parameters match.
  358. return absl::EqualsIgnoreCase(name1, name2) &&
  359. IsSameCodecSpecific(name1, params1, name2, params2);
  360. }
  361. } // namespace cricket