Enable ffv recording
authorJi-hoon Lee <dalton.lee@samsung.com>
Wed, 14 Aug 2019 11:49:50 +0000 (20:49 +0900)
committerJi-hoon Lee <dalton.lee@samsung.com>
Wed, 21 Aug 2019 04:44:08 +0000 (13:44 +0900)
Change-Id: I93a7de4b762768b0b27b093e3224a7e48c29e3df

plugins/wakeup-manager/dependency-tv/src/dependency_tv_audio.cpp

index c648d33..ccf337c 100644 (file)
@@ -17,15 +17,34 @@ using namespace std;
 
 #include <bluetooth_product.h>
 #include <farfield-voice-api.h>
+#include <vconf.h>
 
 #define SMART_CONTROL_EXTEND_CMD       0x03
 #define SMART_CONTROL_START_CMD                0x04
 
+#define VC_AUDIO_ID_BLUETOOTH "VC_AUDIO_ID_BLUETOOTH"
+#define VC_AUDIO_ID_MIC "VC_FARFIELD_VOICE_VD"
+
+/* Sound buf save for test */
+#if 0
+#define BUF_SAVE_MODE
+#endif
+
+#ifdef BUF_SAVE_MODE
+static char g_temp_file_name[128] = {'\0', };
+
+static FILE* g_pFile = NULL;
+
+static int g_count = 1;
+#endif
+
 static int g_bt_extend_count;
 static farfield_voice_h g_farfieldvoice_h = NULL;
 
 static sound_stream_info_h g_volume_stream = NULL;
 static virtual_sound_stream_h g_virtual_sound_stream = NULL;
+#define AUDIO_SOURCE_TYPE_LEN 64
+static char g_audio_source_type[AUDIO_SOURCE_TYPE_LEN];
 
 static long get_current_milliseconds_after_epoch()
 {
@@ -37,6 +56,18 @@ static long get_current_milliseconds_after_epoch()
        return value.count();
 }
 
+static void set_audio_source_type(const char* src_type)
+{
+       if (0 != strncmp(src_type, g_audio_source_type, AUDIO_SOURCE_TYPE_LEN)) {
+               snprintf(g_audio_source_type, AUDIO_SOURCE_TYPE_LEN, "%s", src_type);
+               g_audio_source_type[AUDIO_SOURCE_TYPE_LEN - 1] = '\0';
+               LOGE("[Recorder] source_type [%s]", g_audio_source_type);
+
+               // TODO::
+               vconf_set_str("db/farfieldvoice/devicetype", src_type);
+       }
+}
+
 static void _bt_cb_hid_state_changed(int result, bool connected, const char *remote_address, void *user_data)
 {
        LOGD("[Recorder] Bluetooth Event [%d] Received address [%s]", result, remote_address);
@@ -49,6 +80,8 @@ static void _bt_hid_audio_data_receive_cb(bt_hid_voice_data_s *voice_data, void
        if (nullptr == voice_data) return;
 
        if (g_voice_key_pressed) {
+               set_audio_source_type(VC_AUDIO_ID_BLUETOOTH);
+
                g_proxy_interface.feed_audio_data(WAKEUP_SPEECH_STREAMING_EVENT_CONTINUE,
                        voice_data->audio_buf, voice_data->length);
        } else {
@@ -56,6 +89,12 @@ static void _bt_hid_audio_data_receive_cb(bt_hid_voice_data_s *voice_data, void
                return;
        }
 
+#ifdef BUF_SAVE_MODE
+       /* write pcm buffer */
+       if (g_pFile)
+               fwrite(voice_data->audio_buf, 1, voice_data->length, g_pFile);
+#endif
+
        if (0 == g_buffer_count || 0 == g_buffer_count % 50) {
                LOGD("[Recorder][%d] Recording... : read_size(%d)", g_buffer_count, voice_data->length);
 
@@ -79,11 +118,24 @@ static void _bt_hid_audio_data_receive_cb(bt_hid_voice_data_s *voice_data, void
        return;
 }
 
+static void stereo_to_mono_pcm(char* mono_buffer, const char* stereo_buffer, int len)
+{
+       int cnt = 0;
+       while (cnt < len) {
+               /* The code below collects audio data from only one channel for testing */
+               mono_buffer[cnt / 2] = stereo_buffer[cnt];
+               mono_buffer[cnt / 2 + 1] = stereo_buffer[cnt + 1];
+               cnt += 4;
+       }
+}
+
 static void _ffv_audio_function_cb(void* data, unsigned int length, void* user_data)
 {
        /* When voice key is pressed, _bt_hid_audio should receive audio data */
        if (g_voice_key_pressed) return;
 
+       set_audio_source_type(VC_AUDIO_ID_MIC);
+
        static int g_buffer_count = 0;
        if (0 == g_buffer_count || 0 == g_buffer_count % 50) {
                LOGD("[Recorder INFO] farfield audio function callback is invoked");
@@ -94,8 +146,32 @@ static void _ffv_audio_function_cb(void* data, unsigned int length, void* user_d
        }
        g_buffer_count++;
 
+#ifdef STEREO_TO_MONO
+       int mono_length = length / 2;
+       char* mono_buffer = (char*)calloc(length, sizeof(char));
+       if (!mono_buffer) return;
+       stereo_to_mono_pcm(mono_buffer, (const char*)data, length);
+
+       g_proxy_interface.feed_audio_data(WAKEUP_SPEECH_STREAMING_EVENT_CONTINUE,
+               mono_buffer, mono_length);
+
+#ifdef BUF_SAVE_MODE
+       /* write pcm buffer */
+       if (g_pFile)
+               fwrite(mono_buffer, 1, mono_length, g_pFile);
+#endif
+
+       if (mono_buffer) free(mono_buffer);
+       mono_buffer = NULL;
+#else
        g_proxy_interface.feed_audio_data(WAKEUP_SPEECH_STREAMING_EVENT_CONTINUE,
                data, length);
+#ifdef BUF_SAVE_MODE
+       /* write pcm buffer */
+       if (g_pFile)
+               fwrite(data, 1, length, g_pFile);
+#endif
+#endif
 }
 
 void dependency_tv_audio_initialize(mas_proxy_interface interfaces)
@@ -123,7 +199,7 @@ void dependency_tv_audio_initialize(mas_proxy_interface interfaces)
                LOGD("[Recorder] Bluetooth is available");
        }
 
-    /*
+#if 1
        g_farfieldvoice_h = farfield_voice_init();
        if (NULL == g_farfieldvoice_h) {
                LOGE("[Recorder ERROR] Fail to init farfield_voice_init");
@@ -133,7 +209,9 @@ void dependency_tv_audio_initialize(mas_proxy_interface interfaces)
                LOGI("[Recorder INFO] Register farfield voice audio callback");
                farfield_voice_register_audio_cb(g_farfieldvoice_h, _ffv_audio_function_cb, NULL);
        }
-    */
+#endif
+
+       set_audio_source_type(VC_AUDIO_ID_BLUETOOTH);
 }
 
 void dependency_tv_audio_deinitialize()
@@ -156,14 +234,14 @@ void dependency_tv_audio_deinitialize()
                g_volume_stream = NULL;
        }
 
-    /*
+#if 1
        if (NULL != g_farfieldvoice_h) {
                LOGD("[Recorder INFO] Unregister farfield voice");
                farfield_voice_unregister_audio_cb(g_farfieldvoice_h);
                farfield_voice_final(g_farfieldvoice_h);
                g_farfieldvoice_h = NULL;
        }
-    */
+#endif
 
        bt_hid_unset_audio_data_receive_cb();
        bt_hid_host_deinitialize();
@@ -204,6 +282,41 @@ void dependency_tv_audio_start_recording()
 
                g_bt_extend_count = 0;
        }
+#ifdef BUF_SAVE_MODE
+       if (g_pFile) {
+               fclose(g_pFile);
+               g_pFile = NULL;
+       } else {
+               LOGD("[Recorder Info] File not found!");
+       }
+
+       while (1) {
+               snprintf(g_temp_file_name, sizeof(g_temp_file_name), "/tmp/dependency_tv_%s_%d_%d", g_audio_source_type, getpid(), g_count);
+               int ret = access(g_temp_file_name, 0);
+
+               if (0 == ret) {
+                       LOGD("[Recorder ERROR] File is already exist");
+                       if (0 == remove(g_temp_file_name)) {
+                               LOGD("[Recorder] Remove file");
+                               break;
+                       } else {
+                               g_count++;
+                       }
+               } else {
+                       break;
+               }
+       }
+
+       LOGD("[Recorder] Temp file name=[%s]", g_temp_file_name);
+
+       /* open test file */
+       g_pFile = fopen(g_temp_file_name, "wb+x");
+       if (!g_pFile) {
+               LOGD("[Recorder ERROR] File not found!");
+               return;
+       }
+       g_count++;
+#endif
 }
 
 void dependency_tv_audio_stop_recording()
@@ -284,6 +397,7 @@ void dependency_tv_audio_voice_key_pressed_set(bool pressed)
 
        if (pressed != g_voice_key_pressed) {
                if (pressed) {
+                       set_audio_source_type(VC_AUDIO_ID_BLUETOOTH);
                        dependency_tv_audio_change_system_volume();
                } else {
                        dependency_tv_audio_recover_system_volume();
@@ -306,10 +420,9 @@ void dependency_tv_audio_get_audio_format(int* rate, int* channel, int* audio_ty
 
 void dependency_tv_audio_get_audio_source_type(char** type)
 {
-       static char source_type[] = "VC_AUDIO_ID_BLUETOOTH";
        if (!type) {
                return;
        }
-       *type = source_type;
+       *type = g_audio_source_type;
 }