Fix - Coding rule
[platform/core/uifw/voice-control.git] / server / vcd_recorder.c
1 /*
2 * Copyright (c) 2011-2015 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifdef TV_PRODUCT
18 #define TV_BT_MODE
19 //#define TV_MSF_WIFI_MODE
20 #endif
21
22 #include <audio_io.h>
23 #include <math.h>
24 #include <sound_manager.h>
25 #ifdef TV_PRODUCT
26 #ifdef TV_BT_MODE
27 #include <bluetooth.h>
28 #endif
29 #ifdef TV_MSF_WIFI_MODE
30 #include <MSFVoiceInterface.h>
31 #endif
32 #endif
33
34 #include "vcd_client_data.h"
35 #include "vcd_dbus.h"
36 #include "vcd_recorder.h"
37 #include "vcd_main.h"
38 #include "voice_control_plugin_engine.h"
39
40 #define FRAME_LENGTH 160
41 #define BUFFER_LENGTH FRAME_LENGTH * 2
42
43 #define VCP_AUDIO_ID_NONE               "VC_AUDIO_ID_NONE"              /**< None audio id */
44
45 static vcd_recorder_state_e     g_recorder_state = VCD_RECORDER_STATE_READY;
46
47 static vcd_recoder_audio_cb     g_audio_cb = NULL;
48
49 static vcd_recorder_interrupt_cb        g_interrupt_cb = NULL;
50
51 static audio_in_h       g_audio_h;
52
53 static vcp_audio_type_e g_audio_type;
54
55 static unsigned int     g_audio_rate;
56
57 static int              g_audio_channel;
58
59 static char     g_normal_buffer[BUFFER_LENGTH + 10];
60
61 static bool     g_is_valid_audio_in = false;
62
63 static bool     g_is_valid_bt_in = false;
64
65 static char*    g_current_audio_type = NULL;
66
67 static int      g_buffer_count;
68
69 /* Sound buf save */
70 /*
71 #define BUF_SAVE_MODE
72 */
73
74 #ifdef BUF_SAVE_MODE
75 static FILE* g_normal_file;
76
77 static int g_count = 1;
78 #endif
79
80 static float get_volume_decibel(char* data, int size);
81
82 #ifdef TV_MSF_WIFI_MODE
83 static void __msf_wifi_audio_data_receive_cb(msf_wifi_voice_data_s *voice_data, void* user_data)
84 {
85         if (VCD_RECORDER_STATE_RECORDING != g_recorder_state) {
86                 /*SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Exit audio reading normal func");*/
87                 return;
88         }
89
90         if (NULL != g_audio_cb) {
91                 if (0 != g_audio_cb((void*)voice_data->audio_buf, (unsigned int)voice_data->length)) {
92                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to read audio");
93                         vcd_recorder_stop();
94                 }
95         }
96
97         /* Set volume */
98         if (0 == g_buffer_count % 30) {
99                 float vol_db = get_volume_decibel((char*)voice_data->audio_buf, (unsigned int)voice_data->length);
100                 if (0 != vcdc_send_set_volume(vcd_client_manager_get_pid(), vol_db)) {
101                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder] Fail to send recording volume(%f)", vol_db);
102                 }
103         }
104
105         if (0 == g_buffer_count || 0 == g_buffer_count % 50) {
106                 SLOG(LOG_WARN, TAG_VCD, "[Recorder][%d] Recording... : read_size(%d)", g_buffer_count, voice_data->length);
107
108                 if (100000 == g_buffer_count)
109                         g_buffer_count = 0;
110         }
111
112         g_buffer_count++;
113
114 #ifdef BUF_SAVE_MODE
115         /* write pcm buffer */
116         fwrite(voice_data->audio_buf, 1, voice_data->length, g_normal_file);
117 #endif
118         return;
119 }
120
121 #endif
122
123
124 #ifdef TV_BT_MODE
125 static int g_bt_extend_count;
126
127 #define SMART_CONTROL_EXTEND_CMD        0x03
128 #define SMART_CONTROL_START_CMD         0x04
129
130 static void _bt_cb_hid_state_changed(int result, bool connected, const char *remote_address, void *user_data)
131 {
132         SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Bluetooth Event [%d] Received address [%s]", result, remote_address);
133         return;
134 }
135
136 static void _bt_hid_audio_data_receive_cb(bt_hid_voice_data_s *voice_data, void *user_data)
137 {
138         if (VCD_RECORDER_STATE_RECORDING != g_recorder_state) {
139                 /*SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Exit audio reading normal func");*/
140                 return;
141         }
142
143         if (NULL != g_audio_cb) {
144                 if (0 != g_audio_cb((void*)voice_data->audio_buf, (unsigned int)voice_data->length)) {
145                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to read audio");
146                         vcd_recorder_stop();
147                 }
148         }
149
150         /* Set volume */
151         if (0 == g_buffer_count % 30) {
152                 float vol_db = get_volume_decibel((char*)voice_data->audio_buf, (unsigned int)voice_data->length);
153                 if (0 != vcdc_send_set_volume(vcd_client_manager_get_pid(), vol_db)) {
154                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder] Fail to send recording volume(%f)", vol_db);
155                 }
156         }
157
158         if (0 == g_buffer_count || 0 == g_buffer_count % 50) {
159                 SLOG(LOG_WARN, TAG_VCD, "[Recorder][%d] Recording... : read_size(%d)", g_buffer_count, voice_data->length);
160
161                 if (0 == g_bt_extend_count % 5 && 0 != g_buffer_count) {
162                         const unsigned char input_data[2] = {SMART_CONTROL_EXTEND_CMD, 0x10 };
163                         if (BT_ERROR_NONE != bt_hid_send_rc_command(NULL, input_data, sizeof(input_data))) {
164                                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail bt_hid_send_rc_command(NULL, %s, %d)", input_data, sizeof(input_data));
165                         } else {
166                                 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Extend bt audio recorder");
167                         }
168                 }
169                 g_bt_extend_count++;
170
171                 if (100000 == g_buffer_count) {
172                         g_buffer_count = 0;
173                 }
174         }
175
176         g_buffer_count++;
177
178 #ifdef BUF_SAVE_MODE
179         /* write pcm buffer */
180         fwrite(voice_data->audio_buf, 1, voice_data->length, g_normal_file);
181 #endif
182         return;
183 }
184
185 #endif
186
187 int vcd_recorder_create(vcd_recoder_audio_cb audio_cb, vcd_recorder_interrupt_cb interrupt_cb)
188 {
189         if (NULL == audio_cb || NULL == interrupt_cb) {
190                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Input param is NOT valid");
191                 return VCD_ERROR_INVALID_PARAMETER;
192         }
193
194         int ret = 0;
195
196         /* set init value */
197         g_is_valid_audio_in = false;
198         g_is_valid_bt_in = false;
199         g_current_audio_type = NULL;
200
201         g_audio_type = VCP_AUDIO_TYPE_PCM_S16_LE;
202         g_audio_rate = 16000;
203         g_audio_channel = 1;
204
205         audio_channel_e audio_ch;
206         audio_sample_type_e audio_type;
207
208         switch (g_audio_channel) {
209         case 1: audio_ch = AUDIO_CHANNEL_MONO;          break;
210         case 2: audio_ch = AUDIO_CHANNEL_STEREO;        break;
211         default:
212                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Input channel is not supported");
213                 return VCD_ERROR_OPERATION_FAILED;
214                 break;
215         }
216
217         switch (g_audio_type) {
218         case VCP_AUDIO_TYPE_PCM_S16_LE: audio_type = AUDIO_SAMPLE_TYPE_S16_LE;  break;
219         case VCP_AUDIO_TYPE_PCM_U8:     audio_type = AUDIO_SAMPLE_TYPE_U8;      break;
220         default:
221                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Invalid Audio Type");
222                 return VCD_ERROR_OPERATION_FAILED;
223                 break;
224         }
225
226         ret = audio_in_create(g_audio_rate, audio_ch, audio_type, &g_audio_h);
227         if (AUDIO_IO_ERROR_NONE == ret) {
228                 g_is_valid_audio_in = true;
229         } else {
230                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Rate(%d) Channel(%d) Type(%d)", g_audio_rate, audio_ch, audio_type);
231                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to create audio handle : %d", ret);
232                 g_is_valid_audio_in = false;
233         }
234
235         g_audio_cb = audio_cb;
236         g_interrupt_cb = interrupt_cb;
237         g_recorder_state = VCD_RECORDER_STATE_READY;
238
239 #ifdef TV_BT_MODE
240
241         bool is_bt_failed = false;
242
243         if (false == is_bt_failed && BT_ERROR_NONE != bt_initialize()) {
244                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to init bt");
245                 is_bt_failed = true;
246         }
247
248         if (false == is_bt_failed && BT_ERROR_NONE != bt_hid_host_initialize(_bt_cb_hid_state_changed, NULL)) {
249                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail bt_hid_host_initialize()");
250                 is_bt_failed = true;
251         }
252
253         if (false == is_bt_failed && BT_ERROR_NONE != bt_hid_set_audio_data_receive_cb(_bt_hid_audio_data_receive_cb, NULL)) {
254                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail bt_hid_set_audio_data_receive_cb()");
255                 is_bt_failed = true;
256         }
257
258
259         if (false == is_bt_failed) {
260                 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Bluetooth is available");
261                 g_is_valid_bt_in = true;
262         }
263 #endif
264         /* Select default audio type */
265         if (true == g_is_valid_bt_in) {
266                 g_current_audio_type = strdup(VCP_AUDIO_ID_BLUETOOTH);
267         } else {
268                 g_current_audio_type = strdup(VCP_AUDIO_ID_NONE);
269         }
270
271         SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Audio type : %s", g_current_audio_type);
272
273         return 0;
274 }
275
276 int vcd_recorder_destroy()
277 {
278         if (VCD_RECORDER_STATE_RECORDING == g_recorder_state) {
279                 if (0 == strncmp(VCP_AUDIO_ID_BLUETOOTH, g_current_audio_type, strlen(VCP_AUDIO_ID_BLUETOOTH))) {
280 #ifdef TV_BT_MODE
281                         bt_hid_unset_audio_data_receive_cb();
282 #endif
283                 } else if (0 == strncmp(VCP_AUDIO_ID_MSF, g_current_audio_type, strlen(VCP_AUDIO_ID_MSF))) {
284 #ifdef TV_MSF_WIFI_MODE
285                         UnRegisterMSFAudioCallback();
286 #endif
287                 } else {
288                         audio_in_unprepare(g_audio_h);
289                 }
290                 g_recorder_state = VCD_RECORDER_STATE_READY;
291         }
292
293         audio_in_destroy(g_audio_h);
294
295 #ifdef TV_BT_MODE
296         bt_hid_unset_audio_data_receive_cb();
297
298         bt_hid_host_deinitialize();
299
300         bt_deinitialize();
301 #endif
302
303         g_audio_cb = NULL;
304
305         if (NULL != g_current_audio_type) {
306                 free(g_current_audio_type);
307                 g_current_audio_type = NULL;
308         }
309
310         return 0;
311 }
312
313 int vcd_recorder_set(const char* audio_type, vcp_audio_type_e type, int rate, int channel)
314 {
315         if (NULL == audio_type) {
316                 return VCD_ERROR_INVALID_PARAMETER;
317         }
318
319         if (NULL != g_current_audio_type) {
320                 if (0 == strncmp(g_current_audio_type, audio_type, strlen(g_current_audio_type))) {
321                         SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Current audio type is already set : %s", audio_type);
322                         return 0;
323                 }
324         }
325
326         if (VCD_RECORDER_STATE_READY != g_recorder_state) {
327                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Recorder is NOT ready");
328                 return VCD_ERROR_INVALID_STATE;
329         }
330
331         int ret = -1;
332         /* Check BT audio */
333         if (0 == strncmp(VCP_AUDIO_ID_BLUETOOTH, audio_type, strlen(VCP_AUDIO_ID_BLUETOOTH))) {
334                 if (false == g_is_valid_bt_in) {
335                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder] BT audio is NOT valid");
336                         return VCD_ERROR_OPERATION_REJECTED;
337                 }
338
339                 if (NULL != g_current_audio_type) {
340                         free(g_current_audio_type);
341                         g_current_audio_type = NULL;
342                 }
343
344                 g_current_audio_type = strdup(audio_type);
345         } else if (0 == strncmp(VCP_AUDIO_ID_MSF, audio_type, strlen(VCP_AUDIO_ID_MSF))) {
346                 if (NULL != g_current_audio_type) {
347                         free(g_current_audio_type);
348                         g_current_audio_type = NULL;
349                 }
350
351                 g_current_audio_type = strdup(audio_type);
352         } else {
353                 if (false == g_is_valid_audio_in) {
354                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Audio-in is NOT valid");
355                         return VCD_ERROR_OPERATION_REJECTED;
356                 }
357
358                 if (g_audio_type != type || g_audio_rate != rate || g_audio_channel != channel) {
359                         audio_in_destroy(g_audio_h);
360
361                         audio_channel_e audio_ch;
362                         audio_sample_type_e audio_type;
363
364                         switch (channel) {
365                         case 1: audio_ch = AUDIO_CHANNEL_MONO;          break;
366                         case 2: audio_ch = AUDIO_CHANNEL_STEREO;        break;
367                         default:
368                                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Input channel is not supported");
369                                 return VCD_ERROR_OPERATION_FAILED;
370                                 break;
371                         }
372
373                         switch (type) {
374                         case VCP_AUDIO_TYPE_PCM_S16_LE: audio_type = AUDIO_SAMPLE_TYPE_S16_LE;  break;
375                         case VCP_AUDIO_TYPE_PCM_U8:     audio_type = AUDIO_SAMPLE_TYPE_U8;      break;
376                         default:
377                                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Invalid Audio Type");
378                                 return VCD_ERROR_OPERATION_FAILED;
379                                 break;
380                         }
381
382                         ret = audio_in_create(rate, audio_ch, audio_type, &g_audio_h);
383                         if (AUDIO_IO_ERROR_NONE != ret) {
384                                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to create audio handle : %d", ret);
385                                 g_is_valid_audio_in = false;
386                                 return VCD_ERROR_OPERATION_FAILED;
387                         }
388
389                         g_audio_type = type;
390                         g_audio_rate = rate;
391                         g_audio_channel = channel;
392                 }
393
394 #ifdef TV_BT_MODE
395                 if (NULL != g_current_audio_type) {
396                         free(g_current_audio_type);
397                         g_current_audio_type = NULL;
398                 }
399
400                 g_current_audio_type = strdup(audio_type);
401 #endif
402         }
403
404         SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Current audio type is changed : %s", g_current_audio_type);
405
406         return 0;
407 }
408
409 int vcd_recorder_get(char** audio_type)
410 {
411         if (NULL == audio_type) {
412                 return VCD_ERROR_INVALID_PARAMETER;
413         }
414
415         if (NULL != g_current_audio_type) {
416                 *audio_type = strdup(g_current_audio_type);
417         } else {
418                 SLOG(LOG_WARN, TAG_VCD, "[Recorder] Current audio type is NOT ready", audio_type);
419                 *audio_type = NULL;
420         }
421
422         return 0;
423 }
424
425 static float get_volume_decibel(char* data, int size)
426 {
427 #define MAX_AMPLITUDE_MEAN_16 32768
428
429         int i, depthByte;
430         int count = 0;
431
432         float db = 0.0;
433         float rms = 0.0;
434         unsigned long long square_sum = 0;
435         short pcm16 = 0;
436
437         depthByte = 2;
438
439         for (i = 0; i < size; i += (depthByte<<1)) {
440                 pcm16 = 0;
441                 memcpy(&pcm16, data + i, sizeof(short));
442                 square_sum += pcm16 * pcm16;
443                 count++;
444         }
445
446         if (0 == count)
447                 rms = 0.0;
448         else
449                 rms = sqrt(square_sum/count);
450
451         db = 20 * log10(rms/MAX_AMPLITUDE_MEAN_16);
452         return db;
453 }
454
455 Eina_Bool __read_normal_func(void *data)
456 {
457         int ret = -1;
458
459         if (VCD_RECORDER_STATE_RECORDING != g_recorder_state) {
460                 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Exit audio reading normal func");
461                 return EINA_FALSE;
462         }
463
464         memset(g_normal_buffer, '\0', BUFFER_LENGTH + 10);
465
466         ret = audio_in_read(g_audio_h, g_normal_buffer, BUFFER_LENGTH);
467         if (0 > ret) {
468                 SLOG(LOG_WARN, TAG_VCD, "[Recorder WARNING] Fail to read audio : %d", ret);
469                 g_recorder_state = VCD_RECORDER_STATE_READY;
470                 return EINA_FALSE;
471         }
472
473         if (NULL != g_audio_cb) {
474                 if (0 != g_audio_cb(g_normal_buffer, BUFFER_LENGTH)) {
475                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to read audio : %d", ret);
476                         vcd_recorder_stop();
477                         return EINA_FALSE;
478                 }
479         }
480
481         /* Set volume */
482         if (0 == g_buffer_count % 30) {
483                 float vol_db = get_volume_decibel(g_normal_buffer, BUFFER_LENGTH);
484                 if (0 != vcdc_send_set_volume(vcd_client_manager_get_pid(), vol_db)) {
485                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder] Fail to send recording volume(%f)", vol_db);
486                 }
487         }
488
489         if (0 == g_buffer_count || 0 == g_buffer_count % 50) {
490                 SLOG(LOG_WARN, TAG_VCD, "[Recorder][%d] Recording... : read_size(%d)", g_buffer_count, ret);
491
492                 if (100000 == g_buffer_count) {
493                         g_buffer_count = 0;
494                 }
495         }
496
497         g_buffer_count++;
498
499 #ifdef BUF_SAVE_MODE
500         /* write pcm buffer */
501         fwrite(g_normal_buffer, 1, BUFFER_LENGTH, g_normal_file);
502 #endif
503
504         return EINA_TRUE;
505 }
506
507 int vcd_recorder_start()
508 {
509         int ret = -1;
510         g_buffer_count = 0;
511
512         SLOG(LOG_ERROR, TAG_VCD, "[Recorder] Enter, recorder state(%d)", g_recorder_state);
513
514         if (VCD_RECORDER_STATE_RECORDING == g_recorder_state)   return 0;
515
516         bool started = false;
517         SLOG(LOG_ERROR, TAG_VCD, "[Recorder] audio type : %s", g_current_audio_type);
518         
519         if (NULL != g_current_audio_type) {
520                 if (0 == strncmp(VCP_AUDIO_ID_BLUETOOTH, g_current_audio_type, strlen(VCP_AUDIO_ID_BLUETOOTH))) {
521 #ifdef TV_BT_MODE
522                         const unsigned char input_data[2] = {SMART_CONTROL_START_CMD, 0x00};
523                         int bt_retry = 0;
524                         while (5 > bt_retry) {
525                                 ret = bt_hid_send_rc_command(NULL, input_data, sizeof(input_data));
526                                 if (BT_ERROR_NONE == ret) {
527                                         SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Start bt audio recorder");
528                                         started = true;
529                                         break;
530                                 } else if (BT_ERROR_NOW_IN_PROGRESS == ret) {
531                                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail bt_hid_send_rc_command(NULL, %s, %d) : %d", input_data, sizeof(input_data), ret);
532                                         usleep(50000);
533                                         bt_retry++;
534                                 } else {
535                                         break;
536                                 }
537                         }
538                         if (false == started) {
539                                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to start bt audio");
540                                 return VCD_ERROR_OPERATION_FAILED;
541                         }
542
543                         g_bt_extend_count = 0;
544 #endif
545                 } else if (0 == strncmp(VCP_AUDIO_ID_MSF, g_current_audio_type, strlen(VCP_AUDIO_ID_MSF))) {
546                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder] call RegisterMSFAudioCallback() function");
547 #ifdef TV_MSF_WIFI_MODE
548
549                         ret = RegisterMSFAudioCallback(__msf_wifi_audio_data_receive_cb, NULL);
550                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder] ret = %d", ret);
551                         if (MSFResult_OK == ret) {
552                                 started = true;
553                         } else {
554                                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to start MSF(wifi) audio");
555                                 return VCD_ERROR_OPERATION_FAILED;
556                         }
557 #endif                  
558                 }
559         }
560
561         SLOG(LOG_ERROR, TAG_VCD, "[Recorder] started = %d", started);
562         if (false == started) {
563                 ret = audio_in_prepare(g_audio_h);
564                 if (AUDIO_IO_ERROR_NONE != ret) {
565                         if (AUDIO_IO_ERROR_SOUND_POLICY == ret) {
566                                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Audio is busy.");
567                                 return VCD_ERROR_RECORDER_BUSY;
568                         } else {
569                                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to start audio : %d", ret);
570                         }
571                         return VCD_ERROR_OPERATION_FAILED;
572                 }
573
574                 /* Add ecore timer to read audio data */
575                 ecore_timer_add(0, __read_normal_func, NULL);
576                 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Start audio in recorder");
577         }
578
579         g_recorder_state = VCD_RECORDER_STATE_RECORDING;
580
581 #ifdef BUF_SAVE_MODE
582         char normal_file_name[128] = {'\0',};
583         g_count++;
584
585         snprintf(normal_file_name, sizeof(normal_file_name), "/tmp/vc_normal_%d_%d", getpid(), g_count);
586         SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] File normal name : %s", normal_file_name);
587
588         /* open test file */
589         g_normal_file = fopen(normal_file_name, "wb+");
590         if (!g_normal_file) {
591                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] File not found!");
592         }
593 #endif
594         return 0;
595 }
596
597 int vcd_recorder_stop()
598 {
599         int ret = -1;
600
601         if (VCD_RECORDER_STATE_READY == g_recorder_state)
602                 return 0;
603
604         g_recorder_state = VCD_RECORDER_STATE_READY;
605
606 #ifdef BUF_SAVE_MODE
607         fclose(g_normal_file);
608 #endif
609
610         bool stoped = false;
611
612         if (NULL != g_current_audio_type) {
613                 if (0 == strncmp(VCP_AUDIO_ID_BLUETOOTH, g_current_audio_type, strlen(VCP_AUDIO_ID_BLUETOOTH))) {
614 #ifdef TV_BT_MODE
615                         int bt_retry = 0;
616                         while (5 > bt_retry) {
617                                 ret = bt_hid_rc_stop_sending_voice(NULL);
618                                 if (BT_ERROR_NONE == ret) {
619                                         SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Stop bt audio recorder");
620                                         stoped = true;
621                                         break;
622                                 } else if (BT_ERROR_NOW_IN_PROGRESS == ret) {
623                                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail bt_hid_rc_stop_sending_voice()");
624                                         usleep(50000);
625                                         bt_retry++;
626                                 } else {
627                                         break;
628                                 }
629                         }
630                         if (false == stoped) {
631                                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to stop bt audio");
632                                 return VCD_ERROR_OPERATION_FAILED;
633                         }
634 #endif
635                 } else if (0 == strncmp(VCP_AUDIO_ID_MSF, g_current_audio_type, strlen(VCP_AUDIO_ID_MSF))) {
636 #ifdef TV_MSF_WIFI_MODE
637                         UnRegisterMSFAudioCallback();
638                         stoped = true;
639 #endif
640                 }
641         }
642
643         if (false == stoped) {
644                 ret = audio_in_unprepare(g_audio_h);
645                 if (AUDIO_IO_ERROR_NONE != ret) {
646                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to stop audio : %d", ret);
647                         return VCD_ERROR_OPERATION_FAILED;
648                 }
649         }
650         return 0;
651 }
652
653 int vcd_recorder_get_state()
654 {
655         return g_recorder_state;
656 }