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