[Common/CodeClean] function to get pad caps
authorJaeyun <jy1210.jung@samsung.com>
Thu, 6 May 2021 04:43:59 +0000 (13:43 +0900)
committerSangjung Woo <again4you@gmail.com>
Fri, 7 May 2021 06:13:04 +0000 (15:13 +0900)
Code clean, revise function name and remove unnecessary function to check peer caps.

Signed-off-by: Jaeyun <jy1210.jung@samsung.com>
gst/nnstreamer/include/nnstreamer_plugin_api.h
gst/nnstreamer/tensor_common.c
gst/nnstreamer/tensor_converter/tensor_converter.c
gst/nnstreamer/tensor_demux/gsttensordemux.c
gst/nnstreamer/tensor_if/gsttensorif.c
tests/common/unittest_common.cc

index 2a28915..045204a 100644 (file)
@@ -364,20 +364,12 @@ gst_tensor_get_type_string (tensor_type type);
 
 /**
  * @brief Get tensors caps from tensors config and caps of the peer connected to the pad
+ * @param pad GstPad to check peer caps
  * @param config tensors config structure
- * @param pad GstPad to check if it supports other/tensor
  * @return caps for given config and pad. Caller is responsible for unreffing the returned caps.
  */
 extern GstCaps *
-gst_tensors_get_caps (GstPad * pad, const GstTensorsConfig * config);
-
-/**
- * @brief Check whether the peer connected to the pad supports other/tensor or not
- * @param pad GstPad to check if the peer pad supports other/tensor
- * @return TRUE if other/tensor, FALSE if not
- */
-extern gboolean
-gst_pad_peer_has_tensor_caps (GstPad * pad);
+gst_tensors_get_pad_caps (GstPad * pad, const GstTensorsConfig * config);
 
 /**
  * @brief Find the index value of the given key string array
index ffa36e1..2a0bfc3 100644 (file)
@@ -892,20 +892,48 @@ gst_tensors_config_from_peer (GstPad * pad, GstTensorsConfig * config,
 }
 
 /**
+ * @brief Check whether the peer connected to the pad supports other/tensor or not
+ * @param pad GstPad to check peer caps
+ * @return TRUE if other/tensor, FALSE if not.
+ */
+static gboolean
+_peer_has_tensor_caps (GstPad * pad)
+{
+  GstCaps *peer_caps;
+  gboolean ret = FALSE;
+
+  peer_caps = gst_pad_peer_query_caps (pad, NULL);
+  if (peer_caps) {
+    GstCaps *caps = gst_caps_from_string (GST_TENSOR_CAP_DEFAULT);
+    GstCaps *intersection =
+        gst_caps_intersect_full (caps, peer_caps, GST_CAPS_INTERSECT_FIRST);
+
+    if (!gst_caps_is_empty (intersection))
+      ret = TRUE;
+
+    gst_caps_unref (caps);
+    gst_caps_unref (intersection);
+    gst_caps_unref (peer_caps);
+  }
+
+  return ret;
+}
+
+/**
  * @brief Get tensors caps from tensors config and caps of the peer connected to the pad
- * @param pad GstPad to check if it supports other/tensor
+ * @param pad GstPad to check peer caps
  * @param config tensors config structure
- * @return caps for given config and pad
+ * @return caps for given config and pad. Caller is responsible for unreffing the returned caps.
  */
 GstCaps *
-gst_tensors_get_caps (GstPad * pad, const GstTensorsConfig * config)
+gst_tensors_get_pad_caps (GstPad * pad, const GstTensorsConfig * config)
 {
   GstCaps *caps = NULL;
 
   g_return_val_if_fail (GST_IS_PAD (pad), NULL);
   g_return_val_if_fail (config != NULL, NULL);
 
-  if (config->info.num_tensors == 1 && gst_pad_peer_has_tensor_caps (pad)) {
+  if (config->info.num_tensors == 1 && _peer_has_tensor_caps (pad)) {
     GstTensorConfig tensor_config;
 
     gst_tensor_config_init (&tensor_config);
@@ -921,34 +949,6 @@ gst_tensors_get_caps (GstPad * pad, const GstTensorsConfig * config)
 }
 
 /**
- * @brief Check whether the peer connected to the pad supports other/tensor or not
- * @param pad GstPad to check if the peer pad supports other/tensor
- * @return TRUE if other/tensor, FALSE if not
- */
-gboolean
-gst_pad_peer_has_tensor_caps (GstPad * pad)
-{
-  GstCaps *peer_caps;
-  gboolean ret = FALSE;
-
-  peer_caps = gst_pad_peer_query_caps (pad, NULL);
-  if (peer_caps) {
-    GstCaps *caps = gst_caps_from_string (GST_TENSOR_CAP_DEFAULT);
-    GstCaps *intersection =
-        gst_caps_intersect_full (caps, peer_caps, GST_CAPS_INTERSECT_FIRST);
-
-    if (!gst_caps_is_empty (intersection))
-      ret = TRUE;
-
-    gst_caps_unref (caps);
-    gst_caps_unref (intersection);
-    gst_caps_unref (peer_caps);
-  }
-
-  return ret;
-}
-
-/**
  * @brief Get caps from tensors config (for other/tensors)
  * @param config tensors config info
  * @return caps for given config
index 812c235..a83b5d0 100644 (file)
@@ -1884,7 +1884,7 @@ gst_tensor_converter_update_caps (GstTensorConverter * self,
 {
   GstCaps *curr_caps, *out_caps;
 
-  out_caps = gst_tensors_get_caps (pad, config);
+  out_caps = gst_tensors_get_pad_caps (pad, config);
 
   /* Update pad caps. If it is different */
   curr_caps = gst_pad_get_current_caps (pad);
index 6a894f1..5ac42d6 100644 (file)
@@ -271,26 +271,51 @@ gst_tensor_demux_event (GstPad * pad, GstObject * parent, GstEvent * event)
  * @brief Get tensor config info from configured tensors
  * @param tensor_demux "this" pointer
  * @param config tensor config to be filled
- * @param index index of configured tensors
- * @return
+ * @param nth source ordering
+ * @param total number of tensors
+ * @return TRUE if succesfully configured
  */
 static gboolean
 gst_tensor_demux_get_tensor_config (GstTensorDemux * tensor_demux,
-    GstTensorConfig * config, guint index)
+    GstTensorsConfig * config, const guint nth, const guint total)
 {
-  GstTensorsConfig *tensors_config;
+  gst_tensors_config_init (config);
 
-  g_return_val_if_fail (tensor_demux != NULL, FALSE);
-  g_return_val_if_fail (config != NULL, FALSE);
+  if (tensor_demux->tensorpick != NULL) {
+    gchar *seleted_tensor;
+    gchar **strv;
+    guint i, num, idx;
 
-  gst_tensor_config_init (config);
+    g_assert (g_list_length (tensor_demux->tensorpick) >= nth);
+
+    seleted_tensor = (gchar *) g_list_nth_data (tensor_demux->tensorpick, nth);
+    strv = g_strsplit_set (seleted_tensor, ":+", -1);
+    num = g_strv_length (strv);
 
-  tensors_config = &tensor_demux->tensors_config;
-  g_return_val_if_fail (index < tensors_config->info.num_tensors, FALSE);
+    for (i = 0; i < num; i++) {
+      idx = (guint) g_ascii_strtoll (strv[i], NULL, 10);
+
+      /* Internal error, handle invalid index. */
+      if (idx >= total)
+        return FALSE;
+
+      gst_tensor_info_copy (&config->info.info[i],
+          &tensor_demux->tensors_config.info.info[idx]);
+    }
+
+    config->info.num_tensors = num;
+  } else {
+    /* Internal error, handle invalid index. */
+    if (nth >= total)
+      return FALSE;
+
+    config->info.num_tensors = 1;
+    gst_tensor_info_copy (&config->info.info[0],
+        &tensor_demux->tensors_config.info.info[nth]);
+  }
 
-  config->info = tensors_config->info.info[index];
-  config->rate_n = tensors_config->rate_n;
-  config->rate_d = tensors_config->rate_d;
+  config->rate_n = tensor_demux->tensors_config.rate_n;
+  config->rate_d = tensor_demux->tensors_config.rate_d;
   return TRUE;
 }
 
@@ -299,12 +324,13 @@ gst_tensor_demux_get_tensor_config (GstTensorDemux * tensor_demux,
  * @param tesnor_demux TensorDemux Object
  * @param[out] created will be updated in this function
  * @param nth source ordering
+ * @param total number of tensors
  * @return TensorPad if pad is already created, then return created pad.
  *         If not return new pad after creation.
  */
 static GstTensorPad *
 gst_tensor_demux_get_tensor_pad (GstTensorDemux * tensor_demux,
-    gboolean * created, gint nth)
+    gboolean * created, const guint nth, const guint total)
 {
   GSList *walk;
   GstPad *pad;
@@ -313,6 +339,7 @@ gst_tensor_demux_get_tensor_pad (GstTensorDemux * tensor_demux,
   GstEvent *event;
   gchar *stream_id;
   GstCaps *caps = NULL;
+  GstTensorsConfig config;
 
   walk = tensor_demux->srcpads;
   while (walk) {
@@ -372,43 +399,10 @@ gst_tensor_demux_get_tensor_pad (GstTensorDemux * tensor_demux,
   g_free (stream_id);
   gst_event_unref (event);
 
-  if (tensor_demux->tensorpick != NULL) {
-    gchar *seleted_tensor;
-    gchar **strv;
-    guint num;
-
-    g_assert (g_list_length (tensor_demux->tensorpick) >= nth);
-
-    seleted_tensor = (gchar *) g_list_nth_data (tensor_demux->tensorpick, nth);
-    strv = g_strsplit_set (seleted_tensor, ":+", -1);
-    num = g_strv_length (strv);
-
-    if (num == 1 && gst_pad_peer_has_tensor_caps (pad)) {
-      GstTensorConfig config;
-      gint64 idx = g_ascii_strtoll (strv[0], NULL, 10);
-      if (gst_tensor_demux_get_tensor_config (tensor_demux, &config, idx))
-        caps = gst_tensor_caps_from_config (&config);
-    } else {
-      GstTensorsConfig config;
-      guint i;
-      gst_tensors_config_init (&config);
-      config.rate_n = tensor_demux->tensors_config.rate_n;
-      config.rate_d = tensor_demux->tensors_config.rate_d;
-      config.info.num_tensors = num;
-      for (i = 0; i < num; i++) {
-        gint64 idx = g_ascii_strtoll (strv[i], NULL, 10);
-        config.info.info[i] = tensor_demux->tensors_config.info.info[idx];
-      }
-      caps = gst_tensors_caps_from_config (&config);
-    }
-    g_strfreev (strv);
-  } else {
-    GstTensorConfig config;
-    if (gst_tensor_demux_get_tensor_config (tensor_demux, &config, nth))
-      caps = gst_tensor_caps_from_config (&config);
-  }
+  /* configure nth pad caps */
+  if (gst_tensor_demux_get_tensor_config (tensor_demux, &config, nth, total)) {
+    caps = gst_tensors_get_pad_caps (pad, &config);
 
-  if (caps) {
     gst_pad_set_caps (pad, caps);
     gst_caps_unref (caps);
   } else {
@@ -426,6 +420,7 @@ gst_tensor_demux_get_tensor_pad (GstTensorDemux * tensor_demux,
     }
   }
 
+  gst_tensors_info_free (&config.info);
   return tensorpad;
 }
 
@@ -462,7 +457,7 @@ done:
 static GstFlowReturn
 gst_tensor_demux_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
 {
-  gint num_tensors, num_srcs, i;
+  guint num_tensors, num_srcs, i;
   GstFlowReturn res = GST_FLOW_OK;
   GstTensorDemux *tensor_demux;
   GList *list = NULL;
@@ -487,7 +482,8 @@ gst_tensor_demux_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
     gboolean created;
     GstClockTime ts;
 
-    srcpad = gst_tensor_demux_get_tensor_pad (tensor_demux, &created, i);
+    srcpad = gst_tensor_demux_get_tensor_pad (tensor_demux, &created, i,
+        num_tensors);
     outbuf = gst_buffer_new ();
 
     if (tensor_demux->tensorpick != NULL) {
index b80cabc..71665e5 100644 (file)
@@ -732,7 +732,7 @@ gst_tensor_if_get_tensor_pad (GstTensorIf * tensor_if,
   gst_pad_set_active (pad, TRUE);
   gst_element_add_pad (GST_ELEMENT_CAST (tensor_if), pad);
 
-  caps = gst_tensors_get_caps (pad, config);
+  caps = gst_tensors_get_pad_caps (pad, config);
 
   silent_debug_caps (caps, "out caps");
   gst_pad_set_caps (pad, caps);
index e5d373a..fdc4442 100644 (file)
@@ -1377,81 +1377,6 @@ TEST (versionControl, getVer01)
 }
 
 /**
- * @brief Test pad has tensor cap
- */
-TEST (commonPadCap, tensor0)
-{
-  gchar *pipeline;
-  GstElement *gstpipe;
-  GstElement *src_handle;
-  GstPad *pad;
-  gboolean is_tensor;
-
-  /* Create a nnstreamer pipeline */
-  pipeline = g_strdup_printf ("fakesrc name=fsrc ! fakesink name=fsink");
-  gstpipe = gst_parse_launch (pipeline, NULL);
-  EXPECT_NE (pipeline, nullptr);
-
-  EXPECT_EQ (setPipelineStateSync (gstpipe, GST_STATE_PLAYING, UNITTEST_STATECHANGE_TIMEOUT), 0);
-  g_usleep (100000);
-
-  src_handle = gst_bin_get_by_name (GST_BIN (gstpipe), "fsrc");
-  EXPECT_NE (src_handle, nullptr);
-  pad = gst_element_get_static_pad (src_handle, "src");
-  EXPECT_NE (pad, nullptr);
-
-  /* "any" cap returns tensor cap */
-  is_tensor = gst_pad_peer_has_tensor_caps (pad);
-  EXPECT_EQ (TRUE, is_tensor);
-
-  EXPECT_EQ (setPipelineStateSync (gstpipe, GST_STATE_NULL, UNITTEST_STATECHANGE_TIMEOUT), 0);
-  g_usleep (100000);
-
-  gst_object_unref (pad);
-  gst_object_unref (src_handle);
-  gst_object_unref (gstpipe);
-  g_free (pipeline);
-}
-
-/**
- * @brief Test pad has tensor cap
- */
-TEST (commonPadCap, tensors0)
-{
-  gchar *pipeline;
-  GstElement *gstpipe;
-  GstElement *src_handle;
-  GstPad *pad;
-  gboolean is_tensor;
-
-  /* Create a nnstreamer pipeline */
-  pipeline = g_strdup_printf (
-      "videotestsrc ! videoconvert ! video/x-raw,width=160,height=120,format=RGB,framerate=(fraction)30/1 ! "
-      "tensor_converter name=tsrc ! other/tensors,num_tensors=1,dimensions=(string)3:160:120:1, types=(string)uint8, framerate=(fraction)30/1 ! fakesink");
-  gstpipe = gst_parse_launch (pipeline, NULL);
-  EXPECT_NE (pipeline, nullptr);
-
-  EXPECT_EQ (setPipelineStateSync (gstpipe, GST_STATE_PLAYING, UNITTEST_STATECHANGE_TIMEOUT), 0);
-  g_usleep (100000);
-
-  src_handle = gst_bin_get_by_name (GST_BIN (gstpipe), "tsrc");
-  EXPECT_NE (src_handle, nullptr);
-  pad = gst_element_get_static_pad (src_handle, "src");
-  EXPECT_NE (pad, nullptr);
-
-  is_tensor = gst_pad_peer_has_tensor_caps (pad);
-  EXPECT_EQ (FALSE, is_tensor);
-
-  EXPECT_EQ (setPipelineStateSync (gstpipe, GST_STATE_NULL, UNITTEST_STATECHANGE_TIMEOUT), 0);
-  g_usleep (100000);
-
-  gst_object_unref (pad);
-  gst_object_unref (src_handle);
-  gst_object_unref (gstpipe);
-  g_free (pipeline);
-}
-
-/**
  * @brief Main function for unit test.
  */
 int