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