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