blob: e3878ecd4a78daefae1807b418cc16174faee49f [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
Peter Boström7623ce42015-12-09 12:13:30 +010011#ifndef WEBRTC_TEST_FAKE_ENCODER_H_
12#define WEBRTC_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
ilnikd60d06a2017-04-05 03:02:20 -070017#include "webrtc/api/video_codecs/video_encoder.h"
perkj803d97f2016-11-01 11:45:46 -070018#include "webrtc/base/criticalsection.h"
brandtr49ce67c2017-02-11 00:25:18 -080019#include "webrtc/base/sequenced_task_checker.h"
brandtr696c9c62016-12-19 05:47:28 -080020#include "webrtc/base/task_queue.h"
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000021#include "webrtc/common_types.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010022#include "webrtc/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;
48
49 static const char* kImplementationName;
stefan@webrtc.org360e3762013-08-22 09:29:56 +000050
pbos@webrtc.org273a4142014-12-01 15:23:21 +000051 protected:
pbos@webrtc.orgde1429e2014-04-28 13:00:21 +000052 Clock* const clock_;
brandtre78d2662017-01-16 05:57:16 -080053 VideoCodec config_ GUARDED_BY(crit_sect_);
54 EncodedImageCallback* callback_ GUARDED_BY(crit_sect_);
55 BitrateAllocation target_bitrate_ GUARDED_BY(crit_sect_);
56 int max_target_bitrate_kbps_ GUARDED_BY(crit_sect_);
sprang317005a2017-06-08 07:12:17 -070057 int64_t last_encode_time_ms_ GUARDED_BY(crit_sect_);
brandtr49ce67c2017-02-11 00:25:18 -080058 rtc::CriticalSection crit_sect_;
59
pbos@webrtc.orgc095f512013-08-22 12:34:58 +000060 uint8_t encoded_buffer_[100000];
stefan@webrtc.org360e3762013-08-22 09:29:56 +000061};
stefan@webrtc.org79c33592014-08-06 09:24:53 +000062
63class FakeH264Encoder : public FakeEncoder, public EncodedImageCallback {
64 public:
65 explicit FakeH264Encoder(Clock* clock);
brandtr49ce67c2017-02-11 00:25:18 -080066 virtual ~FakeH264Encoder() = default;
stefan@webrtc.org79c33592014-08-06 09:24:53 +000067
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000068 int32_t RegisterEncodeCompleteCallback(
69 EncodedImageCallback* callback) override;
stefan@webrtc.org79c33592014-08-06 09:24:53 +000070
Sergey Ulanov525df3f2016-08-02 17:46:41 -070071 Result OnEncodedImage(const EncodedImage& encodedImage,
72 const CodecSpecificInfo* codecSpecificInfo,
73 const RTPFragmentationHeader* fragments) override;
stefan@webrtc.org79c33592014-08-06 09:24:53 +000074
75 private:
brandtre78d2662017-01-16 05:57:16 -080076 EncodedImageCallback* callback_ GUARDED_BY(local_crit_sect_);
77 int idr_counter_ GUARDED_BY(local_crit_sect_);
brandtr49ce67c2017-02-11 00:25:18 -080078 rtc::CriticalSection local_crit_sect_;
stefan@webrtc.org79c33592014-08-06 09:24:53 +000079};
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +000080
81class DelayedEncoder : public test::FakeEncoder {
82 public:
83 DelayedEncoder(Clock* clock, int delay_ms);
brandtr49ce67c2017-02-11 00:25:18 -080084 virtual ~DelayedEncoder() = default;
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +000085
perkj803d97f2016-11-01 11:45:46 -070086 void SetDelay(int delay_ms);
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070087 int32_t Encode(const VideoFrame& input_image,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000088 const CodecSpecificInfo* codec_specific_info,
pbos22993e12015-10-19 02:39:06 -070089 const std::vector<FrameType>* frame_types) override;
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +000090
91 private:
brandtr49ce67c2017-02-11 00:25:18 -080092 int delay_ms_ ACCESS_ON(sequence_checker_);
93 rtc::SequencedTaskChecker sequence_checker_;
asapersson@webrtc.org049e4ec2014-11-20 10:19:46 +000094};
brandtr696c9c62016-12-19 05:47:28 -080095
96// This class implements a multi-threaded fake encoder by posting
97// FakeH264Encoder::Encode(.) tasks to |queue1_| and |queue2_|, in an
brandtr49ce67c2017-02-11 00:25:18 -080098// alternating fashion. The class itself does not need to be thread safe,
99// as it is called from the task queue in ViEEncoder.
brandtre78d2662017-01-16 05:57:16 -0800100class MultithreadedFakeH264Encoder : public test::FakeH264Encoder {
brandtr696c9c62016-12-19 05:47:28 -0800101 public:
brandtre78d2662017-01-16 05:57:16 -0800102 explicit MultithreadedFakeH264Encoder(Clock* clock);
brandtr49ce67c2017-02-11 00:25:18 -0800103 virtual ~MultithreadedFakeH264Encoder() = default;
104
105 int32_t InitEncode(const VideoCodec* config,
106 int32_t number_of_cores,
107 size_t max_payload_size) override;
brandtr696c9c62016-12-19 05:47:28 -0800108
109 int32_t Encode(const VideoFrame& input_image,
110 const CodecSpecificInfo* codec_specific_info,
111 const std::vector<FrameType>* frame_types) override;
112
113 int32_t EncodeCallback(const VideoFrame& input_image,
114 const CodecSpecificInfo* codec_specific_info,
115 const std::vector<FrameType>* frame_types);
116
brandtr49ce67c2017-02-11 00:25:18 -0800117 int32_t Release() override;
118
brandtr696c9c62016-12-19 05:47:28 -0800119 protected:
120 class EncodeTask;
121
brandtr49ce67c2017-02-11 00:25:18 -0800122 int current_queue_ ACCESS_ON(sequence_checker_);
123 std::unique_ptr<rtc::TaskQueue> queue1_ ACCESS_ON(sequence_checker_);
124 std::unique_ptr<rtc::TaskQueue> queue2_ ACCESS_ON(sequence_checker_);
125 rtc::SequencedTaskChecker sequence_checker_;
brandtr696c9c62016-12-19 05:47:28 -0800126};
127
pbos@webrtc.orgcb5118c2013-09-03 09:10:37 +0000128} // namespace test
stefan@webrtc.org360e3762013-08-22 09:29:56 +0000129} // namespace webrtc
130
Peter Boström7623ce42015-12-09 12:13:30 +0100131#endif // WEBRTC_TEST_FAKE_ENCODER_H_