Add critical section to avoid thread safety issue
[platform/core/uifw/stt.git] / server / sttd_recorder.c
index 629fb35..b83cc47 100644 (file)
@@ -1,5 +1,5 @@
 /*
-*  Copyright (c) 2011-2014 Samsung Electronics Co., Ltd All Rights Reserved
+*  Copyright (c) 2011-2016 Samsung Electronics Co., Ltd All Rights Reserved
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 #include <pthread.h>
 #include <sound_manager.h>
 #ifdef TV_BT_MODE
-#include <bluetooth.h>
+#include <bluetooth_product.h>
 #endif
 
 #include "stt_defs.h"
 #include "sttd_dbus.h"
 #include "sttd_recorder.h"
 #include "sttd_main.h"
-#include "sttp.h"
 
 
 #define FRAME_LENGTH 160
@@ -43,15 +42,12 @@ typedef enum {
 } sttd_recorder_state;
 
 typedef struct {
-       int                     engine_id;
        int                     uid;
        audio_in_h              audio_h;
-       sttp_audio_type_e       audio_type;
+       stte_audio_type_e       audio_type;
 } stt_recorder_s;
 
-static GSList *g_recorder_list;
-
-static int g_recording_engine_id;
+static stt_recorder_s* g_recorder = NULL;
 
 static stt_recorder_audio_cb   g_audio_cb;
 
@@ -63,6 +59,8 @@ static sttd_recorder_state    g_recorder_state = STTD_RECORDER_STATE_NONE;
 
 static int g_buffer_count;
 
+static int g_stream_focus_id;
+
 /* Sound buf save for test */
 /*
 #define BUF_SAVE_MODE
@@ -76,9 +74,9 @@ static int g_count = 1;
 #endif
 
 #ifdef TV_BT_MODE
-static float get_volume_decibel(char* data, int size, sttp_audio_type_e type);
+static float get_volume_decibel(char* data, int size, stte_audio_type_e type);
 
-static stt_recorder_s* __get_recorder(int engine_id);
+//static stt_recorder_s* __get_recorder(int engine_id);
 
 static int g_bt_extend_count;
 
@@ -100,21 +98,19 @@ static void _bt_hid_audio_data_receive_cb(bt_hid_voice_data_s *voice_data, void
        if (NULL != g_audio_cb) {
                if (0 != g_audio_cb((void*)voice_data->audio_buf, (unsigned int)voice_data->length)) {
                        SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to read audio");
-                       sttd_recorder_stop(g_recording_engine_id);
+                       sttd_recorder_stop();
                }
 
-               stt_recorder_s* recorder;
-               recorder = __get_recorder(g_recording_engine_id);
-               if (NULL == recorder) {
+               if (NULL == g_recorder) {
                        return;
                }
 
-               float vol_db = get_volume_decibel((char*)voice_data->audio_buf, (int)voice_data->length, recorder->audio_type);
-               if (0 != sttdc_send_set_volume(recorder->uid, vol_db)) {
+               float vol_db = get_volume_decibel((char*)voice_data->audio_buf, (int)voice_data->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);
                }
        }
-       
+
        if (0 == g_buffer_count || 0 == g_buffer_count % 50) {
                SLOG(LOG_WARN, TAG_STTD, "[Recorder][%d] Recording... : read_size(%d)", g_buffer_count, voice_data->length);
 
@@ -127,7 +123,7 @@ static void _bt_hid_audio_data_receive_cb(bt_hid_voice_data_s *voice_data, void
                        }
                }
                g_bt_extend_count++;
-               
+
                if (100000 == g_buffer_count) {
                        g_buffer_count = 0;
                }
@@ -162,26 +158,12 @@ const char* __stt_get_focus_changed_reason_code(sound_stream_focus_change_reason
        }
 }
 
-void __recorder_focus_state_cb(sound_stream_info_h stream_info, sound_stream_focus_change_reason_e reason, const char *extra_info, void *user_data)
+void __recorder_focus_state_watch_cb(int id, sound_stream_focus_mask_e focus_mask, sound_stream_focus_state_e focus_state, 
+                                               sound_stream_focus_change_reason_e reason, const char *extra_info, void *user_data)
 {
-       SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Focus state changed cb");
-
-       if (stream_info != g_stream_info_h) {
-               SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Invalid stream info handle");
-               return;
-       }
+       SLOG(LOG_WARN, TAG_STTD, "[Recorder] focus_state_watch_cb is called, focus_state(%d), reason(%s)", (int)focus_state, __stt_get_focus_changed_reason_code(reason));
 
-       int ret;
-       sound_stream_focus_state_e state_for_recording;
-       ret = sound_manager_get_focus_state(g_stream_info_h, NULL, &state_for_recording);
-       if (SOUND_MANAGER_ERROR_NONE != ret) {
-               SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to get focus state");
-               return;
-       }
-
-       SLOG(LOG_WARN, TAG_STTD, "[Recorder] focus state changed to (%d) with reason(%s)", (int)state_for_recording, __stt_get_focus_changed_reason_code(reason));
-       
-       if (STTD_RECORDER_STATE_RECORDING == g_recorder_state && SOUND_STREAM_FOCUS_STATE_RELEASED == state_for_recording) {
+       if (STTD_RECORDER_STATE_RECORDING == g_recorder_state && SOUND_STREAM_FOCUS_STATE_ACQUIRED == focus_state) {
                SLOG(LOG_WARN, TAG_STTD, "[Recorder] Focus released as interrupt");
                if (NULL != g_interrupt_cb) {
                        g_interrupt_cb();
@@ -189,7 +171,6 @@ void __recorder_focus_state_cb(sound_stream_info_h stream_info, sound_stream_foc
        }
 }
 
-
 int sttd_recorder_initialize(stt_recorder_audio_cb audio_cb, stt_recorder_interrupt_cb interrupt_cb)
 {
        if (NULL == audio_cb || NULL == interrupt_cb) {
@@ -209,14 +190,18 @@ int sttd_recorder_initialize(stt_recorder_audio_cb audio_cb, stt_recorder_interr
        g_audio_cb = audio_cb;
        g_interrupt_cb = interrupt_cb;
        g_recorder_state = STTD_RECORDER_STATE_NONE;
-       g_recording_engine_id = -1;
+       g_recorder = NULL;
 
-       if (0 != sound_manager_create_stream_information(SOUND_STREAM_TYPE_VOICE_RECOGNITION, __recorder_focus_state_cb, NULL, &g_stream_info_h)) {
+       if (0 != sound_manager_create_stream_information(SOUND_STREAM_TYPE_VOICE_RECOGNITION, NULL, NULL, &g_stream_info_h)) {
                SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to create stream info");
        }
 
+       if (0 != sound_manager_add_focus_state_watch_cb(SOUND_STREAM_FOCUS_FOR_BOTH, __recorder_focus_state_watch_cb, NULL, &g_stream_focus_id)) {
+               SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to add focus state watch callback");
+       }
+
 #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;
        }
@@ -236,10 +221,22 @@ int sttd_recorder_deinitialize()
                SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy audio in handle mutex.");
        }
 
+       if (0 != sound_manager_remove_focus_state_watch_cb(g_stream_focus_id)) {
+               SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to remove focus state watch callback");
+       }
+
        if (0 != sound_manager_destroy_stream_information(g_stream_info_h)) {
                SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to destroy stream info");
        }
 
+       if (NULL != g_recorder) {
+               audio_in_destroy(g_recorder->audio_h);
+               g_recorder->audio_h = NULL;
+               free(g_recorder);
+               g_recorder = NULL;
+       }
+
+#ifdef __UNUSED_CODES__
        /* Remove all recorder */
        GSList *iter = NULL;
        stt_recorder_s *recorder = NULL;
@@ -251,7 +248,7 @@ int sttd_recorder_deinitialize()
 
                if (NULL != recorder) {
                        g_recorder_list = g_slist_remove(g_recorder_list, recorder);
-                       if(recorder->audio_h) {
+                       if (recorder->audio_h) {
                                audio_in_destroy(recorder->audio_h);
                                recorder->audio_h = NULL;
                        }
@@ -260,11 +257,12 @@ int sttd_recorder_deinitialize()
 
                iter = g_slist_nth(g_recorder_list, 0);
        }
+#endif
 
 #ifdef TV_BT_MODE
-       bt_hid_host_deinitialize ();
+       bt_hid_host_deinitialize();
 
-       bt_deinitialize();
+       bt_product_deinit();
 #endif
 
        g_recorder_state = STTD_RECORDER_STATE_NONE;
@@ -272,26 +270,6 @@ int sttd_recorder_deinitialize()
        return 0;
 }
 
-static stt_recorder_s* __get_recorder(int engine_id)
-{
-       GSList *iter = NULL;
-       stt_recorder_s *recorder = NULL;
-
-       iter = g_slist_nth(g_recorder_list, 0);
-
-       while (NULL != iter) {
-               recorder = iter->data;
-
-               if (recorder->engine_id == engine_id) {
-                       return recorder;
-               }
-
-               iter = g_slist_next(iter);
-       }
-
-       return NULL;
-}
-
 int sttd_recorder_set_audio_session()
 {
        return 0;
@@ -302,10 +280,10 @@ int sttd_recorder_unset_audio_session()
        return 0;
 }
 
-int sttd_recorder_create(int engine_id, int uid, sttp_audio_type_e type, int channel, unsigned int sample_rate)
+int sttd_recorder_create(stte_audio_type_e type, int channel, unsigned int sample_rate)
 {
        /* Check engine id is valid */
-       if (NULL != __get_recorder(engine_id)) {
+       if (NULL != g_recorder) {
                SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is already registered");
                return STTD_ERROR_INVALID_PARAMETER;
        }
@@ -326,8 +304,8 @@ int sttd_recorder_create(int engine_id, int uid, sttp_audio_type_e type, int cha
        }
 
        switch (type) {
-       case STTP_AUDIO_TYPE_PCM_S16_LE:        audio_type = AUDIO_SAMPLE_TYPE_S16_LE;  break;
-       case STTP_AUDIO_TYPE_PCM_U8:            audio_type = AUDIO_SAMPLE_TYPE_U8;      break;
+       case STTE_AUDIO_TYPE_PCM_S16_LE:        audio_type = AUDIO_SAMPLE_TYPE_S16_LE;  break;
+       case STTE_AUDIO_TYPE_PCM_U8:            audio_type = AUDIO_SAMPLE_TYPE_U8;      break;
        default:
                SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Invalid Audio Type");
                return STTD_ERROR_OPERATION_FAILED;
@@ -360,28 +338,25 @@ int sttd_recorder_create(int engine_id, int uid, sttp_audio_type_e type, int cha
                return STTD_ERROR_OUT_OF_MEMORY;
        }
 
-       recorder->engine_id = engine_id;
-       recorder->uid = uid;
+       recorder->uid = -1;
        recorder->audio_h = temp_in_h;
        recorder->audio_type = type;
 
-       g_recorder_list = g_slist_append(g_recorder_list, recorder);
+       g_recorder = recorder;
 
        g_recorder_state = STTD_RECORDER_STATE_READY;
 
        return 0;
 }
 
-int sttd_recorder_destroy(int engine_id)
+int sttd_recorder_destroy()
 {
        // 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 */
-       stt_recorder_s* recorder;
-       recorder = __get_recorder(engine_id);
-       if (NULL == recorder) {
+       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;
@@ -390,8 +365,8 @@ int sttd_recorder_destroy(int engine_id)
 #ifndef TV_BT_MODE
        int ret;
        if (STTD_RECORDER_STATE_RECORDING == g_recorder_state) {
-               if (recorder->audio_h) {
-                       ret = audio_in_unprepare(recorder->audio_h);
+               if (g_recorder->audio_h) {
+                       ret = audio_in_unprepare(g_recorder->audio_h);
                        if (AUDIO_IO_ERROR_NONE != ret) {
                                SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to unprepare audioin : %d", ret);
                        }
@@ -400,14 +375,15 @@ int sttd_recorder_destroy(int engine_id)
                g_recorder_state = STTD_RECORDER_STATE_READY;
        }
 
-       if (recorder->audio_h) {
-               ret = audio_in_destroy(recorder->audio_h);
+       if (g_recorder->audio_h) {
+               ret = audio_in_destroy(g_recorder->audio_h);
                if (AUDIO_IO_ERROR_NONE != ret) {
                        SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to destroy audioin : %d", ret);
                }
-               recorder->audio_h = NULL;
+               g_recorder->audio_h = NULL;
        }
-#else 
+
+#else
        if (STTD_RECORDER_STATE_RECORDING == g_recorder_state) {
                g_recorder_state = STTD_RECORDER_STATE_READY;
        }
@@ -415,9 +391,8 @@ int sttd_recorder_destroy(int engine_id)
        bt_hid_unset_audio_data_receive_cb();
 #endif
 
-       g_recorder_list = g_slist_remove(g_recorder_list, recorder);
-
-       free(recorder);
+       free(g_recorder);
+       g_recorder = NULL;
 
        pthread_mutex_unlock(&sttd_audio_in_handle_mutex);
        SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Leave critical section");
@@ -425,7 +400,7 @@ int sttd_recorder_destroy(int engine_id)
        return 0;
 }
 
-static float get_volume_decibel(char* data, int size, sttp_audio_type_e type)
+static float get_volume_decibel(char* data, int size, stte_audio_type_e type)
 {
        #define MAX_AMPLITUDE_MEAN_16   32768
        #define MAX_AMPLITUDE_MEAN_08   128
@@ -437,7 +412,7 @@ static float get_volume_decibel(char* data, int size, sttp_audio_type_e type)
        float rms = 0.0;
        unsigned long long square_sum = 0;
 
-       if (type == STTP_AUDIO_TYPE_PCM_S16_LE)
+       if (type == STTE_AUDIO_TYPE_PCM_S16_LE)
                depthByte = 2;
        else
                depthByte = 1;
@@ -458,7 +433,7 @@ static float get_volume_decibel(char* data, int size, sttp_audio_type_e type)
        if (0 == count)
                rms = 0.0;
        else
-               rms = sqrt(square_sum/count);
+               rms = sqrt((double)square_sum/count);
 
        if (depthByte == 2)
                db = 20 * log10(rms/MAX_AMPLITUDE_MEAN_16);
@@ -475,9 +450,7 @@ Eina_Bool __read_audio_func(void *data)
        static char g_buffer[BUFFER_LENGTH];
 
        /* Check engine id is valid */
-       stt_recorder_s* recorder;
-       recorder = __get_recorder(g_recording_engine_id);
-       if (NULL == recorder) {
+       if (NULL == g_recorder) {
                return EINA_FALSE;
        }
 
@@ -486,7 +459,7 @@ Eina_Bool __read_audio_func(void *data)
                return EINA_FALSE;
        }
 
-       read_byte = audio_in_read(recorder->audio_h, g_buffer, BUFFER_LENGTH);
+       read_byte = audio_in_read(g_recorder->audio_h, g_buffer, BUFFER_LENGTH);
        if (0 > read_byte) {
                SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Fail to read audio : %d", read_byte);
                g_recorder_state = STTD_RECORDER_STATE_READY;
@@ -495,13 +468,13 @@ Eina_Bool __read_audio_func(void *data)
 
        if (0 != g_audio_cb(g_buffer, read_byte)) {
                SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Fail audio callback");
-               sttd_recorder_stop(g_recording_engine_id);
+               sttd_recorder_stop();
                return EINA_FALSE;
        }
 
        if (0 == g_buffer_count % 30) {
-               float vol_db = get_volume_decibel(g_buffer, BUFFER_LENGTH, recorder->audio_type);
-               if (0 != sttdc_send_set_volume(recorder->uid, vol_db)) {
+               float vol_db = get_volume_decibel(g_buffer, 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);
                }
        }
@@ -526,7 +499,7 @@ Eina_Bool __read_audio_func(void *data)
 }
 #endif
 
-int sttd_recorder_start(int engine_id)
+int sttd_recorder_start(int uid)
 {
        if (STTD_RECORDER_STATE_RECORDING == g_recorder_state)
                return 0;
@@ -534,24 +507,17 @@ int sttd_recorder_start(int engine_id)
        int ret = -1;
 #ifndef TV_BT_MODE
        /* Check engine id is valid */
-       stt_recorder_s* recorder;
-       recorder = __get_recorder(engine_id);
-       if (NULL == recorder) {
+       if (NULL == g_recorder) {
                SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
                return STTD_ERROR_INVALID_PARAMETER;
        }
 
-       ret = sound_manager_acquire_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_RECORDING, NULL);
-       if (SOUND_MANAGER_ERROR_NONE != ret) {
-               SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to acquire focus : %d", ret);
-       } else {
-               ret = audio_in_set_stream_info(recorder->audio_h, g_stream_info_h);
-               if (AUDIO_IO_ERROR_NONE != ret) {
-                       SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to set stream info");
-               }
+       ret = audio_in_set_sound_stream_info(g_recorder->audio_h, g_stream_info_h);
+       if (AUDIO_IO_ERROR_NONE != ret) {
+               SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to set stream info");
        }
-       
-       ret = audio_in_prepare(recorder->audio_h);
+
+       ret = audio_in_prepare(g_recorder->audio_h);
        if (AUDIO_IO_ERROR_NONE != ret) {
                SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to start audio : %d", ret);
                return STTD_ERROR_RECORDER_BUSY;
@@ -585,18 +551,34 @@ int sttd_recorder_start(int engine_id)
        }
 #endif
        g_recorder_state = STTD_RECORDER_STATE_RECORDING;
-       g_recording_engine_id = engine_id;
+       g_recorder->uid = uid;
 
        g_buffer_count = 0;
 
 #ifdef BUF_SAVE_MODE
        g_count++;
 
-       snprintf(g_temp_file_name, sizeof(g_temp_file_name), "/tmp/stt_temp_%d_%d", getpid(), g_count);
+       while (1) {
+               snprintf(g_temp_file_name, sizeof(g_temp_file_name), "/tmp/stt_temp_%d_%d", getpid(), g_count);
+               ret = access(g_temp_file_name, 0);
+
+               if (0 == ret) {
+                       SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] File is already exist");
+                       if (0 == remove(g_temp_file_name)) {
+                               SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Remove file");
+                               break;
+                       } else {
+                               g_count++;
+                       }
+               } else {
+                       break;
+               }
+       }
+
        SECURE_SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Temp file name=[%s]", g_temp_file_name);
 
        /* open test file */
-       g_pFile = fopen(g_temp_file_name, "wb+");
+       g_pFile = fopen(g_temp_file_name, "wb+x");
        if (!g_pFile) {
                SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] File not found!");
                return -1;
@@ -606,26 +588,31 @@ int sttd_recorder_start(int engine_id)
        return 0;
 }
 
-int sttd_recorder_stop(int engine_id)
+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 */
-       stt_recorder_s* recorder;
-       recorder = __get_recorder(engine_id);
-       if (NULL == recorder) {
+       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;
        }
 
-       int ret;
+       g_recorder->uid = -1;
+
+       int ret = -1;
 #ifndef TV_BT_MODE
-       ret = audio_in_unprepare(recorder->audio_h);
+       ret = audio_in_unprepare(g_recorder->audio_h);
        if (AUDIO_IO_ERROR_NONE != ret) {
                SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to unprepare audioin : %d", ret);
        }
-#else 
+#else
        int bt_retry = 0;
        bool stopped = false;
        while (5 > bt_retry) {
@@ -639,26 +626,90 @@ int sttd_recorder_stop(int engine_id)
                        usleep(50000);
                        bt_retry++;
                } else {
+                       SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] bt_hid_rc_stop_sending_voice(): ret(%d)", ret);
                        break;
                }
        }
        if (false == stopped) {
-               SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to stop bt audio");
-               return STTD_ERROR_OPERATION_FAILED;
+               SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to stop bt audio(%d)", ret);
+//             return STTD_ERROR_OPERATION_FAILED;
        }
 #endif
 
        g_recorder_state = STTD_RECORDER_STATE_READY;
-       g_recording_engine_id = -1;
-
-       ret = sound_manager_release_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_RECORDING, NULL);
-       if (SOUND_MANAGER_ERROR_NONE != ret) {
-               SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to release focus :%d", ret);
-       }
 
 #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;
 }