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