Fix unused codes
[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 #endif
17
18 #include <audio_io.h>
19 #include <Ecore.h>
20 #include <math.h>
21 #include <pthread.h>
22 #include <sound_manager.h>
23 #ifdef TV_BT_MODE
24 #include <bluetooth.h>
25 #endif
26
27 #include "stt_defs.h"
28 #include "sttd_dbus.h"
29 #include "sttd_recorder.h"
30 #include "sttd_main.h"
31
32
33 #define FRAME_LENGTH 160
34 #define BUFFER_LENGTH FRAME_LENGTH * 2
35
36 static pthread_mutex_t sttd_audio_in_handle_mutex = PTHREAD_MUTEX_INITIALIZER;
37
38 typedef enum {
39         STTD_RECORDER_STATE_NONE = -1,
40         STTD_RECORDER_STATE_READY = 0,  /**< Recorder is ready to start */
41         STTD_RECORDER_STATE_RECORDING   /**< In the middle of recording */
42 } sttd_recorder_state;
43
44 typedef struct {
45         int                     uid;
46         audio_in_h              audio_h;
47         stte_audio_type_e       audio_type;
48 } stt_recorder_s;
49
50 static stt_recorder_s* g_recorder = NULL;
51
52 static stt_recorder_audio_cb    g_audio_cb;
53
54 static sound_stream_info_h      g_stream_info_h;
55
56 static stt_recorder_interrupt_cb        g_interrupt_cb;
57
58 static sttd_recorder_state      g_recorder_state = STTD_RECORDER_STATE_NONE;
59
60 static int g_buffer_count;
61
62 /* Sound buf save for test */
63 /*
64 #define BUF_SAVE_MODE
65 */
66 #ifdef BUF_SAVE_MODE
67 static char g_temp_file_name[128] = {'\0',};
68
69 static FILE* g_pFile;
70
71 static int g_count = 1;
72 #endif
73
74 #ifdef TV_BT_MODE
75 static float get_volume_decibel(char* data, int size, stte_audio_type_e type);
76
77 //static stt_recorder_s* __get_recorder(int engine_id);
78
79 static int g_bt_extend_count;
80
81 #define SMART_CONTROL_EXTEND_CMD        0x03
82 #define SMART_CONTROL_START_CMD         0x04
83
84 static void _bt_cb_hid_state_changed(int result, bool connected, const char *remote_address, void *user_data)
85 {
86         SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Bluetooth Event [%d] Received address [%s]", result, remote_address);
87 }
88
89 static void _bt_hid_audio_data_receive_cb(bt_hid_voice_data_s *voice_data, void *user_data)
90 {
91         if (STTD_RECORDER_STATE_RECORDING != g_recorder_state) {
92                 /* SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Exit audio reading normal func"); */
93                 return;
94         }
95
96         if (NULL != g_audio_cb) {
97                 if (0 != g_audio_cb((void*)voice_data->audio_buf, (unsigned int)voice_data->length)) {
98                         SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to read audio");
99                         sttd_recorder_stop();
100                 }
101
102                 if (NULL == g_recorder) {
103                         return;
104                 }
105
106                 float vol_db = get_volume_decibel((char*)voice_data->audio_buf, (int)voice_data->length, g_recorder->audio_type);
107                 if (0 != sttdc_send_set_volume(g_recorder->uid, vol_db)) {
108                         SLOG(LOG_ERROR, TAG_STTD, "[Recorder] Fail to send recording volume(%f)", vol_db);
109                 }
110         }
111
112         if (0 == g_buffer_count || 0 == g_buffer_count % 50) {
113                 SLOG(LOG_WARN, TAG_STTD, "[Recorder][%d] Recording... : read_size(%d)", g_buffer_count, voice_data->length);
114
115                 if (0 == g_bt_extend_count % 5 && 0 != g_buffer_count) {
116                         const unsigned char input_data[2] = {SMART_CONTROL_EXTEND_CMD, 0x10 };
117                         if (BT_ERROR_NONE != bt_hid_send_rc_command(NULL, input_data, sizeof(input_data))) {
118                                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail bt_hid_send_rc_command(NULL, %s, %d)", input_data, sizeof(input_data));
119                         } else {
120                                 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Extend bt audio recorder");
121                         }
122                 }
123                 g_bt_extend_count++;
124
125                 if (100000 == g_buffer_count) {
126                         g_buffer_count = 0;
127                 }
128         }
129
130         g_buffer_count++;
131
132 #ifdef BUF_SAVE_MODE
133         /* write pcm buffer */
134         fwrite(data, 1, len, g_pFile);
135 #endif
136         return;
137 }
138 #endif
139
140
141 const char* __stt_get_focus_changed_reason_code(sound_stream_focus_change_reason_e reason)
142 {
143         switch (reason) {
144         case SOUND_STREAM_FOCUS_CHANGED_BY_MEDIA:                       return "SOUND_STREAM_FOCUS_CHANGED_BY_MEDIA";
145         case SOUND_STREAM_FOCUS_CHANGED_BY_SYSTEM:                      return "SOUND_STREAM_FOCUS_CHANGED_BY_SYSTEM";
146         case SOUND_STREAM_FOCUS_CHANGED_BY_ALARM:                       return "SOUND_STREAM_FOCUS_CHANGED_BY_ALARM";
147         case SOUND_STREAM_FOCUS_CHANGED_BY_NOTIFICATION:        return "SOUND_STREAM_FOCUS_CHANGED_BY_NOTIFICATION";
148         case SOUND_STREAM_FOCUS_CHANGED_BY_EMERGENCY:           return "SOUND_STREAM_FOCUS_CHANGED_BY_EMERGENCY";
149         case SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_INFORMATION:   return "SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_INFORMATION";
150         case SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_RECOGNITION:   return "SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_RECOGNITION";
151         case SOUND_STREAM_FOCUS_CHANGED_BY_RINGTONE:            return "SOUND_STREAM_FOCUS_CHANGED_BY_RINGTONE";
152         case SOUND_STREAM_FOCUS_CHANGED_BY_VOIP:                        return "SOUND_STREAM_FOCUS_CHANGED_BY_VOIP";
153         case SOUND_STREAM_FOCUS_CHANGED_BY_CALL:                        return "SOUND_STREAM_FOCUS_CHANGED_BY_CALL";
154         case SOUND_STREAM_FOCUS_CHANGED_BY_MEDIA_EXTERNAL_ONLY: return "SOUND_STREAM_FOCUS_CHANGED_BY_MEDIA_EXTERNAL_ONLY";
155         default:                                                                                        return "Undefined reason code";
156         }
157 }
158
159 void __recorder_focus_state_cb(sound_stream_info_h stream_info, sound_stream_focus_change_reason_e reason, const char *extra_info, void *user_data)
160 {
161         SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Focus state changed cb");
162
163         if (stream_info != g_stream_info_h) {
164                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Invalid stream info handle");
165                 return;
166         }
167
168         int ret;
169         sound_stream_focus_state_e state_for_recording;
170         ret = sound_manager_get_focus_state(g_stream_info_h, NULL, &state_for_recording);
171         if (SOUND_MANAGER_ERROR_NONE != ret) {
172                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to get focus state");
173                 return;
174         }
175
176         SLOG(LOG_WARN, TAG_STTD, "[Recorder] focus state changed to (%d) with reason(%s)", (int)state_for_recording, __stt_get_focus_changed_reason_code(reason));
177
178         if (STTD_RECORDER_STATE_RECORDING == g_recorder_state && SOUND_STREAM_FOCUS_STATE_RELEASED == state_for_recording) {
179                 SLOG(LOG_WARN, TAG_STTD, "[Recorder] Focus released as interrupt");
180                 if (NULL != g_interrupt_cb) {
181                         g_interrupt_cb();
182                 }
183         }
184 }
185
186
187 int sttd_recorder_initialize(stt_recorder_audio_cb audio_cb, stt_recorder_interrupt_cb interrupt_cb)
188 {
189         if (NULL == audio_cb || NULL == interrupt_cb) {
190                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Input param is NOT valid");
191                 return STTD_ERROR_INVALID_PARAMETER;
192         }
193
194         if (STTD_RECORDER_STATE_NONE != g_recorder_state) {
195                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Current state of recorder is recording");
196                 return STTD_ERROR_INVALID_STATE;
197         }
198
199         if (0 != pthread_mutex_init(&sttd_audio_in_handle_mutex, NULL)) {
200                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to initialize audio in handle mutex.");
201         }
202
203         g_audio_cb = audio_cb;
204         g_interrupt_cb = interrupt_cb;
205         g_recorder_state = STTD_RECORDER_STATE_NONE;
206         g_recorder = NULL;
207
208         if (0 != sound_manager_create_stream_information(SOUND_STREAM_TYPE_VOICE_RECOGNITION, __recorder_focus_state_cb, NULL, &g_stream_info_h)) {
209                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to create stream info");
210         }
211
212 #ifdef TV_BT_MODE
213         if (BT_ERROR_NONE != bt_initialize()) {
214                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to init bt");
215                 return STTD_ERROR_OPERATION_FAILED;
216         }
217
218         if (BT_ERROR_NONE != bt_hid_host_initialize(_bt_cb_hid_state_changed, NULL)) {
219                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail bt_hid_host_initialize()");
220                 return STTD_ERROR_OPERATION_FAILED;
221         }
222 #endif
223
224         return 0;
225 }
226
227 int sttd_recorder_deinitialize()
228 {
229         if (0 != pthread_mutex_destroy(&sttd_audio_in_handle_mutex)) {
230                 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy audio in handle mutex.");
231         }
232
233         if (0 != sound_manager_destroy_stream_information(g_stream_info_h)) {
234                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to destroy stream info");
235         }
236
237         if (NULL != g_recorder) {
238                 audio_in_destroy(g_recorder->audio_h);
239                 g_recorder->audio_h = NULL;
240                 free(g_recorder);
241                 g_recorder = NULL;
242         }
243
244 #ifdef __UNUSED_CODES__
245         /* Remove all recorder */
246         GSList *iter = NULL;
247         stt_recorder_s *recorder = NULL;
248
249         iter = g_slist_nth(g_recorder_list, 0);
250
251         while (NULL != iter) {
252                 recorder = iter->data;
253
254                 if (NULL != recorder) {
255                         g_recorder_list = g_slist_remove(g_recorder_list, recorder);
256                         if (recorder->audio_h) {
257                                 audio_in_destroy(recorder->audio_h);
258                                 recorder->audio_h = NULL;
259                         }
260                         free(recorder);
261                 }
262
263                 iter = g_slist_nth(g_recorder_list, 0);
264         }
265 #endif
266
267 #ifdef TV_BT_MODE
268         bt_hid_host_deinitialize();
269
270         bt_deinitialize();
271 #endif
272
273         g_recorder_state = STTD_RECORDER_STATE_NONE;
274
275         return 0;
276 }
277
278 int sttd_recorder_set_audio_session()
279 {
280         return 0;
281 }
282
283 int sttd_recorder_unset_audio_session()
284 {
285         return 0;
286 }
287
288 int sttd_recorder_create(stte_audio_type_e type, int channel, unsigned int sample_rate)
289 {
290         /* Check engine id is valid */
291         if (NULL != g_recorder) {
292                 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is already registered");
293                 return STTD_ERROR_INVALID_PARAMETER;
294         }
295
296         audio_in_h temp_in_h = NULL;
297
298 #ifndef TV_BT_MODE
299         audio_channel_e audio_ch;
300         audio_sample_type_e audio_type;
301
302         switch (channel) {
303         case 1: audio_ch = AUDIO_CHANNEL_MONO;          break;
304         case 2: audio_ch = AUDIO_CHANNEL_STEREO;        break;
305         default:
306                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Input channel is not supported");
307                 return STTD_ERROR_OPERATION_FAILED;
308                 break;
309         }
310
311         switch (type) {
312         case STTE_AUDIO_TYPE_PCM_S16_LE:        audio_type = AUDIO_SAMPLE_TYPE_S16_LE;  break;
313         case STTE_AUDIO_TYPE_PCM_U8:            audio_type = AUDIO_SAMPLE_TYPE_U8;      break;
314         default:
315                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Invalid Audio Type");
316                 return STTD_ERROR_OPERATION_FAILED;
317                 break;
318         }
319
320         int ret;
321         ret = audio_in_create(sample_rate, audio_ch, audio_type, &temp_in_h);
322         if (AUDIO_IO_ERROR_NONE != ret) {
323                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to create audio handle : %d", ret);
324                 return STTD_ERROR_OPERATION_FAILED;
325         }
326 #else
327         if (BT_ERROR_NONE != bt_hid_set_audio_data_receive_cb(_bt_hid_audio_data_receive_cb, NULL)) {
328                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail bt_hid_set_audio_data_receive_cb()");
329                 return STTD_ERROR_OPERATION_FAILED;
330         }
331 #endif
332
333         stt_recorder_s* recorder;
334         recorder = (stt_recorder_s*)calloc(1, sizeof(stt_recorder_s));
335         if (NULL == recorder) {
336 #ifndef TV_BT_MODE
337                 if (temp_in_h) {
338                         audio_in_destroy(temp_in_h);
339                         temp_in_h = NULL;
340                 }
341 #endif
342                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to allocate memory");
343                 return STTD_ERROR_OUT_OF_MEMORY;
344         }
345
346         recorder->uid = -1;
347         recorder->audio_h = temp_in_h;
348         recorder->audio_type = type;
349
350         g_recorder = recorder;
351
352         g_recorder_state = STTD_RECORDER_STATE_READY;
353
354         return 0;
355 }
356
357 int sttd_recorder_destroy()
358 {
359         // critical section required because this function can be called from stt engine thread context
360         SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Enter critical section");
361         pthread_mutex_lock(&sttd_audio_in_handle_mutex);
362
363         /* Check engine id is valid */
364         if (NULL == g_recorder) {
365                 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
366                 pthread_mutex_unlock(&sttd_audio_in_handle_mutex);
367                 return STTD_ERROR_INVALID_PARAMETER;
368         }
369
370 #ifndef TV_BT_MODE
371         int ret;
372         if (STTD_RECORDER_STATE_RECORDING == g_recorder_state) {
373                 if (g_recorder->audio_h) {
374                         ret = audio_in_unprepare(g_recorder->audio_h);
375                         if (AUDIO_IO_ERROR_NONE != ret) {
376                                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to unprepare audioin : %d", ret);
377                         }
378                 }
379
380                 g_recorder_state = STTD_RECORDER_STATE_READY;
381         }
382
383         if (g_recorder->audio_h) {
384                 ret = audio_in_destroy(g_recorder->audio_h);
385                 if (AUDIO_IO_ERROR_NONE != ret) {
386                         SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to destroy audioin : %d", ret);
387                 }
388                 g_recorder->audio_h = NULL;
389         }
390
391 #else
392         if (STTD_RECORDER_STATE_RECORDING == g_recorder_state) {
393                 g_recorder_state = STTD_RECORDER_STATE_READY;
394         }
395
396         bt_hid_unset_audio_data_receive_cb();
397 #endif
398
399         free(g_recorder);
400         g_recorder = NULL;
401
402         pthread_mutex_unlock(&sttd_audio_in_handle_mutex);
403         SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Leave critical section");
404
405         return 0;
406 }
407
408 static float get_volume_decibel(char* data, int size, stte_audio_type_e type)
409 {
410         #define MAX_AMPLITUDE_MEAN_16   32768
411         #define MAX_AMPLITUDE_MEAN_08   128
412
413         int i, depthByte;
414         int count = 0;
415
416         float db = 0.0;
417         float rms = 0.0;
418         unsigned long long square_sum = 0;
419
420         if (type == STTE_AUDIO_TYPE_PCM_S16_LE)
421                 depthByte = 2;
422         else
423                 depthByte = 1;
424
425         for (i = 0; i < size; i += (depthByte<<1)) {
426                 if (depthByte == 2) {
427                         short pcm16 = 0;
428                         memcpy(&pcm16, data + i, sizeof(short));
429                         square_sum += pcm16 * pcm16;
430                 } else {
431                         char pcm8 = 0;
432                         memcpy(&pcm8, data + i, sizeof(char));
433                         square_sum += pcm8 * pcm8;
434                 }
435                 count++;
436         }
437
438         if (0 == count)
439                 rms = 0.0;
440         else
441                 rms = sqrt(square_sum/count);
442
443         if (depthByte == 2)
444                 db = 20 * log10(rms/MAX_AMPLITUDE_MEAN_16);
445         else
446                 db = 20 * log10(rms/MAX_AMPLITUDE_MEAN_08);
447
448         return db;
449 }
450
451 #ifndef TV_BT_MODE
452 Eina_Bool __read_audio_func(void *data)
453 {
454         int read_byte = -1;
455         static char g_buffer[BUFFER_LENGTH];
456
457         /* Check engine id is valid */
458         if (NULL == g_recorder) {
459                 return EINA_FALSE;
460         }
461
462         if (STTD_RECORDER_STATE_READY == g_recorder_state) {
463                 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Exit audio reading func");
464                 return EINA_FALSE;
465         }
466
467         read_byte = audio_in_read(g_recorder->audio_h, g_buffer, BUFFER_LENGTH);
468         if (0 > read_byte) {
469                 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Fail to read audio : %d", read_byte);
470                 g_recorder_state = STTD_RECORDER_STATE_READY;
471                 return EINA_FALSE;
472         }
473
474         if (0 != g_audio_cb(g_buffer, read_byte)) {
475                 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Fail audio callback");
476                 sttd_recorder_stop();
477                 return EINA_FALSE;
478         }
479
480         if (0 == g_buffer_count % 30) {
481                 float vol_db = get_volume_decibel(g_buffer, BUFFER_LENGTH, g_recorder->audio_type);
482                 if (0 != sttdc_send_set_volume(g_recorder->uid, vol_db)) {
483                         SLOG(LOG_ERROR, TAG_STTD, "[Recorder] Fail to send recording volume(%f)", vol_db);
484                 }
485         }
486
487         /* Audio read log */
488         if (0 == g_buffer_count % 50) {
489                 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder][%d] Recording... : read_size(%d)", g_buffer_count, read_byte);
490
491                 if (100000 == g_buffer_count) {
492                         g_buffer_count = 0;
493                 }
494         }
495
496         g_buffer_count++;
497
498 #ifdef BUF_SAVE_MODE
499         /* write pcm buffer */
500         fwrite(g_buffer, 1, BUFFER_LENGTH, g_pFile);
501 #endif
502
503         return EINA_TRUE;
504 }
505 #endif
506
507 int sttd_recorder_start(int uid)
508 {
509         if (STTD_RECORDER_STATE_RECORDING == g_recorder_state)
510                 return 0;
511
512         int ret = -1;
513 #ifndef TV_BT_MODE
514         /* Check engine id is valid */
515         if (NULL == g_recorder) {
516                 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
517                 return STTD_ERROR_INVALID_PARAMETER;
518         }
519
520         ret = sound_manager_acquire_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_RECORDING, NULL);
521         if (SOUND_MANAGER_ERROR_NONE != ret) {
522                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to acquire focus : %d", ret);
523         } else {
524                 ret = audio_in_set_stream_info(g_recorder->audio_h, g_stream_info_h);
525                 if (AUDIO_IO_ERROR_NONE != ret) {
526                         SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to set stream info");
527                 }
528         }
529
530         ret = audio_in_prepare(g_recorder->audio_h);
531         if (AUDIO_IO_ERROR_NONE != ret) {
532                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to start audio : %d", ret);
533                 return STTD_ERROR_RECORDER_BUSY;
534         }
535
536         /* Add ecore timer to read audio data */
537         ecore_timer_add(0, __read_audio_func, NULL);
538
539 #else
540         g_bt_extend_count = 0;
541         const unsigned char input_data[2] = {SMART_CONTROL_START_CMD, 0x00 };
542         int bt_retry = 0;
543         bool started = false;
544         while (5 > bt_retry) {
545                 ret = bt_hid_send_rc_command(NULL, input_data, sizeof(input_data));
546                 if (BT_ERROR_NONE == ret) {
547                         SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Start bt audio recorder");
548                         started = true;
549                         break;
550                 } else if (BT_ERROR_NOW_IN_PROGRESS == ret) {
551                         SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail bt_hid_send_rc_command(NULL, %s, %d)", input_data, sizeof(input_data));
552                         usleep(50000);
553                         bt_retry++;
554                 } else {
555                         break;
556                 }
557         }
558         if (false == started) {
559                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to start bt audio");
560                 return STTD_ERROR_OPERATION_FAILED;
561         }
562 #endif
563         g_recorder_state = STTD_RECORDER_STATE_RECORDING;
564         g_recorder->uid = uid;
565
566         g_buffer_count = 0;
567
568 #ifdef BUF_SAVE_MODE
569         g_count++;
570
571         snprintf(g_temp_file_name, sizeof(g_temp_file_name), "/tmp/stt_temp_%d_%d", getpid(), g_count);
572         SECURE_SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Temp file name=[%s]", g_temp_file_name);
573
574         /* open test file */
575         g_pFile = fopen(g_temp_file_name, "wb+");
576         if (!g_pFile) {
577                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] File not found!");
578                 return -1;
579         }
580 #endif
581
582         return 0;
583 }
584
585 int sttd_recorder_stop()
586 {
587         if (STTD_RECORDER_STATE_READY == g_recorder_state)
588                 return 0;
589
590         /* Check engine id is valid */
591         if (NULL == g_recorder) {
592                 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
593                 return STTD_ERROR_INVALID_PARAMETER;
594         }
595
596         g_recorder->uid = -1;
597
598         int ret;
599 #ifndef TV_BT_MODE
600         ret = audio_in_unprepare(g_recorder->audio_h);
601         if (AUDIO_IO_ERROR_NONE != ret) {
602                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to unprepare audioin : %d", ret);
603         }
604 #else
605         int bt_retry = 0;
606         bool stopped = false;
607         while (5 > bt_retry) {
608                 ret = bt_hid_rc_stop_sending_voice(NULL);
609                 if (BT_ERROR_NONE == ret) {
610                         SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Stop bt audio");
611                         stopped = true;
612                         break;
613                 } else if (BT_ERROR_NOW_IN_PROGRESS == ret) {
614                         SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail bt_hid_rc_stop_sending_voice()");
615                         usleep(50000);
616                         bt_retry++;
617                 } else {
618                         break;
619                 }
620         }
621         if (false == stopped) {
622                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to stop bt audio");
623                 return STTD_ERROR_OPERATION_FAILED;
624         }
625 #endif
626
627         g_recorder_state = STTD_RECORDER_STATE_READY;
628
629         ret = sound_manager_release_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_RECORDING, NULL);
630         if (SOUND_MANAGER_ERROR_NONE != ret) {
631                 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to release focus :%d", ret);
632         }
633
634 #ifdef BUF_SAVE_MODE
635         fclose(g_pFile);
636 #endif
637
638         return 0;
639 }