pylonsrc: fixed Pylon environment initialization

This commit is contained in:
mrstecklo 2020-11-09 17:53:21 +03:00 committed by joshdoe
parent b2f88a5bde
commit 4910bea34c
2 changed files with 48 additions and 12 deletions

View File

@ -41,6 +41,34 @@
#include "common/genicampixelformat.h"
static int plugin_counter = 0;
int gst_pylonsrc_ref_pylon_environment()
{
if(plugin_counter == 0) {
GST_DEBUG("pylonsrc: Initializing Pylon environment");
if(PylonInitialize() != GENAPI_E_OK) {
return -1;
}
}
return ++plugin_counter;
}
int gst_pylonsrc_unref_pylon_environment()
{
if(plugin_counter == 1) {
GST_DEBUG("pylonsrc: Terminating Pylon environment");
if(PylonTerminate() != GENAPI_E_OK) {
return -1;
}
}
if(plugin_counter > 0) {
plugin_counter--;
}
return plugin_counter;
}
/* PylonC */
_Bool pylonc_reset_camera (GstPylonSrc * src);
@ -48,8 +76,6 @@ _Bool pylonc_connect_camera (GstPylonSrc * src);
void pylonc_disconnect_camera (GstPylonSrc * src);
void pylonc_print_camera_info (GstPylonSrc * src,
PYLON_DEVICE_HANDLE deviceHandle, int deviceId);
void pylonc_terminate ();
/* debug category */
GST_DEBUG_CATEGORY_STATIC (gst_pylonsrc_debug_category);
@ -1240,13 +1266,13 @@ gst_pylonsrc_connect_device (GstPylonSrc * src)
size_t numDevices;
pylonc_reset_camera (src);
pylonc_disconnect_camera (src);
pylonc_terminate ();
gst_pylonsrc_unref_pylon_environment ();
GST_DEBUG_OBJECT (src,
"Camera reset. Waiting 6 seconds for it to fully reboot.");
g_usleep (6 * G_USEC_PER_SEC);
PylonInitialize ();
gst_pylonsrc_ref_pylon_environment();
res = PylonEnumerateDevices (&numDevices);
PYLONC_CHECK_ERROR (src, res);
@ -2718,13 +2744,17 @@ gst_pylonsrc_start (GstBaseSrc * bsrc)
{
GstPylonSrc *src = GST_PYLONSRC (bsrc);
if (PylonInitialize () != 0) {
const int count = gst_pylonsrc_ref_pylon_environment();
if (count <= 0) {
GST_ELEMENT_ERROR (src, RESOURCE, FAILED,
("Failed to initialise the camera"),
("Pylon library initialization failed"));
goto error;
} else if (count == 1) {
GST_DEBUG_OBJECT(src, "First object created");
}
if (!gst_pylonsrc_select_device (src) ||
!gst_pylonsrc_connect_device (src) || !gst_pylonsrc_set_resolution (src))
goto error;
@ -2874,17 +2904,13 @@ gst_pylonsrc_finalize (GObject * object)
g_free(src->userid);
pylonc_terminate ();
if(gst_pylonsrc_unref_pylon_environment () == 0) {
GST_DEBUG_OBJECT(src, "Last object finalized");
}
G_OBJECT_CLASS (gst_pylonsrc_parent_class)->finalize (object);
}
/* PylonC functions */
void
pylonc_terminate ()
{
PylonTerminate ();
}
void
pylonc_disconnect_camera (GstPylonSrc * src)

View File

@ -24,6 +24,16 @@
#include <gst/base/gstpushsrc.h>
#include "pylonc/PylonC.h"
// pylonsrc plugin calls PylonInitialize when first plugin is created
// and PylonTerminate when the last plugin is finalized.
// Static variable is used to keep count of existing plugins
// These functions can be used to increase or decrease this counter,
// if pylon environment is needed beyond lifetime of plugins.
// On success return value is the new value of counter
// On failure return value is negative
int gst_pylonsrc_ref_pylon_environment();
int gst_pylonsrc_unref_pylon_environment();
enum {
GST_PYLONSRC_NUM_CAPTURE_BUFFERS = 10
};