forked from JumpingYang001/webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frame_generator_capturer.cc
262 lines (230 loc) · 8.14 KB
/
frame_generator_capturer.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
* Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "test/frame_generator_capturer.h"
#include <utility>
#include <vector>
#include "call/video_send_stream.h"
#include "rtc_base/criticalsection.h"
#include "rtc_base/logging.h"
#include "rtc_base/platform_thread.h"
#include "rtc_base/task_queue.h"
#include "rtc_base/timeutils.h"
#include "system_wrappers/include/clock.h"
namespace webrtc {
namespace test {
class FrameGeneratorCapturer::InsertFrameTask : public rtc::QueuedTask {
public:
// Repeats in |repeat_interval_ms|. One-time if |repeat_interval_ms| == 0.
InsertFrameTask(
webrtc::test::FrameGeneratorCapturer* frame_generator_capturer,
uint32_t repeat_interval_ms)
: frame_generator_capturer_(frame_generator_capturer),
repeat_interval_ms_(repeat_interval_ms),
intended_run_time_ms_(-1) {}
private:
bool Run() override {
bool task_completed = true;
if (repeat_interval_ms_ > 0) {
// This is not a one-off frame. Check if the frame interval for this
// task queue is the same same as the current configured frame rate.
uint32_t current_interval_ms =
1000 / frame_generator_capturer_->GetCurrentConfiguredFramerate();
if (repeat_interval_ms_ != current_interval_ms) {
// Frame rate has changed since task was started, create a new instance.
rtc::TaskQueue::Current()->PostDelayedTask(
std::unique_ptr<rtc::QueuedTask>(new InsertFrameTask(
frame_generator_capturer_, current_interval_ms)),
current_interval_ms);
} else {
// Schedule the next frame capture event to happen at approximately the
// correct absolute time point.
int64_t delay_ms;
int64_t time_now_ms = rtc::TimeMillis();
if (intended_run_time_ms_ > 0) {
delay_ms = time_now_ms - intended_run_time_ms_;
} else {
delay_ms = 0;
intended_run_time_ms_ = time_now_ms;
}
intended_run_time_ms_ += repeat_interval_ms_;
if (delay_ms < repeat_interval_ms_) {
rtc::TaskQueue::Current()->PostDelayedTask(
std::unique_ptr<rtc::QueuedTask>(this),
repeat_interval_ms_ - delay_ms);
} else {
rtc::TaskQueue::Current()->PostDelayedTask(
std::unique_ptr<rtc::QueuedTask>(this), 0);
RTC_LOG(LS_ERROR)
<< "Frame Generator Capturer can't keep up with requested fps";
}
// Repost of this instance, make sure it is not deleted.
task_completed = false;
}
}
frame_generator_capturer_->InsertFrame();
// Task should be deleted only if it's not repeating.
return task_completed;
}
webrtc::test::FrameGeneratorCapturer* const frame_generator_capturer_;
const uint32_t repeat_interval_ms_;
int64_t intended_run_time_ms_;
};
FrameGeneratorCapturer* FrameGeneratorCapturer::Create(
int width,
int height,
absl::optional<FrameGenerator::OutputType> type,
absl::optional<int> num_squares,
int target_fps,
Clock* clock) {
std::unique_ptr<FrameGeneratorCapturer> capturer(new FrameGeneratorCapturer(
clock,
FrameGenerator::CreateSquareGenerator(width, height, type, num_squares),
target_fps));
if (!capturer->Init())
return nullptr;
return capturer.release();
}
FrameGeneratorCapturer* FrameGeneratorCapturer::CreateFromYuvFile(
const std::string& file_name,
size_t width,
size_t height,
int target_fps,
Clock* clock) {
std::unique_ptr<FrameGeneratorCapturer> capturer(new FrameGeneratorCapturer(
clock,
FrameGenerator::CreateFromYuvFile(std::vector<std::string>(1, file_name),
width, height, 1),
target_fps));
if (!capturer->Init())
return nullptr;
return capturer.release();
}
FrameGeneratorCapturer* FrameGeneratorCapturer::CreateSlideGenerator(
int width,
int height,
int frame_repeat_count,
int target_fps,
Clock* clock) {
std::unique_ptr<FrameGeneratorCapturer> capturer(new FrameGeneratorCapturer(
clock,
FrameGenerator::CreateSlideGenerator(width, height, frame_repeat_count),
target_fps));
if (!capturer->Init())
return nullptr;
return capturer.release();
}
FrameGeneratorCapturer::FrameGeneratorCapturer(
Clock* clock,
std::unique_ptr<FrameGenerator> frame_generator,
int target_fps)
: clock_(clock),
sending_(false),
sink_(nullptr),
sink_wants_observer_(nullptr),
frame_generator_(std::move(frame_generator)),
target_fps_(target_fps),
first_frame_capture_time_(-1),
task_queue_("FrameGenCapQ", rtc::TaskQueue::Priority::HIGH) {
RTC_DCHECK(frame_generator_);
RTC_DCHECK_GT(target_fps, 0);
}
FrameGeneratorCapturer::~FrameGeneratorCapturer() {
Stop();
}
void FrameGeneratorCapturer::SetFakeRotation(VideoRotation rotation) {
rtc::CritScope cs(&lock_);
fake_rotation_ = rotation;
}
bool FrameGeneratorCapturer::Init() {
// This check is added because frame_generator_ might be file based and should
// not crash because a file moved.
if (frame_generator_.get() == nullptr)
return false;
int framerate_fps = GetCurrentConfiguredFramerate();
task_queue_.PostDelayedTask(
std::unique_ptr<rtc::QueuedTask>(
new InsertFrameTask(this, 1000 / framerate_fps)),
1000 / framerate_fps);
return true;
}
void FrameGeneratorCapturer::InsertFrame() {
rtc::CritScope cs(&lock_);
if (sending_) {
VideoFrame* frame = frame_generator_->NextFrame();
frame->set_timestamp_us(clock_->TimeInMicroseconds());
frame->set_ntp_time_ms(clock_->CurrentNtpInMilliseconds());
frame->set_rotation(fake_rotation_);
if (first_frame_capture_time_ == -1) {
first_frame_capture_time_ = frame->ntp_time_ms();
}
if (sink_) {
absl::optional<VideoFrame> out_frame = AdaptFrame(*frame);
if (out_frame)
sink_->OnFrame(*out_frame);
}
}
}
void FrameGeneratorCapturer::Start() {
rtc::CritScope cs(&lock_);
sending_ = true;
}
void FrameGeneratorCapturer::Stop() {
rtc::CritScope cs(&lock_);
sending_ = false;
}
void FrameGeneratorCapturer::ChangeResolution(size_t width, size_t height) {
rtc::CritScope cs(&lock_);
frame_generator_->ChangeResolution(width, height);
}
void FrameGeneratorCapturer::SetSinkWantsObserver(SinkWantsObserver* observer) {
rtc::CritScope cs(&lock_);
RTC_DCHECK(!sink_wants_observer_);
sink_wants_observer_ = observer;
}
void FrameGeneratorCapturer::AddOrUpdateSink(
rtc::VideoSinkInterface<VideoFrame>* sink,
const rtc::VideoSinkWants& wants) {
rtc::CritScope cs(&lock_);
RTC_CHECK(!sink_ || sink_ == sink);
sink_ = sink;
if (sink_wants_observer_)
sink_wants_observer_->OnSinkWantsChanged(sink, wants);
// Handle framerate within this class, just pass on resolution for possible
// adaptation.
rtc::VideoSinkWants resolution_wants = wants;
resolution_wants.max_framerate_fps = std::numeric_limits<int>::max();
VideoCapturer::AddOrUpdateSink(sink, resolution_wants);
// Ignore any requests for framerate higher than initially configured.
if (wants.max_framerate_fps < target_fps_) {
wanted_fps_.emplace(wants.max_framerate_fps);
} else {
wanted_fps_.reset();
}
}
void FrameGeneratorCapturer::RemoveSink(
rtc::VideoSinkInterface<VideoFrame>* sink) {
rtc::CritScope cs(&lock_);
RTC_CHECK(sink_ == sink);
sink_ = nullptr;
}
void FrameGeneratorCapturer::ForceFrame() {
// One-time non-repeating task,
// therefore repeat_interval_ms is 0 in InsertFrameTask()
task_queue_.PostTask(
std::unique_ptr<rtc::QueuedTask>(new InsertFrameTask(this, 0)));
}
int FrameGeneratorCapturer::GetCurrentConfiguredFramerate() {
rtc::CritScope cs(&lock_);
if (wanted_fps_ && *wanted_fps_ < target_fps_)
return *wanted_fps_;
return target_fps_;
}
} // namespace test
} // namespace webrtc