video_encoder.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright 2020 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 MEDIA_BASE_VIDEO_ENCODER_H_
  5. #define MEDIA_BASE_VIDEO_ENCODER_H_
  6. #include "base/callback.h"
  7. #include "base/time/time.h"
  8. #include "media/base/bitrate.h"
  9. #include "media/base/encoder_status.h"
  10. #include "media/base/media_export.h"
  11. #include "media/base/svc_scalability_mode.h"
  12. #include "media/base/video_codecs.h"
  13. #include "third_party/abseil-cpp/absl/types/optional.h"
  14. #include "ui/gfx/color_space.h"
  15. #include "ui/gfx/geometry/size.h"
  16. namespace media {
  17. class VideoFrame;
  18. MEDIA_EXPORT uint32_t GetDefaultVideoEncodeBitrate(gfx::Size frame_size,
  19. uint32_t framerate);
  20. // Encoded video frame, its data and metadata.
  21. struct MEDIA_EXPORT VideoEncoderOutput {
  22. VideoEncoderOutput();
  23. VideoEncoderOutput(VideoEncoderOutput&&);
  24. ~VideoEncoderOutput();
  25. // Feel free take this buffer out and use underlying memory as is without
  26. // copying.
  27. std::unique_ptr<uint8_t[]> data;
  28. size_t size = 0;
  29. base::TimeDelta timestamp;
  30. bool key_frame = false;
  31. int temporal_id = 0;
  32. gfx::ColorSpace color_space;
  33. };
  34. class MEDIA_EXPORT VideoEncoder {
  35. public:
  36. // TODO: Move this to a new file if there are more codec specific options.
  37. struct MEDIA_EXPORT AvcOptions {
  38. bool produce_annexb = false;
  39. };
  40. enum class LatencyMode { Realtime, Quality };
  41. struct MEDIA_EXPORT Options {
  42. Options();
  43. Options(const Options&);
  44. ~Options();
  45. absl::optional<Bitrate> bitrate;
  46. absl::optional<double> framerate;
  47. gfx::Size frame_size;
  48. absl::optional<int> keyframe_interval = 10000;
  49. LatencyMode latency_mode = LatencyMode::Realtime;
  50. absl::optional<SVCScalabilityMode> scalability_mode;
  51. // Only used for H264 encoding.
  52. AvcOptions avc;
  53. };
  54. // A sequence of codec specific bytes, commonly known as extradata.
  55. // If available, it should be given to the decoder as part of the
  56. // decoder config.
  57. using CodecDescription = std::vector<uint8_t>;
  58. // Callback for VideoEncoder to report an encoded video frame whenever it
  59. // becomes available.
  60. using OutputCB =
  61. base::RepeatingCallback<void(VideoEncoderOutput output,
  62. absl::optional<CodecDescription>)>;
  63. // Callback to report success and errors in encoder calls.
  64. using EncoderStatusCB = base::OnceCallback<void(EncoderStatus error)>;
  65. struct PendingEncode {
  66. PendingEncode();
  67. PendingEncode(PendingEncode&&);
  68. ~PendingEncode();
  69. EncoderStatusCB done_callback;
  70. scoped_refptr<VideoFrame> frame;
  71. bool key_frame;
  72. };
  73. VideoEncoder();
  74. VideoEncoder(const VideoEncoder&) = delete;
  75. VideoEncoder& operator=(const VideoEncoder&) = delete;
  76. virtual ~VideoEncoder();
  77. // Initializes a VideoEncoder with the given |options|, executing the
  78. // |done_cb| upon completion. |output_cb| is called for each encoded frame
  79. // produced by the coder.
  80. //
  81. // Note:
  82. // 1) Can't be called more than once for the same instance of the encoder.
  83. // 2) No VideoEncoder calls should be made before |done_cb| is executed.
  84. virtual void Initialize(VideoCodecProfile profile,
  85. const Options& options,
  86. OutputCB output_cb,
  87. EncoderStatusCB done_cb) = 0;
  88. // Requests a |frame| to be encoded. The status of the encoder and the frame
  89. // are returned via the provided callback |done_cb|.
  90. //
  91. // |done_cb| will not be called from within this method, and that it will be
  92. // called even if Encode() is never called again.
  93. // After the frame, or several frames, are encoded the encoder calls
  94. // |output_cb| specified in Initialize() for available VideoEncoderOutput.
  95. // |output_cb| may be called before or after |done_cb|,
  96. // including before Encode() returns.
  97. // Encode() does not expect EOS frames, use Flush() to finalize the stream
  98. // and harvest the outputs.
  99. virtual void Encode(scoped_refptr<VideoFrame> frame,
  100. bool key_frame,
  101. EncoderStatusCB done_cb) = 0;
  102. // Adjust encoder options and the output callback for future frames, executing
  103. // the |done_cb| upon completion.
  104. //
  105. // Note:
  106. // 1. Not all options can be changed on the fly.
  107. // 2. ChangeOptions() should be called after calling Flush() and waiting
  108. // for it to finish.
  109. virtual void ChangeOptions(const Options& options,
  110. OutputCB output_cb,
  111. EncoderStatusCB done_cb) = 0;
  112. // Requests all outputs for already encoded frames to be
  113. // produced via |output_cb| and calls |dene_cb| after that.
  114. virtual void Flush(EncoderStatusCB done_cb) = 0;
  115. };
  116. } // namespace media
  117. #endif // MEDIA_BASE_VIDEO_ENCODER_H_