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