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 | |
16 #include "webrtc/base/criticalsection.h" | 13 #include "webrtc/base/criticalsection.h" |
17 #include "webrtc/base/platform_thread.h" | 14 #include "webrtc/base/platform_thread.h" |
18 #include "webrtc/base/task_queue.h" | |
19 #include "webrtc/system_wrappers/include/clock.h" | 15 #include "webrtc/system_wrappers/include/clock.h" |
| 16 #include "webrtc/system_wrappers/include/event_wrapper.h" |
20 #include "webrtc/system_wrappers/include/sleep.h" | 17 #include "webrtc/system_wrappers/include/sleep.h" |
21 #include "webrtc/test/frame_generator.h" | 18 #include "webrtc/test/frame_generator.h" |
22 #include "webrtc/video_send_stream.h" | 19 #include "webrtc/video_send_stream.h" |
23 | 20 |
24 namespace webrtc { | 21 namespace webrtc { |
25 namespace test { | 22 namespace test { |
26 | 23 |
27 class FrameGeneratorCapturer::InsertFrameTask : public rtc::QueuedTask { | |
28 public: | |
29 // Repeats in |repeat_interval_ms|. One-time if |repeat_interval_ms| == 0. | |
30 InsertFrameTask( | |
31 webrtc::test::FrameGeneratorCapturer* frame_generator_capturer, | |
32 uint32_t repeat_interval_ms) | |
33 : frame_generator_capturer_(frame_generator_capturer), | |
34 repeat_interval_ms_(repeat_interval_ms) {} | |
35 | |
36 private: | |
37 bool Run() override { | |
38 if (repeat_interval_ms_ > 0) { | |
39 rtc::TaskQueue::Current()->PostDelayedTask( | |
40 std::unique_ptr<rtc::QueuedTask>(this), | |
41 repeat_interval_ms_); | |
42 } | |
43 frame_generator_capturer_->InsertFrame(); | |
44 // Task should be deleted only if it's not repeating. | |
45 return repeat_interval_ms_ == 0; | |
46 } | |
47 | |
48 webrtc::test::FrameGeneratorCapturer* const frame_generator_capturer_; | |
49 const uint32_t repeat_interval_ms_; | |
50 }; | |
51 | |
52 FrameGeneratorCapturer* FrameGeneratorCapturer::Create(int width, | 24 FrameGeneratorCapturer* FrameGeneratorCapturer::Create(int width, |
53 int height, | 25 int height, |
54 int target_fps, | 26 int target_fps, |
55 Clock* clock) { | 27 Clock* clock) { |
56 FrameGeneratorCapturer* capturer = new FrameGeneratorCapturer( | 28 FrameGeneratorCapturer* capturer = new FrameGeneratorCapturer( |
57 clock, FrameGenerator::CreateSquareGenerator(width, height), target_fps); | 29 clock, FrameGenerator::CreateSquareGenerator(width, height), target_fps); |
58 if (!capturer->Init()) { | 30 if (!capturer->Init()) { |
59 delete capturer; | 31 delete capturer; |
60 return NULL; | 32 return NULL; |
61 } | 33 } |
(...skipping 20 matching lines...) Expand all Loading... |
82 } | 54 } |
83 | 55 |
84 FrameGeneratorCapturer::FrameGeneratorCapturer( | 56 FrameGeneratorCapturer::FrameGeneratorCapturer( |
85 Clock* clock, | 57 Clock* clock, |
86 std::unique_ptr<FrameGenerator> frame_generator, | 58 std::unique_ptr<FrameGenerator> frame_generator, |
87 int target_fps) | 59 int target_fps) |
88 : clock_(clock), | 60 : clock_(clock), |
89 sending_(false), | 61 sending_(false), |
90 sink_(nullptr), | 62 sink_(nullptr), |
91 sink_wants_observer_(nullptr), | 63 sink_wants_observer_(nullptr), |
92 task_queue_("FrameGeneratorQueue", rtc::TaskQueue::Priority::HIGH), | 64 tick_(EventTimerWrapper::Create()), |
| 65 thread_(FrameGeneratorCapturer::Run, this, "FrameGeneratorCapturer"), |
93 frame_generator_(std::move(frame_generator)), | 66 frame_generator_(std::move(frame_generator)), |
94 target_fps_(target_fps), | 67 target_fps_(target_fps), |
95 first_frame_capture_time_(-1) { | 68 first_frame_capture_time_(-1) { |
96 RTC_DCHECK(frame_generator_); | 69 RTC_DCHECK(frame_generator_); |
97 RTC_DCHECK_GT(target_fps, 0); | 70 RTC_DCHECK_GT(target_fps, 0); |
98 } | 71 } |
99 | 72 |
100 FrameGeneratorCapturer::~FrameGeneratorCapturer() { | 73 FrameGeneratorCapturer::~FrameGeneratorCapturer() { |
101 Stop(); | 74 Stop(); |
| 75 |
| 76 thread_.Stop(); |
102 } | 77 } |
103 | 78 |
104 void FrameGeneratorCapturer::SetFakeRotation(VideoRotation rotation) { | 79 void FrameGeneratorCapturer::SetFakeRotation(VideoRotation rotation) { |
105 rtc::CritScope cs(&lock_); | 80 rtc::CritScope cs(&lock_); |
106 fake_rotation_ = rotation; | 81 fake_rotation_ = rotation; |
107 } | 82 } |
108 | 83 |
109 bool FrameGeneratorCapturer::Init() { | 84 bool FrameGeneratorCapturer::Init() { |
110 // This check is added because frame_generator_ might be file based and should | 85 // This check is added because frame_generator_ might be file based and should |
111 // not crash because a file moved. | 86 // not crash because a file moved. |
112 if (frame_generator_.get() == NULL) | 87 if (frame_generator_.get() == NULL) |
113 return false; | 88 return false; |
114 | 89 |
115 task_queue_.PostDelayedTask( | 90 if (!tick_->StartTimer(true, 1000 / target_fps_)) |
116 std::unique_ptr<rtc::QueuedTask>( | 91 return false; |
117 new InsertFrameTask(this, 1000 / target_fps_)), | 92 thread_.Start(); |
118 1000 / target_fps_); | 93 thread_.SetPriority(rtc::kHighPriority); |
119 | |
120 return true; | 94 return true; |
121 } | 95 } |
122 | 96 |
| 97 bool FrameGeneratorCapturer::Run(void* obj) { |
| 98 static_cast<FrameGeneratorCapturer*>(obj)->InsertFrame(); |
| 99 return true; |
| 100 } |
| 101 |
123 void FrameGeneratorCapturer::InsertFrame() { | 102 void FrameGeneratorCapturer::InsertFrame() { |
124 { | 103 { |
125 rtc::CritScope cs(&lock_); | 104 rtc::CritScope cs(&lock_); |
126 if (sending_) { | 105 if (sending_) { |
127 VideoFrame* frame = frame_generator_->NextFrame(); | 106 VideoFrame* frame = frame_generator_->NextFrame(); |
128 frame->set_ntp_time_ms(clock_->CurrentNtpInMilliseconds()); | 107 frame->set_ntp_time_ms(clock_->CurrentNtpInMilliseconds()); |
129 frame->set_rotation(fake_rotation_); | 108 frame->set_rotation(fake_rotation_); |
130 if (first_frame_capture_time_ == -1) { | 109 if (first_frame_capture_time_ == -1) { |
131 first_frame_capture_time_ = frame->ntp_time_ms(); | 110 first_frame_capture_time_ = frame->ntp_time_ms(); |
132 } | 111 } |
133 if (sink_) | 112 if (sink_) |
134 sink_->OnFrame(*frame); | 113 sink_->OnFrame(*frame); |
135 } | 114 } |
136 } | 115 } |
| 116 tick_->Wait(WEBRTC_EVENT_INFINITE); |
137 } | 117 } |
138 | 118 |
139 void FrameGeneratorCapturer::Start() { | 119 void FrameGeneratorCapturer::Start() { |
140 rtc::CritScope cs(&lock_); | 120 rtc::CritScope cs(&lock_); |
141 sending_ = true; | 121 sending_ = true; |
142 } | 122 } |
143 | 123 |
144 void FrameGeneratorCapturer::Stop() { | 124 void FrameGeneratorCapturer::Stop() { |
145 rtc::CritScope cs(&lock_); | 125 rtc::CritScope cs(&lock_); |
146 sending_ = false; | 126 sending_ = false; |
(...skipping 21 matching lines...) Expand all Loading... |
168 } | 148 } |
169 | 149 |
170 void FrameGeneratorCapturer::RemoveSink( | 150 void FrameGeneratorCapturer::RemoveSink( |
171 rtc::VideoSinkInterface<VideoFrame>* sink) { | 151 rtc::VideoSinkInterface<VideoFrame>* sink) { |
172 rtc::CritScope cs(&lock_); | 152 rtc::CritScope cs(&lock_); |
173 RTC_CHECK(sink_ == sink); | 153 RTC_CHECK(sink_ == sink); |
174 sink_ = nullptr; | 154 sink_ = nullptr; |
175 } | 155 } |
176 | 156 |
177 void FrameGeneratorCapturer::ForceFrame() { | 157 void FrameGeneratorCapturer::ForceFrame() { |
178 // One-time non-repeating task, | 158 tick_->Set(); |
179 // therefore repeat_interval_ms is 0 in InsertFrameTask() | |
180 task_queue_.PostTask( | |
181 std::unique_ptr<rtc::QueuedTask>(new InsertFrameTask(this, 0))); | |
182 } | 159 } |
183 | 160 } // test |
184 } // namespace test | 161 } // webrtc |
185 } // namespace webrtc | |
OLD | NEW |