[Android/Api] add timeout in single-shot
authorJaeyun <jy1210.jung@samsung.com>
Tue, 10 Sep 2019 10:33:02 +0000 (19:33 +0900)
committerMyungJoo Ham <myungjoo.ham@samsung.com>
Mon, 16 Sep 2019 06:24:45 +0000 (15:24 +0900)
Add method to set the timeout in single-shot API.

Signed-off-by: Jaeyun Jung <jy1210.jung@samsung.com>
api/android/api/src/main/java/org/nnsuite/nnstreamer/SingleShot.java
api/android/api/src/main/jni/nnstreamer-native-singleshot.c
api/android/sample/src/main/java/org/nnsuite/nnstreamer/sample/MainActivity.java

index 71d8633..19521f2 100644 (file)
@@ -39,6 +39,7 @@ public final class SingleShot implements AutoCloseable {
     private native TensorsData nativeInvoke(long handle, TensorsData in);
     private native TensorsInfo nativeGetInputInfo(long handle);
     private native TensorsInfo nativeGetOutputInfo(long handle);
+    private native boolean nativeSetTimeout(long handle, int timeout);
 
     /**
      * Creates a new <code>SingleShot</code> instance with the given model.
@@ -85,6 +86,10 @@ public final class SingleShot implements AutoCloseable {
      * Even if the model has flexible input data dimensions,
      * input data frames of an instance of a model should share the same dimension.
      *
+     * Note that this has a default timeout of 3 seconds.
+     * If an application wants to change the time to wait for an output,
+     * set the timeout using {@link #setTimeout(int)}.
+     *
      * @param in The input data to be inferred (a single frame, tensor/tensors)
      *
      * @return The output data (a single frame, tensor/tensors)
@@ -144,6 +149,26 @@ public final class SingleShot implements AutoCloseable {
     }
 
     /**
+     * Sets the maximum amount of time to wait for an output, in milliseconds.
+     *
+     * @param timeout The time to wait for an output
+     *
+     * @throws IllegalArgumentException if given param is invalid
+     * @throws IllegalStateException if failed to set the timeout
+     */
+    public void setTimeout(int timeout) {
+        checkPipelineHandle();
+
+        if (timeout <= 0) {
+            throw new IllegalArgumentException("The param timeout is invalid");
+        }
+
+        if (!nativeSetTimeout(mHandle, timeout)) {
+            throw new IllegalStateException("Failed to set the timeout");
+        }
+    }
+
+    /**
      * Internal method to check native handle.
      *
      * @throws IllegalStateException if the pipeline is not constructed
index de0bbdb..41ce479 100644 (file)
@@ -188,3 +188,27 @@ done:
   ml_tensors_info_destroy (info);
   return result;
 }
+
+/**
+ * @brief Native method for single-shot API.
+ */
+jboolean
+Java_org_nnsuite_nnstreamer_SingleShot_nativeSetTimeout (JNIEnv * env, jobject thiz,
+    jlong handle, jint timeout)
+{
+  pipeline_info_s *pipe_info;
+  ml_single_h single;
+  int status;
+
+  pipe_info = CAST_TO_TYPE (handle, pipeline_info_s*);
+  single = pipe_info->pipeline_handle;
+
+  status = ml_single_set_timeout (single, (unsigned int) timeout);
+  if (status != ML_ERROR_NONE) {
+    nns_loge ("Failed to set the timeout.");
+    return JNI_FALSE;
+  }
+
+  nns_logi ("Successfully set the timeout, %d milliseconds.", timeout);
+  return JNI_TRUE;
+}
index aefb0d0..3b788af 100644 (file)
@@ -262,6 +262,9 @@ public class MainActivity extends Activity {
             TensorsInfo outInfo = single.getOutputInfo();
             printTensorsInfo(outInfo);
 
+            /* set timeout (1 second) */
+            single.setTimeout(1000);
+
             /* single-shot invoke */
             for (int i = 0; i < 15; i++) {
                 /* dummy input */