You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
5.8 KiB

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