UnityVideoTrackSource.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #pragma once
  2. #include <mutex>
  3. #include <api/task_queue/task_queue_factory.h>
  4. #include <media/base/adapted_video_track_source.h>
  5. #include <rtc_base/task_queue.h>
  6. #include <absl/types/optional.h>
  7. #include <api/media_stream_interface.h>
  8. #include "VideoFrame.h"
  9. namespace unity
  10. {
  11. namespace webrtc
  12. {
  13. using namespace ::webrtc;
  14. // This class implements webrtc's VideoTrackSourceInterface. To pass frames down
  15. // the webrtc video pipeline, each received a media::VideoFrame is converted to
  16. // a webrtc::VideoFrame, taking any adaptation requested by downstream classes
  17. // into account.
  18. class VideoFrameScheduler;
  19. class UnityVideoTrackSource : public rtc::AdaptedVideoTrackSource
  20. {
  21. public:
  22. struct FrameAdaptationParams
  23. {
  24. bool should_drop_frame;
  25. int crop_x;
  26. int crop_y;
  27. int crop_width;
  28. int crop_height;
  29. int scale_to_width;
  30. int scale_to_height;
  31. };
  32. UnityVideoTrackSource(
  33. bool is_screencast, absl::optional<bool> needs_denoising, TaskQueueFactory* taskQueueFactory);
  34. ~UnityVideoTrackSource() override;
  35. // void SetState(SourceState state);
  36. SourceState state() const override;
  37. bool remote() const override;
  38. bool is_screencast() const override;
  39. absl::optional<bool> needs_denoising() const override;
  40. void OnFrameCaptured(rtc::scoped_refptr<VideoFrame> frame);
  41. using VideoTrackSourceInterface::AddOrUpdateSink;
  42. using VideoTrackSourceInterface::RemoveSink;
  43. static rtc::scoped_refptr<UnityVideoTrackSource>
  44. Create(bool is_screencast, absl::optional<bool> needs_denoising, TaskQueueFactory* taskQueueFactory);
  45. private:
  46. void CaptureNextFrame();
  47. void SendFeedback();
  48. FrameAdaptationParams ComputeAdaptationParams(int width,
  49. int height,
  50. int64_t time_us);
  51. // Delivers |frame| to base class method
  52. // rtc::AdaptedVideoTrackSource::OnFrame(). If the cropping (given via
  53. // |frame->visible_rect()|) has changed since the last delivered frame, the
  54. // whole frame is marked as updated.
  55. // void DeliverFrame(rtc::scoped_refptr<::webrtc::VideoFrame> frame,
  56. // gfx::Rect* update_rect,
  57. // int64_t timestamp_us);
  58. // |thread_checker_| is bound to the libjingle worker thread.
  59. // todo::(kazuki) change compiler vc to clang
  60. // media::VideoFramePool scaled_frame_pool_;
  61. // State for the timestamp translation.
  62. rtc::TimestampAligner timestamp_aligner_;
  63. const bool is_screencast_;
  64. const absl::optional<bool> needs_denoising_;
  65. std::mutex mutex_;
  66. std::unique_ptr<rtc::TaskQueue> taskQueue_;
  67. std::unique_ptr<VideoFrameScheduler> scheduler_;
  68. rtc::scoped_refptr<unity::webrtc::VideoFrame> frame_;
  69. };
  70. } // end namespace webrtc
  71. } // end namespace unity