video_frame_observer.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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(I420FrameReadyCallback callback) {
  27. std::lock_guard<std::mutex> lock{ mutex_ };
  28. i420_callback_ = std::move(callback);
  29. }
  30. void VideoFrameObserver::SetCallback(ARGBFrameReadyCallback callback) {
  31. std::lock_guard<std::mutex> lock{ mutex_ };
  32. argb_callback_ = std::move(callback);
  33. }
  34. ArgbBuffer* VideoFrameObserver::GetArgbScratchBuffer(int width, int height) {
  35. const size_t needed_size = ArgbDataSize(width, height);
  36. if (auto* buffer = argb_scratch_buffer_.get()) {
  37. if (buffer->Size() >= needed_size) {
  38. return buffer;
  39. }
  40. }
  41. argb_scratch_buffer_ = ArgbBuffer::Create(width, height);
  42. return argb_scratch_buffer_.get();
  43. }
  44. void VideoFrameObserver::OnFrame(const webrtc::VideoFrame& frame) {
  45. std::lock_guard<std::mutex> lock{ mutex_ };
  46. if (!i420_callback_ && !argb_callback_)
  47. return;
  48. rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer(
  49. frame.video_frame_buffer());
  50. const int width = frame.width();
  51. const int height = frame.height();
  52. if (buffer->type() != webrtc::VideoFrameBuffer::Type::kI420A) {
  53. // The buffer is not encoded in I420 with alpha channel; use I420 without
  54. // alpha channel as interchange format for the callback, and convert the
  55. // buffer to that (or do nothing if already in I420).
  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 (i420_callback_) {
  63. // i420_callback_(yptr, uptr, vptr, aptr, i420_buffer->StrideY(),
  64. // i420_buffer->StrideU(), i420_buffer->StrideV(), 0, width,
  65. // height);
  66. // }
  67. if (argb_callback_) {
  68. // ArgbBuffer* const argb_buffer = GetArgbScratchBuffer(width, height);
  69. // libyuv::I420ToARGB(yptr, i420_buffer->StrideY(), uptr,
  70. // i420_buffer->StrideU(), vptr, i420_buffer->StrideV(),
  71. // argb_buffer->Data(), argb_buffer->Stride(), width,
  72. // height);
  73. argb_callback_(yptr,i420_buffer->StrideY(),uptr,i420_buffer->StrideU(),vptr,i420_buffer->StrideV(), width*4, width, height);
  74. }
  75. }
  76. else {
  77. // The buffer is encoded in I420 with alpha channel, use it directly.
  78. const webrtc::I420ABufferInterface* i420a_buffer = buffer->GetI420A();
  79. const uint8_t* yptr = i420a_buffer->DataY();
  80. const uint8_t* uptr = i420a_buffer->DataU();
  81. const uint8_t* vptr = i420a_buffer->DataV();
  82. const uint8_t* aptr = i420a_buffer->DataA();
  83. // if (i420_callback_) {
  84. // i420_callback_(yptr, uptr, vptr, aptr, i420a_buffer->StrideY(),
  85. // i420a_buffer->StrideU(), i420a_buffer->StrideV(),
  86. // i420a_buffer->StrideA(), width, height);
  87. // }
  88. if (argb_callback_) {
  89. // ArgbBuffer* const argb_buffer = GetArgbScratchBuffer(width, height);
  90. // libyuv::I420AlphaToARGB(
  91. // yptr, i420a_buffer->StrideY(), uptr, i420a_buffer->StrideU(), vptr,
  92. // i420a_buffer->StrideV(), aptr, i420a_buffer->StrideA(),
  93. // argb_buffer->Data(), argb_buffer->Stride(), width, height, 0);
  94. argb_callback_(yptr, i420a_buffer->StrideY(), uptr, i420a_buffer->StrideU(), vptr, i420a_buffer->StrideV(), width * 4, width, height);
  95. }
  96. }
  97. }