Merge pull request #104232 from Ivorforce/camera-server-shutdown
Deactivate the `CameraServer` by default.
This commit is contained in:
@ -45,6 +45,12 @@
|
|||||||
</description>
|
</description>
|
||||||
</method>
|
</method>
|
||||||
</methods>
|
</methods>
|
||||||
|
<members>
|
||||||
|
<member name="monitoring_feeds" type="bool" setter="set_monitoring_feeds" getter="is_monitoring_feeds" default="false">
|
||||||
|
If [code]true[/code], the server is actively monitoring available camera feeds.
|
||||||
|
This has a performance cost, so only set it to [code]true[/code] when you're actively accessing the camera.
|
||||||
|
</member>
|
||||||
|
</members>
|
||||||
<signals>
|
<signals>
|
||||||
<signal name="camera_feed_added">
|
<signal name="camera_feed_added">
|
||||||
<param index="0" name="id" type="int" />
|
<param index="0" name="id" type="int" />
|
||||||
|
|||||||
@ -162,11 +162,25 @@ bool CameraLinux::_can_query_format(int p_file_descriptor, int p_type) {
|
|||||||
return ioctl(p_file_descriptor, VIDIOC_G_FMT, &format) != -1;
|
return ioctl(p_file_descriptor, VIDIOC_G_FMT, &format) != -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
CameraLinux::CameraLinux() {
|
inline void CameraLinux::set_monitoring_feeds(bool p_monitoring_feeds) {
|
||||||
camera_thread.start(CameraLinux::camera_thread_func, this);
|
if (p_monitoring_feeds == monitoring_feeds) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
CameraServer::set_monitoring_feeds(p_monitoring_feeds);
|
||||||
|
if (p_monitoring_feeds) {
|
||||||
|
camera_thread.start(CameraLinux::camera_thread_func, this);
|
||||||
|
} else {
|
||||||
|
exit_flag.set();
|
||||||
|
if (camera_thread.is_started()) {
|
||||||
|
camera_thread.wait_to_finish();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CameraLinux::~CameraLinux() {
|
CameraLinux::~CameraLinux() {
|
||||||
exit_flag.set();
|
exit_flag.set();
|
||||||
camera_thread.wait_to_finish();
|
if (camera_thread.is_started()) {
|
||||||
|
camera_thread.wait_to_finish();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -52,6 +52,8 @@ private:
|
|||||||
bool _can_query_format(int p_file_descriptor, int p_type);
|
bool _can_query_format(int p_file_descriptor, int p_type);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CameraLinux();
|
CameraLinux() = default;
|
||||||
~CameraLinux();
|
~CameraLinux();
|
||||||
|
|
||||||
|
void set_monitoring_feeds(bool p_monitoring_feeds) override;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -37,7 +37,8 @@
|
|||||||
|
|
||||||
class CameraMacOS : public CameraServer {
|
class CameraMacOS : public CameraServer {
|
||||||
public:
|
public:
|
||||||
CameraMacOS();
|
CameraMacOS() = default;
|
||||||
|
|
||||||
void update_feeds();
|
void update_feeds();
|
||||||
|
void set_monitoring_feeds(bool p_monitoring_feeds) override;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -359,10 +359,20 @@ void CameraMacOS::update_feeds() {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
CameraMacOS::CameraMacOS() {
|
void CameraMacOS::set_monitoring_feeds(bool p_monitoring_feeds) {
|
||||||
// Find available cameras we have at this time
|
if (p_monitoring_feeds == monitoring_feeds) {
|
||||||
update_feeds();
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// should only have one of these....
|
CameraServer::set_monitoring_feeds(p_monitoring_feeds);
|
||||||
device_notifications = [[MyDeviceNotifications alloc] initForServer:this];
|
if (p_monitoring_feeds) {
|
||||||
|
// Find available cameras we have at this time.
|
||||||
|
update_feeds();
|
||||||
|
|
||||||
|
// Get notified on feed changes.
|
||||||
|
device_notifications = [[MyDeviceNotifications alloc] initForServer:this];
|
||||||
|
} else {
|
||||||
|
// Stop monitoring feed changes.
|
||||||
|
device_notifications = nil;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -39,6 +39,10 @@
|
|||||||
CameraServer::CreateFunc CameraServer::create_func = nullptr;
|
CameraServer::CreateFunc CameraServer::create_func = nullptr;
|
||||||
|
|
||||||
void CameraServer::_bind_methods() {
|
void CameraServer::_bind_methods() {
|
||||||
|
ClassDB::bind_method(D_METHOD("set_monitoring_feeds", "is_monitoring_feeds"), &CameraServer::set_monitoring_feeds);
|
||||||
|
ClassDB::bind_method(D_METHOD("is_monitoring_feeds"), &CameraServer::is_monitoring_feeds);
|
||||||
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "monitoring_feeds"), "set_monitoring_feeds", "is_monitoring_feeds");
|
||||||
|
|
||||||
ClassDB::bind_method(D_METHOD("get_feed", "index"), &CameraServer::get_feed);
|
ClassDB::bind_method(D_METHOD("get_feed", "index"), &CameraServer::get_feed);
|
||||||
ClassDB::bind_method(D_METHOD("get_feed_count"), &CameraServer::get_feed_count);
|
ClassDB::bind_method(D_METHOD("get_feed_count"), &CameraServer::get_feed_count);
|
||||||
ClassDB::bind_method(D_METHOD("feeds"), &CameraServer::get_feeds);
|
ClassDB::bind_method(D_METHOD("feeds"), &CameraServer::get_feeds);
|
||||||
@ -61,6 +65,10 @@ CameraServer *CameraServer::get_singleton() {
|
|||||||
return singleton;
|
return singleton;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CameraServer::set_monitoring_feeds(bool p_monitoring_feeds) {
|
||||||
|
monitoring_feeds = p_monitoring_feeds;
|
||||||
|
}
|
||||||
|
|
||||||
int CameraServer::get_free_id() {
|
int CameraServer::get_free_id() {
|
||||||
bool id_exists = true;
|
bool id_exists = true;
|
||||||
int newid = 0;
|
int newid = 0;
|
||||||
@ -80,6 +88,8 @@ int CameraServer::get_free_id() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int CameraServer::get_feed_index(int p_id) {
|
int CameraServer::get_feed_index(int p_id) {
|
||||||
|
ERR_FAIL_COND_V_MSG(!monitoring_feeds, -1, "CameraServer is not actively monitoring feeds; call set_monitoring_feeds(true) first.");
|
||||||
|
|
||||||
for (int i = 0; i < feeds.size(); i++) {
|
for (int i = 0; i < feeds.size(); i++) {
|
||||||
if (feeds[i]->get_id() == p_id) {
|
if (feeds[i]->get_id() == p_id) {
|
||||||
return i;
|
return i;
|
||||||
@ -90,6 +100,8 @@ int CameraServer::get_feed_index(int p_id) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Ref<CameraFeed> CameraServer::get_feed_by_id(int p_id) {
|
Ref<CameraFeed> CameraServer::get_feed_by_id(int p_id) {
|
||||||
|
ERR_FAIL_COND_V_MSG(!monitoring_feeds, nullptr, "CameraServer is not actively monitoring feeds; call set_monitoring_feeds(true) first.");
|
||||||
|
|
||||||
int index = get_feed_index(p_id);
|
int index = get_feed_index(p_id);
|
||||||
|
|
||||||
if (index == -1) {
|
if (index == -1) {
|
||||||
@ -129,16 +141,19 @@ void CameraServer::remove_feed(const Ref<CameraFeed> &p_feed) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Ref<CameraFeed> CameraServer::get_feed(int p_index) {
|
Ref<CameraFeed> CameraServer::get_feed(int p_index) {
|
||||||
|
ERR_FAIL_COND_V_MSG(!monitoring_feeds, nullptr, "CameraServer is not actively monitoring feeds; call set_monitoring_feeds(true) first.");
|
||||||
ERR_FAIL_INDEX_V(p_index, feeds.size(), nullptr);
|
ERR_FAIL_INDEX_V(p_index, feeds.size(), nullptr);
|
||||||
|
|
||||||
return feeds[p_index];
|
return feeds[p_index];
|
||||||
}
|
}
|
||||||
|
|
||||||
int CameraServer::get_feed_count() {
|
int CameraServer::get_feed_count() {
|
||||||
|
ERR_FAIL_COND_V_MSG(!monitoring_feeds, 0, "CameraServer is not actively monitoring feeds; call set_monitoring_feeds(true) first.");
|
||||||
return feeds.size();
|
return feeds.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
TypedArray<CameraFeed> CameraServer::get_feeds() {
|
TypedArray<CameraFeed> CameraServer::get_feeds() {
|
||||||
|
ERR_FAIL_COND_V_MSG(!monitoring_feeds, {}, "CameraServer is not actively monitoring feeds; call set_monitoring_feeds(true) first.");
|
||||||
TypedArray<CameraFeed> return_feeds;
|
TypedArray<CameraFeed> return_feeds;
|
||||||
int cc = get_feed_count();
|
int cc = get_feed_count();
|
||||||
return_feeds.resize(cc);
|
return_feeds.resize(cc);
|
||||||
@ -151,6 +166,7 @@ TypedArray<CameraFeed> CameraServer::get_feeds() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
RID CameraServer::feed_texture(int p_id, CameraServer::FeedImage p_texture) {
|
RID CameraServer::feed_texture(int p_id, CameraServer::FeedImage p_texture) {
|
||||||
|
ERR_FAIL_COND_V_MSG(!monitoring_feeds, RID(), "CameraServer is not actively monitoring feeds; call set_monitoring_feeds(true) first.");
|
||||||
int index = get_feed_index(p_id);
|
int index = get_feed_index(p_id);
|
||||||
ERR_FAIL_COND_V(index == -1, RID());
|
ERR_FAIL_COND_V(index == -1, RID());
|
||||||
|
|
||||||
|
|||||||
@ -64,6 +64,7 @@ private:
|
|||||||
protected:
|
protected:
|
||||||
static CreateFunc create_func;
|
static CreateFunc create_func;
|
||||||
|
|
||||||
|
bool monitoring_feeds = false;
|
||||||
Vector<Ref<CameraFeed>> feeds;
|
Vector<Ref<CameraFeed>> feeds;
|
||||||
|
|
||||||
static CameraServer *singleton;
|
static CameraServer *singleton;
|
||||||
@ -88,6 +89,9 @@ public:
|
|||||||
return server;
|
return server;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
virtual void set_monitoring_feeds(bool p_monitoring_feeds);
|
||||||
|
_FORCE_INLINE_ bool is_monitoring_feeds() const { return monitoring_feeds; }
|
||||||
|
|
||||||
// Right now we identify our feed by it's ID when it's used in the background.
|
// Right now we identify our feed by it's ID when it's used in the background.
|
||||||
// May see if we can change this to purely relying on CameraFeed objects or by name.
|
// May see if we can change this to purely relying on CameraFeed objects or by name.
|
||||||
int get_free_id();
|
int get_free_id();
|
||||||
|
|||||||
Reference in New Issue
Block a user