From 0c84ea6d71bc30ee2aa7f61c6502ea379cc51960 Mon Sep 17 00:00:00 2001 From: Edgar Thier Date: Fri, 16 Dec 2022 08:56:44 +0100 Subject: [PATCH] gst: Fix device lost notification handling for closed devices --- src/CaptureDeviceImpl.cpp | 2 ++ src/gstreamer-1.0/tcamsrc/gsttcammainsrc.cpp | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/CaptureDeviceImpl.cpp b/src/CaptureDeviceImpl.cpp index fede17c2e..ebab9044c 100644 --- a/src/CaptureDeviceImpl.cpp +++ b/src/CaptureDeviceImpl.cpp @@ -87,6 +87,8 @@ CaptureDeviceImpl::~CaptureDeviceImpl() available_output_formats_.clear(); + index_.remove_device_lost(deviceindex_lost_cb); + device_.reset(); } diff --git a/src/gstreamer-1.0/tcamsrc/gsttcammainsrc.cpp b/src/gstreamer-1.0/tcamsrc/gsttcammainsrc.cpp index f0e00dfbd..122fed108 100644 --- a/src/gstreamer-1.0/tcamsrc/gsttcammainsrc.cpp +++ b/src/gstreamer-1.0/tcamsrc/gsttcammainsrc.cpp @@ -341,6 +341,19 @@ static void gst_tcam_mainsrc_device_lost_callback(const tcam::tcam_device_info* { GstTcamMainSrc* self = (GstTcamMainSrc*)user_data; + GstState state; + + // wait for 1 seconds max + gst_element_get_state(GST_ELEMENT(self), &state, nullptr, 1000000000); + + if (!self->device || state == GST_STATE_NULL) + { + // device does not exist + // or source is null (aka no device exists) + // do nothing + return; + } + if (!self->device->is_streaming_) { return; @@ -386,6 +399,8 @@ static bool gst_tcam_mainsrc_init_camera(GstTcamMainSrc* self) return false; } + // this cb will automatically be deleted once the device is closed + // no need for explicit cleanup self->device->device_->register_device_lost_callback(gst_tcam_mainsrc_device_lost_callback, self);