| 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 | |
| 65 | ADD_SIGNAL(MethodInfo("frame_changed")); |
| 66 | ADD_SIGNAL(MethodInfo("format_changed")); |
| 67 | |
| 68 | ADD_GROUP("Feed", "feed_"); |
| 69 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "feed_is_active"), "set_active", "is_active"); |
| 70 | ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "feed_transform"), "set_transform", "get_transform"); |
| 71 | ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "formats"), "", "get_formats"); |
| 72 | |
| 73 | BIND_ENUM_CONSTANT(FEED_NOIMAGE); |
| 74 | BIND_ENUM_CONSTANT(FEED_RGB); |
| 75 | BIND_ENUM_CONSTANT(FEED_YCBCR); |
| 76 | BIND_ENUM_CONSTANT(FEED_YCBCR_SEP); |
| 77 | BIND_ENUM_CONSTANT(FEED_EXTERNAL); |
| 78 | |
| 79 | BIND_ENUM_CONSTANT(FEED_UNSPECIFIED); |
| 80 | BIND_ENUM_CONSTANT(FEED_FRONT); |
| 81 | BIND_ENUM_CONSTANT(FEED_BACK); |
| 82 | } |
| 83 | |
| 84 | int CameraFeed::get_id() const { |
| 85 | return id; |
| 86 | } |
| 87 | |
| 88 | bool CameraFeed::is_active() const { |
| 89 | return active; |
| 90 | } |
| 91 | |
| 92 | void CameraFeed::set_active(bool p_is_active) { |
| 93 | if (p_is_active == active) { |
| 94 | // all good |
| 95 | } else if (p_is_active) { |
| 96 | // attempt to activate this feed |
| 97 | if (activate_feed()) { |
| 98 | active = true; |
| 99 | } |
| 100 | } else { |
| 101 | // just deactivate it |
| 102 | deactivate_feed(); |
| 103 | active = false; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | String CameraFeed::get_name() const { |
| 108 | return name; |
| 109 | } |
| 110 | |
| 111 | void CameraFeed::set_name(String p_name) { |
| 112 | name = p_name; |
| 113 | } |
| 114 | |
| 115 | int CameraFeed::get_base_width() const { |
| 116 | return base_width; |
| 117 | } |
| 118 | |
| 119 | int CameraFeed::get_base_height() const { |
| 120 | return base_height; |
| 121 | } |
| 122 | |
| 123 | CameraFeed::FeedDataType CameraFeed::get_datatype() const { |
| 124 | return datatype; |
| 125 | } |
| 126 | |
| 127 | CameraFeed::FeedPosition CameraFeed::get_position() const { |
| 128 | return position; |
| 129 | } |
| 130 | |
| 131 | void CameraFeed::set_position(CameraFeed::FeedPosition p_position) { |
| 132 | position = p_position; |
| 133 | } |
| 134 | |
| 135 | Transform2D CameraFeed::get_transform() const { |
| 136 | return transform; |
| 137 | } |
| 138 | |
| 139 | void CameraFeed::set_transform(const Transform2D &p_transform) { |
| 140 | transform = p_transform; |
| 141 | } |
| 142 | |
| 143 | RID CameraFeed::get_texture(CameraServer::FeedImage p_which) { |
| 144 | return texture[p_which]; |
| 145 | } |
| 146 | |
| 147 | uint64_t CameraFeed::get_texture_tex_id(CameraServer::FeedImage p_which) { |
| 148 | return RenderingServer::get_singleton()->texture_get_native_handle(texture[p_which]); |
| 149 | } |
| 150 | |
| 151 | CameraFeed::CameraFeed() { |
| 152 | // initialize our feed |
| 153 | id = CameraServer::get_singleton()->get_free_id(); |
| 154 | base_width = 0; |
| 155 | base_height = 0; |
| 156 | name = "???"; |
| 157 | active = false; |
| 158 | datatype = CameraFeed::FEED_RGB; |
| 159 | position = CameraFeed::FEED_UNSPECIFIED; |
| 160 | transform = Transform2D(1.0, 0.0, 0.0, -1.0, 0.0, 1.0); |
| 161 | texture[CameraServer::FEED_Y_IMAGE] = RenderingServer::get_singleton()->texture_2d_placeholder_create(); |
| 162 | texture[CameraServer::FEED_CBCR_IMAGE] = RenderingServer::get_singleton()->texture_2d_placeholder_create(); |
| 163 | } |
| 164 | |
| 165 | CameraFeed::CameraFeed(String p_name, FeedPosition p_position) { |
| 166 | // initialize our feed |
| 167 | id = CameraServer::get_singleton()->get_free_id(); |
| 168 | base_width = 0; |
| 169 | base_height = 0; |
| 170 | name = p_name; |
| 171 | active = false; |
| 172 | datatype = CameraFeed::FEED_NOIMAGE; |
| 173 | position = p_position; |
| 174 | transform = Transform2D(1.0, 0.0, 0.0, -1.0, 0.0, 1.0); |
| 175 | texture[CameraServer::FEED_Y_IMAGE] = RenderingServer::get_singleton()->texture_2d_placeholder_create(); |
| 176 | texture[CameraServer::FEED_CBCR_IMAGE] = RenderingServer::get_singleton()->texture_2d_placeholder_create(); |
| 177 | } |
| 178 | |
| 179 | CameraFeed::~CameraFeed() { |
| 180 | // Free our textures |
| 181 | ERR_FAIL_NULL(RenderingServer::get_singleton()); |
| 182 | RenderingServer::get_singleton()->free_rid(texture[CameraServer::FEED_Y_IMAGE]); |
| 183 | RenderingServer::get_singleton()->free_rid(texture[CameraServer::FEED_CBCR_IMAGE]); |
| 184 | } |
| 185 | |
| 186 | void CameraFeed::set_rgb_image(const Ref<Image> &p_rgb_img) { |
| 187 | ERR_FAIL_COND(p_rgb_img.is_null()); |
| 188 | if (active) { |
| 189 | int new_width = p_rgb_img->get_width(); |
| 190 | int new_height = p_rgb_img->get_height(); |
| 191 | |
| 192 | // Emit `format_changed` signal if feed datatype or frame size is changed. |
| 193 | // The signal is deferred to ensure: |
| 194 | // - They are emitted on Godot's main thread. |
| 195 | // - Both datatype and frame size are updated before the emission. |
| 196 | if (datatype != CameraFeed::FEED_RGB || (base_width != new_width) || (base_height != new_height)) { |
| 197 | call_deferred("emit_signal", format_changed_signal_name); |
| 198 | } |
| 199 | |
| 200 | if ((base_width != new_width) || (base_height != new_height)) { |
| 201 | // We're assuming here that our camera image doesn't change around formats etc, allocate the whole lot... |
| 202 | base_width = new_width; |
| 203 | base_height = new_height; |
| 204 | |
| 205 | RID new_texture = RenderingServer::get_singleton()->texture_2d_create(p_rgb_img); |
| 206 | RenderingServer::get_singleton()->texture_replace(texture[CameraServer::FEED_RGBA_IMAGE], new_texture); |
| 207 | } else { |
| 208 | RenderingServer::get_singleton()->texture_2d_update(texture[CameraServer::FEED_RGBA_IMAGE], p_rgb_img); |
| 209 | } |
| 210 | |
| 211 | datatype = CameraFeed::FEED_RGB; |
| 212 | // Most of the time the pixel data of camera devices comes from threads outside Godot. |
| 213 | // Defer `frame_changed` signals to ensure they are emitted on Godot's main thread. |
| 214 | call_deferred("emit_signal", frame_changed_signal_name); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | void CameraFeed::set_ycbcr_image(const Ref<Image> &p_ycbcr_img) { |
| 219 | ERR_FAIL_COND(p_ycbcr_img.is_null()); |
| 220 | if (active) { |
| 221 | int new_width = p_ycbcr_img->get_width(); |
| 222 | int new_height = p_ycbcr_img->get_height(); |
| 223 | |
| 224 | // Emit `format_changed` signal if feed datatype or frame size is changed. |
| 225 | // The signal is deferred to ensure: |
| 226 | // - They are emitted on Godot's main thread. |
| 227 | // - Both datatype and frame size are updated before the emission. |
| 228 | if (datatype != CameraFeed::FEED_YCBCR || (base_width != new_width) || (base_height != new_height)) { |
| 229 | call_deferred("emit_signal", format_changed_signal_name); |
| 230 | } |
| 231 | |
| 232 | if ((base_width != new_width) || (base_height != new_height)) { |
| 233 | // We're assuming here that our camera image doesn't change around formats etc, allocate the whole lot... |
| 234 | base_width = new_width; |
| 235 | base_height = new_height; |
| 236 | |
| 237 | RID new_texture = RenderingServer::get_singleton()->texture_2d_create(p_ycbcr_img); |
| 238 | RenderingServer::get_singleton()->texture_replace(texture[CameraServer::FEED_RGBA_IMAGE], new_texture); |
| 239 | } else { |
| 240 | RenderingServer::get_singleton()->texture_2d_update(texture[CameraServer::FEED_RGBA_IMAGE], p_ycbcr_img); |
| 241 | } |
| 242 | |
| 243 | datatype = CameraFeed::FEED_YCBCR; |
| 244 | // Most of the time the pixel data of camera devices comes from threads outside Godot. |
| 245 | // Defer `frame_changed` signals to ensure they are emitted on Godot's main thread. |
| 246 | call_deferred("emit_signal", frame_changed_signal_name); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | void CameraFeed::set_ycbcr_images(const Ref<Image> &p_y_img, const Ref<Image> &p_cbcr_img) { |
| 251 | ERR_FAIL_COND(p_y_img.is_null()); |
| 252 | ERR_FAIL_COND(p_cbcr_img.is_null()); |
| 253 | if (active) { |
| 254 | ///@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.. |
| 255 | // Wondering about including that into multiple projects, may cause issues. |
| 256 | // That said, if we convert to RGB, we could enable using texture resources again... |
| 257 | |
| 258 | int new_y_width = p_y_img->get_width(); |
| 259 | int new_y_height = p_y_img->get_height(); |
| 260 | |
| 261 | // Emit `format_changed` signal if feed datatype or frame size is changed. |
| 262 | // The signal is deferred to ensure: |
| 263 | // - They are emitted on Godot's main thread. |
| 264 | // - Both datatype and frame size are updated before the emission. |
| 265 | if (datatype != CameraFeed::FEED_YCBCR_SEP || (base_width != new_y_width) || (base_height != new_y_height)) { |
| 266 | call_deferred("emit_signal", format_changed_signal_name); |
| 267 | } |
| 268 | |
| 269 | if ((base_width != new_y_width) || (base_height != new_y_height)) { |
| 270 | // We're assuming here that our camera image doesn't change around formats etc, allocate the whole lot... |
| 271 | base_width = new_y_width; |
| 272 | base_height = new_y_height; |
| 273 | { |
| 274 | RID new_texture = RenderingServer::get_singleton()->texture_2d_create(p_y_img); |
| 275 | RenderingServer::get_singleton()->texture_replace(texture[CameraServer::FEED_Y_IMAGE], new_texture); |
| 276 | } |
| 277 | { |
| 278 | RID new_texture = RenderingServer::get_singleton()->texture_2d_create(p_cbcr_img); |
| 279 | RenderingServer::get_singleton()->texture_replace(texture[CameraServer::FEED_CBCR_IMAGE], new_texture); |
| 280 | } |
| 281 | } else { |
| 282 | RenderingServer::get_singleton()->texture_2d_update(texture[CameraServer::FEED_Y_IMAGE], p_y_img); |
| 283 | RenderingServer::get_singleton()->texture_2d_update(texture[CameraServer::FEED_CBCR_IMAGE], p_cbcr_img); |
| 284 | } |
| 285 | |
| 286 | datatype = CameraFeed::FEED_YCBCR_SEP; |
| 287 | // Most of the time the pixel data of camera devices comes from threads outside Godot. |
| 288 | // Defer `frame_changed` signals to ensure they are emitted on Godot's main thread. |
| 289 | call_deferred("emit_signal", frame_changed_signal_name); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | void CameraFeed::set_external(int p_width, int p_height) { |
| 294 | // Emit `format_changed` signal if feed datatype or frame size is changed. |
| 295 | // The signal is deferred to ensure: |
| 296 | // - They are emitted on Godot's main thread. |
| 297 | // - Both datatype and frame size are updated before the emission. |
| 298 | if (datatype != CameraFeed::FEED_EXTERNAL || (base_width != p_width) || (base_height != p_height)) { |
| 299 | call_deferred("emit_signal", format_changed_signal_name); |
| 300 | } |
| 301 | |
| 302 | if ((base_width != p_width) || (base_height != p_height)) { |
| 303 | // We're assuming here that our camera image doesn't change around formats etc, allocate the whole lot... |
| 304 | base_width = p_width; |
| 305 | base_height = p_height; |
| 306 | |
| 307 | RID new_texture = RenderingServer::get_singleton()->texture_external_create(p_width, p_height, 0); |
| 308 | RenderingServer::get_singleton()->texture_replace(texture[CameraServer::FEED_YCBCR_IMAGE], new_texture); |
| 309 | } |
| 310 | |
| 311 | datatype = CameraFeed::FEED_EXTERNAL; |
| 312 | // Most of the time the pixel data of camera devices comes from threads outside Godot. |
| 313 | // Defer `frame_changed` signals to ensure they are emitted on Godot's main thread. |
| 314 | call_deferred("emit_signal", frame_changed_signal_name); |
| 315 | } |
| 316 | |
| 317 | bool CameraFeed::activate_feed() { |
| 318 | bool ret = true; |
| 319 | GDVIRTUAL_CALL(_activate_feed, ret); |
| 320 | return ret; |
| 321 | } |
| 322 | |
| 323 | void CameraFeed::deactivate_feed() { |
| 324 | GDVIRTUAL_CALL(_deactivate_feed); |
| 325 | } |
| 326 | |
| 327 | bool CameraFeed::set_format(int p_index, const Dictionary &p_parameters) { |
| 328 | return false; |
| 329 | } |
| 330 | |
| 331 | Array CameraFeed::get_formats() const { |
| 332 | return Array(); |
| 333 | } |
| 334 | |
| 335 | CameraFeed::FeedFormat CameraFeed::get_format() const { |
| 336 | FeedFormat feed_format = {}; |
| 337 | return feed_format; |
| 338 | } |