Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/servers/camera_server.cpp
9887 views
1
/**************************************************************************/
2
/* camera_server.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_server.h"
32
#include "core/variant/typed_array.h"
33
#include "rendering_server.h"
34
#include "servers/camera/camera_feed.h"
35
36
////////////////////////////////////////////////////////
37
// CameraServer
38
39
CameraServer::CreateFunc CameraServer::create_func = nullptr;
40
41
void CameraServer::_bind_methods() {
42
ClassDB::bind_method(D_METHOD("set_monitoring_feeds", "is_monitoring_feeds"), &CameraServer::set_monitoring_feeds);
43
ClassDB::bind_method(D_METHOD("is_monitoring_feeds"), &CameraServer::is_monitoring_feeds);
44
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "monitoring_feeds"), "set_monitoring_feeds", "is_monitoring_feeds");
45
ADD_PROPERTY_DEFAULT("monitoring_feeds", false);
46
47
ClassDB::bind_method(D_METHOD("get_feed", "index"), &CameraServer::get_feed);
48
ClassDB::bind_method(D_METHOD("get_feed_count"), &CameraServer::get_feed_count);
49
ClassDB::bind_method(D_METHOD("feeds"), &CameraServer::get_feeds);
50
51
ClassDB::bind_method(D_METHOD("add_feed", "feed"), &CameraServer::add_feed);
52
ClassDB::bind_method(D_METHOD("remove_feed", "feed"), &CameraServer::remove_feed);
53
54
ADD_SIGNAL(MethodInfo("camera_feed_added", PropertyInfo(Variant::INT, "id")));
55
ADD_SIGNAL(MethodInfo("camera_feed_removed", PropertyInfo(Variant::INT, "id")));
56
ADD_SIGNAL(MethodInfo(feeds_updated_signal_name));
57
58
BIND_ENUM_CONSTANT(FEED_RGBA_IMAGE);
59
BIND_ENUM_CONSTANT(FEED_YCBCR_IMAGE);
60
BIND_ENUM_CONSTANT(FEED_Y_IMAGE);
61
BIND_ENUM_CONSTANT(FEED_CBCR_IMAGE);
62
}
63
64
CameraServer *CameraServer::singleton = nullptr;
65
66
CameraServer *CameraServer::get_singleton() {
67
return singleton;
68
}
69
70
void CameraServer::set_monitoring_feeds(bool p_monitoring_feeds) {
71
monitoring_feeds = p_monitoring_feeds;
72
}
73
74
int CameraServer::get_free_id() {
75
bool id_exists = true;
76
int newid = 0;
77
78
// find a free id
79
while (id_exists) {
80
newid++;
81
id_exists = false;
82
for (int i = 0; i < feeds.size() && !id_exists; i++) {
83
if (feeds[i]->get_id() == newid) {
84
id_exists = true;
85
};
86
};
87
};
88
89
return newid;
90
}
91
92
int CameraServer::get_feed_index(int p_id) {
93
ERR_FAIL_COND_V_MSG(!monitoring_feeds, -1, "CameraServer is not actively monitoring feeds; call set_monitoring_feeds(true) first.");
94
95
for (int i = 0; i < feeds.size(); i++) {
96
if (feeds[i]->get_id() == p_id) {
97
return i;
98
};
99
};
100
101
return -1;
102
}
103
104
Ref<CameraFeed> CameraServer::get_feed_by_id(int p_id) {
105
ERR_FAIL_COND_V_MSG(!monitoring_feeds, nullptr, "CameraServer is not actively monitoring feeds; call set_monitoring_feeds(true) first.");
106
107
int index = get_feed_index(p_id);
108
109
if (index == -1) {
110
return nullptr;
111
} else {
112
return feeds[index];
113
}
114
}
115
116
void CameraServer::add_feed(const Ref<CameraFeed> &p_feed) {
117
ERR_FAIL_COND(p_feed.is_null());
118
119
// add our feed
120
feeds.push_back(p_feed);
121
122
print_verbose("CameraServer: Registered camera " + p_feed->get_name() + " with ID " + itos(p_feed->get_id()) + " and position " + itos(p_feed->get_position()) + " at index " + itos(feeds.size() - 1));
123
124
// let whomever is interested know
125
emit_signal(SNAME("camera_feed_added"), p_feed->get_id());
126
}
127
128
void CameraServer::remove_feed(const Ref<CameraFeed> &p_feed) {
129
for (int i = 0; i < feeds.size(); i++) {
130
if (feeds[i] == p_feed) {
131
int feed_id = p_feed->get_id();
132
133
print_verbose("CameraServer: Removed camera " + p_feed->get_name() + " with ID " + itos(feed_id) + " and position " + itos(p_feed->get_position()));
134
135
// remove it from our array, if this results in our feed being unreferenced it will be destroyed
136
feeds.remove_at(i);
137
138
// let whomever is interested know
139
emit_signal(SNAME("camera_feed_removed"), feed_id);
140
return;
141
};
142
};
143
}
144
145
Ref<CameraFeed> CameraServer::get_feed(int p_index) {
146
ERR_FAIL_COND_V_MSG(!monitoring_feeds, nullptr, "CameraServer is not actively monitoring feeds; call set_monitoring_feeds(true) first.");
147
ERR_FAIL_INDEX_V(p_index, feeds.size(), nullptr);
148
149
return feeds[p_index];
150
}
151
152
int CameraServer::get_feed_count() {
153
ERR_FAIL_COND_V_MSG(!monitoring_feeds, 0, "CameraServer is not actively monitoring feeds; call set_monitoring_feeds(true) first.");
154
return feeds.size();
155
}
156
157
TypedArray<CameraFeed> CameraServer::get_feeds() {
158
ERR_FAIL_COND_V_MSG(!monitoring_feeds, {}, "CameraServer is not actively monitoring feeds; call set_monitoring_feeds(true) first.");
159
TypedArray<CameraFeed> return_feeds;
160
int cc = get_feed_count();
161
return_feeds.resize(cc);
162
163
for (int i = 0; i < feeds.size(); i++) {
164
return_feeds[i] = get_feed(i);
165
};
166
167
return return_feeds;
168
}
169
170
RID CameraServer::feed_texture(int p_id, CameraServer::FeedImage p_texture) {
171
ERR_FAIL_COND_V_MSG(!monitoring_feeds, RID(), "CameraServer is not actively monitoring feeds; call set_monitoring_feeds(true) first.");
172
int index = get_feed_index(p_id);
173
ERR_FAIL_COND_V(index == -1, RID());
174
175
Ref<CameraFeed> feed = get_feed(index);
176
177
return feed->get_texture(p_texture);
178
}
179
180
CameraServer::CameraServer() {
181
singleton = this;
182
}
183
184
CameraServer::~CameraServer() {
185
singleton = nullptr;
186
}
187
188