video_frame_observer.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "pch.h"
  2. #include "../common/comm.h"
  3. #include "video_frame_observer.h"
  4. constexpr int kBufferAlignment = 64;
  5. ArgbBuffer::ArgbBuffer(int width, int height, int stride)
  6. : width_(width),
  7. height_(height),
  8. stride_(stride),
  9. data_(static_cast<uint8_t*>(
  10. webrtc::AlignedMalloc(ArgbDataSize(height, stride),
  11. kBufferAlignment))) {
  12. RTC_DCHECK_GT(width, 0);
  13. RTC_DCHECK_GT(height, 0);
  14. RTC_DCHECK_GE(stride, 4 * width);
  15. }
  16. rtc::scoped_refptr<webrtc::I420BufferInterface> ArgbBuffer::ToI420() {
  17. rtc::scoped_refptr<webrtc::I420Buffer> i420_buffer =
  18. webrtc::I420Buffer::Create(width_, height_, stride_, stride_ / 2,
  19. stride_ / 2);
  20. libyuv::ARGBToI420(Data(), Stride(), i420_buffer->MutableDataY(),
  21. i420_buffer->StrideY(), i420_buffer->MutableDataU(),
  22. i420_buffer->StrideU(), i420_buffer->MutableDataV(),
  23. i420_buffer->StrideV(), width_, height_);
  24. return i420_buffer;
  25. }
  26. void VideoFrameObserver::SetCallback(VideoFrameReadyCallback callback) {
  27. std::lock_guard<std::mutex> lock{ mutex_ };
  28. frame_callback_ = std::move(callback);
  29. }
  30. /*
  31. void VideoFrameObserver::SetCallback(ARGBFrameReadyCallback callback) {
  32. std::lock_guard<std::mutex> lock{ mutex_ };
  33. argb_callback_ = std::move(callback);
  34. }
  35. */
  36. ArgbBuffer* VideoFrameObserver::GetArgbScratchBuffer(int width, int height) {
  37. const size_t needed_size = ArgbDataSize(width, height);
  38. if (auto* buffer = argb_scratch_buffer_.get()) {
  39. if (buffer->Size() >= needed_size) {
  40. return buffer;
  41. }
  42. }
  43. argb_scratch_buffer_ = ArgbBuffer::Create(width, height);
  44. return argb_scratch_buffer_.get();
  45. }
  46. void VideoFrameObserver::OnFrame(const webrtc::VideoFrame& frame) {
  47. std::lock_guard<std::mutex> lock{ mutex_ };
  48. if (!frame_callback_)
  49. return;
  50. frame_callback_(frame);
  51. /*rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer(
  52. frame.video_frame_buffer());
  53. const int width = frame.width();
  54. const int height = frame.height();
  55. if (buffer->type() != webrtc::VideoFrameBuffer::Type::kI420A) {
  56. rtc::scoped_refptr<webrtc::I420BufferInterface> i420_buffer =
  57. buffer->ToI420();
  58. const uint8_t* yptr = i420_buffer->DataY();
  59. const uint8_t* uptr = i420_buffer->DataU();
  60. const uint8_t* vptr = i420_buffer->DataV();
  61. const uint8_t* aptr = nullptr;
  62. if (argb_callback_) {
  63. argb_callback_(yptr,i420_buffer->StrideY(),uptr,i420_buffer->StrideU(),vptr,i420_buffer->StrideV(), width*4, width, height);
  64. }
  65. }
  66. else {
  67. const webrtc::I420ABufferInterface* i420a_buffer = buffer->GetI420A();
  68. const uint8_t* yptr = i420a_buffer->DataY();
  69. const uint8_t* uptr = i420a_buffer->DataU();
  70. const uint8_t* vptr = i420a_buffer->DataV();
  71. const uint8_t* aptr = i420a_buffer->DataA();
  72. if (argb_callback_) {
  73. argb_callback_(yptr, i420a_buffer->StrideY(), uptr, i420a_buffer->StrideU(), vptr, i420a_buffer->StrideV(), width * 4, width, height);
  74. }
  75. }
  76. */
  77. }