Add critical section to avoid thread safety issue
[platform/core/uifw/stt.git] / server / sttd_recorder.c
index 88e371a..b83cc47 100644 (file)
@@ -201,7 +201,7 @@ int sttd_recorder_initialize(stt_recorder_audio_cb audio_cb, stt_recorder_interr
        }
 
 #ifdef TV_BT_MODE
-       if (BT_ERROR_NONE != bt_initialize()) {
+       if (BT_ERROR_NONE != bt_product_init()) {
                SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to init bt");
                return STTD_ERROR_OPERATION_FAILED;
        }
@@ -262,7 +262,7 @@ int sttd_recorder_deinitialize()
 #ifdef TV_BT_MODE
        bt_hid_host_deinitialize();
 
-       bt_deinitialize();
+       bt_product_deinit();
 #endif
 
        g_recorder_state = STTD_RECORDER_STATE_NONE;
@@ -593,9 +593,14 @@ int sttd_recorder_stop()
        if (STTD_RECORDER_STATE_READY == g_recorder_state)
                return 0;
 
+       // critical section required because this function can be called from stt engine thread context
+       SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Enter critical section");
+       pthread_mutex_lock(&sttd_audio_in_handle_mutex);
+
        /* Check engine id is valid */
        if (NULL == g_recorder) {
                SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
+               pthread_mutex_unlock(&sttd_audio_in_handle_mutex);
                return STTD_ERROR_INVALID_PARAMETER;
        }
 
@@ -636,6 +641,75 @@ int sttd_recorder_stop()
 #ifdef BUF_SAVE_MODE
        fclose(g_pFile);
 #endif
+       pthread_mutex_unlock(&sttd_audio_in_handle_mutex);
+       SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Leave critical section");
+
+       return 0;
+}
+
+int sttd_recorder_start_file(int uid, const char *filepath)
+{
+       if (STTD_RECORDER_STATE_RECORDING == g_recorder_state)
+               return 0;
+
+       /* Check engine id is valid */
+       if (NULL == g_recorder) {
+               SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
+               return STTD_ERROR_INVALID_PARAMETER;
+       }
+       g_recorder_state = STTD_RECORDER_STATE_RECORDING;
+       g_recorder->uid = uid;
+
+       int cnt = 0;
+       int totalReadBytes = 0;
+
+       FILE *infile = fopen(filepath, "rb");
+
+       //process the file
+       if (infile != NULL) {
+               while (!feof(infile)) {
+                       static char pcm_buff[BUFFER_LENGTH];
+                       int read_byte = fread(pcm_buff, 1, BUFFER_LENGTH, infile);
+                       totalReadBytes += read_byte;
+                       if (0 != read_byte) {
+                               if (0 != g_audio_cb(pcm_buff, read_byte)) {
+                                       SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to call audio callback");
+                                       fclose(infile);
+                                       return -1;
+                               }
+                               if (0 == cnt % 30) {
+                                       float vol_db = get_volume_decibel(pcm_buff, BUFFER_LENGTH, g_recorder->audio_type);
+                                       if (0 != sttdc_send_set_volume(g_recorder->uid, vol_db)) {
+                                               SLOG(LOG_ERROR, TAG_STTD, "[Recorder] Fail to send recording volume(%f)", vol_db);
+                                       }
+                               }
+
+                               /* Audio read log */
+                               if (0 == cnt % 50)
+                                       SLOG(LOG_DEBUG, TAG_STTD, "[Recorder][%d] Recording... : read_size(%d)", cnt, read_byte);
+                               cnt++;
+                       }
+               }
+               fclose(infile);
+       }
+
+       SLOG(LOG_DEBUG, TAG_STTD, "[Recorder][%d] total bytes(%d)", cnt, totalReadBytes);
+       return 0;
+}
+
+int sttd_recorder_stop_file()
+{
+       if (STTD_RECORDER_STATE_READY == g_recorder_state)
+               return 0;
+
+       /* Check engine id is valid */
+       if (NULL == g_recorder) {
+               SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
+               return STTD_ERROR_INVALID_PARAMETER;
+       }
+
+       g_recorder->uid = -1;
+       g_recorder_state = STTD_RECORDER_STATE_READY;
 
        return 0;
 }