codec.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. #ifndef MEDIA_BASE_CODEC_H_
  11. #define MEDIA_BASE_CODEC_H_
  12. #include <map>
  13. #include <set>
  14. #include <string>
  15. #include <vector>
  16. #include "absl/types/optional.h"
  17. #include "api/rtp_parameters.h"
  18. #include "api/video_codecs/sdp_video_format.h"
  19. #include "media/base/media_constants.h"
  20. #include "rtc_base/system/rtc_export.h"
  21. namespace cricket {
  22. typedef std::map<std::string, std::string> CodecParameterMap;
  23. class FeedbackParam {
  24. public:
  25. FeedbackParam() = default;
  26. FeedbackParam(const std::string& id, const std::string& param)
  27. : id_(id), param_(param) {}
  28. explicit FeedbackParam(const std::string& id)
  29. : id_(id), param_(kParamValueEmpty) {}
  30. bool operator==(const FeedbackParam& other) const;
  31. const std::string& id() const { return id_; }
  32. const std::string& param() const { return param_; }
  33. private:
  34. std::string id_; // e.g. "nack", "ccm"
  35. std::string param_; // e.g. "", "rpsi", "fir"
  36. };
  37. class FeedbackParams {
  38. public:
  39. FeedbackParams();
  40. ~FeedbackParams();
  41. bool operator==(const FeedbackParams& other) const;
  42. bool Has(const FeedbackParam& param) const;
  43. void Add(const FeedbackParam& param);
  44. void Intersect(const FeedbackParams& from);
  45. const std::vector<FeedbackParam>& params() const { return params_; }
  46. private:
  47. bool HasDuplicateEntries() const;
  48. std::vector<FeedbackParam> params_;
  49. };
  50. struct RTC_EXPORT Codec {
  51. int id;
  52. std::string name;
  53. int clockrate;
  54. // Non key-value parameters such as the telephone-event "0‐15" are
  55. // represented using an empty string as key, i.e. {"": "0-15"}.
  56. CodecParameterMap params;
  57. FeedbackParams feedback_params;
  58. virtual ~Codec();
  59. // Indicates if this codec is compatible with the specified codec.
  60. bool Matches(const Codec& codec) const;
  61. bool MatchesCapability(const webrtc::RtpCodecCapability& capability) const;
  62. // Find the parameter for |name| and write the value to |out|.
  63. bool GetParam(const std::string& name, std::string* out) const;
  64. bool GetParam(const std::string& name, int* out) const;
  65. void SetParam(const std::string& name, const std::string& value);
  66. void SetParam(const std::string& name, int value);
  67. // It is safe to input a non-existent parameter.
  68. // Returns true if the parameter existed, false if it did not exist.
  69. bool RemoveParam(const std::string& name);
  70. bool HasFeedbackParam(const FeedbackParam& param) const;
  71. void AddFeedbackParam(const FeedbackParam& param);
  72. // Filter |this| feedbacks params such that only those shared by both |this|
  73. // and |other| are kept.
  74. void IntersectFeedbackParams(const Codec& other);
  75. virtual webrtc::RtpCodecParameters ToCodecParameters() const;
  76. Codec& operator=(const Codec& c);
  77. Codec& operator=(Codec&& c);
  78. bool operator==(const Codec& c) const;
  79. bool operator!=(const Codec& c) const { return !(*this == c); }
  80. protected:
  81. // A Codec can't be created without a subclass.
  82. // Creates a codec with the given parameters.
  83. Codec(int id, const std::string& name, int clockrate);
  84. // Creates an empty codec.
  85. Codec();
  86. Codec(const Codec& c);
  87. Codec(Codec&& c);
  88. };
  89. struct AudioCodec : public Codec {
  90. int bitrate;
  91. size_t channels;
  92. // Creates a codec with the given parameters.
  93. AudioCodec(int id,
  94. const std::string& name,
  95. int clockrate,
  96. int bitrate,
  97. size_t channels);
  98. // Creates an empty codec.
  99. AudioCodec();
  100. AudioCodec(const AudioCodec& c);
  101. AudioCodec(AudioCodec&& c);
  102. ~AudioCodec() override = default;
  103. // Indicates if this codec is compatible with the specified codec.
  104. bool Matches(const AudioCodec& codec) const;
  105. std::string ToString() const;
  106. webrtc::RtpCodecParameters ToCodecParameters() const override;
  107. AudioCodec& operator=(const AudioCodec& c);
  108. AudioCodec& operator=(AudioCodec&& c);
  109. bool operator==(const AudioCodec& c) const;
  110. bool operator!=(const AudioCodec& c) const { return !(*this == c); }
  111. };
  112. struct RTC_EXPORT VideoCodec : public Codec {
  113. absl::optional<std::string> packetization;
  114. // Creates a codec with the given parameters.
  115. VideoCodec(int id, const std::string& name);
  116. // Creates a codec with the given name and empty id.
  117. explicit VideoCodec(const std::string& name);
  118. // Creates an empty codec.
  119. VideoCodec();
  120. VideoCodec(const VideoCodec& c);
  121. explicit VideoCodec(const webrtc::SdpVideoFormat& c);
  122. VideoCodec(VideoCodec&& c);
  123. ~VideoCodec() override = default;
  124. // Indicates if this video codec is the same as the other video codec, e.g. if
  125. // they are both VP8 or VP9, or if they are both H264 with the same H264
  126. // profile. H264 levels however are not compared.
  127. bool Matches(const VideoCodec& codec) const;
  128. std::string ToString() const;
  129. webrtc::RtpCodecParameters ToCodecParameters() const override;
  130. VideoCodec& operator=(const VideoCodec& c);
  131. VideoCodec& operator=(VideoCodec&& c);
  132. bool operator==(const VideoCodec& c) const;
  133. bool operator!=(const VideoCodec& c) const { return !(*this == c); }
  134. // Return packetization which both |local_codec| and |remote_codec| support.
  135. static absl::optional<std::string> IntersectPacketization(
  136. const VideoCodec& local_codec,
  137. const VideoCodec& remote_codec);
  138. static VideoCodec CreateRtxCodec(int rtx_payload_type,
  139. int associated_payload_type);
  140. enum CodecType {
  141. CODEC_VIDEO,
  142. CODEC_RED,
  143. CODEC_ULPFEC,
  144. CODEC_FLEXFEC,
  145. CODEC_RTX,
  146. };
  147. CodecType GetCodecType() const;
  148. // Validates a VideoCodec's payload type, dimensions and bitrates etc. If they
  149. // don't make sense (such as max < min bitrate), and error is logged and
  150. // ValidateCodecFormat returns false.
  151. bool ValidateCodecFormat() const;
  152. private:
  153. void SetDefaultParameters();
  154. };
  155. struct RtpDataCodec : public Codec {
  156. RtpDataCodec(int id, const std::string& name);
  157. RtpDataCodec();
  158. RtpDataCodec(const RtpDataCodec& c);
  159. RtpDataCodec(RtpDataCodec&& c);
  160. ~RtpDataCodec() override = default;
  161. RtpDataCodec& operator=(const RtpDataCodec& c);
  162. RtpDataCodec& operator=(RtpDataCodec&& c);
  163. std::string ToString() const;
  164. };
  165. // For backwards compatibility
  166. // TODO(bugs.webrtc.org/10597): Remove when no longer needed.
  167. typedef RtpDataCodec DataCodec;
  168. // Get the codec setting associated with |payload_type|. If there
  169. // is no codec associated with that payload type it returns nullptr.
  170. template <class Codec>
  171. const Codec* FindCodecById(const std::vector<Codec>& codecs, int payload_type) {
  172. for (const auto& codec : codecs) {
  173. if (codec.id == payload_type)
  174. return &codec;
  175. }
  176. return nullptr;
  177. }
  178. bool HasLntf(const Codec& codec);
  179. bool HasNack(const Codec& codec);
  180. bool HasRemb(const Codec& codec);
  181. bool HasRrtr(const Codec& codec);
  182. bool HasTransportCc(const Codec& codec);
  183. // Returns the first codec in |supported_codecs| that matches |codec|, or
  184. // nullptr if no codec matches.
  185. const VideoCodec* FindMatchingCodec(
  186. const std::vector<VideoCodec>& supported_codecs,
  187. const VideoCodec& codec);
  188. RTC_EXPORT bool IsSameCodec(const std::string& name1,
  189. const CodecParameterMap& params1,
  190. const std::string& name2,
  191. const CodecParameterMap& params2);
  192. RTC_EXPORT void AddH264ConstrainedBaselineProfileToSupportedFormats(
  193. std::vector<webrtc::SdpVideoFormat>* supported_formats);
  194. } // namespace cricket
  195. #endif // MEDIA_BASE_CODEC_H_