blob: 06c0e0392de83a3a2df4b48782cdcb145c6b2608 [file] [log] [blame]
stefan@webrtc.org360e3762013-08-22 09:29:56 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef TEST_FAKE_ENCODER_H_
12#define TEST_FAKE_ENCODER_H_
stefan@webrtc.org360e3762013-08-22 09:29:56 +000013
14#include <vector>
brandtr49ce67c2017-02-11 00:25:18 -080015#include <memory>
stefan@webrtc.org360e3762013-08-22 09:29:56 +000016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/video_codecs/video_encoder.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020018#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#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.org360e3762013-08-22 09:29:56 +000023
24namespace webrtc {
pbos@webrtc.orgcb5118c2013-09-03 09:10:37 +000025namespace test {
stefan@webrtc.org360e3762013-08-22 09:29:56 +000026
27class FakeEncoder : public VideoEncoder {
28 public:
29 explicit FakeEncoder(Clock* clock);
brandtr49ce67c2017-02-11 00:25:18 -080030 virtual ~FakeEncoder() = default;
stefan@webrtc.org360e3762013-08-22 09:29:56 +000031
pbos@webrtc.org3349ae02014-03-13 12:52:27 +000032 // Sets max bitrate. Not thread-safe, call before registering the encoder.
33 void SetMaxBitrate(int max_kbps);
pbos@webrtc.orgcb5118c2013-09-03 09:10:37 +000034
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000035 int32_t InitEncode(const VideoCodec* config,
36 int32_t number_of_cores,
37 size_t max_payload_size) override;
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070038 int32_t Encode(const VideoFrame& input_image,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000039 const CodecSpecificInfo* codec_specific_info,
pbos22993e12015-10-19 02:39:06 -070040 const std::vector<FrameType>* frame_types) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000041 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ång08127a92016-11-16 16:41:30 +010045 int32_t SetRateAllocation(const BitrateAllocation& rate_allocation,
46 uint32_t framerate) override;
Peter Boströmb7d9a972015-12-18 16:01:11 +010047 const char* ImplementationName() const override;
sprang4847ae62017-06-27 07:06:52 -070048 int GetConfiguredInputFramerate() const;
Peter Boströmb7d9a972015-12-18 16:01:11 +010049
50 static const char* kImplementationName;
stefan@webrtc.org360e3762013-08-22 09:29:56 +000051
pbos@webrtc.org273a4142014-12-01 15:23:21 +000052 protected:
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +000053 Clock* const clock_;
danilchapa37de392017-09-09 04:17:22 -070054 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_);
brandtr49ce67c2017-02-11 00:25:18 -080060 rtc::CriticalSection crit_sect_;
61
pbos@webrtc.orgc095f512013-08-22 12:34:58 +000062 uint8_t encoded_buffer_[100000];
sprang4847ae62017-06-27 07:06:52 -070063
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.org360e3762013-08-22 09:29:56 +000067};
stefan@webrtc.org79c33592014-08-06 09:24:53 +000068
69class FakeH264Encoder : public FakeEncoder, public EncodedImageCallback {
70 public:
71 explicit FakeH264Encoder(Clock* clock);
brandtr49ce67c2017-02-11 00:25:18 -080072 virtual ~FakeH264Encoder() = default;
stefan@webrtc.org79c33592014-08-06 09:24:53 +000073
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000074 int32_t RegisterEncodeCompleteCallback(
75 EncodedImageCallback* callback) override;
stefan@webrtc.org79c33592014-08-06 09:24:53 +000076
Sergey Ulanov525df3f2016-08-02 17:46:41 -070077 Result OnEncodedImage(const EncodedImage& encodedImage,
78 const CodecSpecificInfo* codecSpecificInfo,
79 const RTPFragmentationHeader* fragments) override;
stefan@webrtc.org79c33592014-08-06 09:24:53 +000080
81 private:
danilchapa37de392017-09-09 04:17:22 -070082 EncodedImageCallback* callback_ RTC_GUARDED_BY(local_crit_sect_);
83 int idr_counter_ RTC_GUARDED_BY(local_crit_sect_);
brandtr49ce67c2017-02-11 00:25:18 -080084 rtc::CriticalSection local_crit_sect_;
stefan@webrtc.org79c33592014-08-06 09:24:53 +000085};
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +000086
87class DelayedEncoder : public test::FakeEncoder {
88 public:
89 DelayedEncoder(Clock* clock, int delay_ms);
brandtr49ce67c2017-02-11 00:25:18 -080090 virtual ~DelayedEncoder() = default;
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +000091
perkj803d97f2016-11-01 11:45:46 -070092 void SetDelay(int delay_ms);
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070093 int32_t Encode(const VideoFrame& input_image,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000094 const CodecSpecificInfo* codec_specific_info,
pbos22993e12015-10-19 02:39:06 -070095 const std::vector<FrameType>* frame_types) override;
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +000096
97 private:
danilchapa37de392017-09-09 04:17:22 -070098 int delay_ms_ RTC_ACCESS_ON(sequence_checker_);
brandtr49ce67c2017-02-11 00:25:18 -080099 rtc::SequencedTaskChecker sequence_checker_;
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +0000100};
brandtr696c9c62016-12-19 05:47:28 -0800101
102// This class implements a multi-threaded fake encoder by posting
103// FakeH264Encoder::Encode(.) tasks to |queue1_| and |queue2_|, in an
brandtr49ce67c2017-02-11 00:25:18 -0800104// alternating fashion. The class itself does not need to be thread safe,
mflodmancc3d4422017-08-03 08:27:51 -0700105// as it is called from the task queue in VideoStreamEncoder.
brandtre78d2662017-01-16 05:57:16 -0800106class MultithreadedFakeH264Encoder : public test::FakeH264Encoder {
brandtr696c9c62016-12-19 05:47:28 -0800107 public:
brandtre78d2662017-01-16 05:57:16 -0800108 explicit MultithreadedFakeH264Encoder(Clock* clock);
brandtr49ce67c2017-02-11 00:25:18 -0800109 virtual ~MultithreadedFakeH264Encoder() = default;
110
111 int32_t InitEncode(const VideoCodec* config,
112 int32_t number_of_cores,
113 size_t max_payload_size) override;
brandtr696c9c62016-12-19 05:47:28 -0800114
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
brandtr49ce67c2017-02-11 00:25:18 -0800123 int32_t Release() override;
124
brandtr696c9c62016-12-19 05:47:28 -0800125 protected:
126 class EncodeTask;
127
danilchapa37de392017-09-09 04:17:22 -0700128 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_);
brandtr49ce67c2017-02-11 00:25:18 -0800131 rtc::SequencedTaskChecker sequence_checker_;
brandtr696c9c62016-12-19 05:47:28 -0800132};
133
pbos@webrtc.orgcb5118c2013-09-03 09:10:37 +0000134} // namespace test
stefan@webrtc.org360e3762013-08-22 09:29:56 +0000135} // namespace webrtc
136
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200137#endif // TEST_FAKE_ENCODER_H_