Add to stop recording from FFV
[platform/core/uifw/stt.git] / server / sttd_recorder.c
index b24bc07..4452096 100644 (file)
@@ -13,6 +13,7 @@
 
 #ifdef TV_PRODUCT
 #define TV_BT_MODE
+#define TV_FFV_MODE
 #endif
 
 #include <audio_io.h>
 #include <math.h>
 #include <pthread.h>
 #include <sound_manager.h>
+#include <sound_manager_internal.h>
 #ifdef TV_BT_MODE
 #include <bluetooth_product.h>
 #endif
+#ifdef TV_FFV_MODE
+#include <farfield-voice-api.h>
+#endif
 
 #include "stt_defs.h"
 #include "sttd_dbus.h"
 #include "sttd_recorder.h"
 #include "sttd_main.h"
+#include "sttd_engine_agent.h"
+#include "sttd_client_data.h"
 
+#define STTE_AUDIO_ID_NONE             "STT_AUDIO_ID_NONE"             /**< None audio id */
+#ifdef TV_FFV_MODE
+#define STTE_AUDIO_ID_FFV              "STT_FARFIELD_VOICE_VD"
+#endif
 
-#define FRAME_LENGTH 160
+#define FRAME_LENGTH 320
 #define BUFFER_LENGTH FRAME_LENGTH * 2
 
 static pthread_mutex_t sttd_audio_in_handle_mutex = PTHREAD_MUTEX_INITIALIZER;
 
+#ifndef TV_BT_MODE
+static Ecore_Timer* g_audio_timer = NULL;
+#endif
+
 typedef enum {
        STTD_RECORDER_STATE_NONE = -1,
        STTD_RECORDER_STATE_READY = 0,  /**< Recorder is ready to start */
@@ -59,6 +74,14 @@ static sttd_recorder_state   g_recorder_state = STTD_RECORDER_STATE_NONE;
 
 static int g_buffer_count;
 
+static int g_stream_focus_id;
+
+static bool g_is_set_audio_type = false;
+
+#ifdef TV_FFV_MODE
+farfield_voice_h g_farfieldvoice_h = NULL;
+#endif
+
 /* Sound buf save for test */
 /*
 #define BUF_SAVE_MODE
@@ -76,8 +99,6 @@ static float get_volume_decibel(char* data, int size, stte_audio_type_e type);
 
 //static stt_recorder_s* __get_recorder(int engine_id);
 
-static int g_bt_extend_count;
-
 #define SMART_CONTROL_EXTEND_CMD       0x03
 #define SMART_CONTROL_START_CMD                0x04
 
@@ -88,12 +109,29 @@ static void _bt_cb_hid_state_changed(int result, bool connected, const char *rem
 
 static void _bt_hid_audio_data_receive_cb(bt_hid_voice_data_s *voice_data, void *user_data)
 {
+       if (0 >= sttd_client_get_ref_count()) {
+               if (0 == g_buffer_count % 50) {
+                       SLOG(LOG_INFO, TAG_STTD, "[Recorder INFO] BT audio function callback is invoked, but no cliet");
+
+                       if (100000 == g_buffer_count) {
+                               g_buffer_count = 0;
+                       }
+               }
+               g_buffer_count++;
+               return;
+       }
+
        if (STTD_RECORDER_STATE_RECORDING != g_recorder_state) {
-               /* SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Exit audio reading normal func"); */
+               SLOG(LOG_INFO, TAG_STTD, "[Recorder] Exit audio reading normal func");
                return;
        }
 
        if (NULL != g_audio_cb) {
+               if (false == g_is_set_audio_type) {
+                       sttd_engine_agent_set_audio_type(STTE_AUDIO_ID_BLUETOOTH);
+                       g_is_set_audio_type = true;
+               }
+
                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();
@@ -112,16 +150,6 @@ static void _bt_hid_audio_data_receive_cb(bt_hid_voice_data_s *voice_data, void
        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);
 
-               if (0 == g_bt_extend_count % 5 && 0 != g_buffer_count) {
-                       const unsigned char input_data[2] = {SMART_CONTROL_EXTEND_CMD, 0x10 };
-                       if (BT_ERROR_NONE != bt_hid_send_rc_command(NULL, input_data, sizeof(input_data))) {
-                               SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail bt_hid_send_rc_command(NULL, %s, %d)", input_data, sizeof(input_data));
-                       } else {
-                               SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Extend bt audio recorder");
-                       }
-               }
-               g_bt_extend_count++;
-
                if (100000 == g_buffer_count) {
                        g_buffer_count = 0;
                }
@@ -137,6 +165,59 @@ static void _bt_hid_audio_data_receive_cb(bt_hid_voice_data_s *voice_data, void
 }
 #endif
 
+#ifdef TV_FFV_MODE
+static void _ffv_audio_function_cb(void* data, unsigned int length, void* user_data)
+{
+       if (STTD_RECORDER_STATE_RECORDING != g_recorder_state) {
+               SLOG(LOG_INFO, TAG_STTD, "[Recorder] Exit audio reading normal func");
+               return;
+       }
+
+       if (0 >= sttd_client_get_ref_count()) {
+               if (0 == g_buffer_count % 50) {
+                       SLOG(LOG_INFO, TAG_STTD, "[Recorder INFO] farfield audio function callback is invoked, but no cliet");
+
+                       if (100000 == g_buffer_count) {
+                               g_buffer_count = 0;
+                       }
+               }
+               g_buffer_count++;
+               return;
+       }
+
+       if (false == g_is_set_audio_type) {
+               sttd_engine_agent_set_audio_type(STTE_AUDIO_ID_FFV);
+               g_is_set_audio_type = true;
+       }
+
+       if (0 == g_buffer_count % 50) {
+               SLOG(LOG_INFO, TAG_STTD, "[Recorder INFO] farfield audio function callback is invoked");
+
+               if (100000 == g_buffer_count) {
+                       g_buffer_count = 0;
+               }
+       }
+
+       g_buffer_count++;
+
+       if (NULL != g_audio_cb) {
+               if (0 != g_audio_cb(data, length)) {
+                       SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to read audio");
+                       sttd_recorder_stop();
+               }
+
+               if (NULL == g_recorder) {
+                       SLOG(LOG_INFO, TAG_STTD, "[Recorder INFO] g_recoder is NULL");
+                       return;
+               }
+
+               float vol_db = get_volume_decibel((char*)data, (int)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);
+               }
+       }
+}
+#endif
 
 const char* __stt_get_focus_changed_reason_code(sound_stream_focus_change_reason_e reason)
 {
@@ -156,18 +237,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_mask_e focus_mask, sound_stream_focus_state_e focus_state,
-                                                       sound_stream_focus_change_reason_e reason, int sound_behavior, 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");
+       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));
 
-       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 changed to (%d) with reason(%s)", (int)focus_state, __stt_get_focus_changed_reason_code(reason));
-
-       if (STTD_RECORDER_STATE_RECORDING == g_recorder_state && SOUND_STREAM_FOCUS_STATE_RELEASED == focus_state) {
+       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();
@@ -175,7 +250,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) {
@@ -197,12 +271,26 @@ int sttd_recorder_initialize(stt_recorder_audio_cb audio_cb, stt_recorder_interr
        g_recorder_state = STTD_RECORDER_STATE_NONE;
        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_internal(SOUND_STREAM_TYPE_VOICE_RECOGNITION_SERVICE, 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_FFV_MODE
+       g_farfieldvoice_h = farfield_voice_init();
+       if (NULL == g_farfieldvoice_h) {
+               SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to init farfield_voice_init");
+       } else {
+               SLOG(LOG_INFO, TAG_STTD, "[Recorder INFO] Register farfield voice audio callback");
+               farfield_voice_register_audio_cb(g_farfieldvoice_h, _ffv_audio_function_cb, NULL);
+       }
+#endif
+
 #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;
        }
@@ -222,6 +310,10 @@ 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");
        }
@@ -256,10 +348,19 @@ int sttd_recorder_deinitialize()
        }
 #endif
 
+#ifdef TV_FFV_MODE
+       if (NULL != g_farfieldvoice_h) {
+               SLOG(LOG_INFO, TAG_STTD, "[Recorder INFO] Unregister farfield voice");
+               farfield_voice_unregister_audio_cb(g_farfieldvoice_h);
+               farfield_voice_final(g_farfieldvoice_h);
+               g_farfieldvoice_h = NULL;
+       }
+#endif
+
 #ifdef TV_BT_MODE
        bt_hid_host_deinitialize();
 
-       bt_deinitialize();
+       bt_product_deinit();
 #endif
 
        g_recorder_state = STTD_RECORDER_STATE_NONE;
@@ -397,7 +498,7 @@ int sttd_recorder_destroy()
        return 0;
 }
 
-static float get_volume_decibel(char* data, int size, stte_audio_type_e type)
+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
@@ -469,7 +570,10 @@ Eina_Bool __read_audio_func(void *data)
                return EINA_FALSE;
        }
 
-       if (0 == g_buffer_count % 30) {
+       if (NULL == g_recorder)
+               SLOG(LOG_WARN, TAG_STTD, "[Recorder] g_recorder is NULL. It is already stopped and destroyed.");
+
+       if (0 == g_buffer_count % 30 && NULL != g_recorder) {
                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);
@@ -501,22 +605,17 @@ int sttd_recorder_start(int uid)
        if (STTD_RECORDER_STATE_RECORDING == g_recorder_state)
                return 0;
 
-       int ret = -1;
 #ifndef TV_BT_MODE
+       int ret = -1;
        /* 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;
        }
 
-       ret = sound_manager_acquire_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_RECORDING, SOUND_BEHAVIOR_NONE, 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_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_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(g_recorder->audio_h);
@@ -526,32 +625,18 @@ int sttd_recorder_start(int uid)
        }
 
        /* Add ecore timer to read audio data */
-       ecore_timer_add(0, __read_audio_func, NULL);
-
-#else
-       g_bt_extend_count = 0;
-       const unsigned char input_data[2] = {SMART_CONTROL_START_CMD, 0x00 };
-       int bt_retry = 0;
-       bool started = false;
-       while (5 > bt_retry) {
-               ret = bt_hid_send_rc_command(NULL, input_data, sizeof(input_data));
-               if (BT_ERROR_NONE == ret) {
-                       SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Start bt audio recorder");
-                       started = true;
-                       break;
-               } else if (BT_ERROR_NOW_IN_PROGRESS == ret) {
-                       SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail bt_hid_send_rc_command(NULL, %s, %d)", input_data, sizeof(input_data));
-                       usleep(50000);
-                       bt_retry++;
-               } else {
-                       break;
-               }
+       if (NULL != g_audio_timer) {
+               ecore_timer_del(g_audio_timer);
+               g_audio_timer = NULL;
        }
-       if (false == started) {
-               SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to start bt audio");
+
+       g_audio_timer = ecore_timer_add(0, __read_audio_func, NULL);
+       if (NULL == g_audio_timer) {
+               SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Fail to create timer of read_audio");
                return STTD_ERROR_OPERATION_FAILED;
        }
 #endif
+
        g_recorder_state = STTD_RECORDER_STATE_RECORDING;
        g_recorder->uid = uid;
 
@@ -560,11 +645,27 @@ int sttd_recorder_start(int uid)
 #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;
@@ -579,9 +680,17 @@ 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);
+
+       g_is_set_audio_type = false;
+       sttd_engine_agent_set_audio_type(STTE_AUDIO_ID_NONE);
+
        /* 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;
        }
 
@@ -589,6 +698,11 @@ int sttd_recorder_stop()
 
        int ret = -1;
 #ifndef TV_BT_MODE
+       if (NULL != g_audio_timer) {
+               ecore_timer_del(g_audio_timer);
+               g_audio_timer = NULL;
+       }
+
        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);
@@ -617,16 +731,103 @@ int sttd_recorder_stop()
        }
 #endif
 
-       g_recorder_state = STTD_RECORDER_STATE_READY;
-
-       ret = sound_manager_release_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_RECORDING, SOUND_BEHAVIOR_NONE, NULL);
-       if (SOUND_MANAGER_ERROR_NONE != ret) {
-               SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to release focus :%d", ret);
+#ifdef TV_FFV_MODE
+       ret = farfield_voice_set_recognition_state(FARFIELD_VOICE_RECOGNITION_STOP);
+       if (0 != ret) {
+               SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to stop FFV, ret(%d)", ret);
        }
+#endif
+
+       g_recorder_state = STTD_RECORDER_STATE_READY;
 
 #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;
+}
+
+int sttd_recorder_clear()
+{
+#ifdef TV_BT_MODE
+       SLOG(LOG_DEBUG, TAG_STTD, "[Recorder DEBUG] Clear recorder on TV");
+
+       if (STTD_RECORDER_STATE_RECORDING == g_recorder_state) {
+               sttd_recorder_stop();
+       }
+       if (STTD_RECORDER_STATE_READY == g_recorder_state) {
+               sttd_recorder_destroy();
+       }
+#endif
+
+       return STTD_ERROR_NONE;
+}