webrtc_video_encoder.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright 2016 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef REMOTING_CODEC_WEBRTC_VIDEO_ENCODER_H_
  5. #define REMOTING_CODEC_WEBRTC_VIDEO_ENCODER_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <string>
  9. #include "base/callback.h"
  10. #include "base/time/time.h"
  11. #include "third_party/webrtc/api/video/encoded_image.h"
  12. #include "third_party/webrtc/api/video/video_codec_type.h"
  13. #include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h"
  14. namespace webrtc {
  15. class DesktopFrame;
  16. } // namespace webrtc
  17. namespace remoting {
  18. // A class to perform the task of encoding a continuous stream of images. The
  19. // interface is asynchronous to enable maximum throughput.
  20. class WebrtcVideoEncoder {
  21. public:
  22. struct FrameParams {
  23. // Target bitrate in kilobits per second.
  24. int bitrate_kbps = -1;
  25. // Frame duration.
  26. base::TimeDelta duration;
  27. // If set to true then the active map passed to the encoder will only
  28. // contain updated_region() from the current frame. Otherwise the active map
  29. // is not cleared before adding updated_region(), which means it will
  30. // contain a union of updated_region() from all frames since this flag was
  31. // last set. This flag is used to top-off video quality with VP8.
  32. bool clear_active_map = false;
  33. // Indicates that the encoder should encode this frame as a key frame.
  34. bool key_frame = false;
  35. // Target FPS. A value less than 0 means unset.
  36. int fps = -1;
  37. // Quantization parameters for the encoder.
  38. int vpx_min_quantizer = -1;
  39. int vpx_max_quantizer = -1;
  40. };
  41. // Information needed for sending video statistics for each encoded frame.
  42. // The destructor is virtual so that implementations can derive from this
  43. // class to attach more data to the frame.
  44. struct FrameStats {
  45. FrameStats() = default;
  46. FrameStats(const FrameStats&) = default;
  47. FrameStats& operator=(const FrameStats&) = default;
  48. virtual ~FrameStats() = default;
  49. // TODO(crbug.com/1192865): Consolidate all the per-frame statistics
  50. // into a single struct in remoting/protocol.
  51. base::TimeTicks capture_started_time;
  52. base::TimeTicks capture_ended_time;
  53. base::TimeTicks encode_started_time;
  54. base::TimeTicks encode_ended_time;
  55. base::TimeDelta send_pending_delay{base::TimeDelta::Max()};
  56. base::TimeDelta rtt_estimate{base::TimeDelta::Max()};
  57. int bandwidth_estimate_kbps = -1;
  58. };
  59. struct EncodedFrame {
  60. EncodedFrame();
  61. EncodedFrame(const EncodedFrame&) = delete;
  62. EncodedFrame(EncodedFrame&&);
  63. EncodedFrame& operator=(const EncodedFrame&) = delete;
  64. EncodedFrame& operator=(EncodedFrame&&);
  65. ~EncodedFrame();
  66. webrtc::DesktopSize dimensions;
  67. rtc::scoped_refptr<webrtc::EncodedImageBuffer> data;
  68. bool key_frame;
  69. int quantizer;
  70. webrtc::VideoCodecType codec;
  71. uint32_t rtp_timestamp;
  72. std::unique_ptr<FrameStats> stats;
  73. };
  74. enum class EncodeResult {
  75. SUCCEEDED,
  76. // The implementation cannot handle the frame, the size exceeds the
  77. // capability of the codec or the implementation.
  78. FRAME_SIZE_EXCEEDS_CAPABILITY,
  79. // Undefined or unhandled error has happened. This error type should be
  80. // avoided. A more exact error type is preferred.
  81. UNKNOWN_ERROR,
  82. };
  83. // Helper function for the VPX and AOM encoders to determine the number of
  84. // threads needed to efficiently encode a frame based on its width.
  85. static int GetEncoderThreadCount(int frame_width);
  86. // A derived class calls EncodeCallback to return the result of an encoding
  87. // request. SUCCEEDED with an empty EncodedFrame (nullptr) indicates the frame
  88. // should be dropped (unchanged or empty frame). Otherwise EncodeResult shows
  89. // the errors.
  90. typedef base::OnceCallback<void(EncodeResult, std::unique_ptr<EncodedFrame>)>
  91. EncodeCallback;
  92. virtual ~WebrtcVideoEncoder() {}
  93. // Encoder configurable settings, may be provided via SDP or OOB via a
  94. // proprietary message.
  95. virtual void SetLosslessEncode(bool want_lossless) {}
  96. virtual void SetLosslessColor(bool want_lossless) {}
  97. virtual void SetEncoderSpeed(int encoder_speed) {}
  98. // Encode an image stored in |frame|. If frame.updated_region() is empty
  99. // then the encoder may return a frame (e.g. to top-off previously-encoded
  100. // portions of the frame to higher quality) or return nullptr to indicate that
  101. // there is no work to do. |frame| may be nullptr, which is equivalent to a
  102. // frame with an empty updated_region(). |done| callback may be called
  103. // synchronously. It must not be called if the encoder is destroyed while
  104. // the request is pending.
  105. virtual void Encode(std::unique_ptr<webrtc::DesktopFrame> frame,
  106. const FrameParams& param,
  107. EncodeCallback done) = 0;
  108. };
  109. } // namespace remoting
  110. #endif // REMOTING_CODEC_WEBRTC_VIDEO_ENCODER_H_