-
Notifications
You must be signed in to change notification settings - Fork 121
/
defaults.cc
197 lines (170 loc) · 5.01 KB
/
defaults.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
#include "defaults.h"
#include "internals.h"
#include "conductor.h"
#include "webrtc/modules/desktop_capture/desktop_capture_options.h"
namespace Native
{
int I420DataSize(int height, int stride_y, int stride_u, int stride_v)
{
return stride_y * height + (stride_u + stride_v) * ((height + 1) / 2);
}
YuvFramesCapturer2::YuvFramesCapturer2(Conductor & c) :
barcode_interval_(1),
frame_generator_(nullptr),
#if DESKTOP_CAPTURE
desktop_capturer(nullptr),
#endif
run(false),
con(&c)
{
video_buffer = webrtc::I420Buffer::Create(con->width_, con->height_);
frame_data_size_ = I420DataSize(con->height_, video_buffer->StrideY(), video_buffer->StrideU(), video_buffer->StrideV());
video_frame = new webrtc::VideoFrame(video_buffer, 0, 0, webrtc::VideoRotation::kVideoRotation_0);
// Enumerate the supported formats. We have only one supported format.
cricket::VideoFormat format(con->width_, con->height_, cricket::VideoFormat::FpsToInterval(con->caputureFps), cricket::FOURCC_IYUV);
std::vector<cricket::VideoFormat> supported;
supported.push_back(format);
SetSupportedFormats(supported);
set_enable_video_adapter(false);
//set_square_pixel_aspect_ratio(false);
}
YuvFramesCapturer2::~YuvFramesCapturer2()
{
if (video_frame)
{
delete video_frame;
video_frame = nullptr;
}
if (frame_generator_)
{
delete frame_generator_;
}
Stop();
}
cricket::CaptureState YuvFramesCapturer2::Start(const cricket::VideoFormat& capture_format)
{
if (IsRunning())
{
LOG(LS_ERROR) << "Yuv Frame Generator is already running";
return cricket::CS_FAILED;
}
SetCaptureFormat(&capture_format);
barcode_reference_timestamp_millis_ = rtc::TimeNanos();
run = true;
#if DESKTOP_CAPTURE
{
webrtc::DesktopCaptureOptions co;
co.set_allow_directx_capturer(true);
desktop_capturer = webrtc::DesktopCapturer::CreateScreenCapturer(co);
//desktop_capturer = webrtc::DesktopCapturer::CreateWindowCapturer(co);
desktop_capturer->GetSourceList(&desktop_screens);
for each(auto & s in desktop_screens)
{
LOG(INFO) << "screen: " << s.id << " -> " << s.title;
}
desktop_capturer->SelectSource(desktop_screens[0].id);
desktop_capturer->Start(this);
}
#endif
LOG(LS_INFO) << "Yuv Frame Generator started";
return cricket::CS_RUNNING;
}
bool YuvFramesCapturer2::IsRunning()
{
return run;
}
void YuvFramesCapturer2::Stop()
{
run = false;
SetCaptureFormat(nullptr);
}
bool YuvFramesCapturer2::GetPreferredFourccs(std::vector<uint32_t>* fourccs)
{
if (!fourccs)
{
return false;
}
fourccs->push_back(GetSupportedFormats()->at(0).fourcc);
return true;
}
void YuvFramesCapturer2::PushFrame()
{
int64_t camera_time_us = rtc::TimeMicros();
int64_t system_time_us = camera_time_us;
int out_width;
int out_height;
int crop_width;
int crop_height;
int crop_x;
int crop_y;
int64_t translated_camera_time_us;
if (AdaptFrame(con->width_,
con->height_,
camera_time_us,
system_time_us,
&out_width,
&out_height,
&crop_width,
&crop_height,
&crop_x,
&crop_y,
&translated_camera_time_us))
{
if (con->barcodeEnabled)
{
if (frame_generator_ == nullptr)
{
frame_generator_ = new cricket::YuvFrameGenerator(con->width_, con->height_, true);
}
frame_generator_->GenerateNextFrame((uint8_t*)video_buffer->DataY(), static_cast<int32_t>(rtc::TimeNanos() - barcode_reference_timestamp_millis_));
}
video_frame->set_timestamp_us(translated_camera_time_us);
OnFrame(*video_frame, con->width_, con->height_);
}
}
#if DESKTOP_CAPTURE
void YuvFramesCapturer2::CaptureFrame()
{
if (desktop_capturer)
{
desktop_capturer->CaptureFrame();
}
}
// webrtc::DesktopCapturer::Callback implementation
void YuvFramesCapturer2::OnCaptureResult(webrtc::DesktopCapturer::Result result, std::unique_ptr<webrtc::DesktopFrame> frame)
{
if (desktop_capturer)
{
desktop_frame.reset(frame.release());
}
}
#endif
// VideoSinkInterface implementation
void VideoRenderer::OnFrame(const webrtc::VideoFrame& frame)
{
if (remote && con->onRenderRemote)
{
auto b = frame.video_frame_buffer();
con->onRenderRemote((uint8_t*)b->DataY(), b->width(), b->height());
}
else if (con->onRenderLocal)
{
auto b = frame.video_frame_buffer();
con->onRenderLocal((uint8_t*)b->DataY(), b->width(), b->height());
}
}
// AudioTrackSinkInterface implementation
void AudioRenderer::OnData(const void* audio_data,
int bits_per_sample,
int sample_rate,
size_t number_of_channels,
size_t number_of_frames)
{
std::stringstream s;
s << "AudioRenderer::OnData, bps: " << bits_per_sample
<< ", rate: " << sample_rate
<< ", channels: " << number_of_channels
<< ", frames: " << number_of_frames;
::OutputDebugStringA(s.str().c_str());
}
}