fake_encoder.h 6.2 KB

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