| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/test/frame_generator_capturer.h" | 11 #include "webrtc/test/frame_generator_capturer.h" |
| 12 | 12 |
| 13 #include <utility> |
| 14 #include <vector> |
| 15 |
| 13 #include "webrtc/base/criticalsection.h" | 16 #include "webrtc/base/criticalsection.h" |
| 17 #include "webrtc/base/logging.h" |
| 14 #include "webrtc/base/platform_thread.h" | 18 #include "webrtc/base/platform_thread.h" |
| 19 #include "webrtc/base/task_queue.h" |
| 15 #include "webrtc/system_wrappers/include/clock.h" | 20 #include "webrtc/system_wrappers/include/clock.h" |
| 16 #include "webrtc/system_wrappers/include/event_wrapper.h" | |
| 17 #include "webrtc/system_wrappers/include/sleep.h" | 21 #include "webrtc/system_wrappers/include/sleep.h" |
| 18 #include "webrtc/test/frame_generator.h" | 22 #include "webrtc/test/frame_generator.h" |
| 19 #include "webrtc/video_send_stream.h" | 23 #include "webrtc/video_send_stream.h" |
| 20 | 24 |
| 21 namespace webrtc { | 25 namespace webrtc { |
| 22 namespace test { | 26 namespace test { |
| 23 | 27 |
| 28 class FrameGeneratorCapturer::InsertFrameTask : public rtc::QueuedTask { |
| 29 public: |
| 30 // Repeats in |repeat_interval_ms|. One-time if |repeat_interval_ms| == 0. |
| 31 InsertFrameTask( |
| 32 webrtc::test::FrameGeneratorCapturer* frame_generator_capturer, |
| 33 uint32_t repeat_interval_ms) |
| 34 : frame_generator_capturer_(frame_generator_capturer), |
| 35 repeat_interval_ms_(repeat_interval_ms), |
| 36 intended_run_time_ms_(-1) {} |
| 37 |
| 38 private: |
| 39 bool Run() override { |
| 40 if (repeat_interval_ms_ > 0) { |
| 41 int64_t delay_ms; |
| 42 int64_t time_now_ms = rtc::TimeMillis(); |
| 43 if (intended_run_time_ms_ > 0) { |
| 44 delay_ms = time_now_ms - intended_run_time_ms_; |
| 45 } else { |
| 46 delay_ms = 0; |
| 47 intended_run_time_ms_ = time_now_ms; |
| 48 } |
| 49 intended_run_time_ms_ += repeat_interval_ms_; |
| 50 if (delay_ms < repeat_interval_ms_) { |
| 51 rtc::TaskQueue::Current()->PostDelayedTask( |
| 52 std::unique_ptr<rtc::QueuedTask>(this), |
| 53 repeat_interval_ms_ - delay_ms); |
| 54 } else { |
| 55 rtc::TaskQueue::Current()->PostDelayedTask( |
| 56 std::unique_ptr<rtc::QueuedTask>(this), 0); |
| 57 LOG(LS_ERROR) |
| 58 << "Frame Generator Capturer can't keep up with requested fps"; |
| 59 } |
| 60 } |
| 61 frame_generator_capturer_->InsertFrame(); |
| 62 // Task should be deleted only if it's not repeating. |
| 63 return repeat_interval_ms_ == 0; |
| 64 } |
| 65 |
| 66 webrtc::test::FrameGeneratorCapturer* const frame_generator_capturer_; |
| 67 const uint32_t repeat_interval_ms_; |
| 68 int64_t intended_run_time_ms_; |
| 69 }; |
| 70 |
| 24 FrameGeneratorCapturer* FrameGeneratorCapturer::Create(int width, | 71 FrameGeneratorCapturer* FrameGeneratorCapturer::Create(int width, |
| 25 int height, | 72 int height, |
| 26 int target_fps, | 73 int target_fps, |
| 27 Clock* clock) { | 74 Clock* clock) { |
| 28 FrameGeneratorCapturer* capturer = new FrameGeneratorCapturer( | 75 FrameGeneratorCapturer* capturer = new FrameGeneratorCapturer( |
| 29 clock, FrameGenerator::CreateSquareGenerator(width, height), target_fps); | 76 clock, FrameGenerator::CreateSquareGenerator(width, height), target_fps); |
| 30 if (!capturer->Init()) { | 77 if (!capturer->Init()) { |
| 31 delete capturer; | 78 delete capturer; |
| 32 return NULL; | 79 return NULL; |
| 33 } | 80 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 54 } | 101 } |
| 55 | 102 |
| 56 FrameGeneratorCapturer::FrameGeneratorCapturer( | 103 FrameGeneratorCapturer::FrameGeneratorCapturer( |
| 57 Clock* clock, | 104 Clock* clock, |
| 58 std::unique_ptr<FrameGenerator> frame_generator, | 105 std::unique_ptr<FrameGenerator> frame_generator, |
| 59 int target_fps) | 106 int target_fps) |
| 60 : clock_(clock), | 107 : clock_(clock), |
| 61 sending_(false), | 108 sending_(false), |
| 62 sink_(nullptr), | 109 sink_(nullptr), |
| 63 sink_wants_observer_(nullptr), | 110 sink_wants_observer_(nullptr), |
| 64 tick_(EventTimerWrapper::Create()), | |
| 65 thread_(FrameGeneratorCapturer::Run, this, "FrameGeneratorCapturer"), | |
| 66 frame_generator_(std::move(frame_generator)), | 111 frame_generator_(std::move(frame_generator)), |
| 67 target_fps_(target_fps), | 112 target_fps_(target_fps), |
| 68 first_frame_capture_time_(-1) { | 113 first_frame_capture_time_(-1), |
| 114 task_queue_("FrameGenCapQ", |
| 115 rtc::TaskQueue::Priority::HIGH) { |
| 69 RTC_DCHECK(frame_generator_); | 116 RTC_DCHECK(frame_generator_); |
| 70 RTC_DCHECK_GT(target_fps, 0); | 117 RTC_DCHECK_GT(target_fps, 0); |
| 71 } | 118 } |
| 72 | 119 |
| 73 FrameGeneratorCapturer::~FrameGeneratorCapturer() { | 120 FrameGeneratorCapturer::~FrameGeneratorCapturer() { |
| 74 Stop(); | 121 Stop(); |
| 75 | |
| 76 thread_.Stop(); | |
| 77 } | 122 } |
| 78 | 123 |
| 79 void FrameGeneratorCapturer::SetFakeRotation(VideoRotation rotation) { | 124 void FrameGeneratorCapturer::SetFakeRotation(VideoRotation rotation) { |
| 80 rtc::CritScope cs(&lock_); | 125 rtc::CritScope cs(&lock_); |
| 81 fake_rotation_ = rotation; | 126 fake_rotation_ = rotation; |
| 82 } | 127 } |
| 83 | 128 |
| 84 bool FrameGeneratorCapturer::Init() { | 129 bool FrameGeneratorCapturer::Init() { |
| 85 // This check is added because frame_generator_ might be file based and should | 130 // This check is added because frame_generator_ might be file based and should |
| 86 // not crash because a file moved. | 131 // not crash because a file moved. |
| 87 if (frame_generator_.get() == NULL) | 132 if (frame_generator_.get() == NULL) |
| 88 return false; | 133 return false; |
| 89 | 134 |
| 90 if (!tick_->StartTimer(true, 1000 / target_fps_)) | 135 task_queue_.PostDelayedTask( |
| 91 return false; | 136 std::unique_ptr<rtc::QueuedTask>( |
| 92 thread_.Start(); | 137 new InsertFrameTask(this, 1000 / target_fps_)), |
| 93 thread_.SetPriority(rtc::kHighPriority); | 138 1000 / target_fps_); |
| 139 |
| 94 return true; | 140 return true; |
| 95 } | 141 } |
| 96 | 142 |
| 97 bool FrameGeneratorCapturer::Run(void* obj) { | |
| 98 static_cast<FrameGeneratorCapturer*>(obj)->InsertFrame(); | |
| 99 return true; | |
| 100 } | |
| 101 | |
| 102 void FrameGeneratorCapturer::InsertFrame() { | 143 void FrameGeneratorCapturer::InsertFrame() { |
| 103 { | 144 { |
| 104 rtc::CritScope cs(&lock_); | 145 rtc::CritScope cs(&lock_); |
| 105 if (sending_) { | 146 if (sending_) { |
| 106 VideoFrame* frame = frame_generator_->NextFrame(); | 147 VideoFrame* frame = frame_generator_->NextFrame(); |
| 107 frame->set_ntp_time_ms(clock_->CurrentNtpInMilliseconds()); | 148 frame->set_ntp_time_ms(clock_->CurrentNtpInMilliseconds()); |
| 108 frame->set_rotation(fake_rotation_); | 149 frame->set_rotation(fake_rotation_); |
| 109 if (first_frame_capture_time_ == -1) { | 150 if (first_frame_capture_time_ == -1) { |
| 110 first_frame_capture_time_ = frame->ntp_time_ms(); | 151 first_frame_capture_time_ = frame->ntp_time_ms(); |
| 111 } | 152 } |
| 112 if (sink_) | 153 if (sink_) |
| 113 sink_->OnFrame(*frame); | 154 sink_->OnFrame(*frame); |
| 114 } | 155 } |
| 115 } | 156 } |
| 116 tick_->Wait(WEBRTC_EVENT_INFINITE); | |
| 117 } | 157 } |
| 118 | 158 |
| 119 void FrameGeneratorCapturer::Start() { | 159 void FrameGeneratorCapturer::Start() { |
| 120 rtc::CritScope cs(&lock_); | 160 rtc::CritScope cs(&lock_); |
| 121 sending_ = true; | 161 sending_ = true; |
| 122 } | 162 } |
| 123 | 163 |
| 124 void FrameGeneratorCapturer::Stop() { | 164 void FrameGeneratorCapturer::Stop() { |
| 125 rtc::CritScope cs(&lock_); | 165 rtc::CritScope cs(&lock_); |
| 126 sending_ = false; | 166 sending_ = false; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 148 } | 188 } |
| 149 | 189 |
| 150 void FrameGeneratorCapturer::RemoveSink( | 190 void FrameGeneratorCapturer::RemoveSink( |
| 151 rtc::VideoSinkInterface<VideoFrame>* sink) { | 191 rtc::VideoSinkInterface<VideoFrame>* sink) { |
| 152 rtc::CritScope cs(&lock_); | 192 rtc::CritScope cs(&lock_); |
| 153 RTC_CHECK(sink_ == sink); | 193 RTC_CHECK(sink_ == sink); |
| 154 sink_ = nullptr; | 194 sink_ = nullptr; |
| 155 } | 195 } |
| 156 | 196 |
| 157 void FrameGeneratorCapturer::ForceFrame() { | 197 void FrameGeneratorCapturer::ForceFrame() { |
| 158 tick_->Set(); | 198 // One-time non-repeating task, |
| 199 // therefore repeat_interval_ms is 0 in InsertFrameTask() |
| 200 task_queue_.PostTask( |
| 201 std::unique_ptr<rtc::QueuedTask>(new InsertFrameTask(this, 0))); |
| 159 } | 202 } |
| 160 } // test | 203 |
| 161 } // webrtc | 204 } // namespace test |
| 205 } // namespace webrtc |
| OLD | NEW |