video_frame_observer.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #pragma once
  2. #include "api/video/video_frame.h"
  3. #include "api/video/video_sink_interface.h"
  4. #include "callback.h"
  5. using VideoFrameReadyCallback = Callback<const webrtc::VideoFrame&>;
  6. using ARGBFrameReadyCallback = Callback<
  7. const uint8_t*,int,
  8. const uint8_t*,int,
  9. const uint8_t*, int,
  10. const int,const int,
  11. const int>;
  12. constexpr inline size_t ArgbDataSize(int height, int stride) {
  13. return static_cast<size_t>(height) * stride * 4;
  14. }
  15. class ArgbBuffer : public webrtc::VideoFrameBuffer {
  16. public:
  17. // Create a new buffer.
  18. static inline rtc::scoped_refptr<ArgbBuffer> Create(int width, int height) {
  19. return new rtc::RefCountedObject<ArgbBuffer>(width, height, width * 4);
  20. }
  21. //// Create a new buffer and copy the pixel data.
  22. // static rtc::scoped_refptr<ArgbBuffer> Copy(const I010BufferInterface&
  23. // buffer);
  24. //// Convert and put I420 buffer into a new buffer.
  25. // static rtc::scoped_refptr<ArgbBuffer> Copy(const I420BufferInterface&
  26. // buffer);
  27. //// Return a rotated copy of |src|.
  28. // static rtc::scoped_refptr<ArgbBuffer> Rotate(const I010BufferInterface&
  29. // src,
  30. // VideoRotation rotation);
  31. // VideoFrameBuffer implementation.
  32. inline Type type() const override { return VideoFrameBuffer::Type::kNative; }
  33. inline int width() const override { return width_; }
  34. inline int height() const override { return height_; }
  35. rtc::scoped_refptr<webrtc::I420BufferInterface> ToI420() override;
  36. inline uint8_t* Data() { return data_.get(); }
  37. inline const uint8_t* Data() const { return data_.get(); }
  38. inline int Stride() const { return stride_; }
  39. inline constexpr size_t Size() const {
  40. return ArgbDataSize(height_, stride_);
  41. }
  42. protected:
  43. ArgbBuffer(int width, int height, int stride) ;
  44. ~ArgbBuffer() override = default;
  45. private:
  46. const int width_;
  47. const int height_;
  48. const int stride_;
  49. const std::unique_ptr<uint8_t, webrtc::AlignedFreeDeleter> data_;
  50. };
  51. /// Video frame observer to get notified of newly available video frames.
  52. class VideoFrameObserver : public rtc::VideoSinkInterface<webrtc::VideoFrame> {
  53. public:
  54. /// Register a callback to get notified on frame available,
  55. /// and received that frame as a I420-encoded buffer.
  56. /// This is not exclusive and can be used along another ARGB callback.
  57. void SetCallback(VideoFrameReadyCallback callback) ;
  58. /// Register a callback to get notified on frame available,
  59. /// and received that frame as a raw decoded ARGB buffer.
  60. /// This is not exclusive and can be used along another I420 callback.
  61. //void SetCallback(ARGBFrameReadyCallback callback) ;
  62. protected:
  63. ArgbBuffer* GetArgbScratchBuffer(int width, int height);
  64. // VideoSinkInterface interface
  65. void OnFrame(const webrtc::VideoFrame& frame) override;
  66. private:
  67. /// Registered callback for receiving I420-encoded frame.
  68. VideoFrameReadyCallback frame_callback_;
  69. /// Registered callback for receiving raw decoded ARGB frame.
  70. //ARGBFrameReadyCallback argb_callback_;
  71. /// Mutex protecting all callbacks.
  72. std::mutex mutex_;
  73. /// Reusable ARGB scratch buffer to avoid per-frame allocation.
  74. rtc::scoped_refptr<ArgbBuffer> argb_scratch_buffer_;
  75. };