fake_encoder.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 TEST_FAKE_ENCODER_H_
  11. #define TEST_FAKE_ENCODER_H_
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. #include <memory>
  15. #include <vector>
  16. #include "api/fec_controller_override.h"
  17. #include "api/task_queue/task_queue_factory.h"
  18. #include "api/video/encoded_image.h"
  19. #include "api/video/video_bitrate_allocation.h"
  20. #include "api/video/video_frame.h"
  21. #include "api/video_codecs/video_codec.h"
  22. #include "api/video_codecs/video_encoder.h"
  23. #include "modules/include/module_common_types.h"
  24. #include "modules/video_coding/include/video_codec_interface.h"
  25. #include "rtc_base/critical_section.h"
  26. #include "rtc_base/synchronization/sequence_checker.h"
  27. #include "rtc_base/thread_annotations.h"
  28. #include "system_wrappers/include/clock.h"
  29. namespace webrtc {
  30. namespace test {
  31. class FakeEncoder : public VideoEncoder {
  32. public:
  33. explicit FakeEncoder(Clock* clock);
  34. virtual ~FakeEncoder() = default;
  35. // Sets max bitrate. Not thread-safe, call before registering the encoder.
  36. void SetMaxBitrate(int max_kbps) RTC_LOCKS_EXCLUDED(crit_sect_);
  37. void SetQp(int qp) RTC_LOCKS_EXCLUDED(crit_sect_);
  38. void SetFecControllerOverride(
  39. FecControllerOverride* fec_controller_override) override;
  40. int32_t InitEncode(const VideoCodec* config, const Settings& settings)
  41. RTC_LOCKS_EXCLUDED(crit_sect_) override;
  42. int32_t Encode(const VideoFrame& input_image,
  43. const std::vector<VideoFrameType>* frame_types)
  44. RTC_LOCKS_EXCLUDED(crit_sect_) override;
  45. int32_t RegisterEncodeCompleteCallback(EncodedImageCallback* callback)
  46. RTC_LOCKS_EXCLUDED(crit_sect_) override;
  47. int32_t Release() override;
  48. void SetRates(const RateControlParameters& parameters)
  49. RTC_LOCKS_EXCLUDED(crit_sect_) override;
  50. int GetConfiguredInputFramerate() const RTC_LOCKS_EXCLUDED(crit_sect_);
  51. EncoderInfo GetEncoderInfo() const override;
  52. static const char* kImplementationName;
  53. protected:
  54. struct FrameInfo {
  55. bool keyframe;
  56. struct SpatialLayer {
  57. SpatialLayer() = default;
  58. SpatialLayer(int size, int temporal_id)
  59. : size(size), temporal_id(temporal_id) {}
  60. // Size of a current frame in the layer.
  61. int size = 0;
  62. // Temporal index of a current frame in the layer.
  63. int temporal_id = 0;
  64. };
  65. std::vector<SpatialLayer> layers;
  66. };
  67. FrameInfo NextFrame(const std::vector<VideoFrameType>* frame_types,
  68. bool keyframe,
  69. uint8_t num_simulcast_streams,
  70. const VideoBitrateAllocation& target_bitrate,
  71. SimulcastStream simulcast_streams[kMaxSimulcastStreams],
  72. int framerate) RTC_LOCKS_EXCLUDED(crit_sect_);
  73. // Called before the frame is passed to callback_->OnEncodedImage, to let
  74. // subclasses fill out codec_specific, possibly modify encodedImage.
  75. // Returns an RTPFragmentationHeader, if needed by the codec.
  76. virtual std::unique_ptr<RTPFragmentationHeader> EncodeHook(
  77. EncodedImage* encoded_image,
  78. CodecSpecificInfo* codec_specific);
  79. void SetRatesLocked(const RateControlParameters& parameters)
  80. RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
  81. FrameInfo last_frame_info_ RTC_GUARDED_BY(crit_sect_);
  82. Clock* const clock_;
  83. VideoCodec config_ RTC_GUARDED_BY(crit_sect_);
  84. EncodedImageCallback* callback_ RTC_GUARDED_BY(crit_sect_);
  85. RateControlParameters current_rate_settings_ RTC_GUARDED_BY(crit_sect_);
  86. int max_target_bitrate_kbps_ RTC_GUARDED_BY(crit_sect_);
  87. bool pending_keyframe_ RTC_GUARDED_BY(crit_sect_);
  88. uint32_t counter_ RTC_GUARDED_BY(crit_sect_);
  89. rtc::CriticalSection crit_sect_;
  90. bool used_layers_[kMaxSimulcastStreams];
  91. absl::optional<int> qp_ RTC_GUARDED_BY(crit_sect_);
  92. // Current byte debt to be payed over a number of frames.
  93. // The debt is acquired by keyframes overshooting the bitrate target.
  94. size_t debt_bytes_;
  95. };
  96. class FakeH264Encoder : public FakeEncoder {
  97. public:
  98. explicit FakeH264Encoder(Clock* clock);
  99. virtual ~FakeH264Encoder() = default;
  100. private:
  101. std::unique_ptr<RTPFragmentationHeader> EncodeHook(
  102. EncodedImage* encoded_image,
  103. CodecSpecificInfo* codec_specific) override;
  104. int idr_counter_ RTC_GUARDED_BY(local_crit_sect_);
  105. rtc::CriticalSection local_crit_sect_;
  106. };
  107. class DelayedEncoder : public test::FakeEncoder {
  108. public:
  109. DelayedEncoder(Clock* clock, int delay_ms);
  110. virtual ~DelayedEncoder() = default;
  111. void SetDelay(int delay_ms);
  112. int32_t Encode(const VideoFrame& input_image,
  113. const std::vector<VideoFrameType>* frame_types) override;
  114. private:
  115. int delay_ms_ RTC_GUARDED_BY(sequence_checker_);
  116. SequenceChecker sequence_checker_;
  117. };
  118. // This class implements a multi-threaded fake encoder by posting
  119. // FakeH264Encoder::Encode(.) tasks to |queue1_| and |queue2_|, in an
  120. // alternating fashion. The class itself does not need to be thread safe,
  121. // as it is called from the task queue in VideoStreamEncoder.
  122. class MultithreadedFakeH264Encoder : public test::FakeH264Encoder {
  123. public:
  124. MultithreadedFakeH264Encoder(Clock* clock,
  125. TaskQueueFactory* task_queue_factory);
  126. virtual ~MultithreadedFakeH264Encoder() = default;
  127. int32_t InitEncode(const VideoCodec* config,
  128. const Settings& settings) override;
  129. int32_t Encode(const VideoFrame& input_image,
  130. const std::vector<VideoFrameType>* frame_types) override;
  131. int32_t EncodeCallback(const VideoFrame& input_image,
  132. const std::vector<VideoFrameType>* frame_types);
  133. int32_t Release() override;
  134. protected:
  135. class EncodeTask;
  136. TaskQueueFactory* const task_queue_factory_;
  137. int current_queue_ RTC_GUARDED_BY(sequence_checker_);
  138. std::unique_ptr<TaskQueueBase, TaskQueueDeleter> queue1_
  139. RTC_GUARDED_BY(sequence_checker_);
  140. std::unique_ptr<TaskQueueBase, TaskQueueDeleter> queue2_
  141. RTC_GUARDED_BY(sequence_checker_);
  142. SequenceChecker sequence_checker_;
  143. };
  144. } // namespace test
  145. } // namespace webrtc
  146. #endif // TEST_FAKE_ENCODER_H_