diff --git a/c/src/ml-api-service-agent-client.c b/c/src/ml-api-service-agent-client.c index 6d8c450d..5a6785a9 100644 --- a/c/src/ml-api-service-agent-client.c +++ b/c/src/ml-api-service-agent-client.c @@ -519,6 +519,29 @@ ml_service_get_pipeline_state (ml_service_h h, ml_pipeline_state_e * state) return ret; } +/** + * @brief Internal function to release ml-service pipeline data. + */ +int +ml_service_pipeline_release_internal (void *priv) +{ + _ml_service_server_s *server = (_ml_service_server_s *) priv; + g_autoptr (GError) err = NULL; + int ret; + + ret = ml_agent_pipeline_destroy (server->id, &err); + if (ret < 0) { + _ml_error_report_return (ret, + "Failed to invoke the method destroy_pipeline (%s).", + err ? err->message : "Unknown error"); + } + + g_free (server->service_name); + g_free (server); + + return ML_ERROR_NONE; +} + /** * @brief Registers new information of a neural network model. */ diff --git a/c/src/ml-api-service-common.c b/c/src/ml-api-service-common.c index 6fa8057a..14a6197c 100644 --- a/c/src/ml-api-service-common.c +++ b/c/src/ml-api-service-common.c @@ -10,13 +10,11 @@ * @bug No known bugs except for NYI items */ -#include "ml-api-internal.h" #include "ml-api-service.h" #include "ml-api-service-private.h" -#include "ml-agent-interface.h" /** - * @brief Destroy the pipeline of given ml_service_h + * @brief Destroy the service handle. */ int ml_service_destroy (ml_service_h h) @@ -30,53 +28,22 @@ ml_service_destroy (ml_service_h h) _ml_error_report_return (ML_ERROR_INVALID_PARAMETER, "The parameter, 'h' is NULL. It should be a valid ml_service_h."); - if (ML_SERVICE_TYPE_SERVER_PIPELINE == mls->type) { - _ml_service_server_s *server = (_ml_service_server_s *) mls->priv; - GError *err = NULL; - - ret = ml_agent_pipeline_destroy (server->id, &err); - if (ret < 0) { - _ml_error_report ("Failed to invoke the method destroy_pipeline (%s).", - err ? err->message : "Unknown error"); - } - g_clear_error (&err); - - if (ML_ERROR_NONE != ret) - _ml_error_report_return (ret, - "The data of given handle is corrupted. Please check it."); - - g_free (server->service_name); - g_free (server); - } else if (ML_SERVICE_TYPE_CLIENT_QUERY == mls->type) { - _ml_service_query_s *query = (_ml_service_query_s *) mls->priv; - ml_tensors_data_h data_h; - - if (ml_pipeline_src_release_handle (query->src_h)) - _ml_error_report ("Failed to release src handle"); - - if (ml_pipeline_sink_unregister (query->sink_h)) - _ml_error_report ("Failed to unregister sink handle"); - - if (ml_pipeline_destroy (query->pipe_h)) - _ml_error_report ("Failed to destroy pipeline"); - - while ((data_h = g_async_queue_try_pop (query->out_data_queue))) { - ml_tensors_data_destroy (data_h); - } - - g_async_queue_unref (query->out_data_queue); - g_free (query); - } else if (ML_SERVICE_TYPE_REMOTE == mls->type) { - _ml_remote_service_s *mlrs = (_ml_remote_service_s *) mls->priv; - nns_edge_release_handle (mlrs->edge_h); - /** Wait some time until release the edge handle. */ - g_usleep (1000000); - g_free (mlrs); - } else { - _ml_error_report_return (ML_ERROR_INVALID_PARAMETER, - "Invalid type of ml_service_h."); + switch (mls->type) { + case ML_SERVICE_TYPE_SERVER_PIPELINE: + ret = ml_service_pipeline_release_internal (mls->priv); + break; + case ML_SERVICE_TYPE_CLIENT_QUERY: + ret = ml_service_query_release_internal (mls->priv); + break; + case ML_SERVICE_TYPE_REMOTE: + ret = ml_service_remote_release_internal (mls->priv); + break; + default: + _ml_error_report_return (ML_ERROR_INVALID_PARAMETER, + "Invalid type of ml_service_h."); } - g_free (mls); - return ML_ERROR_NONE; + if (ret == ML_ERROR_NONE) + g_free (mls); + return ret; } diff --git a/c/src/ml-api-service-private.h b/c/src/ml-api-service-private.h index 77b4fe43..174db19c 100644 --- a/c/src/ml-api-service-private.h +++ b/c/src/ml-api-service-private.h @@ -20,7 +20,6 @@ #include "pipeline-dbus.h" #include "model-dbus.h" #include "resource-dbus.h" -#include "nnstreamer-edge.h" #include "nnstreamer-tizen-internal.h" #ifdef __cplusplus @@ -60,27 +59,23 @@ typedef struct } _ml_service_server_s; /** - * @brief Structure for ml_service_query + * @brief Internal function to release ml-service pipeline data. */ -typedef struct -{ - ml_pipeline_h pipe_h; - ml_pipeline_src_h src_h; - ml_pipeline_sink_h sink_h; +int ml_service_pipeline_release_internal (void *priv); - gchar *caps; - guint timeout; /**< in ms unit */ - GAsyncQueue *out_data_queue; -} _ml_service_query_s; +/** + * @brief Internal function to release ml-service query data. + */ +int ml_service_query_release_internal (void *priv); +#if defined(ENABLE_REMOTE_SERVICE) /** - * @brief Structure for ml_remote_service + * @brief Internal function to release ml-service remote data. */ -typedef struct -{ - nns_edge_h edge_h; - nns_edge_node_type_e node_type; -} _ml_remote_service_s; +int ml_service_remote_release_internal (void *priv); +#else +#define ml_service_remote_release_internal(...) ML_ERROR_NOT_SUPPORTED +#endif #ifdef __cplusplus } diff --git a/c/src/ml-api-service-query-client.c b/c/src/ml-api-service-query-client.c index f02eaae7..634a44b1 100644 --- a/c/src/ml-api-service-query-client.c +++ b/c/src/ml-api-service-query-client.c @@ -20,6 +20,20 @@ #include "ml-api-service.h" #include "ml-api-service-private.h" +/** + * @brief Structure for ml_service_query + */ +typedef struct +{ + ml_pipeline_h pipe_h; + ml_pipeline_src_h src_h; + ml_pipeline_sink_h sink_h; + + gchar *caps; + guint timeout; /**< in ms unit */ + GAsyncQueue *out_data_queue; +} _ml_service_query_s; + /** * @brief Sink callback for query_client */ @@ -58,6 +72,37 @@ _sink_callback_for_query_client (const ml_tensors_data_h data, g_async_queue_push (mls->out_data_queue, copied_data); } +/** + * @brief Internal function to release ml-service query data. + */ +int +ml_service_query_release_internal (void *priv) +{ + _ml_service_query_s *query = (_ml_service_query_s *) priv; + ml_tensors_data_h data_h; + + if (!query) + return ML_ERROR_INVALID_PARAMETER; + + if (ml_pipeline_src_release_handle (query->src_h)) + _ml_error_report ("Failed to release src handle"); + + if (ml_pipeline_sink_unregister (query->sink_h)) + _ml_error_report ("Failed to unregister sink handle"); + + if (ml_pipeline_destroy (query->pipe_h)) + _ml_error_report ("Failed to destroy pipeline"); + + while ((data_h = g_async_queue_try_pop (query->out_data_queue))) { + ml_tensors_data_destroy (data_h); + } + + g_async_queue_unref (query->out_data_queue); + g_free (query); + + return ML_ERROR_NONE; +} + /** * @brief Creates query client service handle with given ml-option handle. */ diff --git a/c/src/ml-api-service-remote.c b/c/src/ml-api-service-remote.c index 840179b4..246660cd 100644 --- a/c/src/ml-api-service-remote.c +++ b/c/src/ml-api-service-remote.c @@ -16,6 +16,7 @@ #include #include #include +#include #include "ml-api-internal.h" #include "ml-api-service.h" @@ -51,6 +52,15 @@ typedef struct nns_edge_node_type_e node_type; } edge_info_s; +/** + * @brief Structure for ml_remote_service + */ +typedef struct +{ + nns_edge_h edge_h; + nns_edge_node_type_e node_type; +} _ml_remote_service_s; + /** * @brief Get ml-service node type from ml_option. */ @@ -480,6 +490,26 @@ _mlrs_create_edge_handle (nns_edge_h * edge_h, edge_info_s * edge_info) return ret; } +/** + * @brief Internal function to release ml-service remote data. + */ +int +ml_service_remote_release_internal (void *priv) +{ + _ml_remote_service_s *mlrs = (_ml_remote_service_s *) priv; + + if (!mlrs) + return ML_ERROR_INVALID_PARAMETER; + + nns_edge_release_handle (mlrs->edge_h); + + /** Wait some time until release the edge handle. */ + g_usleep (1000000); + g_free (mlrs); + + return ML_ERROR_NONE; +} + /** * @brief Creates ml-service handle with given ml-option handle. */ diff --git a/meson.build b/meson.build index d5b2634d..0223fad7 100644 --- a/meson.build +++ b/meson.build @@ -40,6 +40,7 @@ if get_option('enable-ml-service') nnstreamer_edge_dep = dependency('nnstreamer-edge', required: false) curl_dep = cc.find_library('curl', required: false) if nnstreamer_edge_dep.found() and curl_dep.found() + add_project_arguments('-DENABLE_REMOTE_SERVICE=1', language: ['c', 'cpp']) support_remote_service = true endif endif