stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 | [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 04:47:31 | [diff] [blame^] | 11 | #ifndef TEST_FAKE_ENCODER_H_ |
| 12 | #define TEST_FAKE_ENCODER_H_ |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 | [diff] [blame] | 13 | |
| 14 | #include <vector> |
brandtr | 49ce67c | 2017-02-11 08:25:18 | [diff] [blame] | 15 | #include <memory> |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 | [diff] [blame] | 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame^] | 17 | #include "api/video_codecs/video_encoder.h" |
| 18 | #include "common_types.h" |
| 19 | #include "rtc_base/criticalsection.h" |
| 20 | #include "rtc_base/sequenced_task_checker.h" |
| 21 | #include "rtc_base/task_queue.h" |
| 22 | #include "system_wrappers/include/clock.h" |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
pbos@webrtc.org | cb5118c | 2013-09-03 09:10:37 | [diff] [blame] | 25 | namespace test { |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 | [diff] [blame] | 26 | |
| 27 | class FakeEncoder : public VideoEncoder { |
| 28 | public: |
| 29 | explicit FakeEncoder(Clock* clock); |
brandtr | 49ce67c | 2017-02-11 08:25:18 | [diff] [blame] | 30 | virtual ~FakeEncoder() = default; |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 | [diff] [blame] | 31 | |
pbos@webrtc.org | 3349ae0 | 2014-03-13 12:52:27 | [diff] [blame] | 32 | // Sets max bitrate. Not thread-safe, call before registering the encoder. |
| 33 | void SetMaxBitrate(int max_kbps); |
pbos@webrtc.org | cb5118c | 2013-09-03 09:10:37 | [diff] [blame] | 34 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 | [diff] [blame] | 35 | int32_t InitEncode(const VideoCodec* config, |
| 36 | int32_t number_of_cores, |
| 37 | size_t max_payload_size) override; |
Miguel Casas-Sanchez | 4765070 | 2015-05-30 00:21:40 | [diff] [blame] | 38 | int32_t Encode(const VideoFrame& input_image, |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 | [diff] [blame] | 39 | const CodecSpecificInfo* codec_specific_info, |
pbos | 22993e1 | 2015-10-19 09:39:06 | [diff] [blame] | 40 | const std::vector<FrameType>* frame_types) override; |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 | [diff] [blame] | 41 | int32_t RegisterEncodeCompleteCallback( |
| 42 | EncodedImageCallback* callback) override; |
| 43 | int32_t Release() override; |
| 44 | int32_t SetChannelParameters(uint32_t packet_loss, int64_t rtt) override; |
Erik Språng | 08127a9 | 2016-11-16 15:41:30 | [diff] [blame] | 45 | int32_t SetRateAllocation(const BitrateAllocation& rate_allocation, |
| 46 | uint32_t framerate) override; |
Peter Boström | b7d9a97 | 2015-12-18 15:01:11 | [diff] [blame] | 47 | const char* ImplementationName() const override; |
sprang | 4847ae6 | 2017-06-27 14:06:52 | [diff] [blame] | 48 | int GetConfiguredInputFramerate() const; |
Peter Boström | b7d9a97 | 2015-12-18 15:01:11 | [diff] [blame] | 49 | |
| 50 | static const char* kImplementationName; |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 | [diff] [blame] | 51 | |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 | [diff] [blame] | 52 | protected: |
pbos@webrtc.org | de1429e | 2014-04-28 13:00:21 | [diff] [blame] | 53 | Clock* const clock_; |
danilchap | a37de39 | 2017-09-09 11:17:22 | [diff] [blame] | 54 | VideoCodec config_ RTC_GUARDED_BY(crit_sect_); |
| 55 | EncodedImageCallback* callback_ RTC_GUARDED_BY(crit_sect_); |
| 56 | BitrateAllocation target_bitrate_ RTC_GUARDED_BY(crit_sect_); |
| 57 | int configured_input_framerate_ RTC_GUARDED_BY(crit_sect_); |
| 58 | int max_target_bitrate_kbps_ RTC_GUARDED_BY(crit_sect_); |
| 59 | bool pending_keyframe_ RTC_GUARDED_BY(crit_sect_); |
brandtr | 49ce67c | 2017-02-11 08:25:18 | [diff] [blame] | 60 | rtc::CriticalSection crit_sect_; |
| 61 | |
pbos@webrtc.org | c095f51 | 2013-08-22 12:34:58 | [diff] [blame] | 62 | uint8_t encoded_buffer_[100000]; |
sprang | 4847ae6 | 2017-06-27 14:06:52 | [diff] [blame] | 63 | |
| 64 | // Current byte debt to be payed over a number of frames. |
| 65 | // The debt is acquired by keyframes overshooting the bitrate target. |
| 66 | size_t debt_bytes_; |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 | [diff] [blame] | 67 | }; |
stefan@webrtc.org | 79c3359 | 2014-08-06 09:24:53 | [diff] [blame] | 68 | |
| 69 | class FakeH264Encoder : public FakeEncoder, public EncodedImageCallback { |
| 70 | public: |
| 71 | explicit FakeH264Encoder(Clock* clock); |
brandtr | 49ce67c | 2017-02-11 08:25:18 | [diff] [blame] | 72 | virtual ~FakeH264Encoder() = default; |
stefan@webrtc.org | 79c3359 | 2014-08-06 09:24:53 | [diff] [blame] | 73 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 | [diff] [blame] | 74 | int32_t RegisterEncodeCompleteCallback( |
| 75 | EncodedImageCallback* callback) override; |
stefan@webrtc.org | 79c3359 | 2014-08-06 09:24:53 | [diff] [blame] | 76 | |
Sergey Ulanov | 525df3f | 2016-08-03 00:46:41 | [diff] [blame] | 77 | Result OnEncodedImage(const EncodedImage& encodedImage, |
| 78 | const CodecSpecificInfo* codecSpecificInfo, |
| 79 | const RTPFragmentationHeader* fragments) override; |
stefan@webrtc.org | 79c3359 | 2014-08-06 09:24:53 | [diff] [blame] | 80 | |
| 81 | private: |
danilchap | a37de39 | 2017-09-09 11:17:22 | [diff] [blame] | 82 | EncodedImageCallback* callback_ RTC_GUARDED_BY(local_crit_sect_); |
| 83 | int idr_counter_ RTC_GUARDED_BY(local_crit_sect_); |
brandtr | 49ce67c | 2017-02-11 08:25:18 | [diff] [blame] | 84 | rtc::CriticalSection local_crit_sect_; |
stefan@webrtc.org | 79c3359 | 2014-08-06 09:24:53 | [diff] [blame] | 85 | }; |
asapersson@webrtc.org | 049e4ec | 2014-11-20 10:19:46 | [diff] [blame] | 86 | |
| 87 | class DelayedEncoder : public test::FakeEncoder { |
| 88 | public: |
| 89 | DelayedEncoder(Clock* clock, int delay_ms); |
brandtr | 49ce67c | 2017-02-11 08:25:18 | [diff] [blame] | 90 | virtual ~DelayedEncoder() = default; |
asapersson@webrtc.org | 049e4ec | 2014-11-20 10:19:46 | [diff] [blame] | 91 | |
perkj | 803d97f | 2016-11-01 18:45:46 | [diff] [blame] | 92 | void SetDelay(int delay_ms); |
Miguel Casas-Sanchez | 4765070 | 2015-05-30 00:21:40 | [diff] [blame] | 93 | int32_t Encode(const VideoFrame& input_image, |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 | [diff] [blame] | 94 | const CodecSpecificInfo* codec_specific_info, |
pbos | 22993e1 | 2015-10-19 09:39:06 | [diff] [blame] | 95 | const std::vector<FrameType>* frame_types) override; |
asapersson@webrtc.org | 049e4ec | 2014-11-20 10:19:46 | [diff] [blame] | 96 | |
| 97 | private: |
danilchap | a37de39 | 2017-09-09 11:17:22 | [diff] [blame] | 98 | int delay_ms_ RTC_ACCESS_ON(sequence_checker_); |
brandtr | 49ce67c | 2017-02-11 08:25:18 | [diff] [blame] | 99 | rtc::SequencedTaskChecker sequence_checker_; |
asapersson@webrtc.org | 049e4ec | 2014-11-20 10:19:46 | [diff] [blame] | 100 | }; |
brandtr | 696c9c6 | 2016-12-19 13:47:28 | [diff] [blame] | 101 | |
| 102 | // This class implements a multi-threaded fake encoder by posting |
| 103 | // FakeH264Encoder::Encode(.) tasks to |queue1_| and |queue2_|, in an |
brandtr | 49ce67c | 2017-02-11 08:25:18 | [diff] [blame] | 104 | // alternating fashion. The class itself does not need to be thread safe, |
mflodman | cc3d442 | 2017-08-03 15:27:51 | [diff] [blame] | 105 | // as it is called from the task queue in VideoStreamEncoder. |
brandtr | e78d266 | 2017-01-16 13:57:16 | [diff] [blame] | 106 | class MultithreadedFakeH264Encoder : public test::FakeH264Encoder { |
brandtr | 696c9c6 | 2016-12-19 13:47:28 | [diff] [blame] | 107 | public: |
brandtr | e78d266 | 2017-01-16 13:57:16 | [diff] [blame] | 108 | explicit MultithreadedFakeH264Encoder(Clock* clock); |
brandtr | 49ce67c | 2017-02-11 08:25:18 | [diff] [blame] | 109 | virtual ~MultithreadedFakeH264Encoder() = default; |
| 110 | |
| 111 | int32_t InitEncode(const VideoCodec* config, |
| 112 | int32_t number_of_cores, |
| 113 | size_t max_payload_size) override; |
brandtr | 696c9c6 | 2016-12-19 13:47:28 | [diff] [blame] | 114 | |
| 115 | int32_t Encode(const VideoFrame& input_image, |
| 116 | const CodecSpecificInfo* codec_specific_info, |
| 117 | const std::vector<FrameType>* frame_types) override; |
| 118 | |
| 119 | int32_t EncodeCallback(const VideoFrame& input_image, |
| 120 | const CodecSpecificInfo* codec_specific_info, |
| 121 | const std::vector<FrameType>* frame_types); |
| 122 | |
brandtr | 49ce67c | 2017-02-11 08:25:18 | [diff] [blame] | 123 | int32_t Release() override; |
| 124 | |
brandtr | 696c9c6 | 2016-12-19 13:47:28 | [diff] [blame] | 125 | protected: |
| 126 | class EncodeTask; |
| 127 | |
danilchap | a37de39 | 2017-09-09 11:17:22 | [diff] [blame] | 128 | int current_queue_ RTC_ACCESS_ON(sequence_checker_); |
| 129 | std::unique_ptr<rtc::TaskQueue> queue1_ RTC_ACCESS_ON(sequence_checker_); |
| 130 | std::unique_ptr<rtc::TaskQueue> queue2_ RTC_ACCESS_ON(sequence_checker_); |
brandtr | 49ce67c | 2017-02-11 08:25:18 | [diff] [blame] | 131 | rtc::SequencedTaskChecker sequence_checker_; |
brandtr | 696c9c6 | 2016-12-19 13:47:28 | [diff] [blame] | 132 | }; |
| 133 | |
pbos@webrtc.org | cb5118c | 2013-09-03 09:10:37 | [diff] [blame] | 134 | } // namespace test |
stefan@webrtc.org | 360e376 | 2013-08-22 09:29:56 | [diff] [blame] | 135 | } // namespace webrtc |
| 136 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame^] | 137 | #endif // TEST_FAKE_ENCODER_H_ |