• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2018 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 
11 #ifndef TEST_VIDEO_ENCODER_PROXY_FACTORY_H_
12 #define TEST_VIDEO_ENCODER_PROXY_FACTORY_H_
13 
14 #include <memory>
15 #include <vector>
16 
17 #include "api/video_codecs/video_encoder.h"
18 #include "api/video_codecs/video_encoder_factory.h"
19 
20 namespace webrtc {
21 namespace test {
22 
23 namespace {
24 const VideoEncoder::Capabilities kCapabilities(false);
25 }
26 
27 // An encoder factory with a single underlying VideoEncoder object,
28 // intended for test purposes. Each call to CreateVideoEncoder returns
29 // a proxy for the same encoder, typically an instance of FakeEncoder.
30 class VideoEncoderProxyFactory final : public VideoEncoderFactory {
31  public:
VideoEncoderProxyFactory(VideoEncoder * encoder)32   explicit VideoEncoderProxyFactory(VideoEncoder* encoder)
33       : VideoEncoderProxyFactory(encoder, nullptr) {}
34 
VideoEncoderProxyFactory(VideoEncoder * encoder,EncoderSelectorInterface * encoder_selector)35   explicit VideoEncoderProxyFactory(VideoEncoder* encoder,
36                                     EncoderSelectorInterface* encoder_selector)
37       : encoder_(encoder),
38         encoder_selector_(encoder_selector),
39         num_simultaneous_encoder_instances_(0),
40         max_num_simultaneous_encoder_instances_(0) {
41     codec_info_.is_hardware_accelerated = false;
42     codec_info_.has_internal_source = false;
43   }
44 
45   // Unused by tests.
GetSupportedFormats()46   std::vector<SdpVideoFormat> GetSupportedFormats() const override {
47     RTC_NOTREACHED();
48     return {};
49   }
50 
QueryVideoEncoder(const SdpVideoFormat & format)51   CodecInfo QueryVideoEncoder(const SdpVideoFormat& format) const override {
52     return codec_info_;
53   }
54 
CreateVideoEncoder(const SdpVideoFormat & format)55   std::unique_ptr<VideoEncoder> CreateVideoEncoder(
56       const SdpVideoFormat& format) override {
57     ++num_simultaneous_encoder_instances_;
58     max_num_simultaneous_encoder_instances_ =
59         std::max(max_num_simultaneous_encoder_instances_,
60                  num_simultaneous_encoder_instances_);
61     return std::make_unique<EncoderProxy>(encoder_, this);
62   }
63 
GetEncoderSelector()64   std::unique_ptr<EncoderSelectorInterface> GetEncoderSelector()
65       const override {
66     if (encoder_selector_ != nullptr) {
67       return std::make_unique<EncoderSelectorProxy>(encoder_selector_);
68     }
69 
70     return nullptr;
71   }
72 
SetIsHardwareAccelerated(bool is_hardware_accelerated)73   void SetIsHardwareAccelerated(bool is_hardware_accelerated) {
74     codec_info_.is_hardware_accelerated = is_hardware_accelerated;
75   }
SetHasInternalSource(bool has_internal_source)76   void SetHasInternalSource(bool has_internal_source) {
77     codec_info_.has_internal_source = has_internal_source;
78   }
79 
GetMaxNumberOfSimultaneousEncoderInstances()80   int GetMaxNumberOfSimultaneousEncoderInstances() {
81     return max_num_simultaneous_encoder_instances_;
82   }
83 
84  private:
OnDestroyVideoEncoder()85   void OnDestroyVideoEncoder() {
86     RTC_CHECK_GT(num_simultaneous_encoder_instances_, 0);
87     --num_simultaneous_encoder_instances_;
88   }
89 
90   // Wrapper class, since CreateVideoEncoder needs to surrender
91   // ownership to the object it returns.
92   class EncoderProxy final : public VideoEncoder {
93    public:
EncoderProxy(VideoEncoder * encoder,VideoEncoderProxyFactory * encoder_factory)94     explicit EncoderProxy(VideoEncoder* encoder,
95                           VideoEncoderProxyFactory* encoder_factory)
96         : encoder_(encoder), encoder_factory_(encoder_factory) {}
~EncoderProxy()97     ~EncoderProxy() { encoder_factory_->OnDestroyVideoEncoder(); }
98 
99    private:
SetFecControllerOverride(FecControllerOverride * fec_controller_override)100     void SetFecControllerOverride(
101         FecControllerOverride* fec_controller_override) override {
102       encoder_->SetFecControllerOverride(fec_controller_override);
103     }
104 
Encode(const VideoFrame & input_image,const std::vector<VideoFrameType> * frame_types)105     int32_t Encode(const VideoFrame& input_image,
106                    const std::vector<VideoFrameType>* frame_types) override {
107       return encoder_->Encode(input_image, frame_types);
108     }
109 
InitEncode(const VideoCodec * config,const Settings & settings)110     int32_t InitEncode(const VideoCodec* config,
111                        const Settings& settings) override {
112       return encoder_->InitEncode(config, settings);
113     }
114 
RegisterEncodeCompleteCallback(EncodedImageCallback * callback)115     int32_t RegisterEncodeCompleteCallback(
116         EncodedImageCallback* callback) override {
117       return encoder_->RegisterEncodeCompleteCallback(callback);
118     }
119 
Release()120     int32_t Release() override { return encoder_->Release(); }
121 
SetRates(const RateControlParameters & parameters)122     void SetRates(const RateControlParameters& parameters) override {
123       encoder_->SetRates(parameters);
124     }
125 
GetEncoderInfo()126     VideoEncoder::EncoderInfo GetEncoderInfo() const override {
127       return encoder_->GetEncoderInfo();
128     }
129 
130     VideoEncoder* const encoder_;
131     VideoEncoderProxyFactory* const encoder_factory_;
132   };
133 
134   class EncoderSelectorProxy final : public EncoderSelectorInterface {
135    public:
EncoderSelectorProxy(EncoderSelectorInterface * encoder_selector)136     explicit EncoderSelectorProxy(EncoderSelectorInterface* encoder_selector)
137         : encoder_selector_(encoder_selector) {}
138 
OnCurrentEncoder(const SdpVideoFormat & format)139     void OnCurrentEncoder(const SdpVideoFormat& format) override {
140       encoder_selector_->OnCurrentEncoder(format);
141     }
142 
OnAvailableBitrate(const DataRate & rate)143     absl::optional<SdpVideoFormat> OnAvailableBitrate(
144         const DataRate& rate) override {
145       return encoder_selector_->OnAvailableBitrate(rate);
146     }
147 
OnEncoderBroken()148     absl::optional<SdpVideoFormat> OnEncoderBroken() override {
149       return encoder_selector_->OnEncoderBroken();
150     }
151 
152    private:
153     EncoderSelectorInterface* const encoder_selector_;
154   };
155 
156   VideoEncoder* const encoder_;
157   EncoderSelectorInterface* const encoder_selector_;
158   CodecInfo codec_info_;
159 
160   int num_simultaneous_encoder_instances_;
161   int max_num_simultaneous_encoder_instances_;
162 };
163 
164 }  // namespace test
165 }  // namespace webrtc
166 
167 #endif  // TEST_VIDEO_ENCODER_PROXY_FACTORY_H_
168