codec.h 7.5 KB

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