From e68eb7bb232fe83e03ae699d932cf9fcbfeec192 Mon Sep 17 00:00:00 2001 From: Santosh Mahto Date: Mon, 24 Feb 2025 19:46:12 +0530 Subject: [PATCH] analytics: Add python api to get relation path A new api `Mtd.relation_path()` is added to get relation chain between two Mtd in RelationMeta. Usage: ``` for i in mtd1.relation_path(mtd2, max_span=4, relation_type=...): pass ``` Part-of: --- .../gst-python/gi/overrides/GstAnalytics.py | 3 ++ .../gi/overrides/gstanalyticsmodule.c | 36 +++++++++++++++++++ .../gst-python/testsuite/test_analytics.py | 10 ++++++ 3 files changed, 49 insertions(+) diff --git a/subprojects/gst-python/gi/overrides/GstAnalytics.py b/subprojects/gst-python/gi/overrides/GstAnalytics.py index 0d6cbc21b4..ceb0dfb5fb 100644 --- a/subprojects/gst-python/gi/overrides/GstAnalytics.py +++ b/subprojects/gst-python/gi/overrides/GstAnalytics.py @@ -50,6 +50,9 @@ class Mtd(GstAnalytics.Mtd): return _gi_gst_analytics.AnalyticsMtdDirectRelatedIterator( sys.modules[__name__], self, relation, mtd_type) + def relation_path(self, mtd, max_span = 0, reltype = GstAnalytics.RelTypes.ANY): + return _gi_gst_analytics.AnalyticsMtdRelationPath( + sys.modules[__name__], self, mtd.get_id(), max_span, reltype); __all__.append('Mtd') diff --git a/subprojects/gst-python/gi/overrides/gstanalyticsmodule.c b/subprojects/gst-python/gi/overrides/gstanalyticsmodule.c index 363e5b70b5..c9bc61cad3 100644 --- a/subprojects/gst-python/gi/overrides/gstanalyticsmodule.c +++ b/subprojects/gst-python/gi/overrides/gstanalyticsmodule.c @@ -219,7 +219,43 @@ _gst_analytics_relation_meta_iterator_next (_GstAnalyticsRelationMetaIterator * return py_result; } +static PyObject * +_gi_gst_analytics_mtd_relation_path (PyObject * self, PyObject * args) +{ + PyObject *py_module = NULL; + PyObject *py_mtd; + guint id; + guint max_span; + GstAnalyticsRelTypes reltype; + GArray *relation_path; + GstAnalyticsMtd *mtd; + PyObject *pathList; + + if (!PyArg_ParseTuple (args, "OOIII", &py_module, &py_mtd, &id, &max_span, + &reltype)) { + return NULL; + } + + relation_path = g_array_new (FALSE, FALSE, sizeof (guint)); + mtd = (GstAnalyticsMtd *) pygobject_get (py_mtd); + gst_analytics_relation_meta_exist (mtd->meta, mtd->id, id, + max_span, reltype, &relation_path); + + pathList = PyList_New (relation_path->len); + for (guint i = 0; i < relation_path->len; i++) { + guint id = g_array_index (relation_path, guint, i); + PyList_SetItem (pathList, i, PyLong_FromUnsignedLong (id)); + } + g_array_free (relation_path, TRUE); + + return pathList; +} + static PyMethodDef _gi_gst_analytics_functions[] = { + {"AnalyticsMtdRelationPath", + (PyCFunction) _gi_gst_analytics_mtd_relation_path, + METH_VARARGS, + "Returns the relation path between two Mtd"}, {NULL, NULL, 0, NULL} }; diff --git a/subprojects/gst-python/testsuite/test_analytics.py b/subprojects/gst-python/testsuite/test_analytics.py index 13dc0461cf..f124ae04da 100644 --- a/subprojects/gst-python/testsuite/test_analytics.py +++ b/subprojects/gst-python/testsuite/test_analytics.py @@ -325,3 +325,13 @@ class TestAnalyticsRelationMetaIterator(TestCase): assert(count == 2) + # Create a relation path as od_mtd -> cls_mtd -> trk_mtd -> seg_mtd + rmeta.set_relation(GstAnalytics.RelTypes.NONE, od_mtd.id, trk_mtd.id) # clear relation + rmeta.set_relation(GstAnalytics.RelTypes.RELATE_TO, cls_mtd.id, trk_mtd.id) + rmeta.set_relation(GstAnalytics.RelTypes.RELATE_TO, trk_mtd.id, seg_mtd.id) + count = 0 + expected_rel_ids = [od_mtd.id, cls_mtd.id, trk_mtd.id, seg_mtd.id] + for i in od_mtd.relation_path(seg_mtd, max_span=4): + assert i == expected_rel_ids[count] + count += 1 + assert(count == 4)