You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
5.0 KiB

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