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