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