From a4ff2a325f7f75453eca8912faf89af88c12279f Mon Sep 17 00:00:00 2001 From: brunoherbelin Date: Fri, 25 Sep 2020 21:44:38 +0200 Subject: [PATCH] Improved detection of unplugged device --- DeviceSource.cpp | 17 ++++++++++++++--- DeviceSource.h | 2 ++ GlmToolkit.h | 2 +- Stream.cpp | 9 ++++----- Stream.h | 2 +- 5 files changed, 22 insertions(+), 10 deletions(-) diff --git a/DeviceSource.cpp b/DeviceSource.cpp index 3c25243..4753aaa 100644 --- a/DeviceSource.cpp +++ b/DeviceSource.cpp @@ -23,7 +23,6 @@ Device::callback_device_monitor (GstBus * bus, GstMessage * message, gpointer us { GstDevice *device; gchar *name; - gchar *stru; switch (GST_MESSAGE_TYPE (message)) { case GST_MESSAGE_DEVICE_ADDED: { @@ -41,6 +40,9 @@ Device::callback_device_monitor (GstBus * bus, GstMessage * message, gpointer us DeviceConfigSet confs = getDeviceConfigs(pipe.str()); manager().src_config_.push_back(confs); + manager().list_uptodate_ = false; + +// gchar *stru; // stru = gst_structure_to_string( gst_device_get_properties(device) ); // g_print("New device %s \n", stru); @@ -54,6 +56,8 @@ Device::callback_device_monitor (GstBus * bus, GstMessage * message, gpointer us // g_print("Device removed: %s\n", name); g_free (name); + manager().list_uptodate_ = false; + gst_object_unref (device); } break; @@ -126,7 +130,7 @@ Device::Device() src_config_.push_back(confs); } g_list_free(devices); - + list_uptodate_ = true; } @@ -141,6 +145,13 @@ bool Device::exists(const std::string &device) const return d != src_name_.end(); } +bool Device::unplugged(const std::string &device) const +{ + if (list_uptodate_) + return false; + return !exists(device); +} + std::string Device::name(int index) const { if (index > -1 && index < src_name_.size()) @@ -240,7 +251,7 @@ void DeviceSource::accept(Visitor& v) bool DeviceSource::failed() const { - return stream_->failed() || !Device::manager().exists(device_); + return stream_->failed() || Device::manager().unplugged(device_); } diff --git a/DeviceSource.h b/DeviceSource.h index 3487a3d..d2ccc6e 100644 --- a/DeviceSource.h +++ b/DeviceSource.h @@ -72,6 +72,7 @@ public: DeviceConfigSet config (int index) const; bool exists (const std::string &device) const; + bool unplugged (const std::string &device) const; int index (const std::string &device) const; static gboolean callback_device_monitor (GstBus *, GstMessage *, gpointer); @@ -85,6 +86,7 @@ private: std::vector< DeviceConfigSet > src_config_; static DeviceConfigSet getDeviceConfigs(const std::string &src_description); + bool list_uptodate_; GstDeviceMonitor *monitor_; }; diff --git a/GlmToolkit.h b/GlmToolkit.h index b1f78e3..8855080 100644 --- a/GlmToolkit.h +++ b/GlmToolkit.h @@ -46,7 +46,7 @@ public: static const char* aspect_ratio_names[6] = { "1:1", "4:3", "3:2", "16:10", "16:9", "21:9" }; -static const char* height_names[9] = { "16", "64", "200", "320", "480", "720p", "1080p", "1440", "4K" }; +static const char* height_names[10] = { "16", "64", "200", "320", "480", "576", "720p", "1080p", "1440", "4K" }; glm::ivec2 resolutionFromDescription(int aspectratio, int height); diff --git a/Stream.cpp b/Stream.cpp index f488081..b102283 100644 --- a/Stream.cpp +++ b/Stream.cpp @@ -31,8 +31,8 @@ Stream::Stream() description_ = "undefined"; pipeline_ = nullptr; - width_ = 800; - height_ = 600; + width_ = -1; + height_ = -1; single_frame_ = false; live_ = false; ready_ = false; @@ -76,9 +76,8 @@ void Stream::open(const std::string &gstreamer_description, int w, int h) { // set gstreamer pipeline source description_ = gstreamer_description; - - if (w>0) width_ = w; - if (h>0) height_ = h; + width_ = w; + height_ = h; // close before re-openning if (isOpen()) diff --git a/Stream.h b/Stream.h index 7d3307a..ea29768 100644 --- a/Stream.h +++ b/Stream.h @@ -29,7 +29,7 @@ public: /** * Open a media using gstreamer pipeline keyword * */ - void open(const std::string &gstreamer_description, int w = -1, int h = -1); + void open(const std::string &gstreamer_description, int w = 1024, int h = 576); /** * Get description string * */