check null before use when dbus close connection
[platform/core/uifw/voice-control.git] / server / vcd_recorder.c
1 /*
2  * Copyright (c) 2011-2017 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifdef TV_PRODUCT
18 #define TV_BT_MODE
19 #define TV_MSF_WIFI_MODE
20 #endif
21
22 #include <audio_io.h>
23 #include <math.h>
24 #include <sound_manager.h>
25 #include <sound_manager_internal.h>
26 #ifdef TV_PRODUCT
27 #ifdef TV_BT_MODE
28 #include <bluetooth_product.h>
29 #endif
30 #ifdef TV_MSF_WIFI_MODE
31 #include <MSFVoiceInterface.h>
32 #endif
33 #endif
34
35 #include "vcd_client_data.h"
36 #include "vcd_config.h"
37 #include "vcd_dbus.h"
38 #include "vcd_engine_agent.h"
39 #include "vcd_recorder.h"
40 #include "vcd_main.h"
41
42 #define FRAME_LENGTH 160
43 #define BUFFER_LENGTH FRAME_LENGTH * 2
44
45 #define FOCUS_SERVER_READY              "/tmp/.focus_server_ready"
46
47 #define VCE_AUDIO_ID_NONE               "VC_AUDIO_ID_NONE"              /**< None audio id */
48
49 static vcd_recorder_state_e     g_recorder_state = VCD_RECORDER_STATE_READY;
50
51 static vcd_recoder_audio_cb     g_audio_cb = NULL;
52
53 static vcd_recorder_interrupt_cb        g_interrupt_cb = NULL;
54
55 static audio_in_h       g_audio_h;
56
57 static sound_stream_info_h      g_stream_info_h;
58
59 static vce_audio_type_e g_audio_type;
60
61 static int      g_audio_rate;
62
63 static int              g_audio_channel;
64
65 static char     g_normal_buffer[BUFFER_LENGTH + 10];
66
67 static bool     g_is_valid_audio_in = false;
68
69 static bool     g_is_valid_bt_in = false;
70
71 static char*    g_current_audio_type = NULL;
72
73 static int      g_buffer_count;
74
75 static int      g_device_id = -1;
76
77 /* Sound buf save */
78 /*
79 #define BUF_SAVE_MODE
80  */
81
82 #ifdef BUF_SAVE_MODE
83 static FILE* g_normal_file;
84
85 static int g_count = 1;
86 #endif
87
88 static float get_volume_decibel(char* data, int size);
89
90 #ifdef TV_MSF_WIFI_MODE
91 static void __msf_wifi_audio_data_receive_cb(msf_wifi_voice_data_s *voice_data, void* user_data)
92 {
93         if (0 != strncmp(g_current_audio_type, VCE_AUDIO_ID_WIFI, sizeof(VCE_AUDIO_ID_WIFI))) {
94                 vcd_state_e state = vcd_config_get_service_state();
95                 if (VCD_STATE_READY == state) {
96                         vcd_recorder_set(VCE_AUDIO_ID_WIFI, VCE_AUDIO_TYPE_PCM_S16_LE, 16000, 1);
97                 } else {
98                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] current audio type is (%s)", g_current_audio_type);
99                         return;
100                 }
101         }
102
103         if (VCD_RECORDER_STATE_RECORDING != g_recorder_state) {
104                 SLOG(LOG_WARN, TAG_VCD, "[Recorder] Not start yet, but send audio data vi Wi-Fi");
105                 vcd_recorder_start();
106         }
107
108         if (NULL != g_audio_cb) {
109                 if (0 != g_audio_cb((void*)voice_data->audio_buf, (unsigned int)voice_data->length)) {
110                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to read audio");
111                         vcd_recorder_stop();
112                 }
113         }
114
115         /* Set volume */
116         if (0 == g_buffer_count % 30) {
117                 float vol_db = get_volume_decibel((char*)voice_data->audio_buf, (unsigned int)voice_data->length);
118                 if (0 != vcdc_send_set_volume(vcd_client_manager_get_pid(), vol_db)) {
119                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder] Fail to send recording volume(%f)", vol_db);
120                 }
121         }
122
123         if (0 == g_buffer_count || 0 == g_buffer_count % 50) {
124                 SLOG(LOG_WARN, TAG_VCD, "[Recorder][%d] Recording... : read_size(%ld)", g_buffer_count, voice_data->length);
125
126                 if (100000 == g_buffer_count)
127                         g_buffer_count = 0;
128         }
129
130         g_buffer_count++;
131
132 #ifdef BUF_SAVE_MODE
133         /* write pcm buffer */
134         fwrite(voice_data->audio_buf, 1, voice_data->length, g_normal_file);
135 #endif
136         return;
137 }
138
139 #endif
140
141
142 #ifdef TV_BT_MODE
143 static int g_bt_extend_count;
144
145 #define SMART_CONTROL_EXTEND_CMD        0x03
146 #define SMART_CONTROL_START_CMD         0x04
147
148 static void _bt_cb_hid_state_changed(int result, bool connected, const char *remote_address, void *user_data)
149 {
150         SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Bluetooth Event [%d] Received address [%s]", result, remote_address);
151         return;
152 }
153
154 static void _bt_hid_audio_data_receive_cb(bt_hid_voice_data_s *voice_data, void *user_data)
155 {
156         if (0 != strncmp(g_current_audio_type, VCE_AUDIO_ID_BLUETOOTH, sizeof(VCE_AUDIO_ID_BLUETOOTH))) {
157                 vcd_state_e state = vcd_config_get_service_state();
158                 if (VCD_STATE_READY == state) {
159                         vcd_recorder_set(VCE_AUDIO_ID_BLUETOOTH, VCE_AUDIO_TYPE_PCM_S16_LE, 16000, 1);
160                 } else {
161                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] current audio type is (%s)", g_current_audio_type);
162                         return;
163                 }
164         }
165
166         if (VCD_RECORDER_STATE_RECORDING != g_recorder_state) {
167                 SLOG(LOG_WARN, TAG_VCD, "[Recorder] Not start yet, but send audio data vi Bluetooth");
168                 //              vcd_recorder_start();
169         }
170
171         if (NULL != g_audio_cb) {
172                 if (0 != g_audio_cb((void*)voice_data->audio_buf, (unsigned int)voice_data->length)) {
173                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to read audio");
174                         vcd_recorder_stop();
175                 }
176         }
177
178         /* Set volume */
179         if (0 == g_buffer_count % 15) {
180                 float vol_db = get_volume_decibel((char*)voice_data->audio_buf, (unsigned int)voice_data->length);
181                 if (0 != vcdc_send_set_volume(vcd_client_manager_get_pid(), vol_db)) {
182                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder] Fail to send recording volume(%f)", vol_db);
183                 }
184         }
185
186         if (0 == g_buffer_count || 0 == g_buffer_count % 50) {
187                 SLOG(LOG_WARN, TAG_VCD, "[Recorder][%d] Recording... : read_size(%d)", g_buffer_count, voice_data->length);
188
189                 if (0 == g_bt_extend_count % 5 && 0 != g_buffer_count) {
190                         const unsigned char input_data[2] = {SMART_CONTROL_EXTEND_CMD, 0x10 };
191                         if (BT_ERROR_NONE != bt_hid_send_rc_command(NULL, input_data, sizeof(input_data))) {
192                                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail bt_hid_send_rc_command");
193                         } else {
194                                 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Extend bt audio recorder");
195                         }
196                 }
197                 g_bt_extend_count++;
198
199                 if (100000 == g_buffer_count) {
200                         g_buffer_count = 0;
201                 }
202         }
203
204         g_buffer_count++;
205
206 #ifdef BUF_SAVE_MODE
207         /* write pcm buffer */
208         fwrite(voice_data->audio_buf, 1, voice_data->length, g_normal_file);
209 #endif
210         return;
211 }
212
213 #endif
214
215 #if 1
216 static const char* __get_focus_changed_reason_code(sound_stream_focus_change_reason_e reason)
217 {
218         switch (reason) {
219         case SOUND_STREAM_FOCUS_CHANGED_BY_MEDIA:               return "SOUND_STREAM_FOCUS_CHANGED_BY_MEDIA";
220         case SOUND_STREAM_FOCUS_CHANGED_BY_SYSTEM:              return "SOUND_STREAM_FOCUS_CHANGED_BY_SYSTEM";
221         case SOUND_STREAM_FOCUS_CHANGED_BY_ALARM:               return "SOUND_STREAM_FOCUS_CHANGED_BY_ALARM";
222         case SOUND_STREAM_FOCUS_CHANGED_BY_NOTIFICATION:        return "SOUND_STREAM_FOCUS_CHANGED_BY_NOTIFICATION";
223         case SOUND_STREAM_FOCUS_CHANGED_BY_EMERGENCY:           return "SOUND_STREAM_FOCUS_CHANGED_BY_EMERGENCY";
224         case SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_INFORMATION:   return "SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_INFORMATION";
225         case SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_RECOGNITION:   return "SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_RECOGNITION";
226         case SOUND_STREAM_FOCUS_CHANGED_BY_RINGTONE:            return "SOUND_STREAM_FOCUS_CHANGED_BY_RINGTONE";
227         case SOUND_STREAM_FOCUS_CHANGED_BY_VOIP:                return "SOUND_STREAM_FOCUS_CHANGED_BY_VOIP";
228         case SOUND_STREAM_FOCUS_CHANGED_BY_CALL:                return "SOUND_STREAM_FOCUS_CHANGED_BY_CALL";
229         case SOUND_STREAM_FOCUS_CHANGED_BY_MEDIA_EXTERNAL_ONLY: return "SOUND_STREAM_FOCUS_CHANGED_BY_MEDIA_EXTERNAL_ONLY";
230         default:                                                return "Undefined reason code";
231         }
232 }
233
234 static void __recorder_focus_state_cb(sound_stream_info_h stream_info, sound_stream_focus_mask_e focus_mask, sound_stream_focus_state_e focus_state,
235                 sound_stream_focus_change_reason_e reason, int sound_behavior, const char *extra_info, void *user_data)
236 {
237         SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Focus state changed cb");
238
239         if (stream_info != g_stream_info_h) {
240                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Invalid stream info handle");
241                 return;
242         }
243
244         SLOG(LOG_WARN, TAG_VCD, "[Recorder] focus state chagned to (%d) with reason (%s)", (int)focus_state, __get_focus_changed_reason_code(reason));
245
246         if (VCD_RECORDER_STATE_RECORDING == g_recorder_state && SOUND_STREAM_FOCUS_STATE_RELEASED == focus_state) {
247                 SLOG(LOG_WARN, TAG_VCD, "[Recorder] Focus released as interrupt");
248                 if (NULL != g_interrupt_cb) {
249                         g_interrupt_cb();
250                 }
251         }
252 }
253 #endif
254
255 static int __apply_device_for_stream_routing()
256 {
257         sound_device_list_h device_list = NULL;
258         sound_device_h device = NULL;
259         sound_device_type_e type;
260         sound_device_io_direction_e io_direction;
261
262         if (0 != sound_manager_get_current_device_list(SOUND_DEVICE_IO_DIRECTION_IN_MASK, &device_list)) {
263                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to get current device list");
264                 return -1;
265         }
266
267         int ret = 0;
268         while (0 == (ret = sound_manager_get_next_device(device_list, &device))) {
269                 if (0 != sound_manager_get_device_type(device, &type)) {
270                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to get device tyep");
271                         continue;
272                 }
273                 if (0 != sound_manager_get_device_io_direction(device, &io_direction)) {
274                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to get device io direction");
275                         continue;
276                 }
277                 if (SOUND_DEVICE_USB_AUDIO == type && SOUND_DEVICE_IO_DIRECTION_IN == io_direction) {
278                         if (0 != sound_manager_add_device_for_stream_routing(g_stream_info_h, device)) {
279                                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to add device");
280                                 continue;
281                         }
282                         if (0 != sound_manager_apply_stream_routing(g_stream_info_h)) {
283                                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR} Fail to apply stream routing");
284                                 continue;
285                         }
286                         SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Apply device for stream routing");
287                         return 0;
288                 }
289         }
290
291         SLOG(LOG_WARN, TAG_VCD, "[Recorder] No device");
292         return -1;
293 }
294
295 static void __device_connection_changed_cb(sound_device_h device, bool is_connected, void *user_data)
296 {
297         sound_device_type_e type;
298         if (is_connected) {
299                 if (0 != sound_manager_get_device_type(device, &type)) {
300                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to get device tyep");
301                         return;
302                 }
303                 if (type == SOUND_DEVICE_USB_AUDIO) {
304                         bool is_recording_state = false;
305                         if (VCD_RECORDER_STATE_RECORDING == g_recorder_state) {
306                                 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Stop recorder");
307                                 vcd_recorder_stop();
308                                 is_recording_state = true;
309                         }
310                         if (0 != sound_manager_remove_device_for_stream_routing(g_stream_info_h, device))
311                                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to remove device");
312
313                         if (0 != sound_manager_add_device_for_stream_routing(g_stream_info_h, device)) {
314                                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to add device");
315                                 return;
316                         }
317                         if (0 != sound_manager_apply_stream_routing(g_stream_info_h)) {
318                                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR} Fail to apply stream routing");
319                                 return;
320                         }
321                         if (true == is_recording_state) {
322                                 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Start recorder");
323                                 vcd_recorder_start();
324                         }
325                         SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Apply device for stream routing");
326                 }
327         }
328         return;
329 }
330
331 int vcd_recorder_create(vcd_recoder_audio_cb audio_cb, vcd_recorder_interrupt_cb interrupt_cb)
332 {
333         if (NULL == audio_cb || NULL == interrupt_cb) {
334                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Input param is NOT valid");
335                 return VCD_ERROR_INVALID_PARAMETER;
336         }
337
338         /* check focus server */
339         int cnt = 0;
340         while (1) {
341                 if (0 == access(FOCUS_SERVER_READY, F_OK)) {
342                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder SUCCESS] focus server is available");
343                         break;
344                 } else {
345                         if (0 == cnt++ % 10)
346                                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] focus server is not available");
347                         usleep(50000);
348                 }
349         }
350
351         int ret = 0;
352
353         /* set init value */
354         g_is_valid_audio_in = false;
355         g_is_valid_bt_in = false;
356         g_current_audio_type = NULL;
357
358         g_audio_type = VCE_AUDIO_TYPE_PCM_S16_LE;
359         g_audio_rate = 16000;
360         g_audio_channel = 1;
361
362         audio_channel_e audio_ch;
363         audio_sample_type_e audio_type;
364
365         SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] AUdio type(%d) rate(%d) channel(%d)", g_audio_type, g_audio_rate, g_audio_channel);
366
367         switch (g_audio_channel) {
368         case 1: audio_ch = AUDIO_CHANNEL_MONO;          break;
369         case 2: audio_ch = AUDIO_CHANNEL_STEREO;        break;
370         default:
371                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Input channel is not supported");
372                 return VCD_ERROR_OPERATION_FAILED;
373                 break;
374         }
375
376         switch (g_audio_type) {
377         case VCE_AUDIO_TYPE_PCM_S16_LE: audio_type = AUDIO_SAMPLE_TYPE_S16_LE;  break;
378         case VCE_AUDIO_TYPE_PCM_U8:     audio_type = AUDIO_SAMPLE_TYPE_U8;      break;
379         default:
380                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Invalid Audio Type");
381                 return VCD_ERROR_OPERATION_FAILED;
382                 break;
383         }
384
385         ret = audio_in_create(g_audio_rate, audio_ch, audio_type, &g_audio_h);
386         if (AUDIO_IO_ERROR_NONE == ret) {
387                 g_is_valid_audio_in = true;
388         } else {
389                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Rate(%d) Channel(%d) Type(%d)", g_audio_rate, audio_ch, audio_type);
390                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to create audio handle : %d", ret);
391                 g_is_valid_audio_in = false;
392         }
393
394         ret = sound_manager_add_device_connection_changed_cb(SOUND_DEVICE_IO_DIRECTION_IN_MASK, __device_connection_changed_cb, NULL, &g_device_id);
395         if (0 != ret)
396                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to add device connection changed callback");
397
398         if (0 != sound_manager_create_stream_information_internal(SOUND_STREAM_TYPE_VOICE_RECOGNITION_SERVICE, __recorder_focus_state_cb, NULL, &g_stream_info_h)) {
399                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to create stream info");
400         } else {
401                 __apply_device_for_stream_routing();
402         }
403
404         if (0 != audio_in_set_sound_stream_info(g_audio_h, g_stream_info_h)) {
405                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to set stream info");
406         }
407
408         g_audio_cb = audio_cb;
409         g_interrupt_cb = interrupt_cb;
410         g_recorder_state = VCD_RECORDER_STATE_READY;
411
412 #ifdef TV_BT_MODE
413
414         bool is_bt_failed = false;
415
416         if (false == is_bt_failed && BT_ERROR_NONE != bt_product_init()) {
417                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to init bt");
418                 is_bt_failed = true;
419         }
420
421         if (false == is_bt_failed && BT_ERROR_NONE != bt_hid_host_initialize(_bt_cb_hid_state_changed, NULL)) {
422                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail bt_hid_host_initialize()");
423                 is_bt_failed = true;
424         }
425
426         if (false == is_bt_failed && BT_ERROR_NONE != bt_hid_set_audio_data_receive_cb(_bt_hid_audio_data_receive_cb, NULL)) {
427                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail bt_hid_set_audio_data_receive_cb()");
428                 is_bt_failed = true;
429         }
430
431
432         if (false == is_bt_failed) {
433                 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Bluetooth is available");
434                 g_is_valid_bt_in = true;
435         }
436 #endif
437         /* Select default audio type */
438         if (true == g_is_valid_audio_in) {
439                 g_current_audio_type = strdup(VCE_AUDIO_ID_NONE);
440         } else {
441                 if (true == g_is_valid_bt_in) {
442                         g_current_audio_type = strdup(VCE_AUDIO_ID_BLUETOOTH);
443                 } else {
444                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] No valid audio");
445                         return -1;
446                 }
447         }
448
449         SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Audio type : %s", g_current_audio_type);
450
451         return 0;
452 }
453
454 int vcd_recorder_destroy()
455 {
456         if (VCD_RECORDER_STATE_RECORDING == g_recorder_state) {
457                 if (0 == strncmp(VCE_AUDIO_ID_BLUETOOTH, g_current_audio_type, strlen(VCE_AUDIO_ID_BLUETOOTH))) {
458 #ifdef TV_BT_MODE
459                         bt_hid_unset_audio_data_receive_cb();
460 #endif
461                 } else if (0 == strncmp(VCE_AUDIO_ID_WIFI, g_current_audio_type, strlen(VCE_AUDIO_ID_WIFI))) {
462 #ifdef TV_MSF_WIFI_MODE
463                         UnRegisterMSFAudioCallback();
464 #endif
465                 } else {
466                         audio_in_unprepare(g_audio_h);
467                 }
468                 g_recorder_state = VCD_RECORDER_STATE_READY;
469         }
470
471         int ret = sound_manager_remove_device_connection_changed_cb(g_device_id);
472         if (0 != ret)
473                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to remove device connection changed callback, ret(%d)", ret);
474
475         ret = sound_manager_destroy_stream_information(g_stream_info_h);
476         if (0 != ret)
477                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to destroy stream info, ret(%d)", ret);
478
479         audio_in_destroy(g_audio_h);
480
481 #ifdef TV_BT_MODE
482         bt_hid_unset_audio_data_receive_cb();
483
484         bt_hid_host_deinitialize();
485
486         bt_product_deinit();
487 #endif
488
489         g_audio_cb = NULL;
490
491         if (NULL != g_current_audio_type) {
492                 free(g_current_audio_type);
493                 g_current_audio_type = NULL;
494         }
495
496         return 0;
497 }
498
499 int vcd_recorder_set(const char* audio_type, vce_audio_type_e type, int rate, int channel)
500 {
501         if (NULL == audio_type) {
502                 return VCD_ERROR_INVALID_PARAMETER;
503         }
504
505         if (NULL != g_current_audio_type) {
506                 if (0 == strncmp(g_current_audio_type, audio_type, strlen(g_current_audio_type))) {
507                         SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Current audio type is already set : %s", audio_type);
508                         return 0;
509                 }
510         } else {
511                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Recorder is NOT created");
512                 return VCD_ERROR_INVALID_STATE;
513         }
514
515         SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] set audio type (%s)", audio_type);
516         vcd_engine_set_audio_type(audio_type);
517
518         if (VCD_RECORDER_STATE_READY != g_recorder_state) {
519                 if (NULL != g_current_audio_type) {
520                         if ((!strncmp(g_current_audio_type, VCE_AUDIO_ID_NONE, strlen(g_current_audio_type)) &&
521                                                 strncmp(audio_type, VCE_AUDIO_ID_BLUETOOTH, strlen(audio_type)) &&
522                                                 strncmp(audio_type, VCE_AUDIO_ID_WIFI, strlen(audio_type))) ||
523                                         (strncmp(g_current_audio_type, VCE_AUDIO_ID_BLUETOOTH, strlen(g_current_audio_type)) &&
524                                          strncmp(g_current_audio_type, VCE_AUDIO_ID_WIFI, strlen(g_current_audio_type)) &&
525                                          strncmp(g_current_audio_type, VCE_AUDIO_ID_NONE, strlen(g_current_audio_type)) &&
526                                          !strncmp(audio_type, VCE_AUDIO_ID_NONE, strlen(audio_type)))) {
527                                 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Skip stop recording while Recorder is NOT ready");
528                         } else {
529                                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Recorder is NOT ready");
530                                 vcd_recorder_stop();
531                                 //return VCD_ERROR_INVALID_STATE;
532                         }
533                 }
534         }
535
536         int ret = -1;
537         /* Check BT audio */
538         if (0 == strncmp(VCE_AUDIO_ID_BLUETOOTH, audio_type, strlen(VCE_AUDIO_ID_BLUETOOTH))) {
539                 if (false == g_is_valid_bt_in) {
540                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder] BT audio is NOT valid");
541                         return VCD_ERROR_OPERATION_REJECTED;
542                 }
543
544                 if (NULL != g_current_audio_type) {
545                         free(g_current_audio_type);
546                         g_current_audio_type = NULL;
547                 }
548
549                 g_current_audio_type = strdup(audio_type);
550         } else if (0 == strncmp(VCE_AUDIO_ID_WIFI, audio_type, strlen(VCE_AUDIO_ID_WIFI))) {
551                 if (NULL != g_current_audio_type) {
552                         free(g_current_audio_type);
553                         g_current_audio_type = NULL;
554                 }
555
556                 g_current_audio_type = strdup(audio_type);
557         } else {
558                 if (false == g_is_valid_audio_in) {
559                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Audio-in is NOT valid");
560                         return VCD_ERROR_OPERATION_REJECTED;
561                 }
562
563                 if (g_audio_type != type || g_audio_rate != rate || g_audio_channel != channel) {
564                         SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] New audio type(%d) rate(%d) channel(%d)", type, rate, channel);
565                         audio_in_destroy(g_audio_h);
566
567                         audio_channel_e audio_ch;
568                         audio_sample_type_e audio_type;
569
570                         switch (channel) {
571                         case 1: audio_ch = AUDIO_CHANNEL_MONO;          break;
572                         case 2: audio_ch = AUDIO_CHANNEL_STEREO;        break;
573                         default:
574                                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Input channel is not supported");
575                                 return VCD_ERROR_OPERATION_FAILED;
576                                 break;
577                         }
578
579                         switch (type) {
580                         case VCE_AUDIO_TYPE_PCM_S16_LE: audio_type = AUDIO_SAMPLE_TYPE_S16_LE;  break;
581                         case VCE_AUDIO_TYPE_PCM_U8:     audio_type = AUDIO_SAMPLE_TYPE_U8;      break;
582                         default:
583                                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Invalid Audio Type");
584                                 return VCD_ERROR_OPERATION_FAILED;
585                                 break;
586                         }
587
588                         ret = audio_in_create(rate, audio_ch, audio_type, &g_audio_h);
589                         if (AUDIO_IO_ERROR_NONE != ret) {
590                                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to create audio handle : %d", ret);
591                                 g_is_valid_audio_in = false;
592                                 return VCD_ERROR_OPERATION_FAILED;
593                         }
594
595                         if (0 != audio_in_set_sound_stream_info(g_audio_h, g_stream_info_h)) {
596                                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to set stream info");
597                         }
598
599                         g_audio_type = type;
600                         g_audio_rate = rate;
601                         g_audio_channel = channel;
602                 }
603
604 #ifdef TV_BT_MODE
605                 if (NULL != g_current_audio_type) {
606                         free(g_current_audio_type);
607                         g_current_audio_type = NULL;
608                 }
609
610                 g_current_audio_type = strdup(audio_type);
611 #endif
612         }
613
614         SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Current audio type is changed : %s", g_current_audio_type);
615
616         return 0;
617 }
618
619 int vcd_recorder_get(char** audio_type)
620 {
621         if (NULL == audio_type) {
622                 return VCD_ERROR_INVALID_PARAMETER;
623         }
624
625         if (NULL != g_current_audio_type) {
626                 *audio_type = strdup(g_current_audio_type);
627         } else {
628                 SLOG(LOG_WARN, TAG_VCD, "[Recorder] Current audio type(%s) is NOT ready", *audio_type);
629                 *audio_type = NULL;
630         }
631
632         return 0;
633 }
634
635 static float get_volume_decibel(char* data, int size)
636 {
637 #define MAX_AMPLITUDE_MEAN_16 32768
638
639         int i, depthByte;
640         int count = 0;
641
642         float db = 0.0;
643         float rms = 0.0;
644         unsigned long long square_sum = 0;
645         short pcm16 = 0;
646
647         depthByte = 2;
648
649         for (i = 0; i < size; i += (depthByte<<1)) {
650                 pcm16 = 0;
651                 memcpy(&pcm16, data + i, sizeof(short));
652                 square_sum += pcm16 * pcm16;
653                 count++;
654         }
655
656         if (0 == count || 0 == square_sum) {
657                 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] No data");
658                 rms = 1.0;
659         } else {
660                 rms = sqrt((float)square_sum/count);
661         }
662
663         db = 20 * log10(rms/MAX_AMPLITUDE_MEAN_16);
664         return db;
665 }
666
667 Eina_Bool __read_normal_func(void *data)
668 {
669         int ret = -1;
670
671         if (VCD_RECORDER_STATE_RECORDING != g_recorder_state) {
672                 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Exit audio reading normal func");
673                 return EINA_FALSE;
674         }
675
676         memset(g_normal_buffer, '\0', BUFFER_LENGTH + 10);
677
678         ret = audio_in_read(g_audio_h, g_normal_buffer, BUFFER_LENGTH);
679         if (0 > ret) {
680                 SLOG(LOG_WARN, TAG_VCD, "[Recorder WARNING] Fail to read audio : %d", ret);
681                 g_recorder_state = VCD_RECORDER_STATE_READY;
682                 return EINA_FALSE;
683         }
684
685         if (NULL != g_audio_cb) {
686                 if (0 != g_audio_cb(g_normal_buffer, BUFFER_LENGTH)) {
687                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to read audio : %d", ret);
688                         vcd_recorder_stop();
689                         return EINA_FALSE;
690                 }
691         }
692
693         /* Set volume */
694         if (0 == g_buffer_count % 30) {
695                 float vol_db = get_volume_decibel(g_normal_buffer, BUFFER_LENGTH);
696                 if (0 != vcdc_send_set_volume(vcd_client_manager_get_pid(), vol_db)) {
697                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder] Fail to send recording volume(%f)", vol_db);
698                 }
699         }
700
701         if (0 == g_buffer_count || 0 == g_buffer_count % 50) {
702                 SLOG(LOG_WARN, TAG_VCD, "[Recorder][%d] Recording... : read_size(%d)", g_buffer_count, ret);
703
704                 if (100000 == g_buffer_count) {
705                         g_buffer_count = 0;
706                 }
707         }
708
709         g_buffer_count++;
710
711 #ifdef BUF_SAVE_MODE
712         /* write pcm buffer */
713         fwrite(g_normal_buffer, 1, BUFFER_LENGTH, g_normal_file);
714 #endif
715
716         return EINA_TRUE;
717 }
718
719 static void __timer_read_normal_func(void *data)
720 {
721         SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] before __read_normal_func");
722         ecore_timer_add(0, __read_normal_func, NULL);
723         return;
724 }
725
726 static void __check_audio_format()
727 {
728         vce_audio_type_e type;
729         int rate;
730         int channel;
731
732         int ret = vcd_engine_get_audio_format(VCE_AUDIO_ID_NONE, &type, &rate, &channel);
733         if (0 != ret) {
734                 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio format : %d", ret);
735                 return;
736         }
737
738         if (false == g_is_valid_audio_in) {
739                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Audio-in is NOT valid");
740                 return;
741         }
742
743         if (g_audio_type != type || g_audio_rate != rate || g_audio_channel != channel) {
744                 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] New audio type(%d) rate(%d) channel(%d)", type, rate, channel);
745                 audio_in_destroy(g_audio_h);
746
747                 audio_channel_e audio_ch;
748                 audio_sample_type_e audio_type;
749
750                 switch (channel) {
751                 case 1: audio_ch = AUDIO_CHANNEL_MONO;          break;
752                 case 2: audio_ch = AUDIO_CHANNEL_STEREO;        break;
753                 default:
754                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Input channel is not supported");
755                         return;
756                         break;
757                 }
758
759                 switch (type) {
760                 case VCE_AUDIO_TYPE_PCM_S16_LE: audio_type = AUDIO_SAMPLE_TYPE_S16_LE;  break;
761                 case VCE_AUDIO_TYPE_PCM_U8:     audio_type = AUDIO_SAMPLE_TYPE_U8;      break;
762                 default:
763                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Invalid Audio Type");
764                         return;
765                         break;
766                 }
767
768                 ret = audio_in_create(rate, audio_ch, audio_type, &g_audio_h);
769                 if (AUDIO_IO_ERROR_NONE != ret) {
770                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to create audio handle : %d", ret);
771                         g_is_valid_audio_in = false;
772                         return;
773                 }
774
775                 if (0 != audio_in_set_sound_stream_info(g_audio_h, g_stream_info_h)) {
776                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to set stream info");
777                 }
778
779                 g_audio_type = type;
780                 g_audio_rate = rate;
781                 g_audio_channel = channel;
782         } else {
783                 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] audio type(%d) rate(%d) channel(%d)", g_audio_type, g_audio_rate, g_audio_channel);
784         }
785 }
786
787
788
789 int vcd_recorder_start()
790 {
791         int ret = -1;
792         g_buffer_count = 0;
793
794         SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Enter, recorder state(%d)", g_recorder_state);
795
796         if (VCD_RECORDER_STATE_RECORDING == g_recorder_state)   return 0;
797
798         bool started = false;
799         SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] audio type : %s", g_current_audio_type);
800
801         if (NULL != g_current_audio_type) {
802                 if (0 == strncmp(VCE_AUDIO_ID_BLUETOOTH, g_current_audio_type, strlen(VCE_AUDIO_ID_BLUETOOTH))) {
803 #ifdef TV_BT_MODE
804                         const unsigned char input_data[2] = {SMART_CONTROL_START_CMD, 0x00};
805                         int bt_retry = 0;
806                         while (5 > bt_retry) {
807                                 ret = bt_hid_send_rc_command(NULL, input_data, sizeof(input_data));
808                                 if (BT_ERROR_NONE == ret) {
809                                         SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Start bt audio recorder");
810                                         started = true;
811                                         break;
812                                 } else if (BT_ERROR_NOW_IN_PROGRESS == ret) {
813                                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail bt_hid_send_rc_command : %d", ret);
814                                         usleep(50000);
815                                         bt_retry++;
816                                 } else {
817                                         break;
818                                 }
819                         }
820                         if (false == started) {
821                                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to start bt audio");
822                                 return VCD_ERROR_OPERATION_FAILED;
823                         }
824
825                         g_bt_extend_count = 0;
826 #endif
827                 } else if (0 == strncmp(VCE_AUDIO_ID_WIFI, g_current_audio_type, strlen(VCE_AUDIO_ID_WIFI))) {
828                         SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] call RegisterMSFAudioCallback() function");
829 #ifdef TV_MSF_WIFI_MODE
830
831                         ret = RegisterMSFAudioCallback(__msf_wifi_audio_data_receive_cb, NULL);
832                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder] ret = %d", ret);
833                         if (MSFResult_OK == ret) {
834                                 started = true;
835                         } else {
836                                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to start Wi-Fi audio");
837                                 return VCD_ERROR_OPERATION_FAILED;
838                         }
839 #endif
840                 }
841         }
842
843         SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] started = %d", started);
844         if (false == started) {
845                 /* check audio format */
846                 __check_audio_format();
847 #if 0
848                 ret = sound_manager_acquire_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_RECORDING, NULL);
849                 if (SOUND_MANAGER_ERROR_NONE != ret) {
850                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to acquire focus : %d", ret);
851                 } else {
852                         ret = audio_in_set_sound_stream_info(g_audio_h, g_stream_info_h);
853                         if (AUDIO_IO_ERROR_NONE != ret) {
854                                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to set stream info : %d", ret);
855                         }
856                 }
857 #endif
858
859                 ret = audio_in_prepare(g_audio_h);
860                 if (AUDIO_IO_ERROR_NONE != ret) {
861                         if (AUDIO_IO_ERROR_SOUND_POLICY == ret) {
862                                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Audio is busy.");
863                                 return VCD_ERROR_RECORDER_BUSY;
864                         } else {
865                                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to start audio : %d", ret);
866                         }
867                         return VCD_ERROR_OPERATION_FAILED;
868                 }
869
870                 /* Add ecore timer to read audio data */
871                 ecore_main_loop_thread_safe_call_async(__timer_read_normal_func, NULL);
872                 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Start audio in recorder");
873         }
874
875         g_recorder_state = VCD_RECORDER_STATE_RECORDING;
876
877 #ifdef BUF_SAVE_MODE
878         char normal_file_name[128] = {'\0',};
879         g_count++;
880
881         while (1) {
882                 snprintf(normal_file_name, sizeof(normal_file_name), "/tmp/vc_normal_%d_%d", getpid(), g_count);
883                 ret = access(normal_file_name, 0);
884
885                 if (0 == ret) {
886                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] File is already exist");
887                         if (0 == remove(normal_file_name)) {
888                                 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Remove file");
889                                 break;
890                         } else {
891                                 g_count++;
892                         }
893                 } else {
894                         break;
895                 }
896         }
897
898         SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] File normal name : %s", normal_file_name);
899
900         /* open test file */
901         g_normal_file = fopen(normal_file_name, "wb+x");
902         if (!g_normal_file) {
903                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] File not found!");
904         }
905 #endif
906         return 0;
907 }
908
909 int vcd_recorder_stop()
910 {
911         int ret = -1;
912
913         if (VCD_RECORDER_STATE_READY == g_recorder_state)
914                 return 0;
915
916         g_recorder_state = VCD_RECORDER_STATE_READY;
917
918 #ifdef BUF_SAVE_MODE
919         fclose(g_normal_file);
920 #endif
921
922         bool stoped = false;
923
924         if (NULL != g_current_audio_type) {
925                 if (0 == strncmp(VCE_AUDIO_ID_BLUETOOTH, g_current_audio_type, strlen(VCE_AUDIO_ID_BLUETOOTH))) {
926 #ifdef TV_BT_MODE
927                         int bt_retry = 0;
928                         while (5 > bt_retry) {
929                                 ret = bt_hid_rc_stop_sending_voice(NULL);
930                                 if (BT_ERROR_NONE == ret) {
931                                         SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Stop bt audio recorder");
932                                         stoped = true;
933                                         break;
934                                 } else if (BT_ERROR_NOW_IN_PROGRESS == ret) {
935                                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail bt_hid_rc_stop_sending_voice()");
936                                         usleep(50000);
937                                         bt_retry++;
938                                 } else {
939                                         break;
940                                 }
941                         }
942                         if (NULL != g_current_audio_type && 
943                                         (!strncmp(g_current_audio_type, VCE_AUDIO_ID_BLUETOOTH, sizeof(VCE_AUDIO_ID_BLUETOOTH)) ||
944                                          !strncmp(g_current_audio_type, VCE_AUDIO_ID_WIFI, sizeof(VCE_AUDIO_ID_WIFI)))) {
945                                 SLOG(LOG_DEBUG, TAG_VCD, "[DEBUG] Recorder reset to NONE");
946                                 vcd_recorder_set(VCE_AUDIO_ID_NONE, g_audio_type, g_audio_rate, g_audio_channel);
947                         }
948
949                         if (false == stoped) {
950                                 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to stop bt audio");
951                                 return VCD_ERROR_OPERATION_FAILED;
952                         }
953 #endif
954                 } else if (0 == strncmp(VCE_AUDIO_ID_WIFI, g_current_audio_type, strlen(VCE_AUDIO_ID_WIFI))) {
955 #ifdef TV_MSF_WIFI_MODE
956                         UnRegisterMSFAudioCallback();
957                         if (NULL != g_current_audio_type && 
958                                         (!strncmp(g_current_audio_type, VCE_AUDIO_ID_BLUETOOTH, sizeof(VCE_AUDIO_ID_BLUETOOTH)) ||
959                                          !strncmp(g_current_audio_type, VCE_AUDIO_ID_WIFI, sizeof(VCE_AUDIO_ID_WIFI)))) {
960                                 SLOG(LOG_DEBUG, TAG_VCD, "[DEBUG] Recorder reset to NONE");
961                                 vcd_recorder_set(VCE_AUDIO_ID_NONE, g_audio_type, g_audio_rate, g_audio_channel);
962                         }
963                         stoped = true;
964 #endif
965                 } else  {
966                         SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] current audio type is NONE");
967                 }
968         }
969
970         if (false == stoped) {
971                 ret = audio_in_unprepare(g_audio_h);
972                 if (AUDIO_IO_ERROR_NONE != ret) {
973                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to stop audio : %d", ret);
974                         return VCD_ERROR_OPERATION_FAILED;
975                 }
976
977 #if 0
978                 ret = sound_manager_release_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_RECORDING, NULL);
979                 if (SOUND_MANAGER_ERROR_NONE != ret) {
980                         SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to release focus : %d", ret);
981                 }
982 #endif
983         }
984
985         return 0;
986 }
987
988 int vcd_recorder_get_state()
989 {
990         return g_recorder_state;
991 }