fake_periodic_video_source.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright 2018 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef PC_TEST_FAKE_PERIODIC_VIDEO_SOURCE_H_
  11. #define PC_TEST_FAKE_PERIODIC_VIDEO_SOURCE_H_
  12. #include <memory>
  13. #include "api/video/video_source_interface.h"
  14. #include "media/base/fake_frame_source.h"
  15. #include "media/base/video_broadcaster.h"
  16. #include "rtc_base/synchronization/mutex.h"
  17. #include "rtc_base/task_queue_for_test.h"
  18. #include "rtc_base/task_utils/repeating_task.h"
  19. namespace webrtc {
  20. class FakePeriodicVideoSource final
  21. : public rtc::VideoSourceInterface<VideoFrame> {
  22. public:
  23. static constexpr int kDefaultFrameIntervalMs = 33;
  24. static constexpr int kDefaultWidth = 640;
  25. static constexpr int kDefaultHeight = 480;
  26. struct Config {
  27. int width = kDefaultWidth;
  28. int height = kDefaultHeight;
  29. int frame_interval_ms = kDefaultFrameIntervalMs;
  30. VideoRotation rotation = kVideoRotation_0;
  31. int64_t timestamp_offset_ms = 0;
  32. };
  33. FakePeriodicVideoSource() : FakePeriodicVideoSource(Config()) {}
  34. explicit FakePeriodicVideoSource(Config config)
  35. : frame_source_(
  36. config.width,
  37. config.height,
  38. config.frame_interval_ms * rtc::kNumMicrosecsPerMillisec,
  39. config.timestamp_offset_ms * rtc::kNumMicrosecsPerMillisec),
  40. task_queue_(std::make_unique<TaskQueueForTest>(
  41. "FakePeriodicVideoTrackSource")) {
  42. thread_checker_.Detach();
  43. frame_source_.SetRotation(config.rotation);
  44. TimeDelta frame_interval = TimeDelta::Millis(config.frame_interval_ms);
  45. RepeatingTaskHandle::Start(task_queue_->Get(), [this, frame_interval] {
  46. if (broadcaster_.wants().rotation_applied) {
  47. broadcaster_.OnFrame(frame_source_.GetFrameRotationApplied());
  48. } else {
  49. broadcaster_.OnFrame(frame_source_.GetFrame());
  50. }
  51. return frame_interval;
  52. });
  53. }
  54. rtc::VideoSinkWants wants() const {
  55. MutexLock lock(&mutex_);
  56. return wants_;
  57. }
  58. void RemoveSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override {
  59. RTC_DCHECK(thread_checker_.IsCurrent());
  60. broadcaster_.RemoveSink(sink);
  61. }
  62. void AddOrUpdateSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink,
  63. const rtc::VideoSinkWants& wants) override {
  64. RTC_DCHECK(thread_checker_.IsCurrent());
  65. {
  66. MutexLock lock(&mutex_);
  67. wants_ = wants;
  68. }
  69. broadcaster_.AddOrUpdateSink(sink, wants);
  70. }
  71. void Stop() {
  72. RTC_DCHECK(task_queue_);
  73. task_queue_.reset();
  74. }
  75. private:
  76. rtc::ThreadChecker thread_checker_;
  77. rtc::VideoBroadcaster broadcaster_;
  78. cricket::FakeFrameSource frame_source_;
  79. mutable Mutex mutex_;
  80. rtc::VideoSinkWants wants_ RTC_GUARDED_BY(&mutex_);
  81. std::unique_ptr<TaskQueueForTest> task_queue_;
  82. };
  83. } // namespace webrtc
  84. #endif // PC_TEST_FAKE_PERIODIC_VIDEO_SOURCE_H_