Fix null pointer access issue when check audio_id
[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 #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
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
420 #ifndef TV_BT_MODE
421         audio_channel_e audio_ch;
422         audio_sample_type_e audio_type;
423
424         switch (channel) {
425         case 1: audio_ch = AUDIO_CHANNEL_MONO;          break;
426         case 2: audio_ch = AUDIO_CHANNEL_STEREO;        break;
427         default:
428                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Input channel is not supported");
429                 return STTD_ERROR_OPERATION_FAILED;
430                 break;
431         }
432
433         switch (type) {
434         case STTE_AUDIO_TYPE_PCM_S16_LE:        audio_type = AUDIO_SAMPLE_TYPE_S16_LE;  break;
435         case STTE_AUDIO_TYPE_PCM_U8:            audio_type = AUDIO_SAMPLE_TYPE_U8;      break;
436         default:
437                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Invalid Audio Type");
438                 return STTD_ERROR_OPERATION_FAILED;
439                 break;
440         }
441
442         int ret;
443         ret = audio_in_create(sample_rate, audio_ch, audio_type, &temp_in_h);
444         if (AUDIO_IO_ERROR_NONE != ret) {
445                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to create audio handle : %d", ret);
446                 return STTD_ERROR_OPERATION_FAILED;
447         }
448 #endif
449
450         stt_recorder_s* recorder;
451         recorder = (stt_recorder_s*)calloc(1, sizeof(stt_recorder_s));
452         if (NULL == recorder) {
453 #ifndef TV_BT_MODE
454                 if (temp_in_h) {
455                         audio_in_destroy(temp_in_h);
456                         temp_in_h = NULL;
457                 }
458 #endif
459                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to allocate memory");
460                 return STTD_ERROR_OUT_OF_MEMORY;
461         }
462
463         recorder->uid = STT_INVALID_UID;
464         recorder->audio_h = temp_in_h;
465         recorder->audio_type = type;
466
467         g_recorder = recorder;
468
469         g_recorder_state = STTD_RECORDER_STATE_READY;
470
471         return 0;
472 }
473
474 int sttd_recorder_destroy()
475 {
476         // critical section required because this function can be called from stt engine thread context
477         SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Enter critical section");
478         pthread_mutex_lock(&sttd_audio_in_handle_mutex);
479
480         /* Check engine id is valid */
481         if (NULL == g_recorder) {
482                 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
483                 pthread_mutex_unlock(&sttd_audio_in_handle_mutex);
484                 return STTD_ERROR_INVALID_PARAMETER;
485         }
486
487 #ifndef TV_BT_MODE
488         int ret;
489         if (STTD_RECORDER_STATE_RECORDING == g_recorder_state) {
490                 if (g_recorder->audio_h) {
491                         ret = audio_in_unprepare(g_recorder->audio_h);
492                         if (AUDIO_IO_ERROR_NONE != ret) {
493                                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to unprepare audioin : %d", ret);
494                         }
495                 }
496
497                 g_recorder_state = STTD_RECORDER_STATE_READY;
498         }
499
500         if (g_recorder->audio_h) {
501                 ret = audio_in_destroy(g_recorder->audio_h);
502                 if (AUDIO_IO_ERROR_NONE != ret) {
503                         SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to destroy audioin : %d", ret);
504                 }
505                 g_recorder->audio_h = NULL;
506         }
507
508 #else
509         if (STTD_RECORDER_STATE_RECORDING == g_recorder_state) {
510                 g_recorder_state = STTD_RECORDER_STATE_READY;
511         }
512
513         bt_hid_unset_audio_data_receive_cb();
514 #endif
515
516         free(g_recorder);
517         g_recorder = NULL;
518
519         pthread_mutex_unlock(&sttd_audio_in_handle_mutex);
520         SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Leave critical section");
521
522         return 0;
523 }
524
525 float get_volume_decibel(char* data, int size, stte_audio_type_e type)
526 {
527         #define MAX_AMPLITUDE_MEAN_16   32768
528         #define MAX_AMPLITUDE_MEAN_08   128
529
530         int i, depthByte;
531         int count = 0;
532
533         float db = 0.0;
534         float rms = 0.0;
535         unsigned long long square_sum = 0;
536
537         if (type == STTE_AUDIO_TYPE_PCM_S16_LE)
538                 depthByte = 2;
539         else
540                 depthByte = 1;
541
542         for (i = 0; i < size; i += (depthByte<<1)) {
543                 if (depthByte == 2) {
544                         short pcm16 = 0;
545                         memcpy(&pcm16, data + i, sizeof(short));
546                         square_sum += pcm16 * pcm16;
547                 } else {
548                         char pcm8 = 0;
549                         memcpy(&pcm8, data + i, sizeof(char));
550                         square_sum += pcm8 * pcm8;
551                 }
552                 count++;
553         }
554
555         if (0 == count)
556                 rms = 0.0;
557         else
558                 rms = sqrt((double)square_sum/count);
559
560         if (depthByte == 2)
561                 db = 20 * log10(rms/MAX_AMPLITUDE_MEAN_16);
562         else
563                 db = 20 * log10(rms/MAX_AMPLITUDE_MEAN_08);
564
565         return db;
566 }
567
568 #ifndef TV_BT_MODE
569 Eina_Bool __read_audio_func(void *data)
570 {
571         int read_byte = -1;
572         static char g_buffer[BUFFER_LENGTH];
573
574         /* Check engine id is valid */
575         if (NULL == g_recorder) {
576                 return EINA_FALSE;
577         }
578
579         if (STTD_RECORDER_STATE_READY == g_recorder_state) {
580                 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Exit audio reading func");
581                 return EINA_FALSE;
582         }
583
584         read_byte = audio_in_read(g_recorder->audio_h, g_buffer, BUFFER_LENGTH);
585         if (0 > read_byte) {
586                 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Fail to read audio : %d", read_byte);
587                 g_recorder_state = STTD_RECORDER_STATE_READY;
588                 return EINA_FALSE;
589         }
590
591         if (0 != g_audio_cb(g_buffer, read_byte)) {
592                 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Fail audio callback");
593                 sttd_recorder_stop();
594                 return EINA_FALSE;
595         }
596
597         if (NULL == g_recorder)
598                 SLOG(LOG_WARN, TAG_STTD, "[Recorder] g_recorder is NULL. It is already stopped and destroyed.");
599
600         if (0 == g_buffer_count % 30 && NULL != g_recorder) {
601                 float vol_db = get_volume_decibel(g_buffer, BUFFER_LENGTH, g_recorder->audio_type);
602                 if (0 != sttdc_send_set_volume(g_recorder->uid, vol_db)) {
603                         SLOG(LOG_ERROR, TAG_STTD, "[Recorder] Fail to send recording volume(%f)", vol_db);
604                 }
605         }
606
607         /* Audio read log */
608         if (0 == g_buffer_count % 50) {
609                 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder][%d] Recording... : read_size(%d)", g_buffer_count, read_byte);
610
611                 if (100000 == g_buffer_count) {
612                         g_buffer_count = 0;
613                 }
614         }
615
616         g_buffer_count++;
617
618 #ifdef BUF_SAVE_MODE
619         /* write pcm buffer */
620         fwrite(g_buffer, 1, BUFFER_LENGTH, g_pFile);
621 #endif
622
623         return EINA_TRUE;
624 }
625 #endif
626
627 #ifdef TV_BT_MODE
628 bool __is_tct_app(const char* appid)
629 {
630         int cnt = 0;
631         if (NULL == appid) {
632                 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARN] appid is NULL");
633                 return false;
634         }
635
636         for (cnt = 0 ; cnt < 5 ; cnt++) {
637                 if (0 == strncmp(appid, g_tct_apps[cnt], strlen(appid))) {
638                         SLOG(LOG_INFO, TAG_STTD, "[Recoder INFO] Current app(%s) is TCT app.", appid);
639                         return true;
640                 }
641         }
642
643         return false;
644 }
645 #endif
646
647 int sttd_recorder_start(unsigned int uid, const char* appid)
648 {
649         if (STTD_RECORDER_STATE_RECORDING == g_recorder_state)
650                 return 0;
651
652         char* client_audio_id = NULL;
653         int ret = sttd_client_get_audio_id(uid, &client_audio_id);
654         if (STTD_ERROR_NONE != ret || NULL == client_audio_id) {
655                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to get audio id: %d", ret);
656                 return ret;
657         }
658
659         ret = sttd_engine_agent_set_audio_type(client_audio_id);
660         if (STTD_ERROR_NONE != ret) {
661                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to get audio id: %d", ret);
662                 free(client_audio_id);
663                 return ret;
664         }
665
666 #ifdef TV_FFV_MODE
667                 if (0 == strncmp(client_audio_id, STTE_AUDIO_ID_FFV, strlen(client_audio_id))) {
668                         if (NULL != g_farfieldvoice_h) {
669                                 SLOG(LOG_INFO, TAG_STTD, "[Recorder INFO] Register farfield voice audio callback");
670                                 farfield_voice_register_audio_cb(g_farfieldvoice_h, _ffv_audio_function_cb, NULL);
671                         }
672                 }
673 #endif
674 #ifdef TV_BT_MODE
675                 if (0 == strncmp(client_audio_id, STTE_AUDIO_ID_BLUETOOTH, strlen(client_audio_id))) {
676                         if (BT_ERROR_NONE != bt_hid_set_audio_data_receive_cb(_bt_hid_audio_data_receive_cb, NULL)) {
677                                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail bt_hid_set_audio_data_receive_cb()");
678                                 return STTD_ERROR_OPERATION_FAILED;
679                         }
680                 }
681 #endif
682
683         free(client_audio_id);
684         client_audio_id = NULL;
685
686 #ifndef TV_BT_MODE
687         ret = -1;
688         /* Check engine id is valid */
689         if (NULL == g_recorder) {
690                 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
691                 return STTD_ERROR_INVALID_PARAMETER;
692         }
693
694         ret = audio_in_set_sound_stream_info(g_recorder->audio_h, g_stream_info_h);
695         if (AUDIO_IO_ERROR_NONE != ret) {
696                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to set stream info");
697         }
698
699         ret = audio_in_prepare(g_recorder->audio_h);
700         if (AUDIO_IO_ERROR_NONE != ret) {
701                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to start audio : %d", ret);
702                 return STTD_ERROR_RECORDER_BUSY;
703         }
704
705         /* Add ecore timer to read audio data */
706         if (NULL != g_audio_timer) {
707                 ecore_timer_del(g_audio_timer);
708                 g_audio_timer = NULL;
709         }
710
711         g_audio_timer = ecore_timer_add(0, __read_audio_func, NULL);
712         if (NULL == g_audio_timer) {
713                 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Fail to create timer of read_audio");
714                 return STTD_ERROR_OPERATION_FAILED;
715         }
716 #else
717         if (true == __is_tct_app(appid)) {
718                 int ret = -1;
719                 int bt_retry = 0;
720                 bool started = false;
721                 while (5 > bt_retry) {
722                         ret = bt_hid_rc_start_sending_voice(NULL);
723                         if (BT_ERROR_NONE == ret) {
724                                 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Start bt audio");
725                                 started = true;
726                                 break;
727                         } else if (BT_ERROR_NOW_IN_PROGRESS == ret) {
728                                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail bt_hid_rc_start_sending_voice()");
729                                 usleep(50000);
730                                 bt_retry++;
731                         } else {
732                                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] bt_hid_rc_start_sending_voice(): ret(%d)", ret);
733                                 break;
734                         }
735                 }
736                 if (false == started) {
737                         SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to start bt audio(%d)", ret);
738                 }
739         }
740 #endif
741
742         g_recorder_state = STTD_RECORDER_STATE_RECORDING;
743         g_recorder->uid = uid;
744
745         g_buffer_count = 0;
746
747 #ifdef BUF_SAVE_MODE
748         g_count++;
749
750         while (1) {
751                 snprintf(g_temp_file_name, sizeof(g_temp_file_name), "/tmp/stt_temp_%d_%d", getpid(), g_count);
752                 ret = access(g_temp_file_name, 0);
753
754                 if (0 == ret) {
755                         SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] File is already exist");
756                         if (0 == remove(g_temp_file_name)) {
757                                 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Remove file");
758                                 break;
759                         } else {
760                                 g_count++;
761                         }
762                 } else {
763                         break;
764                 }
765         }
766
767         SECURE_SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Temp file name=[%s]", g_temp_file_name);
768
769         /* open test file */
770         g_pFile = fopen(g_temp_file_name, "wb+x");
771         if (!g_pFile) {
772                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] File not found!");
773                 return -1;
774         }
775 #endif
776
777         return 0;
778 }
779
780 int sttd_recorder_stop()
781 {
782         if (STTD_RECORDER_STATE_READY == g_recorder_state)
783                 return 0;
784
785         // critical section required because this function can be called from stt engine thread context
786         SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Enter critical section");
787         pthread_mutex_lock(&sttd_audio_in_handle_mutex);
788
789         sttd_engine_agent_set_audio_type(STTE_AUDIO_ID_NONE);
790
791         /* Check engine id is valid */
792         if (NULL == g_recorder) {
793                 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
794                 pthread_mutex_unlock(&sttd_audio_in_handle_mutex);
795                 return STTD_ERROR_INVALID_PARAMETER;
796         }
797
798         g_recorder->uid = STT_INVALID_UID;
799
800         int ret = -1;
801 #ifndef TV_BT_MODE
802         if (NULL != g_audio_timer) {
803                 ecore_timer_del(g_audio_timer);
804                 g_audio_timer = NULL;
805         }
806
807         ret = audio_in_unprepare(g_recorder->audio_h);
808         if (AUDIO_IO_ERROR_NONE != ret) {
809                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to unprepare audioin : %d", ret);
810         }
811 #else
812         int bt_retry = 0;
813         bool stopped = false;
814         while (5 > bt_retry) {
815                 ret = bt_hid_rc_stop_sending_voice(NULL);
816                 if (BT_ERROR_NONE == ret) {
817                         SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Stop bt audio");
818                         stopped = true;
819                         break;
820                 } else if (BT_ERROR_NOW_IN_PROGRESS == ret) {
821                         SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail bt_hid_rc_stop_sending_voice()");
822                         usleep(50000);
823                         bt_retry++;
824                 } else {
825                         SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] bt_hid_rc_stop_sending_voice(): ret(%d)", ret);
826                         break;
827                 }
828         }
829         if (false == stopped) {
830                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to stop bt audio(%d)", ret);
831 //              return STTD_ERROR_OPERATION_FAILED;
832         }
833 #endif
834
835 #ifdef TV_FFV_MODE
836         ret = farfield_voice_set_recognition_state(FARFIELD_VOICE_RECOGNITION_STOP);
837         if (0 != ret) {
838                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to stop FFV, ret(%d)", ret);
839         }
840 #endif
841
842         g_recorder_state = STTD_RECORDER_STATE_READY;
843
844 #ifdef BUF_SAVE_MODE
845         fclose(g_pFile);
846 #endif
847         pthread_mutex_unlock(&sttd_audio_in_handle_mutex);
848         SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Leave critical section");
849
850         return 0;
851 }
852
853 int sttd_recorder_start_file(unsigned int uid, const char *filepath)
854 {
855         if (STTD_RECORDER_STATE_RECORDING == g_recorder_state)
856                 return 0;
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                 return STTD_ERROR_INVALID_PARAMETER;
862         }
863         g_recorder_state = STTD_RECORDER_STATE_RECORDING;
864         g_recorder->uid = uid;
865
866         int cnt = 0;
867         int totalReadBytes = 0;
868
869         FILE *infile = fopen(filepath, "rb");
870
871         //process the file
872         if (infile != NULL) {
873                 while (!feof(infile)) {
874                         static char pcm_buff[BUFFER_LENGTH];
875                         int read_byte = fread(pcm_buff, 1, BUFFER_LENGTH, infile);
876                         totalReadBytes += read_byte;
877                         if (0 != read_byte) {
878                                 if (0 != g_audio_cb(pcm_buff, read_byte)) {
879                                         SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to call audio callback");
880                                         fclose(infile);
881                                         return -1;
882                                 }
883                                 if (0 == cnt % 30) {
884                                         float vol_db = get_volume_decibel(pcm_buff, BUFFER_LENGTH, g_recorder->audio_type);
885                                         if (0 != sttdc_send_set_volume(g_recorder->uid, vol_db)) {
886                                                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder] Fail to send recording volume(%f)", vol_db);
887                                         }
888                                 }
889
890                                 /* Audio read log */
891                                 if (0 == cnt % 50)
892                                         SLOG(LOG_DEBUG, TAG_STTD, "[Recorder][%d] Recording... : read_size(%d)", cnt, read_byte);
893                                 cnt++;
894                         }
895                 }
896                 fclose(infile);
897         }
898
899         SLOG(LOG_DEBUG, TAG_STTD, "[Recorder][%d] total bytes(%d)", cnt, totalReadBytes);
900         return 0;
901 }
902
903 int sttd_recorder_stop_file()
904 {
905         if (STTD_RECORDER_STATE_READY == g_recorder_state)
906                 return 0;
907
908         /* Check engine id is valid */
909         if (NULL == g_recorder) {
910                 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
911                 return STTD_ERROR_INVALID_PARAMETER;
912         }
913
914         g_recorder->uid = STT_INVALID_UID;
915         g_recorder_state = STTD_RECORDER_STATE_READY;
916
917         return 0;
918 }
919
920 int sttd_recorder_clear()
921 {
922 #ifdef TV_BT_MODE
923         SLOG(LOG_DEBUG, TAG_STTD, "[Recorder DEBUG] Clear recorder on TV");
924
925         if (STTD_RECORDER_STATE_RECORDING == g_recorder_state) {
926                 sttd_recorder_stop();
927         }
928         if (STTD_RECORDER_STATE_READY == g_recorder_state) {
929                 sttd_recorder_destroy();
930         }
931 #endif
932
933         return STTD_ERROR_NONE;
934 }