UnityVideoTrackSource.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "pch.h"
  2. #include "UnityVideoTrackSource.h"
  3. #include "VideoFrameAdapter.h"
  4. #include "VideoFrameScheduler.h"
  5. namespace unity
  6. {
  7. namespace webrtc
  8. {
  9. rtc::scoped_refptr<UnityVideoTrackSource> UnityVideoTrackSource::Create(
  10. bool is_screencast, absl::optional<bool> needs_denoising, TaskQueueFactory* taskQueueFactory)
  11. {
  12. return new rtc::RefCountedObject<UnityVideoTrackSource>(is_screencast, needs_denoising, taskQueueFactory);
  13. }
  14. UnityVideoTrackSource::UnityVideoTrackSource(
  15. bool is_screencast, absl::optional<bool> needs_denoising, TaskQueueFactory* taskQueueFactory)
  16. : AdaptedVideoTrackSource(/*required_alignment=*/1)
  17. , is_screencast_(is_screencast)
  18. , frame_(nullptr)
  19. {
  20. taskQueue_ = std::make_unique<rtc::TaskQueue>(
  21. taskQueueFactory->CreateTaskQueue("VideoFrameScheduler", TaskQueueFactory::Priority::NORMAL));
  22. scheduler_ = std::make_unique<VideoFrameScheduler>(taskQueue_->Get());
  23. scheduler_->Start(std::bind(&UnityVideoTrackSource::CaptureNextFrame, this));
  24. }
  25. UnityVideoTrackSource::~UnityVideoTrackSource() { scheduler_ = nullptr; }
  26. UnityVideoTrackSource::FrameAdaptationParams
  27. UnityVideoTrackSource::ComputeAdaptationParams(int width, int height, int64_t time_us)
  28. {
  29. FrameAdaptationParams result { false, 0, 0, 0, 0, 0, 0 };
  30. result.should_drop_frame = !AdaptFrame(
  31. width,
  32. height,
  33. time_us,
  34. &result.scale_to_width,
  35. &result.scale_to_height,
  36. &result.crop_width,
  37. &result.crop_height,
  38. &result.crop_x,
  39. &result.crop_y);
  40. return result;
  41. }
  42. UnityVideoTrackSource::SourceState UnityVideoTrackSource::state() const { return kLive; }
  43. bool UnityVideoTrackSource::remote() const { return false; }
  44. bool UnityVideoTrackSource::is_screencast() const { return is_screencast_; }
  45. absl::optional<bool> UnityVideoTrackSource::needs_denoising() const { return needs_denoising_; }
  46. void UnityVideoTrackSource::CaptureNextFrame()
  47. {
  48. const std::unique_lock<std::mutex> lock(mutex_);
  49. if (!frame_)
  50. return;
  51. const int orig_width = frame_->size().width();
  52. const int orig_height = frame_->size().height();
  53. const int64_t now_us = rtc::TimeMicros();
  54. FrameAdaptationParams frame_adaptation_params =
  55. ComputeAdaptationParams(orig_width, orig_height, now_us);
  56. if (frame_adaptation_params.should_drop_frame)
  57. {
  58. frame_ = nullptr;
  59. return;
  60. }
  61. const webrtc::TimeDelta timestamp = frame_->timestamp();
  62. rtc::scoped_refptr<VideoFrameAdapter> frame_adapter(
  63. new rtc::RefCountedObject<VideoFrameAdapter>(std::move(frame_)));
  64. ::webrtc::VideoFrame::Builder builder = ::webrtc::VideoFrame::Builder()
  65. .set_video_frame_buffer(std::move(frame_adapter))
  66. .set_timestamp_us(timestamp.us());
  67. OnFrame(builder.build());
  68. }
  69. void UnityVideoTrackSource::SendFeedback()
  70. {
  71. float maxFramerate = video_adapter()->GetMaxFramerate();
  72. if (maxFramerate == std::numeric_limits<float>::infinity())
  73. return;
  74. scheduler_->SetMaxFramerateFps(static_cast<int>(maxFramerate));
  75. }
  76. void UnityVideoTrackSource::OnFrameCaptured(rtc::scoped_refptr<VideoFrame> frame)
  77. {
  78. SendFeedback();
  79. const std::unique_lock<std::mutex> lock(mutex_);
  80. frame_ = frame;
  81. }
  82. } // end namespace webrtc
  83. } // end namespace unity