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