video_encoder_proxy_factory.h 5.1 KB

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