Change to VOICE_RECOGNITION_SERVICE as sound stream
[platform/core/uifw/stt.git] / server / sttd_recorder.c
1 /*
2 *  Copyright (c) 2011-2016 Samsung Electronics Co., Ltd All Rights Reserved
3 *  Licensed under the Apache License, Version 2.0 (the "License");
4 *  you may not use this file except in compliance with the License.
5 *  You may obtain a copy of the License at
6 *  http://www.apache.org/licenses/LICENSE-2.0
7 *  Unless required by applicable law or agreed to in writing, software
8 *  distributed under the License is distributed on an "AS IS" BASIS,
9 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 *  See the License for the specific language governing permissions and
11 *  limitations under the License.
12 */
13
14 #ifdef TV_PRODUCT
15 #define TV_BT_MODE
16 #endif
17
18 #include <audio_io.h>
19 #include <Ecore.h>
20 #include <math.h>
21 #include <pthread.h>
22 #include <sound_manager.h>
23 #include <sound_manager_internal.h>
24 #ifdef TV_BT_MODE
25 #include <bluetooth_product.h>
26 #endif
27
28 #include "stt_defs.h"
29 #include "sttd_dbus.h"
30 #include "sttd_recorder.h"
31 #include "sttd_main.h"
32
33
34 #define FRAME_LENGTH 160
35 #define BUFFER_LENGTH FRAME_LENGTH * 2
36
37 static pthread_mutex_t sttd_audio_in_handle_mutex = PTHREAD_MUTEX_INITIALIZER;
38
39 typedef enum {
40         STTD_RECORDER_STATE_NONE = -1,
41         STTD_RECORDER_STATE_READY = 0,  /**< Recorder is ready to start */
42         STTD_RECORDER_STATE_RECORDING   /**< In the middle of recording */
43 } sttd_recorder_state;
44
45 typedef struct {
46         int                     uid;
47         audio_in_h              audio_h;
48         stte_audio_type_e       audio_type;
49 } stt_recorder_s;
50
51 static stt_recorder_s* g_recorder = NULL;
52
53 static stt_recorder_audio_cb    g_audio_cb;
54
55 static sound_stream_info_h      g_stream_info_h;
56
57 static stt_recorder_interrupt_cb        g_interrupt_cb;
58
59 static sttd_recorder_state      g_recorder_state = STTD_RECORDER_STATE_NONE;
60
61 static int g_buffer_count;
62
63 static int g_stream_focus_id;
64
65 /* Sound buf save for test */
66 /*
67 #define BUF_SAVE_MODE
68 */
69 #ifdef BUF_SAVE_MODE
70 static char g_temp_file_name[128] = {'\0',};
71
72 static FILE* g_pFile;
73
74 static int g_count = 1;
75 #endif
76
77 #ifdef TV_BT_MODE
78 static float get_volume_decibel(char* data, int size, stte_audio_type_e type);
79
80 //static stt_recorder_s* __get_recorder(int engine_id);
81
82 static int g_bt_extend_count;
83
84 #define SMART_CONTROL_EXTEND_CMD        0x03
85 #define SMART_CONTROL_START_CMD         0x04
86
87 static void _bt_cb_hid_state_changed(int result, bool connected, const char *remote_address, void *user_data)
88 {
89         SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Bluetooth Event [%d] Received address [%s]", result, remote_address);
90 }
91
92 static void _bt_hid_audio_data_receive_cb(bt_hid_voice_data_s *voice_data, void *user_data)
93 {
94         if (STTD_RECORDER_STATE_RECORDING != g_recorder_state) {
95                 /* SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Exit audio reading normal func"); */
96                 return;
97         }
98
99         if (NULL != g_audio_cb) {
100                 if (0 != g_audio_cb((void*)voice_data->audio_buf, (unsigned int)voice_data->length)) {
101                         SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to read audio");
102                         sttd_recorder_stop();
103                 }
104
105                 if (NULL == g_recorder) {
106                         return;
107                 }
108
109                 float vol_db = get_volume_decibel((char*)voice_data->audio_buf, (int)voice_data->length, g_recorder->audio_type);
110                 if (0 != sttdc_send_set_volume(g_recorder->uid, vol_db)) {
111                         SLOG(LOG_ERROR, TAG_STTD, "[Recorder] Fail to send recording volume(%f)", vol_db);
112                 }
113         }
114
115         if (0 == g_buffer_count || 0 == g_buffer_count % 50) {
116                 SLOG(LOG_WARN, TAG_STTD, "[Recorder][%d] Recording... : read_size(%d)", g_buffer_count, voice_data->length);
117
118                 if (0 == g_bt_extend_count % 5 && 0 != g_buffer_count) {
119                         const unsigned char input_data[2] = {SMART_CONTROL_EXTEND_CMD, 0x10 };
120                         if (BT_ERROR_NONE != bt_hid_send_rc_command(NULL, input_data, sizeof(input_data))) {
121                                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail bt_hid_send_rc_command(NULL, %s, %d)", input_data, sizeof(input_data));
122                         } else {
123                                 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Extend bt audio recorder");
124                         }
125                 }
126                 g_bt_extend_count++;
127
128                 if (100000 == g_buffer_count) {
129                         g_buffer_count = 0;
130                 }
131         }
132
133         g_buffer_count++;
134
135 #ifdef BUF_SAVE_MODE
136         /* write pcm buffer */
137         fwrite(data, 1, len, g_pFile);
138 #endif
139         return;
140 }
141 #endif
142
143
144 const char* __stt_get_focus_changed_reason_code(sound_stream_focus_change_reason_e reason)
145 {
146         switch (reason) {
147         case SOUND_STREAM_FOCUS_CHANGED_BY_MEDIA:                       return "SOUND_STREAM_FOCUS_CHANGED_BY_MEDIA";
148         case SOUND_STREAM_FOCUS_CHANGED_BY_SYSTEM:                      return "SOUND_STREAM_FOCUS_CHANGED_BY_SYSTEM";
149         case SOUND_STREAM_FOCUS_CHANGED_BY_ALARM:                       return "SOUND_STREAM_FOCUS_CHANGED_BY_ALARM";
150         case SOUND_STREAM_FOCUS_CHANGED_BY_NOTIFICATION:        return "SOUND_STREAM_FOCUS_CHANGED_BY_NOTIFICATION";
151         case SOUND_STREAM_FOCUS_CHANGED_BY_EMERGENCY:           return "SOUND_STREAM_FOCUS_CHANGED_BY_EMERGENCY";
152         case SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_INFORMATION:   return "SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_INFORMATION";
153         case SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_RECOGNITION:   return "SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_RECOGNITION";
154         case SOUND_STREAM_FOCUS_CHANGED_BY_RINGTONE:            return "SOUND_STREAM_FOCUS_CHANGED_BY_RINGTONE";
155         case SOUND_STREAM_FOCUS_CHANGED_BY_VOIP:                        return "SOUND_STREAM_FOCUS_CHANGED_BY_VOIP";
156         case SOUND_STREAM_FOCUS_CHANGED_BY_CALL:                        return "SOUND_STREAM_FOCUS_CHANGED_BY_CALL";
157         case SOUND_STREAM_FOCUS_CHANGED_BY_MEDIA_EXTERNAL_ONLY: return "SOUND_STREAM_FOCUS_CHANGED_BY_MEDIA_EXTERNAL_ONLY";
158         default:                                                                                        return "Undefined reason code";
159         }
160 }
161
162 void __recorder_focus_state_watch_cb(int id, sound_stream_focus_mask_e focus_mask, sound_stream_focus_state_e focus_state, 
163                                                 sound_stream_focus_change_reason_e reason, const char *extra_info, void *user_data)
164 {
165         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));
166
167         if (STTD_RECORDER_STATE_RECORDING == g_recorder_state && SOUND_STREAM_FOCUS_STATE_ACQUIRED == focus_state) {
168                 SLOG(LOG_WARN, TAG_STTD, "[Recorder] Focus released as interrupt");
169                 if (NULL != g_interrupt_cb) {
170                         g_interrupt_cb();
171                 }
172         }
173 }
174
175 int sttd_recorder_initialize(stt_recorder_audio_cb audio_cb, stt_recorder_interrupt_cb interrupt_cb)
176 {
177         if (NULL == audio_cb || NULL == interrupt_cb) {
178                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Input param is NOT valid");
179                 return STTD_ERROR_INVALID_PARAMETER;
180         }
181
182         if (STTD_RECORDER_STATE_NONE != g_recorder_state) {
183                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Current state of recorder is recording");
184                 return STTD_ERROR_INVALID_STATE;
185         }
186
187         if (0 != pthread_mutex_init(&sttd_audio_in_handle_mutex, NULL)) {
188                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to initialize audio in handle mutex.");
189         }
190
191         g_audio_cb = audio_cb;
192         g_interrupt_cb = interrupt_cb;
193         g_recorder_state = STTD_RECORDER_STATE_NONE;
194         g_recorder = NULL;
195
196         if (0 != sound_manager_create_stream_information_internal(SOUND_STREAM_TYPE_VOICE_RECOGNITION_SERVICE, NULL, NULL, &g_stream_info_h)) {
197                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to create stream info");
198         }
199
200         if (0 != sound_manager_add_focus_state_watch_cb(SOUND_STREAM_FOCUS_FOR_BOTH, __recorder_focus_state_watch_cb, NULL, &g_stream_focus_id)) {
201                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to add focus state watch callback");
202         }
203
204 #ifdef TV_BT_MODE
205         if (BT_ERROR_NONE != bt_product_init()) {
206                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to init bt");
207                 return STTD_ERROR_OPERATION_FAILED;
208         }
209
210         if (BT_ERROR_NONE != bt_hid_host_initialize(_bt_cb_hid_state_changed, NULL)) {
211                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail bt_hid_host_initialize()");
212                 return STTD_ERROR_OPERATION_FAILED;
213         }
214 #endif
215
216         return 0;
217 }
218
219 int sttd_recorder_deinitialize()
220 {
221         if (0 != pthread_mutex_destroy(&sttd_audio_in_handle_mutex)) {
222                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy audio in handle mutex.");
223         }
224
225         if (0 != sound_manager_remove_focus_state_watch_cb(g_stream_focus_id)) {
226                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to remove focus state watch callback");
227         }
228
229         if (0 != sound_manager_destroy_stream_information(g_stream_info_h)) {
230                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to destroy stream info");
231         }
232
233         if (NULL != g_recorder) {
234                 audio_in_destroy(g_recorder->audio_h);
235                 g_recorder->audio_h = NULL;
236                 free(g_recorder);
237                 g_recorder = NULL;
238         }
239
240 #ifdef __UNUSED_CODES__
241         /* Remove all recorder */
242         GSList *iter = NULL;
243         stt_recorder_s *recorder = NULL;
244
245         iter = g_slist_nth(g_recorder_list, 0);
246
247         while (NULL != iter) {
248                 recorder = iter->data;
249
250                 if (NULL != recorder) {
251                         g_recorder_list = g_slist_remove(g_recorder_list, recorder);
252                         if (recorder->audio_h) {
253                                 audio_in_destroy(recorder->audio_h);
254                                 recorder->audio_h = NULL;
255                         }
256                         free(recorder);
257                 }
258
259                 iter = g_slist_nth(g_recorder_list, 0);
260         }
261 #endif
262
263 #ifdef TV_BT_MODE
264         bt_hid_host_deinitialize();
265
266         bt_product_deinit();
267 #endif
268
269         g_recorder_state = STTD_RECORDER_STATE_NONE;
270
271         return 0;
272 }
273
274 int sttd_recorder_set_audio_session()
275 {
276         return 0;
277 }
278
279 int sttd_recorder_unset_audio_session()
280 {
281         return 0;
282 }
283
284 int sttd_recorder_create(stte_audio_type_e type, int channel, unsigned int sample_rate)
285 {
286         /* Check engine id is valid */
287         if (NULL != g_recorder) {
288                 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is already registered");
289                 return STTD_ERROR_INVALID_PARAMETER;
290         }
291
292         audio_in_h temp_in_h = NULL;
293
294 #ifndef TV_BT_MODE
295         audio_channel_e audio_ch;
296         audio_sample_type_e audio_type;
297
298         switch (channel) {
299         case 1: audio_ch = AUDIO_CHANNEL_MONO;          break;
300         case 2: audio_ch = AUDIO_CHANNEL_STEREO;        break;
301         default:
302                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Input channel is not supported");
303                 return STTD_ERROR_OPERATION_FAILED;
304                 break;
305         }
306
307         switch (type) {
308         case STTE_AUDIO_TYPE_PCM_S16_LE:        audio_type = AUDIO_SAMPLE_TYPE_S16_LE;  break;
309         case STTE_AUDIO_TYPE_PCM_U8:            audio_type = AUDIO_SAMPLE_TYPE_U8;      break;
310         default:
311                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Invalid Audio Type");
312                 return STTD_ERROR_OPERATION_FAILED;
313                 break;
314         }
315
316         int ret;
317         ret = audio_in_create(sample_rate, audio_ch, audio_type, &temp_in_h);
318         if (AUDIO_IO_ERROR_NONE != ret) {
319                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to create audio handle : %d", ret);
320                 return STTD_ERROR_OPERATION_FAILED;
321         }
322 #else
323         if (BT_ERROR_NONE != bt_hid_set_audio_data_receive_cb(_bt_hid_audio_data_receive_cb, NULL)) {
324                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail bt_hid_set_audio_data_receive_cb()");
325                 return STTD_ERROR_OPERATION_FAILED;
326         }
327 #endif
328
329         stt_recorder_s* recorder;
330         recorder = (stt_recorder_s*)calloc(1, sizeof(stt_recorder_s));
331         if (NULL == recorder) {
332 #ifndef TV_BT_MODE
333                 if (temp_in_h) {
334                         audio_in_destroy(temp_in_h);
335                         temp_in_h = NULL;
336                 }
337 #endif
338                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to allocate memory");
339                 return STTD_ERROR_OUT_OF_MEMORY;
340         }
341
342         recorder->uid = -1;
343         recorder->audio_h = temp_in_h;
344         recorder->audio_type = type;
345
346         g_recorder = recorder;
347
348         g_recorder_state = STTD_RECORDER_STATE_READY;
349
350         return 0;
351 }
352
353 int sttd_recorder_destroy()
354 {
355         // critical section required because this function can be called from stt engine thread context
356         SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Enter critical section");
357         pthread_mutex_lock(&sttd_audio_in_handle_mutex);
358
359         /* Check engine id is valid */
360         if (NULL == g_recorder) {
361                 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
362                 pthread_mutex_unlock(&sttd_audio_in_handle_mutex);
363                 return STTD_ERROR_INVALID_PARAMETER;
364         }
365
366 #ifndef TV_BT_MODE
367         int ret;
368         if (STTD_RECORDER_STATE_RECORDING == g_recorder_state) {
369                 if (g_recorder->audio_h) {
370                         ret = audio_in_unprepare(g_recorder->audio_h);
371                         if (AUDIO_IO_ERROR_NONE != ret) {
372                                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to unprepare audioin : %d", ret);
373                         }
374                 }
375
376                 g_recorder_state = STTD_RECORDER_STATE_READY;
377         }
378
379         if (g_recorder->audio_h) {
380                 ret = audio_in_destroy(g_recorder->audio_h);
381                 if (AUDIO_IO_ERROR_NONE != ret) {
382                         SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to destroy audioin : %d", ret);
383                 }
384                 g_recorder->audio_h = NULL;
385         }
386
387 #else
388         if (STTD_RECORDER_STATE_RECORDING == g_recorder_state) {
389                 g_recorder_state = STTD_RECORDER_STATE_READY;
390         }
391
392         bt_hid_unset_audio_data_receive_cb();
393 #endif
394
395         free(g_recorder);
396         g_recorder = NULL;
397
398         pthread_mutex_unlock(&sttd_audio_in_handle_mutex);
399         SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Leave critical section");
400
401         return 0;
402 }
403
404 static float get_volume_decibel(char* data, int size, stte_audio_type_e type)
405 {
406         #define MAX_AMPLITUDE_MEAN_16   32768
407         #define MAX_AMPLITUDE_MEAN_08   128
408
409         int i, depthByte;
410         int count = 0;
411
412         float db = 0.0;
413         float rms = 0.0;
414         unsigned long long square_sum = 0;
415
416         if (type == STTE_AUDIO_TYPE_PCM_S16_LE)
417                 depthByte = 2;
418         else
419                 depthByte = 1;
420
421         for (i = 0; i < size; i += (depthByte<<1)) {
422                 if (depthByte == 2) {
423                         short pcm16 = 0;
424                         memcpy(&pcm16, data + i, sizeof(short));
425                         square_sum += pcm16 * pcm16;
426                 } else {
427                         char pcm8 = 0;
428                         memcpy(&pcm8, data + i, sizeof(char));
429                         square_sum += pcm8 * pcm8;
430                 }
431                 count++;
432         }
433
434         if (0 == count)
435                 rms = 0.0;
436         else
437                 rms = sqrt((double)square_sum/count);
438
439         if (depthByte == 2)
440                 db = 20 * log10(rms/MAX_AMPLITUDE_MEAN_16);
441         else
442                 db = 20 * log10(rms/MAX_AMPLITUDE_MEAN_08);
443
444         return db;
445 }
446
447 #ifndef TV_BT_MODE
448 Eina_Bool __read_audio_func(void *data)
449 {
450         int read_byte = -1;
451         static char g_buffer[BUFFER_LENGTH];
452
453         /* Check engine id is valid */
454         if (NULL == g_recorder) {
455                 return EINA_FALSE;
456         }
457
458         if (STTD_RECORDER_STATE_READY == g_recorder_state) {
459                 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Exit audio reading func");
460                 return EINA_FALSE;
461         }
462
463         read_byte = audio_in_read(g_recorder->audio_h, g_buffer, BUFFER_LENGTH);
464         if (0 > read_byte) {
465                 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Fail to read audio : %d", read_byte);
466                 g_recorder_state = STTD_RECORDER_STATE_READY;
467                 return EINA_FALSE;
468         }
469
470         if (0 != g_audio_cb(g_buffer, read_byte)) {
471                 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Fail audio callback");
472                 sttd_recorder_stop();
473                 return EINA_FALSE;
474         }
475
476         if (0 == g_buffer_count % 30) {
477                 float vol_db = get_volume_decibel(g_buffer, BUFFER_LENGTH, g_recorder->audio_type);
478                 if (0 != sttdc_send_set_volume(g_recorder->uid, vol_db)) {
479                         SLOG(LOG_ERROR, TAG_STTD, "[Recorder] Fail to send recording volume(%f)", vol_db);
480                 }
481         }
482
483         /* Audio read log */
484         if (0 == g_buffer_count % 50) {
485                 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder][%d] Recording... : read_size(%d)", g_buffer_count, read_byte);
486
487                 if (100000 == g_buffer_count) {
488                         g_buffer_count = 0;
489                 }
490         }
491
492         g_buffer_count++;
493
494 #ifdef BUF_SAVE_MODE
495         /* write pcm buffer */
496         fwrite(g_buffer, 1, BUFFER_LENGTH, g_pFile);
497 #endif
498
499         return EINA_TRUE;
500 }
501 #endif
502
503 int sttd_recorder_start(int uid)
504 {
505         if (STTD_RECORDER_STATE_RECORDING == g_recorder_state)
506                 return 0;
507
508         int ret = -1;
509 #ifndef TV_BT_MODE
510         /* Check engine id is valid */
511         if (NULL == g_recorder) {
512                 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
513                 return STTD_ERROR_INVALID_PARAMETER;
514         }
515
516         ret = audio_in_set_sound_stream_info(g_recorder->audio_h, g_stream_info_h);
517         if (AUDIO_IO_ERROR_NONE != ret) {
518                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to set stream info");
519         }
520
521         ret = audio_in_prepare(g_recorder->audio_h);
522         if (AUDIO_IO_ERROR_NONE != ret) {
523                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to start audio : %d", ret);
524                 return STTD_ERROR_RECORDER_BUSY;
525         }
526
527         /* Add ecore timer to read audio data */
528         ecore_timer_add(0, __read_audio_func, NULL);
529
530 #else
531         g_bt_extend_count = 0;
532         const unsigned char input_data[2] = {SMART_CONTROL_START_CMD, 0x00 };
533         int bt_retry = 0;
534         bool started = false;
535         while (5 > bt_retry) {
536                 ret = bt_hid_send_rc_command(NULL, input_data, sizeof(input_data));
537                 if (BT_ERROR_NONE == ret) {
538                         SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Start bt audio recorder");
539                         started = true;
540                         break;
541                 } else if (BT_ERROR_NOW_IN_PROGRESS == ret) {
542                         SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail bt_hid_send_rc_command(NULL, %s, %d)", input_data, sizeof(input_data));
543                         usleep(50000);
544                         bt_retry++;
545                 } else {
546                         break;
547                 }
548         }
549         if (false == started) {
550                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to start bt audio");
551                 return STTD_ERROR_OPERATION_FAILED;
552         }
553 #endif
554         g_recorder_state = STTD_RECORDER_STATE_RECORDING;
555         g_recorder->uid = uid;
556
557         g_buffer_count = 0;
558
559 #ifdef BUF_SAVE_MODE
560         g_count++;
561
562         while (1) {
563                 snprintf(g_temp_file_name, sizeof(g_temp_file_name), "/tmp/stt_temp_%d_%d", getpid(), g_count);
564                 ret = access(g_temp_file_name, 0);
565
566                 if (0 == ret) {
567                         SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] File is already exist");
568                         if (0 == remove(g_temp_file_name)) {
569                                 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Remove file");
570                                 break;
571                         } else {
572                                 g_count++;
573                         }
574                 } else {
575                         break;
576                 }
577         }
578
579         SECURE_SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Temp file name=[%s]", g_temp_file_name);
580
581         /* open test file */
582         g_pFile = fopen(g_temp_file_name, "wb+x");
583         if (!g_pFile) {
584                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] File not found!");
585                 return -1;
586         }
587 #endif
588
589         return 0;
590 }
591
592 int sttd_recorder_stop()
593 {
594         if (STTD_RECORDER_STATE_READY == g_recorder_state)
595                 return 0;
596
597         // critical section required because this function can be called from stt engine thread context
598         SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Enter critical section");
599         pthread_mutex_lock(&sttd_audio_in_handle_mutex);
600
601         /* Check engine id is valid */
602         if (NULL == g_recorder) {
603                 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
604                 pthread_mutex_unlock(&sttd_audio_in_handle_mutex);
605                 return STTD_ERROR_INVALID_PARAMETER;
606         }
607
608         g_recorder->uid = -1;
609
610         int ret = -1;
611 #ifndef TV_BT_MODE
612         ret = audio_in_unprepare(g_recorder->audio_h);
613         if (AUDIO_IO_ERROR_NONE != ret) {
614                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to unprepare audioin : %d", ret);
615         }
616 #else
617         int bt_retry = 0;
618         bool stopped = false;
619         while (5 > bt_retry) {
620                 ret = bt_hid_rc_stop_sending_voice(NULL);
621                 if (BT_ERROR_NONE == ret) {
622                         SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Stop bt audio");
623                         stopped = true;
624                         break;
625                 } else if (BT_ERROR_NOW_IN_PROGRESS == ret) {
626                         SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail bt_hid_rc_stop_sending_voice()");
627                         usleep(50000);
628                         bt_retry++;
629                 } else {
630                         SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] bt_hid_rc_stop_sending_voice(): ret(%d)", ret);
631                         break;
632                 }
633         }
634         if (false == stopped) {
635                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to stop bt audio(%d)", ret);
636 //              return STTD_ERROR_OPERATION_FAILED;
637         }
638 #endif
639
640         g_recorder_state = STTD_RECORDER_STATE_READY;
641
642 #ifdef BUF_SAVE_MODE
643         fclose(g_pFile);
644 #endif
645         pthread_mutex_unlock(&sttd_audio_in_handle_mutex);
646         SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Leave critical section");
647
648         return 0;
649 }
650
651 int sttd_recorder_start_file(int uid, const char *filepath)
652 {
653         if (STTD_RECORDER_STATE_RECORDING == g_recorder_state)
654                 return 0;
655
656         /* Check engine id is valid */
657         if (NULL == g_recorder) {
658                 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
659                 return STTD_ERROR_INVALID_PARAMETER;
660         }
661         g_recorder_state = STTD_RECORDER_STATE_RECORDING;
662         g_recorder->uid = uid;
663
664         int cnt = 0;
665         int totalReadBytes = 0;
666
667         FILE *infile = fopen(filepath, "rb");
668
669         //process the file
670         if (infile != NULL) {
671                 while (!feof(infile)) {
672                         static char pcm_buff[BUFFER_LENGTH];
673                         int read_byte = fread(pcm_buff, 1, BUFFER_LENGTH, infile);
674                         totalReadBytes += read_byte;
675                         if (0 != read_byte) {
676                                 if (0 != g_audio_cb(pcm_buff, read_byte)) {
677                                         SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to call audio callback");
678                                         fclose(infile);
679                                         return -1;
680                                 }
681                                 if (0 == cnt % 30) {
682                                         float vol_db = get_volume_decibel(pcm_buff, BUFFER_LENGTH, g_recorder->audio_type);
683                                         if (0 != sttdc_send_set_volume(g_recorder->uid, vol_db)) {
684                                                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder] Fail to send recording volume(%f)", vol_db);
685                                         }
686                                 }
687
688                                 /* Audio read log */
689                                 if (0 == cnt % 50)
690                                         SLOG(LOG_DEBUG, TAG_STTD, "[Recorder][%d] Recording... : read_size(%d)", cnt, read_byte);
691                                 cnt++;
692                         }
693                 }
694                 fclose(infile);
695         }
696
697         SLOG(LOG_DEBUG, TAG_STTD, "[Recorder][%d] total bytes(%d)", cnt, totalReadBytes);
698         return 0;
699 }
700
701 int sttd_recorder_stop_file()
702 {
703         if (STTD_RECORDER_STATE_READY == g_recorder_state)
704                 return 0;
705
706         /* Check engine id is valid */
707         if (NULL == g_recorder) {
708                 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
709                 return STTD_ERROR_INVALID_PARAMETER;
710         }
711
712         g_recorder->uid = -1;
713         g_recorder_state = STTD_RECORDER_STATE_READY;
714
715         return 0;
716 }
717
718 int sttd_recorder_clear()
719 {
720 #ifdef TV_BT_MODE
721         SLOG(LOG_DEBUG, TAG_STTD, "[Recorder DEBUG] Clear recorder on TV");
722
723         if (STTD_RECORDER_STATE_RECORDING == g_recorder_state) {
724                 sttd_recorder_stop();
725         }
726         if (STTD_RECORDER_STATE_READY == g_recorder_state) {
727                 sttd_recorder_destroy();
728         }
729 #endif
730
731         return STTD_ERROR_NONE;
732 }