blob: 96218e5c274a587caa2747d37efaa91e00fd1743 [file] [log] [blame]
stefan@webrtc.org360e3762013-08-22 09:29:561/*
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
Peter Boström7623ce42015-12-09 11:13:3011#ifndef WEBRTC_TEST_FAKE_ENCODER_H_
12#define WEBRTC_TEST_FAKE_ENCODER_H_
stefan@webrtc.org360e3762013-08-22 09:29:5613
14#include <vector>
brandtr49ce67c2017-02-11 08:25:1815#include <memory>
stefan@webrtc.org360e3762013-08-22 09:29:5616
ilnikd60d06a2017-04-05 10:02:2017#include "webrtc/api/video_codecs/video_encoder.h"
pbos@webrtc.orgab990ae2014-09-17 09:02:2518#include "webrtc/common_types.h"
Edward Lemurc20978e2017-07-06 17:44:3419#include "webrtc/rtc_base/criticalsection.h"
20#include "webrtc/rtc_base/sequenced_task_checker.h"
21#include "webrtc/rtc_base/task_queue.h"
Henrik Kjellander98f53512015-10-28 17:17:4022#include "webrtc/system_wrappers/include/clock.h"
stefan@webrtc.org360e3762013-08-22 09:29:5623
24namespace webrtc {
pbos@webrtc.orgcb5118c2013-09-03 09:10:3725namespace test {
stefan@webrtc.org360e3762013-08-22 09:29:5626
27class FakeEncoder : public VideoEncoder {
28 public:
29 explicit FakeEncoder(Clock* clock);
brandtr49ce67c2017-02-11 08:25:1830 virtual ~FakeEncoder() = default;
stefan@webrtc.org360e3762013-08-22 09:29:5631
pbos@webrtc.org3349ae02014-03-13 12:52:2732 // Sets max bitrate. Not thread-safe, call before registering the encoder.
33 void SetMaxBitrate(int max_kbps);
pbos@webrtc.orgcb5118c2013-09-03 09:10:3734
kjellander@webrtc.org14665ff2015-03-04 12:58:3535 int32_t InitEncode(const VideoCodec* config,
36 int32_t number_of_cores,
37 size_t max_payload_size) override;
Miguel Casas-Sanchez47650702015-05-30 00:21:4038 int32_t Encode(const VideoFrame& input_image,
kjellander@webrtc.org14665ff2015-03-04 12:58:3539 const CodecSpecificInfo* codec_specific_info,
pbos22993e12015-10-19 09:39:0640 const std::vector<FrameType>* frame_types) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:3541 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 15:41:3045 int32_t SetRateAllocation(const BitrateAllocation& rate_allocation,
46 uint32_t framerate) override;
Peter Boströmb7d9a972015-12-18 15:01:1147 const char* ImplementationName() const override;
sprang4847ae62017-06-27 14:06:5248 int GetConfiguredInputFramerate() const;
Peter Boströmb7d9a972015-12-18 15:01:1149
50 static const char* kImplementationName;
stefan@webrtc.org360e3762013-08-22 09:29:5651
pbos@webrtc.org273a4142014-12-01 15:23:2152 protected:
pbos@webrtc.orgde1429e2014-04-28 13:00:2153 Clock* const clock_;
brandtre78d2662017-01-16 13:57:1654 VideoCodec config_ GUARDED_BY(crit_sect_);
55 EncodedImageCallback* callback_ GUARDED_BY(crit_sect_);
56 BitrateAllocation target_bitrate_ GUARDED_BY(crit_sect_);
sprang4847ae62017-06-27 14:06:5257 int configured_input_framerate_ GUARDED_BY(crit_sect_);
brandtre78d2662017-01-16 13:57:1658 int max_target_bitrate_kbps_ GUARDED_BY(crit_sect_);
sprang4847ae62017-06-27 14:06:5259 bool pending_keyframe_ GUARDED_BY(crit_sect_);
brandtr49ce67c2017-02-11 08:25:1860 rtc::CriticalSection crit_sect_;
61
pbos@webrtc.orgc095f512013-08-22 12:34:5862 uint8_t encoded_buffer_[100000];
sprang4847ae62017-06-27 14:06:5263
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:5667};
stefan@webrtc.org79c33592014-08-06 09:24:5368
69class FakeH264Encoder : public FakeEncoder, public EncodedImageCallback {
70 public:
71 explicit FakeH264Encoder(Clock* clock);
brandtr49ce67c2017-02-11 08:25:1872 virtual ~FakeH264Encoder() = default;
stefan@webrtc.org79c33592014-08-06 09:24:5373
kjellander@webrtc.org14665ff2015-03-04 12:58:3574 int32_t RegisterEncodeCompleteCallback(
75 EncodedImageCallback* callback) override;
stefan@webrtc.org79c33592014-08-06 09:24:5376
Sergey Ulanov525df3f2016-08-03 00:46:4177 Result OnEncodedImage(const EncodedImage& encodedImage,
78 const CodecSpecificInfo* codecSpecificInfo,
79 const RTPFragmentationHeader* fragments) override;
stefan@webrtc.org79c33592014-08-06 09:24:5380
81 private:
brandtre78d2662017-01-16 13:57:1682 EncodedImageCallback* callback_ GUARDED_BY(local_crit_sect_);
83 int idr_counter_ GUARDED_BY(local_crit_sect_);
brandtr49ce67c2017-02-11 08:25:1884 rtc::CriticalSection local_crit_sect_;
stefan@webrtc.org79c33592014-08-06 09:24:5385};
asapersson@webrtc.org049e4ec2014-11-20 10:19:4686
87class DelayedEncoder : public test::FakeEncoder {
88 public:
89 DelayedEncoder(Clock* clock, int delay_ms);
brandtr49ce67c2017-02-11 08:25:1890 virtual ~DelayedEncoder() = default;
asapersson@webrtc.org049e4ec2014-11-20 10:19:4691
perkj803d97f2016-11-01 18:45:4692 void SetDelay(int delay_ms);
Miguel Casas-Sanchez47650702015-05-30 00:21:4093 int32_t Encode(const VideoFrame& input_image,
kjellander@webrtc.org14665ff2015-03-04 12:58:3594 const CodecSpecificInfo* codec_specific_info,
pbos22993e12015-10-19 09:39:0695 const std::vector<FrameType>* frame_types) override;
asapersson@webrtc.org049e4ec2014-11-20 10:19:4696
97 private:
brandtr49ce67c2017-02-11 08:25:1898 int delay_ms_ ACCESS_ON(sequence_checker_);
99 rtc::SequencedTaskChecker sequence_checker_;
asapersson@webrtc.org049e4ec2014-11-20 10:19:46100};
brandtr696c9c62016-12-19 13:47:28101
102// This class implements a multi-threaded fake encoder by posting
103// FakeH264Encoder::Encode(.) tasks to |queue1_| and |queue2_|, in an
brandtr49ce67c2017-02-11 08:25:18104// alternating fashion. The class itself does not need to be thread safe,
mflodmancc3d4422017-08-03 15:27:51105// as it is called from the task queue in VideoStreamEncoder.
brandtre78d2662017-01-16 13:57:16106class MultithreadedFakeH264Encoder : public test::FakeH264Encoder {
brandtr696c9c62016-12-19 13:47:28107 public:
brandtre78d2662017-01-16 13:57:16108 explicit MultithreadedFakeH264Encoder(Clock* clock);
brandtr49ce67c2017-02-11 08:25:18109 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 13:47:28114
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 08:25:18123 int32_t Release() override;
124
brandtr696c9c62016-12-19 13:47:28125 protected:
126 class EncodeTask;
127
brandtr49ce67c2017-02-11 08:25:18128 int current_queue_ ACCESS_ON(sequence_checker_);
129 std::unique_ptr<rtc::TaskQueue> queue1_ ACCESS_ON(sequence_checker_);
130 std::unique_ptr<rtc::TaskQueue> queue2_ ACCESS_ON(sequence_checker_);
131 rtc::SequencedTaskChecker sequence_checker_;
brandtr696c9c62016-12-19 13:47:28132};
133
pbos@webrtc.orgcb5118c2013-09-03 09:10:37134} // namespace test
stefan@webrtc.org360e3762013-08-22 09:29:56135} // namespace webrtc
136
Peter Boström7623ce42015-12-09 11:13:30137#endif // WEBRTC_TEST_FAKE_ENCODER_H_