34
34
35
35
#include < cstdlib>
36
36
37
- EM_ASYNC_JS (void , godot_js_camera_get_cameras, (void *context, CameraLibrary_OnGetCamerasCallback p_callback_ptr), {
38
- await GodotCamera.api .getCameras (context, p_callback_ptr);
39
- });
40
-
41
- EM_ASYNC_JS (void , godot_js_camera_get_capabilities, (void *context, const char *p_device_id_ptr, CameraLibrary_OnGetCapabilitiesCallback p_callback_ptr), {
42
- await GodotCamera.api .getCameraCapabilities (p_device_id_ptr, context, p_callback_ptr);
43
- });
44
-
45
37
CameraDriverWeb *CameraDriverWeb::singleton = nullptr ;
46
38
Array CameraDriverWeb::_camera_info_key;
47
39
@@ -53,7 +45,7 @@ CameraDriverWeb *CameraDriverWeb::get_singleton() {
53
45
return singleton;
54
46
}
55
47
56
- void CameraDriverWeb::_on_get_cameras_callback (void *context, const char *json_ptr) {
48
+ void CameraDriverWeb::_on_get_cameras_callback (void *context, void *callback, const char *json_ptr) {
57
49
if (!json_ptr) {
58
50
print_error (" CameraDriverWeb::_on_get_cameras_callback: json_ptr is null" );
59
51
return ;
@@ -75,8 +67,7 @@ void CameraDriverWeb::_on_get_cameras_callback(void *context, const char *json_p
75
67
return ;
76
68
}
77
69
Array devices_array = v_devices;
78
- Vector<CameraInfo> *camera_info = reinterpret_cast <Vector<CameraInfo> *>(context);
79
- camera_info->clear ();
70
+ Vector<CameraInfo> camera_info;
80
71
for (int i = 0 ; i < devices_array.size (); i++) {
81
72
Variant device_variant = devices_array.get (i);
82
73
if (device_variant.get_type () == Variant::DICTIONARY) {
@@ -86,88 +77,62 @@ void CameraDriverWeb::_on_get_cameras_callback(void *context, const char *json_p
86
77
info.index = device_dict[KEY_INDEX];
87
78
info.device_id = device_dict[KEY_ID];
88
79
info.label = device_dict[KEY_LABEL];
89
- camera_info->push_back (info);
80
+ Variant v_caps_data = device_dict.get (KEY_CAPABILITIES, Variant ());
81
+ if (v_caps_data.get_type () == Variant::DICTIONARY) {
82
+ Dictionary caps_dict = v_caps_data;
83
+ if (caps_dict.has (KEY_WIDTH) && caps_dict.has (KEY_HEIGHT)) {
84
+ Variant v_width_val = caps_dict.get (KEY_WIDTH, Variant ());
85
+ Variant v_height_val = caps_dict.get (KEY_HEIGHT, Variant ());
86
+ int width = 0 ;
87
+ int height = 0 ;
88
+
89
+ // Helper to extract 'max' from a capability dictionary or use direct value
90
+ auto get_max_or_direct = [](const Variant &p_val) -> int {
91
+ if (p_val.get_type () == Variant::DICTIONARY) {
92
+ Dictionary d = p_val;
93
+ if (d.has (KEY_MAX)) {
94
+ return d[KEY_MAX];
95
+ }
96
+ } else if (p_val.get_type () == Variant::INT) {
97
+ return p_val;
98
+ } else if (p_val.get_type () == Variant::FLOAT) {
99
+ return static_cast <int >(p_val.operator float ());
100
+ }
101
+ return 0 ;
102
+ };
103
+
104
+ width = get_max_or_direct (v_width_val);
105
+ height = get_max_or_direct (v_height_val);
106
+
107
+ if (width > 0 && height > 0 ) {
108
+ CapabilityInfo capability;
109
+ capability.width = width;
110
+ capability.height = height;
111
+ info.capability = capability;
112
+ } else {
113
+ WARN_PRINT (" Could not extract valid width/height from capabilities structure." );
114
+ }
115
+ } else {
116
+ WARN_PRINT (" Capabilities object does not directly contain top-level width/height keys." );
117
+ }
118
+ } else {
119
+ WARN_PRINT (" Camera info entry has no capabilities or capabilities are not a dictionary." );
120
+ }
121
+ camera_info.push_back (info);
90
122
} else {
91
123
WARN_PRINT (" Camera info entry missing required keys (index, id, label)." );
92
124
}
93
125
}
94
126
}
127
+ CameraDriverWeb_OnGetCamerasCallback on_get_cameras_callback = reinterpret_cast <CameraDriverWeb_OnGetCamerasCallback>(callback);
128
+ on_get_cameras_callback (context, const_cast <Vector<CameraInfo> &>(camera_info));
95
129
} else {
96
130
ERR_PRINT (" CameraDriverWeb::_on_get_cameras_callback: Failed to parse JSON response or response is not a Dictionary." );
97
131
}
98
132
}
99
133
100
- void CameraDriverWeb::_on_get_capabilities_callback (void *context, const char *json_ptr) {
101
- if (!json_ptr) {
102
- ERR_PRINT (" CameraDriverWeb::_on_get_capabilities_callback: json_ptr is null" );
103
- return ;
104
- }
105
- String json_string = String::utf8 (json_ptr);
106
- Variant json_variant = JSON::parse_string (json_string);
107
-
108
- if (json_variant.get_type () == Variant::DICTIONARY) {
109
- Dictionary json_dict = json_variant;
110
- Variant v_error = json_dict[KEY_ERROR];
111
- if (v_error.get_type () == Variant::STRING) {
112
- String error_str = v_error;
113
- ERR_PRINT (vformat (" Camera capabilities error from JS: %s" , error_str));
114
- return ;
115
- }
116
- Variant v_caps_data = json_dict.get (KEY_CAPABILITIES, Variant ());
117
- if (v_caps_data.get_type () != Variant::DICTIONARY) {
118
- ERR_PRINT (" Camera capabilities error: 'capabilities' data is not a dictionary or missing." );
119
- return ;
120
- }
121
- Dictionary caps_dict = v_caps_data;
122
- Vector<CapabilityInfo> *capabilities = reinterpret_cast <Vector<CapabilityInfo> *>(context);
123
- capabilities->clear ();
124
-
125
- if (caps_dict.has (KEY_WIDTH) && caps_dict.has (KEY_HEIGHT)) {
126
- Variant v_width_val = caps_dict.get (KEY_WIDTH, Variant ());
127
- Variant v_height_val = caps_dict.get (KEY_HEIGHT, Variant ());
128
- int width = 0 ;
129
- int height = 0 ;
130
-
131
- // Helper to extract 'max' from a capability dictionary or use direct value
132
- auto get_max_or_direct = [](const Variant &p_val) -> int {
133
- if (p_val.get_type () == Variant::DICTIONARY) {
134
- Dictionary d = p_val;
135
- if (d.has (KEY_MAX)) {
136
- return d[KEY_MAX];
137
- }
138
- } else if (p_val.get_type () == Variant::INT) {
139
- return p_val;
140
- } else if (p_val.get_type () == Variant::FLOAT) {
141
- return static_cast <int >(p_val.operator float ());
142
- }
143
- return 0 ;
144
- };
145
-
146
- width = get_max_or_direct (v_width_val);
147
- height = get_max_or_direct (v_height_val);
148
-
149
- if (width > 0 && height > 0 ) {
150
- CapabilityInfo info;
151
- info.width = width;
152
- info.height = height;
153
- capabilities->push_back (info);
154
- } else {
155
- WARN_PRINT (" Could not extract valid width/height from capabilities structure." );
156
- }
157
- } else {
158
- WARN_PRINT (" Capabilities object does not directly contain top-level width/height keys." );
159
- }
160
- } else {
161
- ERR_PRINT (" CameraDriverWeb::_on_get_capabilities_callback: Failed to parse JSON response or response is not a Dictionary." );
162
- }
163
- }
164
-
165
- void CameraDriverWeb::get_cameras (Vector<CameraInfo> *r_camera_info) {
166
- godot_js_camera_get_cameras ((void *)r_camera_info, &_on_get_cameras_callback);
167
- }
168
-
169
- void CameraDriverWeb::get_capabilities (Vector<CapabilityInfo> *r_capabilities, const String &p_device_id) {
170
- godot_js_camera_get_capabilities ((void *)r_capabilities, p_device_id.utf8 ().get_data (), &_on_get_capabilities_callback);
134
+ void CameraDriverWeb::get_cameras (void *context, CameraDriverWeb_OnGetCamerasCallback callback) {
135
+ godot_js_camera_get_cameras (context, (void *)callback, &_on_get_cameras_callback);
171
136
}
172
137
173
138
void CameraDriverWeb::get_pixel_data (void *context, const String &p_device_id, const int width, const int height, CameraLibrary_OnGetPixelDataCallback p_callback, CameraLibrary_OnDeniedCallback p_denied_callback) {
0 commit comments