Implement new API functions for audio streaming 35/293735/2
authorSuyeon Hwang <stom.hwang@samsung.com>
Tue, 30 May 2023 02:31:29 +0000 (11:31 +0900)
committerSuyeon Hwang <stom.hwang@samsung.com>
Thu, 6 Jul 2023 08:08:20 +0000 (17:08 +0900)
- Requirements:
The client library needs to provide new API for starting, sending,
stopping audio streaming feature. And also, the client library needs to
provide new API for getting audio format information.

- Contents:
To provide new API functions for audio streaming, this patch adds
implementation for these new functions. Through this patch, this module
has prepared to provide new API for audio streaming.

Change-Id: I88016a602f44c0325e7e58fc63c03872a8533f6d
Signed-off-by: Suyeon Hwang <stom.hwang@samsung.com>
client/stt.c
client/stt_client.c
client/stt_client.h
include/stt_internal.h

index 7eaa071..b2ed49f 100644 (file)
@@ -1251,6 +1251,38 @@ int stt_is_recognition_type_supported(stt_h stt, const char* type, bool* support
        return STT_ERROR_NONE;
 }
 
+int stt_get_audio_format(stt_h stt, stt_audio_type_e* type, int* rate, int* num_of_channels)
+{
+       stt_client_s* client = NULL;
+       int temp = __stt_check_precondition(stt, &client);
+       if (STT_ERROR_NONE != temp)
+               return temp;
+
+       RETVM_IF(NULL == type || NULL == rate || NULL == num_of_channels, STT_ERROR_INVALID_PARAMETER, "[ERROR] Input parameter is NULL");
+       RETVM_IF(client->current_state == STT_STATE_CREATED, STT_ERROR_INVALID_STATE, "[ERROR] Invalid State: Current state(%d) is CREATED", client->current_state);
+
+       int ret = STT_ERROR_OPERATION_FAILED;
+       int count = 0;
+       while (STT_ERROR_NONE != ret) {
+               ret = stt_dbus_request_get_audio_format(client->uid, type, rate, num_of_channels);
+               if (STT_ERROR_NONE != ret) {
+                       //LCOV_EXCL_START
+                       if (false == __stt_is_necessary_to_retry_dbus_request(count, ret)) {
+                               SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get audio format : %d/%s", ret, get_error_message(ret));
+                               return ret;
+                       }
+                       count++;
+                       //LCOV_EXCL_STOP
+               } else {
+                       SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Audio format: type(%d), rate(%d), number of channels(%d)",
+                                *type, *rate, *num_of_channels);
+                       break;
+               }
+       }
+
+       return STT_ERROR_NONE;
+}
+
 int stt_set_silence_detection(stt_h stt, stt_option_silence_detection_e type)
 {
        stt_client_s* client = NULL;
@@ -1411,7 +1443,7 @@ int stt_start(stt_h stt, const char* language, const char* type)
        if (STT_ERROR_NONE != tmp)
                return tmp;
 
-       RETVM_IF(STT_INTERNAL_STATE_NONE != client->internal_state, STT_ERROR_IN_PROGRESS_TO_RECORDING, "[ERROR] Invalid State: Current state(%d) is not READY", client->current_state);
+       RETVM_IF(STT_INTERNAL_STATE_NONE != client->internal_state, STT_ERROR_IN_PROGRESS_TO_RECORDING, "[ERROR] Invalid State : Internal state is NOT none : %d", client->current_state);
        SLOG(LOG_INFO, TAG_STTC, "===== STT START");
 
        int ret = -1;
@@ -1447,6 +1479,7 @@ int stt_start(stt_h stt, const char* language, const char* type)
                client->internal_state = STT_INTERNAL_STATE_NONE;
        } else {
                SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Start is successful but not done");
+               client->is_streaming = false;
        }
 
        free(temp);
@@ -1478,6 +1511,11 @@ int stt_stop(stt_h stt)
                return STT_ERROR_IN_PROGRESS_TO_PROCESSING;
        }
 
+       if (client->is_streaming) {
+               SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Audio streaming is activated.");
+               return STT_ERROR_OPERATION_FAILED;
+       }
+
        client->internal_state = STT_INTERNAL_STATE_STOPPING;
        int ret = stt_dbus_request_stop(client->uid);
        if (0 != ret) {
@@ -1493,6 +1531,119 @@ int stt_stop(stt_h stt)
        return ret;
 }
 
+int stt_start_audio_streaming(stt_h stt, const char* language, const char* type)
+{
+       stt_client_s* client = NULL;
+       int tmp = __stt_check_precondition_with_state(stt, &client, STT_STATE_READY);
+       if (STT_ERROR_NONE != tmp)
+               return tmp;
+
+       RETVM_IF(STT_INTERNAL_STATE_NONE != client->internal_state, STT_ERROR_IN_PROGRESS_TO_RECORDING, "[ERROR] Invalid State : Internal state is NOT none : %d", client->current_state);
+       SLOG(LOG_INFO, TAG_STTC, "===== STT START AUDIO STREAMING");
+
+       int ret = STT_ERROR_NONE;
+       char appid[1024] = {0, };
+       ret = aul_app_get_appid_bypid(getpid(), appid, sizeof(appid) - 1);
+
+       if ((AUL_R_OK != ret) || (0 == strlen(appid))) {
+               SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to get application ID"); //LCOV_EXCL_LINE
+       } else {
+               SLOG(LOG_DEBUG, TAG_STTC, "[DEBUG] Current app id is %s", appid);
+       }
+
+       const char* language_param = NULL == language ? "default" : language;
+       if (true == client->credential_needed && NULL == client->credential) {
+               SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Do not have app credential for this engine(%s)", client->current_engine_id); //LCOV_EXCL_LINE
+               return STT_ERROR_PERMISSION_DENIED;
+       }
+
+       client->internal_state = STT_INTERNAL_STATE_STARTING;
+       ret = stt_dbus_request_start_audio_streaming(client->uid, language_param, type, client->silence, appid, client->credential, client->audio_id);
+       if (0 != ret) {
+               SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to start : ret(%d/%s)", ret, get_error_message(ret)); //LCOV_EXCL_LINE
+               client->internal_state = STT_INTERNAL_STATE_NONE;
+               return ret;
+       }
+
+       client->is_streaming = true;
+       SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Start audio streaming is successful but not done");
+       SLOG(LOG_INFO, TAG_STTC, "=====");
+       return STT_ERROR_NONE;
+}
+
+int stt_send_audio_streaming(stt_h stt, const char* data, size_t data_size)
+{
+       stt_client_s* client = NULL;
+       int tmp = __stt_check_precondition_with_state(stt, &client, STT_STATE_RECORDING);
+       if (STT_ERROR_NONE != tmp)
+               return tmp;
+
+       SLOG(LOG_INFO, TAG_STTC, "===== STT SEND AUDIO STREAMING");
+
+       if (STT_INTERNAL_STATE_STARTING == client->internal_state) {
+               SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STARTING : %d", client->internal_state); //LCOV_EXCL_LINE
+               return STT_ERROR_IN_PROGRESS_TO_RECORDING;
+       } else if (STT_INTERNAL_STATE_CANCELING == client->internal_state) {
+               SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is CANCELING : %d", client->internal_state); //LCOV_EXCL_LINE
+               return STT_ERROR_IN_PROGRESS_TO_READY;
+       } else if (STT_INTERNAL_STATE_STOPPING == client->internal_state) {
+               SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STOPPING : %d", client->internal_state); //LCOV_EXCL_LINE
+               return STT_ERROR_IN_PROGRESS_TO_PROCESSING;
+       }
+
+       if (false == client->is_streaming) {
+               SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Audio streaming is not activated.");
+               return STT_ERROR_OPERATION_FAILED;
+       }
+
+       int ret = stt_dbus_request_send_audio_streaming(client->uid, data, data_size);
+       if (STT_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to start : ret(%d/%s)", ret, get_error_message(ret)); //LCOV_EXCL_LINE
+               return ret;
+       }
+
+       SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Send audio streaming is successful");
+       SLOG(LOG_INFO, TAG_STTC, "=====");
+       return STT_ERROR_NONE;
+}
+
+int stt_stop_audio_streaming(stt_h stt)
+{
+       stt_client_s* client = NULL;
+       int temp = __stt_check_precondition_with_state(stt, &client, STT_STATE_RECORDING);
+       if (STT_ERROR_NONE != temp)
+               return temp;
+
+       SLOG(LOG_INFO, TAG_STTC, "===== STT STOP AUDIO STREAMING");
+
+       if (STT_INTERNAL_STATE_STARTING == client->internal_state) {
+               SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STARTING : %d", client->internal_state); //LCOV_EXCL_LINE
+               return STT_ERROR_IN_PROGRESS_TO_RECORDING;
+       } else if (STT_INTERNAL_STATE_CANCELING == client->internal_state) {
+               SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is CANCELING : %d", client->internal_state); //LCOV_EXCL_LINE
+               return STT_ERROR_IN_PROGRESS_TO_READY;
+       } else if (STT_INTERNAL_STATE_STOPPING == client->internal_state) {
+               SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Invalid State : Internal state is STOPPING : %d", client->internal_state); //LCOV_EXCL_LINE
+               return STT_ERROR_IN_PROGRESS_TO_PROCESSING;
+       }
+
+       if (false == client->is_streaming) {
+               SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Audio streaming is not activated.");
+               return STT_ERROR_OPERATION_FAILED;
+       }
+
+       client->internal_state = STT_INTERNAL_STATE_STOPPING;
+       int ret = stt_dbus_request_stop_audio_streaming(client->uid);
+       if (0 != ret) {
+               SLOG(LOG_ERROR, TAG_STTC, "[ERROR] Fail to stop : ret(%d/%s)", ret, get_error_message(ret)); //LCOV_EXCL_LINE
+               client->internal_state = STT_INTERNAL_STATE_NONE;
+               return ret;
+       }
+
+       SLOG(LOG_INFO, TAG_STTC, "[SUCCESS] Stop is successful but not done");
+       SLOG(LOG_DEBUG, TAG_STTC, "=====");
+       return STT_ERROR_NONE;
+}
 
 int stt_cancel(stt_h stt)
 {
index 1a3a39a..a339561 100644 (file)
@@ -95,6 +95,7 @@ int stt_client_new(stt_h* stt)
        client->cb_ref_count = 0;
 
        client->internal = false;
+       client->is_streaming = false;
 
        g_client_list = g_list_append(g_client_list, client);
 
index 5748ca8..26c1b33 100644 (file)
@@ -86,6 +86,8 @@ typedef struct {
 
        /* is this internal? */
        bool    internal;
+
+       bool    is_streaming;
 } stt_client_s;
 
 
index 9e94ab9..0e8589c 100644 (file)
@@ -237,6 +237,13 @@ int stt_set_audio_type(stt_h stt, const char *audio_id);
  */
 int stt_get_audio_type(stt_h stt, char **audio_id);
 
+
+int stt_start_audio_streaming(stt_h stt, const char* language, const char* type);
+int stt_send_audio_streaming(stt_h stt, const char* data, size_t data_size);
+int stt_stop_audio_streaming(stt_h stt);
+int stt_get_audio_format(stt_h stt, stt_audio_type_e* type, int* rate, int* num_of_channels);
+
+
 #ifdef __cplusplus
 }
 #endif