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