VideoFrameAdapter.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #pragma once
  2. #include <vector>
  3. #include <api/video/video_frame.h>
  4. #include "VideoFrame.h"
  5. namespace unity
  6. {
  7. namespace webrtc
  8. {
  9. using namespace ::webrtc;
  10. class ScalableBufferInterface : public VideoFrameBuffer
  11. {
  12. public:
  13. virtual bool scaled() const = 0;
  14. protected:
  15. ~ScalableBufferInterface() override { }
  16. };
  17. class VideoFrameAdapter : public ScalableBufferInterface
  18. {
  19. public:
  20. class ScaledBuffer : public ScalableBufferInterface
  21. {
  22. public:
  23. ScaledBuffer(rtc::scoped_refptr<VideoFrameAdapter> parent, int width, int height);
  24. ~ScaledBuffer() override;
  25. VideoFrameBuffer::Type type() const override;
  26. int width() const override { return width_; }
  27. int height() const override { return height_; }
  28. bool scaled() const final { return true; }
  29. rtc::scoped_refptr<webrtc::I420BufferInterface> ToI420() override;
  30. const webrtc::I420BufferInterface* GetI420() const override;
  31. rtc::scoped_refptr<webrtc::VideoFrameBuffer>
  32. GetMappedFrameBuffer(rtc::ArrayView<webrtc::VideoFrameBuffer::Type> types) override;
  33. rtc::scoped_refptr<VideoFrame> GetVideoFrame() const { return parent_->frame_; }
  34. rtc::scoped_refptr<webrtc::VideoFrameBuffer> CropAndScale(
  35. int offset_x, int offset_y, int crop_width, int crop_height, int scaled_width, int scaled_height)
  36. override;
  37. private:
  38. const rtc::scoped_refptr<VideoFrameAdapter> parent_;
  39. const int width_;
  40. const int height_;
  41. };
  42. explicit VideoFrameAdapter(rtc::scoped_refptr<VideoFrame> frame);
  43. static ::webrtc::VideoFrame CreateVideoFrame(rtc::scoped_refptr<VideoFrame> frame);
  44. rtc::scoped_refptr<VideoFrame> GetVideoFrame() const { return frame_; }
  45. VideoFrameBuffer::Type type() const override;
  46. int width() const override { return size_.width(); }
  47. int height() const override { return size_.height(); }
  48. bool scaled() const override { return false; }
  49. const I420BufferInterface* GetI420() const override;
  50. rtc::scoped_refptr<I420BufferInterface> ToI420() override;
  51. rtc::scoped_refptr<webrtc::VideoFrameBuffer> CropAndScale(
  52. int offset_x, int offset_y, int crop_width, int crop_height, int scaled_width, int scaled_height) override;
  53. protected:
  54. ~VideoFrameAdapter() override { }
  55. private:
  56. rtc::scoped_refptr<webrtc::VideoFrameBuffer> GetOrCreateFrameBufferForSize(const Size& size);
  57. rtc::scoped_refptr<I420BufferInterface>
  58. ConvertToVideoFrameBuffer(rtc::scoped_refptr<VideoFrame> video_frame) const;
  59. // todo(kazuki):
  60. // Need this buffer because the type() method returns kI420.
  61. mutable rtc::scoped_refptr<I420BufferInterface> i420Buffer_;
  62. std::vector<rtc::scoped_refptr<VideoFrameBuffer>> scaledI40Buffers_;
  63. const rtc::scoped_refptr<VideoFrame> frame_;
  64. const Size size_;
  65. mutable std::mutex scaleLock_;
  66. mutable std::mutex convertLock_;
  67. };
  68. }
  69. }