video_frame_observer.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #pragma once
  2. #include "api/video/video_frame.h"
  3. #include "api/video/video_sink_interface.h"
  4. #include "callback.h"
  5. using I420FrameReadyCallback = Callback<const void*,
  6. const void*,
  7. const void*,
  8. const void*,
  9. int,
  10. int,
  11. int,
  12. int,
  13. int,
  14. int>;
  15. using ARGBFrameReadyCallback = Callback<
  16. const uint8_t*,int,
  17. const uint8_t*,int,
  18. const uint8_t*, int,
  19. const int,const int,
  20. const int>;
  21. //根据图像的高度和每行数据的字节数,计算出整个图像数据所需的总字节数
  22. constexpr inline size_t ArgbDataSize(int height, int stride)
  23. {
  24. return static_cast<size_t>(height) * stride * 4;
  25. }
  26. /*
  27. 自定义的视频帧缓冲区管理类
  28. */
  29. class ArgbBuffer : public webrtc::VideoFrameBuffer {
  30. public:
  31. // Create a new buffer.
  32. /*
  33. 静态方法用于创建一个新的 ArgbBuffer实例
  34. 该方法接收视频帧的宽度和高度,并计算出所需的缓冲区大小
  35. 返回类型是rtc::scoped_refptr<ArgbBuffer>,它是一个智能指针,用于管理 ArgbBuffer 的生命周期
  36. */
  37. static inline rtc::scoped_refptr<ArgbBuffer> Create(int width, int height) {
  38. return new rtc::RefCountedObject<ArgbBuffer>(width, height, width * 4);
  39. }
  40. //// Create a new buffer and copy the pixel data.
  41. // static rtc::scoped_refptr<ArgbBuffer> Copy(const I010BufferInterface&
  42. // buffer);
  43. //// Convert and put I420 buffer into a new buffer.
  44. // static rtc::scoped_refptr<ArgbBuffer> Copy(const I420BufferInterface&
  45. // buffer);
  46. //// Return a rotated copy of |src|.
  47. // static rtc::scoped_refptr<ArgbBuffer> Rotate(const I010BufferInterface&
  48. // src,
  49. // VideoRotation rotation);
  50. // VideoFrameBuffer implementation.
  51. inline Type type() const override { return VideoFrameBuffer::Type::kNative; }
  52. inline int width() const override { return width_; }
  53. inline int height() const override { return height_; }
  54. rtc::scoped_refptr<webrtc::I420BufferInterface> ToI420() override;
  55. inline uint8_t* Data() { return data_.get(); }
  56. inline const uint8_t* Data() const { return data_.get(); }
  57. inline int Stride() const { return stride_; }
  58. inline constexpr size_t Size() const {
  59. return ArgbDataSize(height_, stride_);
  60. }
  61. protected:
  62. ArgbBuffer(int width, int height, int stride) ;
  63. ~ArgbBuffer() override = default;
  64. private:
  65. const int width_;
  66. const int height_;
  67. const int stride_;
  68. //指向视频数据的智能指针,使用 webrtc::AlignedFreeDeleter 来管理内存释放
  69. const std::unique_ptr<uint8_t, webrtc::AlignedFreeDeleter> data_;
  70. };
  71. /*
  72. 管理视频帧的接口类
  73. */
  74. /// Video frame observer to get notified of newly available video frames.
  75. class VideoFrameObserver : public rtc::VideoSinkInterface<webrtc::VideoFrame> {
  76. public:
  77. /// Register a callback to get notified on frame available,
  78. /// and received that frame as a I420-encoded buffer.
  79. /// This is not exclusive and can be used along another ARGB callback.
  80. void SetCallback(I420FrameReadyCallback callback) ;
  81. /// Register a callback to get notified on frame available,
  82. /// and received that frame as a raw decoded ARGB buffer.
  83. /// This is not exclusive and can be used along another I420 callback.
  84. void SetCallback(ARGBFrameReadyCallback callback) ;
  85. protected:
  86. //获取一个足够容纳指定大小图像数据的缓冲区(ArgbBuffer),如果已有的缓冲区足够大,则直接返回;否则,创建一个新的缓冲区并返回
  87. ArgbBuffer* GetArgbScratchBuffer(int width, int height);
  88. // VideoSinkInterface interface
  89. void OnFrame(const webrtc::VideoFrame& frame) override;
  90. private:
  91. /// Registered callback for receiving I420-encoded frame.
  92. I420FrameReadyCallback i420_callback_;
  93. /// Registered callback for receiving raw decoded ARGB frame.
  94. ARGBFrameReadyCallback argb_callback_;
  95. /// Mutex protecting all callbacks.
  96. std::mutex mutex_;
  97. /// Reusable ARGB scratch buffer to avoid per-frame allocation.
  98. //定义一个名为argb_scratch_buffer_ 的ArgbBuffer类型的智能指针,指向ARGB暂存缓冲区
  99. rtc::scoped_refptr<ArgbBuffer> argb_scratch_buffer_;
  100. };