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