stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef TEST_FAKE_ENCODER_H_ |
| 12 | #define TEST_FAKE_ENCODER_H_ |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 13 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 16 | |
brandtr | 49ce67c | 2017-02-11 00:25:18 -0800 | [diff] [blame] | 17 | #include <memory> |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 18 | #include <vector> |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 19 | |
Elad Alon | 8f01c4e | 2019-06-28 15:19:43 +0200 | [diff] [blame] | 20 | #include "api/fec_controller_override.h" |
Danil Chapovalov | 22ed366 | 2019-03-19 19:39:49 +0100 | [diff] [blame] | 21 | #include "api/task_queue/task_queue_factory.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 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" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | #include "api/video_codecs/video_encoder.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 27 | #include "modules/include/module_common_types.h" |
| 28 | #include "modules/video_coding/include/video_codec_interface.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 29 | #include "rtc_base/critical_section.h" |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 30 | #include "rtc_base/synchronization/sequence_checker.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 31 | #include "rtc_base/thread_annotations.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 32 | #include "system_wrappers/include/clock.h" |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 33 | |
| 34 | namespace webrtc { |
pbos@webrtc.org | cb5118c | 2013-09-03 09:10:37 +0000 | [diff] [blame] | 35 | namespace test { |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 36 | |
| 37 | class FakeEncoder : public VideoEncoder { |
| 38 | public: |
| 39 | explicit FakeEncoder(Clock* clock); |
brandtr | 49ce67c | 2017-02-11 00:25:18 -0800 | [diff] [blame] | 40 | virtual ~FakeEncoder() = default; |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 41 | |
pbos@webrtc.org | 3349ae0 | 2014-03-13 12:52:27 +0000 | [diff] [blame] | 42 | // Sets max bitrate. Not thread-safe, call before registering the encoder. |
| 43 | void SetMaxBitrate(int max_kbps); |
pbos@webrtc.org | cb5118c | 2013-09-03 09:10:37 +0000 | [diff] [blame] | 44 | |
Elad Alon | 8f01c4e | 2019-06-28 15:19:43 +0200 | [diff] [blame] | 45 | void SetFecControllerOverride( |
| 46 | FecControllerOverride* fec_controller_override) override; |
| 47 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 48 | int32_t InitEncode(const VideoCodec* config, |
Elad Alon | 370f93a | 2019-06-11 14:57:57 +0200 | [diff] [blame] | 49 | const Settings& settings) override; |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 50 | int32_t Encode(const VideoFrame& input_image, |
Niels Möller | 87e2d78 | 2019-03-07 10:18:23 +0100 | [diff] [blame] | 51 | const std::vector<VideoFrameType>* frame_types) override; |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 52 | int32_t RegisterEncodeCompleteCallback( |
| 53 | EncodedImageCallback* callback) override; |
| 54 | int32_t Release() override; |
Erik Språng | 16cb8f5 | 2019-04-12 13:59:09 +0200 | [diff] [blame] | 55 | void SetRates(const RateControlParameters& parameters) override; |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 56 | int GetConfiguredInputFramerate() const; |
Erik Språng | df351f4 | 2018-11-06 11:33:15 +0100 | [diff] [blame] | 57 | EncoderInfo GetEncoderInfo() const override; |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 58 | |
| 59 | static const char* kImplementationName; |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 60 | |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 61 | protected: |
Ilya Nikolaevskiy | b0588e6 | 2018-08-27 14:12:27 +0200 | [diff] [blame] | 62 | struct FrameInfo { |
| 63 | bool keyframe; |
| 64 | struct SpatialLayer { |
| 65 | SpatialLayer() = default; |
| 66 | SpatialLayer(int size, int temporal_id) |
| 67 | : size(size), temporal_id(temporal_id) {} |
| 68 | // Size of a current frame in the layer. |
| 69 | int size = 0; |
| 70 | // Temporal index of a current frame in the layer. |
| 71 | int temporal_id = 0; |
| 72 | }; |
| 73 | std::vector<SpatialLayer> layers; |
| 74 | }; |
| 75 | |
Niels Möller | 87e2d78 | 2019-03-07 10:18:23 +0100 | [diff] [blame] | 76 | FrameInfo NextFrame(const std::vector<VideoFrameType>* frame_types, |
Ilya Nikolaevskiy | b0588e6 | 2018-08-27 14:12:27 +0200 | [diff] [blame] | 77 | bool keyframe, |
| 78 | uint8_t num_simulcast_streams, |
| 79 | const VideoBitrateAllocation& target_bitrate, |
| 80 | SimulcastStream simulcast_streams[kMaxSimulcastStreams], |
| 81 | int framerate); |
| 82 | |
Niels Möller | d738071 | 2019-03-06 10:09:47 +0100 | [diff] [blame] | 83 | // Called before the frame is passed to callback_->OnEncodedImage, to let |
| 84 | // subclasses fill out codec_specific, possibly modify encodedImage. |
| 85 | // Returns an RTPFragmentationHeader, if needed by the codec. |
| 86 | virtual std::unique_ptr<RTPFragmentationHeader> EncodeHook( |
| 87 | EncodedImage* encoded_image, |
| 88 | CodecSpecificInfo* codec_specific); |
| 89 | |
Yves Gerey | 0414040 | 2018-10-08 11:39:18 +0200 | [diff] [blame] | 90 | FrameInfo last_frame_info_ RTC_GUARDED_BY(crit_sect_); |
pbos@webrtc.org | de1429e | 2014-04-28 13:00:21 +0000 | [diff] [blame] | 91 | Clock* const clock_; |
Ilya Nikolaevskiy | b0588e6 | 2018-08-27 14:12:27 +0200 | [diff] [blame] | 92 | |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 93 | VideoCodec config_ RTC_GUARDED_BY(crit_sect_); |
| 94 | EncodedImageCallback* callback_ RTC_GUARDED_BY(crit_sect_); |
Erik Språng | 16cb8f5 | 2019-04-12 13:59:09 +0200 | [diff] [blame] | 95 | RateControlParameters current_rate_settings_ RTC_GUARDED_BY(crit_sect_); |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 96 | int max_target_bitrate_kbps_ RTC_GUARDED_BY(crit_sect_); |
| 97 | bool pending_keyframe_ RTC_GUARDED_BY(crit_sect_); |
Per Kjellander | 17fc7e2 | 2018-11-06 11:01:06 +0100 | [diff] [blame] | 98 | uint32_t counter_ RTC_GUARDED_BY(crit_sect_); |
brandtr | 49ce67c | 2017-02-11 00:25:18 -0800 | [diff] [blame] | 99 | rtc::CriticalSection crit_sect_; |
Ilya Nikolaevskiy | b0588e6 | 2018-08-27 14:12:27 +0200 | [diff] [blame] | 100 | bool used_layers_[kMaxSimulcastStreams]; |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 101 | |
| 102 | // Current byte debt to be payed over a number of frames. |
| 103 | // The debt is acquired by keyframes overshooting the bitrate target. |
| 104 | size_t debt_bytes_; |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 105 | }; |
stefan@webrtc.org | 79c3359 | 2014-08-06 09:24:53 +0000 | [diff] [blame] | 106 | |
Niels Möller | d738071 | 2019-03-06 10:09:47 +0100 | [diff] [blame] | 107 | class FakeH264Encoder : public FakeEncoder { |
stefan@webrtc.org | 79c3359 | 2014-08-06 09:24:53 +0000 | [diff] [blame] | 108 | public: |
| 109 | explicit FakeH264Encoder(Clock* clock); |
brandtr | 49ce67c | 2017-02-11 00:25:18 -0800 | [diff] [blame] | 110 | virtual ~FakeH264Encoder() = default; |
stefan@webrtc.org | 79c3359 | 2014-08-06 09:24:53 +0000 | [diff] [blame] | 111 | |
stefan@webrtc.org | 79c3359 | 2014-08-06 09:24:53 +0000 | [diff] [blame] | 112 | private: |
Niels Möller | d738071 | 2019-03-06 10:09:47 +0100 | [diff] [blame] | 113 | std::unique_ptr<RTPFragmentationHeader> EncodeHook( |
| 114 | EncodedImage* encoded_image, |
| 115 | CodecSpecificInfo* codec_specific) override; |
| 116 | |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 117 | int idr_counter_ RTC_GUARDED_BY(local_crit_sect_); |
brandtr | 49ce67c | 2017-02-11 00:25:18 -0800 | [diff] [blame] | 118 | rtc::CriticalSection local_crit_sect_; |
stefan@webrtc.org | 79c3359 | 2014-08-06 09:24:53 +0000 | [diff] [blame] | 119 | }; |
asapersson@webrtc.org | 049e4ec | 2014-11-20 10:19:46 +0000 | [diff] [blame] | 120 | |
| 121 | class DelayedEncoder : public test::FakeEncoder { |
| 122 | public: |
| 123 | DelayedEncoder(Clock* clock, int delay_ms); |
brandtr | 49ce67c | 2017-02-11 00:25:18 -0800 | [diff] [blame] | 124 | virtual ~DelayedEncoder() = default; |
asapersson@webrtc.org | 049e4ec | 2014-11-20 10:19:46 +0000 | [diff] [blame] | 125 | |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 126 | void SetDelay(int delay_ms); |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 127 | int32_t Encode(const VideoFrame& input_image, |
Niels Möller | 87e2d78 | 2019-03-07 10:18:23 +0100 | [diff] [blame] | 128 | const std::vector<VideoFrameType>* frame_types) override; |
asapersson@webrtc.org | 049e4ec | 2014-11-20 10:19:46 +0000 | [diff] [blame] | 129 | |
| 130 | private: |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 131 | int delay_ms_ RTC_GUARDED_BY(sequence_checker_); |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 132 | SequenceChecker sequence_checker_; |
asapersson@webrtc.org | 049e4ec | 2014-11-20 10:19:46 +0000 | [diff] [blame] | 133 | }; |
brandtr | 696c9c6 | 2016-12-19 05:47:28 -0800 | [diff] [blame] | 134 | |
| 135 | // This class implements a multi-threaded fake encoder by posting |
| 136 | // FakeH264Encoder::Encode(.) tasks to |queue1_| and |queue2_|, in an |
brandtr | 49ce67c | 2017-02-11 00:25:18 -0800 | [diff] [blame] | 137 | // alternating fashion. The class itself does not need to be thread safe, |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 138 | // as it is called from the task queue in VideoStreamEncoder. |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 139 | class MultithreadedFakeH264Encoder : public test::FakeH264Encoder { |
brandtr | 696c9c6 | 2016-12-19 05:47:28 -0800 | [diff] [blame] | 140 | public: |
Danil Chapovalov | 22ed366 | 2019-03-19 19:39:49 +0100 | [diff] [blame] | 141 | MultithreadedFakeH264Encoder(Clock* clock, |
| 142 | TaskQueueFactory* task_queue_factory); |
brandtr | 49ce67c | 2017-02-11 00:25:18 -0800 | [diff] [blame] | 143 | virtual ~MultithreadedFakeH264Encoder() = default; |
| 144 | |
| 145 | int32_t InitEncode(const VideoCodec* config, |
Elad Alon | 370f93a | 2019-06-11 14:57:57 +0200 | [diff] [blame] | 146 | const Settings& settings) override; |
brandtr | 696c9c6 | 2016-12-19 05:47:28 -0800 | [diff] [blame] | 147 | |
| 148 | int32_t Encode(const VideoFrame& input_image, |
Niels Möller | 87e2d78 | 2019-03-07 10:18:23 +0100 | [diff] [blame] | 149 | const std::vector<VideoFrameType>* frame_types) override; |
brandtr | 696c9c6 | 2016-12-19 05:47:28 -0800 | [diff] [blame] | 150 | |
| 151 | int32_t EncodeCallback(const VideoFrame& input_image, |
Niels Möller | 87e2d78 | 2019-03-07 10:18:23 +0100 | [diff] [blame] | 152 | const std::vector<VideoFrameType>* frame_types); |
brandtr | 696c9c6 | 2016-12-19 05:47:28 -0800 | [diff] [blame] | 153 | |
brandtr | 49ce67c | 2017-02-11 00:25:18 -0800 | [diff] [blame] | 154 | int32_t Release() override; |
| 155 | |
brandtr | 696c9c6 | 2016-12-19 05:47:28 -0800 | [diff] [blame] | 156 | protected: |
| 157 | class EncodeTask; |
| 158 | |
Danil Chapovalov | 22ed366 | 2019-03-19 19:39:49 +0100 | [diff] [blame] | 159 | TaskQueueFactory* const task_queue_factory_; |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 160 | int current_queue_ RTC_GUARDED_BY(sequence_checker_); |
Danil Chapovalov | 22ed366 | 2019-03-19 19:39:49 +0100 | [diff] [blame] | 161 | std::unique_ptr<TaskQueueBase, TaskQueueDeleter> queue1_ |
| 162 | RTC_GUARDED_BY(sequence_checker_); |
| 163 | std::unique_ptr<TaskQueueBase, TaskQueueDeleter> queue2_ |
| 164 | RTC_GUARDED_BY(sequence_checker_); |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 165 | SequenceChecker sequence_checker_; |
brandtr | 696c9c6 | 2016-12-19 05:47:28 -0800 | [diff] [blame] | 166 | }; |
| 167 | |
pbos@webrtc.org | cb5118c | 2013-09-03 09:10:37 +0000 | [diff] [blame] | 168 | } // namespace test |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 +0000 | [diff] [blame] | 169 | } // namespace webrtc |
| 170 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 171 | #endif // TEST_FAKE_ENCODER_H_ |