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