mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer.git
synced 2024-11-23 10:11:08 +00:00
openni2src: Some random cleanup and minor fixes
This commit is contained in:
parent
64675f0712
commit
1ba3edf682
1 changed files with 117 additions and 110 deletions
|
@ -41,20 +41,14 @@
|
|||
#endif
|
||||
|
||||
#include "gstopenni2src.h"
|
||||
\
|
||||
|
||||
GST_DEBUG_CATEGORY_STATIC (openni2src_debug);
|
||||
static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
|
||||
GST_PAD_SRC,
|
||||
GST_PAD_ALWAYS,
|
||||
GST_STATIC_CAPS ("video/x-raw, "
|
||||
"format = (string) {RGBA, RGB, GRAY16_LE} "
|
||||
"framerate = (fraction) [0/1, MAX], "
|
||||
"width = (int) [ 1, MAX ], "
|
||||
"height = (int) [ 1, MAX ]")
|
||||
GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{RGBA, RGB, GRAY16_LE}"))
|
||||
);
|
||||
|
||||
static GstElementClass *parent_class = NULL;
|
||||
|
||||
enum
|
||||
{
|
||||
PROP_0,
|
||||
|
@ -80,7 +74,8 @@ gst_openni2_src_sourcetype_get_type (void)
|
|||
static const GEnumValue values[] = {
|
||||
{SOURCETYPE_DEPTH, "Get depth readings", "depth"},
|
||||
{SOURCETYPE_COLOR, "Get color readings", "color"},
|
||||
{SOURCETYPE_BOTH, "Get color and depth (as alpha) readings - EXPERIMENTAL",
|
||||
{SOURCETYPE_BOTH,
|
||||
"Get color and depth (as alpha) readings - EXPERIMENTAL",
|
||||
"both"},
|
||||
{0, NULL, NULL},
|
||||
};
|
||||
|
@ -107,8 +102,7 @@ static GstStateChangeReturn gst_openni2_src_change_state (GstElement * element,
|
|||
GstStateChange transition);
|
||||
|
||||
/* pushsrc method */
|
||||
static GstFlowReturn gst_openni2src_fill (GstPushSrc * src,
|
||||
GstBuffer * buf);
|
||||
static GstFlowReturn gst_openni2src_fill (GstPushSrc * src, GstBuffer * buf);
|
||||
|
||||
/* OpenNI2 interaction methods */
|
||||
static gboolean openni2_initialise_library ();
|
||||
|
@ -117,10 +111,10 @@ static GstFlowReturn openni2_read_gstbuffer (GstOpenni2Src * src,
|
|||
GstBuffer * buf);
|
||||
static void openni2_finalise (GstOpenni2Src * src);
|
||||
|
||||
#define parent_class gst_openni2_src_parent_class
|
||||
G_DEFINE_TYPE (GstOpenni2Src, gst_openni2_src, GST_TYPE_PUSH_SRC)
|
||||
|
||||
static void
|
||||
gst_openni2_src_class_init (GstOpenni2SrcClass * klass)
|
||||
static void gst_openni2_src_class_init (GstOpenni2SrcClass * klass)
|
||||
{
|
||||
GObjectClass *gobject_class;
|
||||
GstPushSrcClass *pushsrc_class;
|
||||
|
@ -130,7 +124,6 @@ gst_openni2_src_class_init (GstOpenni2SrcClass * klass)
|
|||
gobject_class = (GObjectClass *) klass;
|
||||
basesrc_class = (GstBaseSrcClass *) klass;
|
||||
pushsrc_class = (GstPushSrcClass *) klass;
|
||||
parent_class = (GstElementClass *) g_type_class_peek_parent (klass);
|
||||
|
||||
gobject_class->dispose = gst_openni2_src_dispose;
|
||||
gobject_class->finalize = gst_openni2_src_finalize;
|
||||
|
@ -139,8 +132,7 @@ gst_openni2_src_class_init (GstOpenni2SrcClass * klass)
|
|||
g_object_class_install_property
|
||||
(gobject_class, PROP_LOCATION,
|
||||
g_param_spec_string ("location", "Location",
|
||||
"Source uri, can be a file or a device.", "",
|
||||
(GParamFlags)
|
||||
"Source uri, can be a file or a device.", "", (GParamFlags)
|
||||
(G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
|
||||
g_object_class_install_property (gobject_class, PROP_SOURCETYPE,
|
||||
g_param_spec_enum ("sourcetype",
|
||||
|
@ -183,8 +175,10 @@ static void
|
|||
gst_openni2_src_dispose (GObject * object)
|
||||
{
|
||||
GstOpenni2Src *ni2src = GST_OPENNI2_SRC (object);
|
||||
|
||||
if (ni2src->gst_caps)
|
||||
gst_caps_unref (ni2src->gst_caps);
|
||||
|
||||
G_OBJECT_CLASS (parent_class)->dispose (object);
|
||||
}
|
||||
|
||||
|
@ -225,6 +219,7 @@ gst_openni2_src_set_property (GObject * object, guint prop_id,
|
|||
openni2src->uri_name = NULL;
|
||||
}
|
||||
openni2src->uri_name = g_value_dup_string (value);
|
||||
|
||||
/* Action! */
|
||||
openni2_initialise_devices (openni2src);
|
||||
break;
|
||||
|
@ -270,6 +265,7 @@ gst_openni2_src_start (GstBaseSrc * bsrc)
|
|||
{
|
||||
GstOpenni2Src *src = GST_OPENNI2_SRC (bsrc);
|
||||
openni::Status rc = openni::STATUS_OK;
|
||||
|
||||
if (src->depth.isValid ()) {
|
||||
rc = src->depth.start ();
|
||||
if (rc != openni::STATUS_OK) {
|
||||
|
@ -278,6 +274,7 @@ gst_openni2_src_start (GstBaseSrc * bsrc)
|
|||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
if (src->color.isValid ()) {
|
||||
rc = src->color.start ();
|
||||
if (rc != openni::STATUS_OK) {
|
||||
|
@ -286,6 +283,7 @@ gst_openni2_src_start (GstBaseSrc * bsrc)
|
|||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
@ -298,6 +296,7 @@ gst_openni2_src_stop (GstBaseSrc * bsrc)
|
|||
src->depth.stop ();
|
||||
if (src->color.isValid ())
|
||||
src->color.stop ();
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
@ -306,15 +305,17 @@ gst_openni2_src_get_caps (GstBaseSrc * src, GstCaps * filter)
|
|||
{
|
||||
GstOpenni2Src *ni2src;
|
||||
GstCaps *caps;
|
||||
|
||||
ni2src = GST_OPENNI2_SRC (src);
|
||||
|
||||
GST_OBJECT_LOCK (ni2src);
|
||||
if (ni2src->gst_caps) {
|
||||
GST_OBJECT_UNLOCK (ni2src);
|
||||
return (filter)
|
||||
? gst_caps_intersect_full (filter, ni2src->gst_caps, GST_CAPS_INTERSECT_FIRST)
|
||||
? gst_caps_intersect_full (filter, ni2src->gst_caps,
|
||||
GST_CAPS_INTERSECT_FIRST)
|
||||
: gst_caps_ref (ni2src->gst_caps);
|
||||
}
|
||||
|
||||
// If we are here, we need to compose the caps and return them.
|
||||
caps = gst_caps_new_empty ();
|
||||
if (ni2src->colorpixfmt != openni::PIXEL_FORMAT_RGB888)
|
||||
|
@ -327,30 +328,28 @@ gst_openni2_src_get_caps (GstBaseSrc * src, GstCaps * filter)
|
|||
"framerate", GST_TYPE_FRACTION, ni2src->fps, 1,
|
||||
"pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
|
||||
"width", G_TYPE_INT, ni2src->width,
|
||||
"height", G_TYPE_INT, ni2src->height,
|
||||
NULL);
|
||||
"height", G_TYPE_INT, ni2src->height, NULL);
|
||||
} else if (ni2src->depth.isValid () && ni2src->sourcetype == SOURCETYPE_DEPTH) {
|
||||
caps = gst_caps_new_simple ("video/x-raw",
|
||||
"format", G_TYPE_STRING, "GRAY16_LE",
|
||||
"framerate", GST_TYPE_FRACTION, ni2src->fps, 1,
|
||||
"pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
|
||||
"width", G_TYPE_INT, ni2src->width,
|
||||
"height", G_TYPE_INT, ni2src->height,
|
||||
NULL);
|
||||
"height", G_TYPE_INT, ni2src->height, NULL);
|
||||
} else if (ni2src->color.isValid () && ni2src->sourcetype == SOURCETYPE_COLOR) {
|
||||
caps = gst_caps_new_simple ("video/x-raw",
|
||||
"format", G_TYPE_STRING, "RGB",
|
||||
"framerate", GST_TYPE_FRACTION, ni2src->fps, 1,
|
||||
"pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
|
||||
"width", G_TYPE_INT, ni2src->width,
|
||||
"height", G_TYPE_INT, ni2src->height,
|
||||
NULL);
|
||||
"height", G_TYPE_INT, ni2src->height, NULL);
|
||||
}
|
||||
GST_INFO_OBJECT (ni2src, "probed caps: %" GST_PTR_FORMAT, caps);
|
||||
ni2src->gst_caps = gst_caps_ref (caps);
|
||||
GST_OBJECT_UNLOCK (ni2src);
|
||||
return (filter)
|
||||
? gst_caps_intersect_full (filter, ni2src->gst_caps, GST_CAPS_INTERSECT_FIRST)
|
||||
? gst_caps_intersect_full (filter, ni2src->gst_caps,
|
||||
GST_CAPS_INTERSECT_FIRST)
|
||||
: gst_caps_ref (ni2src->gst_caps);
|
||||
}
|
||||
|
||||
|
@ -420,7 +419,8 @@ openni2_initialise_library ()
|
|||
openni::Status rc = openni::STATUS_OK;
|
||||
rc = openni::OpenNI::initialize ();
|
||||
if (rc != openni::STATUS_OK) {
|
||||
GST_ERROR("Initialization failed: %s", openni::OpenNI::getExtendedError ());
|
||||
GST_ERROR ("Initialization failed: %s",
|
||||
openni::OpenNI::getExtendedError ());
|
||||
openni::OpenNI::shutdown ();
|
||||
return GST_FLOW_ERROR;
|
||||
}
|
||||
|
@ -432,6 +432,7 @@ openni2_initialise_devices (GstOpenni2Src * src)
|
|||
{
|
||||
openni::Status rc = openni::STATUS_OK;
|
||||
const char *deviceURI = openni::ANY_DEVICE;
|
||||
|
||||
if (src->uri_name)
|
||||
deviceURI = src->uri_name;
|
||||
|
||||
|
@ -452,9 +453,10 @@ openni2_initialise_devices (GstOpenni2Src * src)
|
|||
GST_ERROR_OBJECT (src, "%s", openni::OpenNI::getExtendedError ());
|
||||
src->depth.destroy ();
|
||||
}
|
||||
} else
|
||||
} else {
|
||||
GST_WARNING_OBJECT (src, "Couldn't find depth stream: %s",
|
||||
openni::OpenNI::getExtendedError ());
|
||||
}
|
||||
|
||||
/** color sensor **/
|
||||
rc = src->color.create (src->device, openni::SENSOR_COLOR);
|
||||
|
@ -465,10 +467,10 @@ openni2_initialise_devices (GstOpenni2Src * src)
|
|||
openni::OpenNI::getExtendedError ());
|
||||
src->color.destroy ();
|
||||
}
|
||||
} else
|
||||
} else {
|
||||
GST_WARNING_OBJECT (src, "Couldn't find color stream: %s",
|
||||
openni::OpenNI::getExtendedError ());
|
||||
|
||||
}
|
||||
|
||||
if (!src->depth.isValid () && !src->color.isValid ()) {
|
||||
GST_ERROR_OBJECT (src, "No valid streams. Exiting\n");
|
||||
|
@ -528,6 +530,7 @@ openni2_read_gstbuffer (GstOpenni2Src * src, GstBuffer * buf)
|
|||
openni::Status rc = openni::STATUS_OK;
|
||||
openni::VideoStream * pStream = &(src->depth);
|
||||
int changedStreamDummy;
|
||||
GstMapInfo info;
|
||||
|
||||
/* Block until we get some data */
|
||||
rc = openni::OpenNI::waitForAnyStream (&pStream, 1, &changedStreamDummy,
|
||||
|
@ -538,7 +541,6 @@ openni2_read_gstbuffer (GstOpenni2Src * src, GstBuffer * buf)
|
|||
return GST_FLOW_ERROR;
|
||||
}
|
||||
|
||||
GstMapInfo info;
|
||||
if (src->depth.isValid () && src->color.isValid () &&
|
||||
src->sourcetype == SOURCETYPE_BOTH) {
|
||||
rc = src->depth.readFrame (&src->depthFrame);
|
||||
|
@ -554,23 +556,27 @@ openni2_read_gstbuffer (GstOpenni2Src * src, GstBuffer * buf)
|
|||
return GST_FLOW_ERROR;
|
||||
}
|
||||
if ((src->colorFrame.getStrideInBytes () != src->colorFrame.getWidth ()) ||
|
||||
(src->depthFrame.getStrideInBytes() != 2*src->depthFrame.getWidth())) {
|
||||
(src->depthFrame.getStrideInBytes () !=
|
||||
2 * src->depthFrame.getWidth ())) {
|
||||
// This case is not handled - yet :B
|
||||
GST_ERROR_OBJECT (src, "Stride does not coincide with width");
|
||||
return GST_FLOW_ERROR;
|
||||
}
|
||||
|
||||
int framesize = src->colorFrame.getDataSize() + src->depthFrame.getDataSize()/2;
|
||||
int framesize =
|
||||
src->colorFrame.getDataSize () + src->depthFrame.getDataSize () / 2;
|
||||
buf = gst_buffer_new_and_alloc (framesize);
|
||||
/* Copy colour information */
|
||||
gst_buffer_map (buf, &info, (GstMapFlags) (GST_MAP_WRITE));
|
||||
memcpy(info.data, src->colorFrame.getData(), src->colorFrame.getDataSize());
|
||||
memcpy (info.data, src->colorFrame.getData (),
|
||||
src->colorFrame.getDataSize ());
|
||||
guint8 *pData = info.data + src->colorFrame.getDataSize ();
|
||||
/* Add depth as 8bit alpha channel, depth is 16bit samples. */
|
||||
guint16 *pDepth = (guint16 *) src->depthFrame.getData ();
|
||||
for (int i = 0; i < src->depthFrame.getDataSize () / 2; ++i)
|
||||
pData[i] = pDepth[i] >> 8;
|
||||
GST_WARNING_OBJECT (src, "sending buffer (%d+%d)B [%08llu]",
|
||||
GST_BUFFER_PTS (buf) = src->colorFrame.getTimestamp () * 1000;
|
||||
GST_LOG_OBJECT (src, "sending buffer (%d+%d)B [%08llu]",
|
||||
src->colorFrame.getDataSize (),
|
||||
src->depthFrame.getDataSize (),
|
||||
(long long) src->depthFrame.getTimestamp ());
|
||||
|
@ -593,7 +599,7 @@ openni2_read_gstbuffer (GstOpenni2Src * src, GstBuffer * buf)
|
|||
gst_buffer_map (buf, &info, (GstMapFlags) (GST_MAP_WRITE));
|
||||
memcpy (info.data, src->depthFrame.getData (), framesize);
|
||||
GST_BUFFER_PTS (buf) = src->depthFrame.getTimestamp () * 1000;
|
||||
GST_WARNING_OBJECT (src, "sending buffer (%dx%d)=%dB [%08llu]",
|
||||
GST_LOG_OBJECT (src, "sending buffer (%dx%d)=%dB [%08llu]",
|
||||
src->depthFrame.getWidth (),
|
||||
src->depthFrame.getHeight (),
|
||||
src->depthFrame.getDataSize (),
|
||||
|
@ -611,12 +617,13 @@ openni2_read_gstbuffer (GstOpenni2Src * src, GstBuffer * buf)
|
|||
GST_ERROR_OBJECT (src, "Stride does not coincide with width");
|
||||
return GST_FLOW_ERROR;
|
||||
}
|
||||
|
||||
int framesize = src->colorFrame.getDataSize ();
|
||||
buf = gst_buffer_new_and_alloc (framesize);
|
||||
gst_buffer_map (buf, &info, (GstMapFlags) (GST_MAP_WRITE));
|
||||
memcpy (info.data, src->depthFrame.getData (), framesize);
|
||||
GST_BUFFER_PTS (buf) = src->colorFrame.getTimestamp () * 1000;
|
||||
GST_WARNING_OBJECT (src, "sending buffer (%dx%d)=%dB [%08llu]",
|
||||
GST_LOG_OBJECT (src, "sending buffer (%dx%d)=%dB [%08llu]",
|
||||
src->colorFrame.getWidth (),
|
||||
src->colorFrame.getHeight (),
|
||||
src->colorFrame.getDataSize (),
|
||||
|
|
Loading…
Reference in a new issue