simulcast_encoder_adapter.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2014 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 MEDIA_ENGINE_SIMULCAST_ENCODER_ADAPTER_H_
  12. #define MEDIA_ENGINE_SIMULCAST_ENCODER_ADAPTER_H_
  13. #include <memory>
  14. #include <stack>
  15. #include <string>
  16. #include <utility>
  17. #include <vector>
  18. #include "absl/types/optional.h"
  19. #include "api/fec_controller_override.h"
  20. #include "api/video_codecs/sdp_video_format.h"
  21. #include "api/video_codecs/video_encoder.h"
  22. #include "modules/video_coding/include/video_codec_interface.h"
  23. #include "modules/video_coding/utility/framerate_controller.h"
  24. #include "rtc_base/atomic_ops.h"
  25. #include "rtc_base/synchronization/sequence_checker.h"
  26. #include "rtc_base/system/no_unique_address.h"
  27. #include "rtc_base/system/rtc_export.h"
  28. namespace webrtc {
  29. class SimulcastRateAllocator;
  30. class VideoEncoderFactory;
  31. // SimulcastEncoderAdapter implements simulcast support by creating multiple
  32. // webrtc::VideoEncoder instances with the given VideoEncoderFactory.
  33. // The object is created and destroyed on the worker thread, but all public
  34. // interfaces should be called from the encoder task queue.
  35. class RTC_EXPORT SimulcastEncoderAdapter : public VideoEncoder {
  36. public:
  37. // TODO(bugs.webrtc.org/11000): Remove when downstream usage is gone.
  38. SimulcastEncoderAdapter(VideoEncoderFactory* primarty_factory,
  39. const SdpVideoFormat& format);
  40. // |primary_factory| produces the first-choice encoders to use.
  41. // |fallback_factory|, if non-null, is used to create fallback encoder that
  42. // will be used if InitEncode() fails for the primary encoder.
  43. SimulcastEncoderAdapter(VideoEncoderFactory* primary_factory,
  44. VideoEncoderFactory* fallback_factory,
  45. const SdpVideoFormat& format);
  46. ~SimulcastEncoderAdapter() override;
  47. // Implements VideoEncoder.
  48. void SetFecControllerOverride(
  49. FecControllerOverride* fec_controller_override) override;
  50. int Release() override;
  51. int InitEncode(const VideoCodec* codec_settings,
  52. const VideoEncoder::Settings& settings) override;
  53. int Encode(const VideoFrame& input_image,
  54. const std::vector<VideoFrameType>* frame_types) override;
  55. int RegisterEncodeCompleteCallback(EncodedImageCallback* callback) override;
  56. void SetRates(const RateControlParameters& parameters) override;
  57. void OnPacketLossRateUpdate(float packet_loss_rate) override;
  58. void OnRttUpdate(int64_t rtt_ms) override;
  59. void OnLossNotification(const LossNotification& loss_notification) override;
  60. // Eventual handler for the contained encoders' EncodedImageCallbacks, but
  61. // called from an internal helper that also knows the correct stream
  62. // index.
  63. EncodedImageCallback::Result OnEncodedImage(
  64. size_t stream_idx,
  65. const EncodedImage& encoded_image,
  66. const CodecSpecificInfo* codec_specific_info);
  67. EncoderInfo GetEncoderInfo() const override;
  68. private:
  69. struct StreamInfo {
  70. StreamInfo(std::unique_ptr<VideoEncoder> encoder,
  71. std::unique_ptr<EncodedImageCallback> callback,
  72. std::unique_ptr<FramerateController> framerate_controller,
  73. uint16_t width,
  74. uint16_t height,
  75. bool send_stream)
  76. : encoder(std::move(encoder)),
  77. callback(std::move(callback)),
  78. framerate_controller(std::move(framerate_controller)),
  79. width(width),
  80. height(height),
  81. key_frame_request(false),
  82. send_stream(send_stream) {}
  83. std::unique_ptr<VideoEncoder> encoder;
  84. std::unique_ptr<EncodedImageCallback> callback;
  85. std::unique_ptr<FramerateController> framerate_controller;
  86. uint16_t width;
  87. uint16_t height;
  88. bool key_frame_request;
  89. bool send_stream;
  90. };
  91. enum class StreamResolution {
  92. OTHER,
  93. HIGHEST,
  94. LOWEST,
  95. };
  96. // Populate the codec settings for each simulcast stream.
  97. void PopulateStreamCodec(const webrtc::VideoCodec& inst,
  98. int stream_index,
  99. uint32_t start_bitrate_kbps,
  100. StreamResolution stream_resolution,
  101. webrtc::VideoCodec* stream_codec);
  102. bool Initialized() const;
  103. void DestroyStoredEncoders();
  104. volatile int inited_; // Accessed atomically.
  105. VideoEncoderFactory* const primary_encoder_factory_;
  106. VideoEncoderFactory* const fallback_encoder_factory_;
  107. const SdpVideoFormat video_format_;
  108. VideoCodec codec_;
  109. std::vector<StreamInfo> streaminfos_;
  110. EncodedImageCallback* encoded_complete_callback_;
  111. // Used for checking the single-threaded access of the encoder interface.
  112. RTC_NO_UNIQUE_ADDRESS SequenceChecker encoder_queue_;
  113. // Store encoders in between calls to Release and InitEncode, so they don't
  114. // have to be recreated. Remaining encoders are destroyed by the destructor.
  115. std::stack<std::unique_ptr<VideoEncoder>> stored_encoders_;
  116. const absl::optional<unsigned int> experimental_boosted_screenshare_qp_;
  117. const bool boost_base_layer_quality_;
  118. const bool prefer_temporal_support_on_base_layer_;
  119. };
  120. } // namespace webrtc
  121. #endif // MEDIA_ENGINE_SIMULCAST_ENCODER_ADAPTER_H_