Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/servers/camera/camera_feed.cpp
20889 views
1
/**************************************************************************/
2
/* camera_feed.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "camera_feed.h"
32
33
#include "servers/rendering/rendering_server.h"
34
35
void CameraFeed::_bind_methods() {
36
ClassDB::bind_method(D_METHOD("get_id"), &CameraFeed::get_id);
37
38
ClassDB::bind_method(D_METHOD("is_active"), &CameraFeed::is_active);
39
ClassDB::bind_method(D_METHOD("set_active", "active"), &CameraFeed::set_active);
40
41
ClassDB::bind_method(D_METHOD("get_name"), &CameraFeed::get_name);
42
ClassDB::bind_method(D_METHOD("set_name", "name"), &CameraFeed::set_name);
43
44
ClassDB::bind_method(D_METHOD("get_position"), &CameraFeed::get_position);
45
ClassDB::bind_method(D_METHOD("set_position", "position"), &CameraFeed::set_position);
46
47
// Note, for transform some feeds may override what the user sets (such as ARKit)
48
ClassDB::bind_method(D_METHOD("get_transform"), &CameraFeed::get_transform);
49
ClassDB::bind_method(D_METHOD("set_transform", "transform"), &CameraFeed::set_transform);
50
51
ClassDB::bind_method(D_METHOD("set_rgb_image", "rgb_image"), &CameraFeed::set_rgb_image);
52
ClassDB::bind_method(D_METHOD("set_ycbcr_image", "ycbcr_image"), &CameraFeed::set_ycbcr_image);
53
ClassDB::bind_method(D_METHOD("set_ycbcr_images", "y_image", "cbcr_image"), &CameraFeed::set_ycbcr_images);
54
ClassDB::bind_method(D_METHOD("set_external", "width", "height"), &CameraFeed::set_external);
55
ClassDB::bind_method(D_METHOD("get_texture_tex_id", "feed_image_type"), &CameraFeed::get_texture_tex_id);
56
57
ClassDB::bind_method(D_METHOD("get_datatype"), &CameraFeed::get_datatype);
58
59
ClassDB::bind_method(D_METHOD("get_formats"), &CameraFeed::get_formats);
60
ClassDB::bind_method(D_METHOD("set_format", "index", "parameters"), &CameraFeed::set_format);
61
62
GDVIRTUAL_BIND(_activate_feed);
63
GDVIRTUAL_BIND(_deactivate_feed);
64
GDVIRTUAL_BIND(_set_format, "index", "parameters");
65
GDVIRTUAL_BIND(_get_formats);
66
67
ADD_SIGNAL(MethodInfo("frame_changed"));
68
ADD_SIGNAL(MethodInfo("format_changed"));
69
70
ADD_GROUP("Feed", "feed_");
71
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "feed_is_active"), "set_active", "is_active");
72
ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "feed_transform"), "set_transform", "get_transform");
73
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "formats"), "", "get_formats");
74
75
BIND_ENUM_CONSTANT(FEED_NOIMAGE);
76
BIND_ENUM_CONSTANT(FEED_RGB);
77
BIND_ENUM_CONSTANT(FEED_YCBCR);
78
BIND_ENUM_CONSTANT(FEED_YCBCR_SEP);
79
BIND_ENUM_CONSTANT(FEED_EXTERNAL);
80
81
BIND_ENUM_CONSTANT(FEED_UNSPECIFIED);
82
BIND_ENUM_CONSTANT(FEED_FRONT);
83
BIND_ENUM_CONSTANT(FEED_BACK);
84
}
85
86
int CameraFeed::get_id() const {
87
return id;
88
}
89
90
bool CameraFeed::is_active() const {
91
return active;
92
}
93
94
void CameraFeed::set_active(bool p_is_active) {
95
if (p_is_active == active) {
96
// all good
97
} else if (p_is_active) {
98
// attempt to activate this feed
99
if (activate_feed()) {
100
active = true;
101
}
102
} else {
103
// just deactivate it
104
deactivate_feed();
105
active = false;
106
}
107
}
108
109
String CameraFeed::get_name() const {
110
return name;
111
}
112
113
void CameraFeed::set_name(String p_name) {
114
name = p_name;
115
}
116
117
int CameraFeed::get_base_width() const {
118
return base_width;
119
}
120
121
int CameraFeed::get_base_height() const {
122
return base_height;
123
}
124
125
CameraFeed::FeedDataType CameraFeed::get_datatype() const {
126
return datatype;
127
}
128
129
CameraFeed::FeedPosition CameraFeed::get_position() const {
130
return position;
131
}
132
133
void CameraFeed::set_position(CameraFeed::FeedPosition p_position) {
134
position = p_position;
135
}
136
137
Transform2D CameraFeed::get_transform() const {
138
return transform;
139
}
140
141
void CameraFeed::set_transform(const Transform2D &p_transform) {
142
transform = p_transform;
143
}
144
145
RID CameraFeed::get_texture(CameraServer::FeedImage p_which) {
146
return texture[p_which];
147
}
148
149
uint64_t CameraFeed::get_texture_tex_id(CameraServer::FeedImage p_which) {
150
return RenderingServer::get_singleton()->texture_get_native_handle(texture[p_which]);
151
}
152
153
CameraFeed::CameraFeed() {
154
// initialize our feed
155
id = CameraServer::get_singleton()->get_free_id();
156
base_width = 0;
157
base_height = 0;
158
name = "???";
159
active = false;
160
datatype = CameraFeed::FEED_RGB;
161
position = CameraFeed::FEED_UNSPECIFIED;
162
transform = Transform2D(1.0, 0.0, 0.0, -1.0, 0.0, 1.0);
163
texture[CameraServer::FEED_Y_IMAGE] = RenderingServer::get_singleton()->texture_2d_placeholder_create();
164
texture[CameraServer::FEED_CBCR_IMAGE] = RenderingServer::get_singleton()->texture_2d_placeholder_create();
165
}
166
167
CameraFeed::CameraFeed(String p_name, FeedPosition p_position) {
168
// initialize our feed
169
id = CameraServer::get_singleton()->get_free_id();
170
base_width = 0;
171
base_height = 0;
172
name = p_name;
173
active = false;
174
datatype = CameraFeed::FEED_NOIMAGE;
175
position = p_position;
176
transform = Transform2D(1.0, 0.0, 0.0, -1.0, 0.0, 1.0);
177
texture[CameraServer::FEED_Y_IMAGE] = RenderingServer::get_singleton()->texture_2d_placeholder_create();
178
texture[CameraServer::FEED_CBCR_IMAGE] = RenderingServer::get_singleton()->texture_2d_placeholder_create();
179
}
180
181
CameraFeed::~CameraFeed() {
182
// Free our textures
183
ERR_FAIL_NULL(RenderingServer::get_singleton());
184
RenderingServer::get_singleton()->free_rid(texture[CameraServer::FEED_Y_IMAGE]);
185
RenderingServer::get_singleton()->free_rid(texture[CameraServer::FEED_CBCR_IMAGE]);
186
}
187
188
void CameraFeed::set_rgb_image(const Ref<Image> &p_rgb_img) {
189
ERR_FAIL_COND(p_rgb_img.is_null());
190
if (active) {
191
int new_width = p_rgb_img->get_width();
192
int new_height = p_rgb_img->get_height();
193
194
if (datatype != CameraFeed::FEED_RGB || (base_width != new_width) || (base_height != new_height)) {
195
base_width = new_width;
196
base_height = new_height;
197
198
RID new_texture = RenderingServer::get_singleton()->texture_2d_create(p_rgb_img);
199
RenderingServer::get_singleton()->texture_replace(texture[CameraServer::FEED_RGBA_IMAGE], new_texture);
200
201
// `format_changed` signal is deferred to ensure:
202
// - They are emitted on Godot's main thread.
203
// - Both datatype and frame size are updated before the emission.
204
call_deferred("emit_signal", format_changed_signal_name);
205
} else {
206
RenderingServer::get_singleton()->texture_2d_update(texture[CameraServer::FEED_RGBA_IMAGE], p_rgb_img);
207
}
208
209
datatype = CameraFeed::FEED_RGB;
210
// Most of the time the pixel data of camera devices comes from threads outside Godot.
211
// Defer `frame_changed` signals to ensure they are emitted on Godot's main thread.
212
call_deferred("emit_signal", frame_changed_signal_name);
213
}
214
}
215
216
void CameraFeed::set_ycbcr_image(const Ref<Image> &p_ycbcr_img) {
217
ERR_FAIL_COND(p_ycbcr_img.is_null());
218
if (active) {
219
int new_width = p_ycbcr_img->get_width();
220
int new_height = p_ycbcr_img->get_height();
221
222
if (datatype != CameraFeed::FEED_YCBCR || (base_width != new_width) || (base_height != new_height)) {
223
base_width = new_width;
224
base_height = new_height;
225
226
RID new_texture = RenderingServer::get_singleton()->texture_2d_create(p_ycbcr_img);
227
RenderingServer::get_singleton()->texture_replace(texture[CameraServer::FEED_YCBCR_IMAGE], new_texture);
228
229
// `format_changed` signal is deferred to ensure:
230
// - They are emitted on Godot's main thread.
231
// - Both datatype and frame size are updated before the emission.
232
call_deferred("emit_signal", format_changed_signal_name);
233
} else {
234
RenderingServer::get_singleton()->texture_2d_update(texture[CameraServer::FEED_YCBCR_IMAGE], p_ycbcr_img);
235
}
236
237
datatype = CameraFeed::FEED_YCBCR;
238
// Most of the time the pixel data of camera devices comes from threads outside Godot.
239
// Defer `frame_changed` signals to ensure they are emitted on Godot's main thread.
240
call_deferred("emit_signal", frame_changed_signal_name);
241
}
242
}
243
244
void CameraFeed::set_ycbcr_images(const Ref<Image> &p_y_img, const Ref<Image> &p_cbcr_img) {
245
ERR_FAIL_COND(p_y_img.is_null());
246
ERR_FAIL_COND(p_cbcr_img.is_null());
247
if (active) {
248
///@TODO investigate whether we can use thirdparty/misc/yuv2rgb.h here to convert our YUV data to RGB, our shader approach is potentially faster though..
249
// Wondering about including that into multiple projects, may cause issues.
250
// That said, if we convert to RGB, we could enable using texture resources again...
251
252
int new_y_width = p_y_img->get_width();
253
int new_y_height = p_y_img->get_height();
254
255
if (datatype != CameraFeed::FEED_YCBCR_SEP || (base_width != new_y_width) || (base_height != new_y_height)) {
256
base_width = new_y_width;
257
base_height = new_y_height;
258
{
259
RID new_texture = RenderingServer::get_singleton()->texture_2d_create(p_y_img);
260
RenderingServer::get_singleton()->texture_replace(texture[CameraServer::FEED_Y_IMAGE], new_texture);
261
}
262
{
263
RID new_texture = RenderingServer::get_singleton()->texture_2d_create(p_cbcr_img);
264
RenderingServer::get_singleton()->texture_replace(texture[CameraServer::FEED_CBCR_IMAGE], new_texture);
265
}
266
267
// `format_changed` signal is deferred to ensure:
268
// - They are emitted on Godot's main thread.
269
// - Both datatype and frame size are updated before the emission.
270
call_deferred("emit_signal", format_changed_signal_name);
271
} else {
272
RenderingServer::get_singleton()->texture_2d_update(texture[CameraServer::FEED_Y_IMAGE], p_y_img);
273
RenderingServer::get_singleton()->texture_2d_update(texture[CameraServer::FEED_CBCR_IMAGE], p_cbcr_img);
274
}
275
276
datatype = CameraFeed::FEED_YCBCR_SEP;
277
// Most of the time the pixel data of camera devices comes from threads outside Godot.
278
// Defer `frame_changed` signals to ensure they are emitted on Godot's main thread.
279
call_deferred("emit_signal", frame_changed_signal_name);
280
}
281
}
282
283
void CameraFeed::set_external(int p_width, int p_height) {
284
if (datatype != CameraFeed::FEED_EXTERNAL || (base_width != p_width) || (base_height != p_height)) {
285
base_width = p_width;
286
base_height = p_height;
287
288
RID new_texture = RenderingServer::get_singleton()->texture_external_create(p_width, p_height, 0);
289
RenderingServer::get_singleton()->texture_replace(texture[CameraServer::FEED_YCBCR_IMAGE], new_texture);
290
291
// `format_changed` signal is deferred to ensure:
292
// - They are emitted on Godot's main thread.
293
// - Both datatype and frame size are updated before the emission.
294
call_deferred("emit_signal", format_changed_signal_name);
295
}
296
297
datatype = CameraFeed::FEED_EXTERNAL;
298
// Most of the time the pixel data of camera devices comes from threads outside Godot.
299
// Defer `frame_changed` signals to ensure they are emitted on Godot's main thread.
300
call_deferred("emit_signal", frame_changed_signal_name);
301
}
302
303
bool CameraFeed::activate_feed() {
304
bool ret = true;
305
GDVIRTUAL_CALL(_activate_feed, ret);
306
return ret;
307
}
308
309
void CameraFeed::deactivate_feed() {
310
GDVIRTUAL_CALL(_deactivate_feed);
311
}
312
313
bool CameraFeed::set_format(int p_index, const Dictionary &p_parameters) {
314
bool ret = false;
315
GDVIRTUAL_CALL(_set_format, p_index, p_parameters, ret);
316
return ret;
317
}
318
319
Array CameraFeed::get_formats() const {
320
Array ret;
321
GDVIRTUAL_CALL(_get_formats, ret);
322
return ret;
323
}
324
325
CameraFeed::FeedFormat CameraFeed::get_format() const {
326
FeedFormat feed_format = {};
327
return feed_format;
328
}
329
330