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