video_encoder_config.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright (c) 2013 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 API_VIDEO_CODECS_VIDEO_ENCODER_CONFIG_H_
  11. #define API_VIDEO_CODECS_VIDEO_ENCODER_CONFIG_H_
  12. #include <stddef.h>
  13. #include <string>
  14. #include <vector>
  15. #include "absl/types/optional.h"
  16. #include "api/scoped_refptr.h"
  17. #include "api/video_codecs/sdp_video_format.h"
  18. #include "api/video_codecs/video_codec.h"
  19. #include "rtc_base/ref_count.h"
  20. namespace webrtc {
  21. // The |VideoStream| struct describes a simulcast layer, or "stream".
  22. struct VideoStream {
  23. VideoStream();
  24. ~VideoStream();
  25. VideoStream(const VideoStream& other);
  26. std::string ToString() const;
  27. // Width in pixels.
  28. size_t width;
  29. // Height in pixels.
  30. size_t height;
  31. // Frame rate in fps.
  32. int max_framerate;
  33. // Bitrate, in bps, for the stream.
  34. int min_bitrate_bps;
  35. int target_bitrate_bps;
  36. int max_bitrate_bps;
  37. // Scaling factor applied to the stream size.
  38. // |width| and |height| values are already scaled down.
  39. double scale_resolution_down_by;
  40. // Maximum Quantization Parameter to use when encoding the stream.
  41. int max_qp;
  42. // Determines the number of temporal layers that the stream should be
  43. // encoded with. This value should be greater than zero.
  44. // TODO(brandtr): This class is used both for configuring the encoder
  45. // (meaning that this field _must_ be set), and for signaling the app-level
  46. // encoder settings (meaning that the field _may_ be set). We should separate
  47. // this and remove this optional instead.
  48. absl::optional<size_t> num_temporal_layers;
  49. // The priority of this stream, to be used when allocating resources
  50. // between multiple streams.
  51. absl::optional<double> bitrate_priority;
  52. // If this stream is enabled by the user, or not.
  53. bool active;
  54. };
  55. class VideoEncoderConfig {
  56. public:
  57. // These are reference counted to permit copying VideoEncoderConfig and be
  58. // kept alive until all encoder_specific_settings go out of scope.
  59. // TODO(kthelgason): Consider removing the need for copying VideoEncoderConfig
  60. // and use absl::optional for encoder_specific_settings instead.
  61. class EncoderSpecificSettings : public rtc::RefCountInterface {
  62. public:
  63. // TODO(pbos): Remove FillEncoderSpecificSettings as soon as VideoCodec is
  64. // not in use and encoder implementations ask for codec-specific structs
  65. // directly.
  66. void FillEncoderSpecificSettings(VideoCodec* codec_struct) const;
  67. virtual void FillVideoCodecVp8(VideoCodecVP8* vp8_settings) const;
  68. virtual void FillVideoCodecVp9(VideoCodecVP9* vp9_settings) const;
  69. virtual void FillVideoCodecH264(VideoCodecH264* h264_settings) const;
  70. private:
  71. ~EncoderSpecificSettings() override {}
  72. friend class VideoEncoderConfig;
  73. };
  74. class H264EncoderSpecificSettings : public EncoderSpecificSettings {
  75. public:
  76. explicit H264EncoderSpecificSettings(const VideoCodecH264& specifics);
  77. void FillVideoCodecH264(VideoCodecH264* h264_settings) const override;
  78. private:
  79. VideoCodecH264 specifics_;
  80. };
  81. class Vp8EncoderSpecificSettings : public EncoderSpecificSettings {
  82. public:
  83. explicit Vp8EncoderSpecificSettings(const VideoCodecVP8& specifics);
  84. void FillVideoCodecVp8(VideoCodecVP8* vp8_settings) const override;
  85. private:
  86. VideoCodecVP8 specifics_;
  87. };
  88. class Vp9EncoderSpecificSettings : public EncoderSpecificSettings {
  89. public:
  90. explicit Vp9EncoderSpecificSettings(const VideoCodecVP9& specifics);
  91. void FillVideoCodecVp9(VideoCodecVP9* vp9_settings) const override;
  92. private:
  93. VideoCodecVP9 specifics_;
  94. };
  95. enum class ContentType {
  96. kRealtimeVideo,
  97. kScreen,
  98. };
  99. class VideoStreamFactoryInterface : public rtc::RefCountInterface {
  100. public:
  101. // An implementation should return a std::vector<VideoStream> with the
  102. // wanted VideoStream settings for the given video resolution.
  103. // The size of the vector may not be larger than
  104. // |encoder_config.number_of_streams|.
  105. virtual std::vector<VideoStream> CreateEncoderStreams(
  106. int width,
  107. int height,
  108. const VideoEncoderConfig& encoder_config) = 0;
  109. protected:
  110. ~VideoStreamFactoryInterface() override {}
  111. };
  112. VideoEncoderConfig& operator=(VideoEncoderConfig&&) = default;
  113. VideoEncoderConfig& operator=(const VideoEncoderConfig&) = delete;
  114. // Mostly used by tests. Avoid creating copies if you can.
  115. VideoEncoderConfig Copy() const { return VideoEncoderConfig(*this); }
  116. VideoEncoderConfig();
  117. VideoEncoderConfig(VideoEncoderConfig&&);
  118. ~VideoEncoderConfig();
  119. std::string ToString() const;
  120. // TODO(nisse): Consolidate on one of these.
  121. VideoCodecType codec_type;
  122. SdpVideoFormat video_format;
  123. rtc::scoped_refptr<VideoStreamFactoryInterface> video_stream_factory;
  124. std::vector<SpatialLayer> spatial_layers;
  125. ContentType content_type;
  126. rtc::scoped_refptr<const EncoderSpecificSettings> encoder_specific_settings;
  127. // Padding will be used up to this bitrate regardless of the bitrate produced
  128. // by the encoder. Padding above what's actually produced by the encoder helps
  129. // maintaining a higher bitrate estimate. Padding will however not be sent
  130. // unless the estimated bandwidth indicates that the link can handle it.
  131. int min_transmit_bitrate_bps;
  132. int max_bitrate_bps;
  133. // The bitrate priority used for all VideoStreams.
  134. double bitrate_priority;
  135. // The simulcast layer's configurations set by the application for this video
  136. // sender. These are modified by the video_stream_factory before being passed
  137. // down to lower layers for the video encoding.
  138. // |simulcast_layers| is also used for configuring non-simulcast (when there
  139. // is a single VideoStream).
  140. std::vector<VideoStream> simulcast_layers;
  141. // Max number of encoded VideoStreams to produce.
  142. size_t number_of_streams;
  143. // Legacy Google conference mode flag for simulcast screenshare
  144. bool legacy_conference_mode;
  145. private:
  146. // Access to the copy constructor is private to force use of the Copy()
  147. // method for those exceptional cases where we do use it.
  148. VideoEncoderConfig(const VideoEncoderConfig&);
  149. };
  150. } // namespace webrtc
  151. #endif // API_VIDEO_CODECS_VIDEO_ENCODER_CONFIG_H_