[Android/Custom] drop input buffer
authorJaeyun <jy1210.jung@samsung.com>
Wed, 3 Jun 2020 02:40:36 +0000 (11:40 +0900)
committerMyungJoo Ham <myungjoo.ham@samsung.com>
Thu, 4 Jun 2020 05:43:29 +0000 (14:43 +0900)
In custom-filter interface, returning null in invoke-callback will drop the input buffer.

Signed-off-by: Jaeyun <jy1210.jung@samsung.com>
api/android/api/src/androidTest/java/org/nnsuite/nnstreamer/APITestCustomFilter.java
api/android/api/src/main/java/org/nnsuite/nnstreamer/CustomFilter.java
api/android/api/src/main/jni/nnstreamer-native-customfilter.c

index 14fbbf3..d21055b 100644 (file)
@@ -184,8 +184,8 @@ public class APITestCustomFilter {
             /* start pipeline */
             pipe.start();
 
-            /* push input buffer */
-            for (int i = 0; i < 15; i++) {
+            /* push input buffer repeatedly */
+            for (int i = 0; i < 2048; i++) {
                 TensorsData in = TensorsData.allocate(info);
                 ByteBuffer input = in.getTensorData(0);
 
@@ -196,24 +196,50 @@ public class APITestCustomFilter {
                 in.setTensorData(0, input);
 
                 pipe.inputData("srcx", in);
-                Thread.sleep(50);
+                Thread.sleep(20);
             }
 
             /* sleep 300 to pass all input buffers to sink */
             Thread.sleep(300);
 
+            /* stop pipeline */
+            pipe.stop();
+
             /* check received data from sink */
             assertFalse(mInvalidState);
-            assertEquals(15, mReceived);
+            assertEquals(2048, mReceived);
         } catch (Exception e) {
             fail();
         }
     }
 
     @Test
-    public void testInputBuffer() {
+    public void testDropBuffer() {
+        CustomFilter customDrop = CustomFilter.registerCustomFilter("custom-drop",
+                new CustomFilter.CustomFilterCallback() {
+            int received = 0;
+
+            @Override
+            public TensorsInfo getOutputInfo(TensorsInfo in) {
+                return in;
+            }
+
+            @Override
+            public TensorsData invoke(TensorsData in) {
+                received++;
+
+                if (received <= 5) {
+                    return in;
+                }
+
+                /* return null to drop the incoming buffer */
+                return null;
+            }
+        });
+
         String desc = "appsrc name=srcx ! " +
                 "other/tensor,dimension=(string)10:1:1:1,type=(string)int32,framerate=(fraction)0/1 ! " +
+                "tensor_filter framework=" + customDrop.getName() + " ! " +
                 "tensor_filter framework=" + mCustomPassthrough.getName() + " ! " +
                 "tensor_filter framework=" + mCustomConvert.getName() + " ! " +
                 "tensor_filter framework=" + mCustomAdd.getName() + " ! " +
@@ -230,7 +256,7 @@ public class APITestCustomFilter {
             pipe.start();
 
             /* push input buffer repeatedly */
-            for (int i = 0; i < 2048; i++) {
+            for (int i = 0; i < 24; i++) {
                 TensorsData in = TensorsData.allocate(info);
                 ByteBuffer input = in.getTensorData(0);
 
@@ -252,7 +278,7 @@ public class APITestCustomFilter {
 
             /* check received data from sink */
             assertFalse(mInvalidState);
-            assertTrue(mReceived > 0);
+            assertEquals(5, mReceived);
         } catch (Exception e) {
             fail();
         }
index 5635b3e..9337880 100644 (file)
@@ -55,6 +55,7 @@ public final class CustomFilter implements AutoCloseable {
          * Called synchronously while processing the pipeline.
          *
          * NNStreamer filter invokes the given custom-filter callback while processing the pipeline.
+         * Note that, if it is unnecessary to execute the input data, return null to drop the buffer.
          *
          * @param in The input data (a single frame, tensor/tensors)
          *
index 655a096..66dbccc 100644 (file)
@@ -121,6 +121,12 @@ nns_customfilter_invoke (const GstTensorFilterProperties * prop,
     goto done;
   }
 
+  if (obj_out_data == NULL) {
+    /* drop current buffer */
+    ret = 1;
+    goto done;
+  }
+
   if (!nns_parse_tensors_data (pipe_info, env, obj_out_data, &out_data, NULL)) {
     nns_loge ("Failed to parse output data.");
     goto done;