2 * Copyright (c) 2011-2017 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #define TV_MSF_WIFI_MODE
24 #include <sound_manager.h>
25 #include <sound_manager_internal.h>
28 #include <bluetooth_product.h>
30 #ifdef TV_MSF_WIFI_MODE
31 #include <MSFVoiceInterface.h>
35 #include "vcd_client_data.h"
36 #include "vcd_config.h"
38 #include "vcd_engine_agent.h"
39 #include "vcd_recorder.h"
41 #include "voice_control_plugin_engine.h"
43 #define FRAME_LENGTH 160
44 #define BUFFER_LENGTH FRAME_LENGTH * 2
46 #define FOCUS_SERVER_READY "/tmp/.focus_server_ready"
48 #define VCP_AUDIO_ID_NONE "VC_AUDIO_ID_NONE" /**< None audio id */
50 static vcd_recorder_state_e g_recorder_state = VCD_RECORDER_STATE_READY;
52 static vcd_recoder_audio_cb g_audio_cb = NULL;
54 static vcd_recorder_interrupt_cb g_interrupt_cb = NULL;
56 static audio_in_h g_audio_h;
58 static sound_stream_info_h g_stream_info_h;
60 static vcp_audio_type_e g_audio_type;
62 static int g_audio_rate;
64 static int g_audio_channel;
66 static char g_normal_buffer[BUFFER_LENGTH + 10];
68 static bool g_is_valid_audio_in = false;
70 static bool g_is_valid_bt_in = false;
72 static char* g_current_audio_type = NULL;
74 static int g_buffer_count;
76 static int g_device_id = -1;
84 static FILE* g_normal_file;
86 static int g_count = 1;
89 static float get_volume_decibel(char* data, int size);
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)
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);
99 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] current audio type is (%s)", g_current_audio_type);
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();
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");
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);
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);
127 if (100000 == g_buffer_count)
134 /* write pcm buffer */
135 fwrite(voice_data->audio_buf, 1, voice_data->length, g_normal_file);
144 static int g_bt_extend_count;
146 #define SMART_CONTROL_EXTEND_CMD 0x03
147 #define SMART_CONTROL_START_CMD 0x04
149 static void _bt_cb_hid_state_changed(int result, bool connected, const char *remote_address, void *user_data)
151 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Bluetooth Event [%d] Received address [%s]", result, remote_address);
155 static void _bt_hid_audio_data_receive_cb(bt_hid_voice_data_s *voice_data, void *user_data)
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);
162 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] current audio type is (%s)", g_current_audio_type);
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();
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");
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);
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);
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");
195 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Extend bt audio recorder");
200 if (100000 == g_buffer_count) {
208 /* write pcm buffer */
209 fwrite(voice_data->audio_buf, 1, voice_data->length, g_normal_file);
217 static const char* __get_focus_changed_reason_code(sound_stream_focus_change_reason_e 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";
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)
238 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Focus state changed cb");
240 if (stream_info != g_stream_info_h) {
241 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Invalid stream info handle");
245 SLOG(LOG_WARN, TAG_VCD, "[Recorder] focus state chagned to (%d) with reason (%s)", (int)focus_state, __get_focus_changed_reason_code(reason));
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) {
256 static int __apply_device_for_stream_routing()
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;
263 if (0 != sound_manager_get_device_list(SOUND_DEVICE_IO_DIRECTION_IN_MASK, &device_list)) {
264 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to get current device list");
269 while (0 == 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");
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");
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");
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");
292 sound_manager_free_device_list(device_list);
296 SLOG(LOG_WARN, TAG_VCD, "[Recorder] No device");
298 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Apply device for stream routing");
303 static void __device_connection_changed_cb(sound_device_h device, bool is_connected, void *user_data)
305 sound_device_type_e type;
307 if (0 != sound_manager_get_device_type(device, &type)) {
308 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to get device tyep");
311 if (type == SOUND_DEVICE_USB_AUDIO) {
312 bool is_recording_state = false;
313 if (VCD_RECORDER_STATE_RECORDING == g_recorder_state) {
314 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Stop recorder");
316 is_recording_state = true;
318 if (0 != sound_manager_remove_device_for_stream_routing(g_stream_info_h, device))
319 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to remove device");
321 if (0 != sound_manager_add_device_for_stream_routing(g_stream_info_h, device)) {
322 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to add device");
325 if (0 != sound_manager_apply_stream_routing(g_stream_info_h)) {
326 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR} Fail to apply stream routing");
329 if (true == is_recording_state) {
330 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Start recorder");
331 vcd_recorder_start();
333 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Apply device for stream routing");
339 int vcd_recorder_create(vcd_recoder_audio_cb audio_cb, vcd_recorder_interrupt_cb interrupt_cb)
341 if (NULL == audio_cb || NULL == interrupt_cb) {
342 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Input param is NOT valid");
343 return VCD_ERROR_INVALID_PARAMETER;
346 /* check focus server */
349 if (0 == access(FOCUS_SERVER_READY, F_OK)) {
350 SLOG(LOG_ERROR, TAG_VCD, "[Recorder SUCCESS] focus server is available");
354 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] focus server is not available");
362 g_is_valid_audio_in = false;
363 g_is_valid_bt_in = false;
364 g_current_audio_type = NULL;
366 g_audio_type = VCP_AUDIO_TYPE_PCM_S16_LE;
367 g_audio_rate = 16000;
370 audio_channel_e audio_ch;
371 audio_sample_type_e audio_type;
373 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] AUdio type(%d) rate(%d) channel(%d)", g_audio_type, g_audio_rate, g_audio_channel);
375 switch (g_audio_channel) {
376 case 1: audio_ch = AUDIO_CHANNEL_MONO; break;
377 case 2: audio_ch = AUDIO_CHANNEL_STEREO; break;
379 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Input channel is not supported");
380 return VCD_ERROR_OPERATION_FAILED;
384 switch (g_audio_type) {
385 case VCP_AUDIO_TYPE_PCM_S16_LE: audio_type = AUDIO_SAMPLE_TYPE_S16_LE; break;
386 case VCP_AUDIO_TYPE_PCM_U8: audio_type = AUDIO_SAMPLE_TYPE_U8; break;
388 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Invalid Audio Type");
389 return VCD_ERROR_OPERATION_FAILED;
393 ret = audio_in_create(g_audio_rate, audio_ch, audio_type, &g_audio_h);
394 if (AUDIO_IO_ERROR_NONE == ret) {
395 g_is_valid_audio_in = true;
397 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Rate(%d) Channel(%d) Type(%d)", g_audio_rate, audio_ch, audio_type);
398 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to create audio handle : %d", ret);
399 g_is_valid_audio_in = false;
402 ret = sound_manager_add_device_connection_changed_cb(SOUND_DEVICE_IO_DIRECTION_IN_MASK, __device_connection_changed_cb, NULL, &g_device_id);
404 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to add device connection changed callback");
406 if (0 != sound_manager_create_stream_information_internal(SOUND_STREAM_TYPE_VOICE_RECOGNITION_SERVICE, __recorder_focus_state_cb, NULL, &g_stream_info_h)) {
407 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to create stream info");
409 __apply_device_for_stream_routing();
412 if (0 != audio_in_set_sound_stream_info(g_audio_h, g_stream_info_h)) {
413 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to set stream info");
416 g_audio_cb = audio_cb;
417 g_interrupt_cb = interrupt_cb;
418 g_recorder_state = VCD_RECORDER_STATE_READY;
422 bool is_bt_failed = false;
424 if (false == is_bt_failed && BT_ERROR_NONE != bt_product_init()) {
425 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to init bt");
429 if (false == is_bt_failed && BT_ERROR_NONE != bt_hid_host_initialize(_bt_cb_hid_state_changed, NULL)) {
430 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail bt_hid_host_initialize()");
434 if (false == is_bt_failed && BT_ERROR_NONE != bt_hid_set_audio_data_receive_cb(_bt_hid_audio_data_receive_cb, NULL)) {
435 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail bt_hid_set_audio_data_receive_cb()");
440 if (false == is_bt_failed) {
441 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Bluetooth is available");
442 g_is_valid_bt_in = true;
445 /* Select default audio type */
446 if (true == g_is_valid_audio_in) {
447 g_current_audio_type = strdup(VCP_AUDIO_ID_NONE);
449 if (true == g_is_valid_bt_in) {
450 g_current_audio_type = strdup(VCP_AUDIO_ID_BLUETOOTH);
452 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] No valid audio");
457 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Audio type : %s", g_current_audio_type);
462 int vcd_recorder_destroy()
464 if (VCD_RECORDER_STATE_RECORDING == g_recorder_state) {
465 if (0 == strncmp(VCP_AUDIO_ID_BLUETOOTH, g_current_audio_type, strlen(VCP_AUDIO_ID_BLUETOOTH))) {
467 bt_hid_unset_audio_data_receive_cb();
469 } else if (0 == strncmp(VCP_AUDIO_ID_MSF, g_current_audio_type, strlen(VCP_AUDIO_ID_MSF))) {
470 #ifdef TV_MSF_WIFI_MODE
471 UnRegisterMSFAudioCallback();
474 audio_in_unprepare(g_audio_h);
476 g_recorder_state = VCD_RECORDER_STATE_READY;
479 int ret = sound_manager_remove_device_connection_changed_cb(g_device_id);
481 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to remove device connection changed callback, ret(%d)", ret);
483 ret = sound_manager_destroy_stream_information(g_stream_info_h);
485 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to destroy stream info, ret(%d)", ret);
487 audio_in_destroy(g_audio_h);
490 bt_hid_unset_audio_data_receive_cb();
492 bt_hid_host_deinitialize();
499 if (NULL != g_current_audio_type) {
500 free(g_current_audio_type);
501 g_current_audio_type = NULL;
507 int vcd_recorder_set(const char* audio_type, vcp_audio_type_e type, int rate, int channel)
509 if (NULL == audio_type) {
510 return VCD_ERROR_INVALID_PARAMETER;
513 if (NULL != g_current_audio_type) {
514 if (0 == strncmp(g_current_audio_type, audio_type, strlen(g_current_audio_type))) {
515 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Current audio type is already set : %s", audio_type);
519 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Recorder is NOT created");
520 return VCD_ERROR_INVALID_STATE;
523 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] set audio type (%s)", audio_type);
524 vcd_engine_set_audio_type(audio_type);
526 if (VCD_RECORDER_STATE_READY != g_recorder_state) {
527 if ((!strncmp(g_current_audio_type, VCP_AUDIO_ID_NONE, strlen(g_current_audio_type)) &&
528 strncmp(audio_type, VCP_AUDIO_ID_BLUETOOTH, strlen(audio_type)) &&
529 strncmp(audio_type, VCP_AUDIO_ID_MSF, strlen(audio_type))) ||
530 (strncmp(g_current_audio_type, VCP_AUDIO_ID_BLUETOOTH, strlen(g_current_audio_type)) &&
531 strncmp(g_current_audio_type, VCP_AUDIO_ID_MSF, strlen(g_current_audio_type)) &&
532 strncmp(g_current_audio_type, VCP_AUDIO_ID_NONE, strlen(g_current_audio_type)) &&
533 !strncmp(audio_type, VCP_AUDIO_ID_NONE, strlen(audio_type)))) {
534 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Skip stop recording while Recorder is NOT ready");
536 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Recorder is NOT ready");
538 //return VCD_ERROR_INVALID_STATE;
544 if (0 == strncmp(VCP_AUDIO_ID_BLUETOOTH, audio_type, strlen(VCP_AUDIO_ID_BLUETOOTH))) {
545 if (false == g_is_valid_bt_in) {
546 SLOG(LOG_ERROR, TAG_VCD, "[Recorder] BT audio is NOT valid");
547 return VCD_ERROR_OPERATION_REJECTED;
550 if (NULL != g_current_audio_type) {
551 free(g_current_audio_type);
552 g_current_audio_type = NULL;
555 g_current_audio_type = strdup(audio_type);
556 } else if (0 == strncmp(VCP_AUDIO_ID_MSF, audio_type, strlen(VCP_AUDIO_ID_MSF))) {
557 if (NULL != g_current_audio_type) {
558 free(g_current_audio_type);
559 g_current_audio_type = NULL;
562 g_current_audio_type = strdup(audio_type);
564 if (false == g_is_valid_audio_in) {
565 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Audio-in is NOT valid");
566 return VCD_ERROR_OPERATION_REJECTED;
569 if (g_audio_type != type || g_audio_rate != rate || g_audio_channel != channel) {
570 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] New audio type(%d) rate(%d) channel(%d)", type, rate, channel);
571 audio_in_destroy(g_audio_h);
573 audio_channel_e audio_ch;
574 audio_sample_type_e audio_type;
577 case 1: audio_ch = AUDIO_CHANNEL_MONO; break;
578 case 2: audio_ch = AUDIO_CHANNEL_STEREO; break;
580 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Input channel is not supported");
581 return VCD_ERROR_OPERATION_FAILED;
586 case VCP_AUDIO_TYPE_PCM_S16_LE: audio_type = AUDIO_SAMPLE_TYPE_S16_LE; break;
587 case VCP_AUDIO_TYPE_PCM_U8: audio_type = AUDIO_SAMPLE_TYPE_U8; break;
589 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Invalid Audio Type");
590 return VCD_ERROR_OPERATION_FAILED;
594 ret = audio_in_create(rate, audio_ch, audio_type, &g_audio_h);
595 if (AUDIO_IO_ERROR_NONE != ret) {
596 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to create audio handle : %d", ret);
597 g_is_valid_audio_in = false;
598 return VCD_ERROR_OPERATION_FAILED;
601 if (0 != audio_in_set_sound_stream_info(g_audio_h, g_stream_info_h)) {
602 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to set stream info");
607 g_audio_channel = channel;
611 if (NULL != g_current_audio_type) {
612 free(g_current_audio_type);
613 g_current_audio_type = NULL;
616 g_current_audio_type = strdup(audio_type);
620 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Current audio type is changed : %s", g_current_audio_type);
625 int vcd_recorder_get(char** audio_type)
627 if (NULL == audio_type) {
628 return VCD_ERROR_INVALID_PARAMETER;
631 if (NULL != g_current_audio_type) {
632 *audio_type = strdup(g_current_audio_type);
634 SLOG(LOG_WARN, TAG_VCD, "[Recorder] Current audio type(%s) is NOT ready", *audio_type);
641 static float get_volume_decibel(char* data, int size)
643 #define MAX_AMPLITUDE_MEAN_16 32768
650 unsigned long long square_sum = 0;
655 for (i = 0; i < size; i += (depthByte<<1)) {
657 memcpy(&pcm16, data + i, sizeof(short));
658 square_sum += pcm16 * pcm16;
662 if (0 == count || 0 == square_sum) {
663 SLOG(LOG_ERROR, TAG_VCD, "[ERROR] No data");
666 rms = sqrt((float)square_sum/count);
669 db = 20 * log10(rms/MAX_AMPLITUDE_MEAN_16);
673 Eina_Bool __read_normal_func(void *data)
677 if (VCD_RECORDER_STATE_RECORDING != g_recorder_state) {
678 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Exit audio reading normal func");
682 memset(g_normal_buffer, '\0', BUFFER_LENGTH + 10);
684 ret = audio_in_read(g_audio_h, g_normal_buffer, BUFFER_LENGTH);
686 SLOG(LOG_WARN, TAG_VCD, "[Recorder WARNING] Fail to read audio : %d", ret);
687 g_recorder_state = VCD_RECORDER_STATE_READY;
691 if (NULL != g_audio_cb) {
692 if (0 != g_audio_cb(g_normal_buffer, BUFFER_LENGTH)) {
693 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to read audio : %d", ret);
700 if (0 == g_buffer_count % 30) {
701 float vol_db = get_volume_decibel(g_normal_buffer, BUFFER_LENGTH);
702 if (0 != vcdc_send_set_volume(vcd_client_manager_get_pid(), vol_db)) {
703 SLOG(LOG_ERROR, TAG_VCD, "[Recorder] Fail to send recording volume(%f)", vol_db);
707 if (0 == g_buffer_count || 0 == g_buffer_count % 50) {
708 SLOG(LOG_WARN, TAG_VCD, "[Recorder][%d] Recording... : read_size(%d)", g_buffer_count, ret);
710 if (100000 == g_buffer_count) {
718 /* write pcm buffer */
719 fwrite(g_normal_buffer, 1, BUFFER_LENGTH, g_normal_file);
725 static void __timer_read_normal_func(void *data)
727 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] before __read_normal_func");
728 ecore_timer_add(0, __read_normal_func, NULL);
732 static void __check_audio_format()
734 vcp_audio_type_e type;
738 int ret = vcd_engine_get_audio_format(VCP_AUDIO_ID_NONE, &type, &rate, &channel);
740 SLOG(LOG_ERROR, TAG_VCD, "[Server ERROR] Fail to get audio format : %d", ret);
744 if (false == g_is_valid_audio_in) {
745 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Audio-in is NOT valid");
749 if (g_audio_type != type || g_audio_rate != rate || g_audio_channel != channel) {
750 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] New audio type(%d) rate(%d) channel(%d)", type, rate, channel);
751 audio_in_destroy(g_audio_h);
753 audio_channel_e audio_ch;
754 audio_sample_type_e audio_type;
757 case 1: audio_ch = AUDIO_CHANNEL_MONO; break;
758 case 2: audio_ch = AUDIO_CHANNEL_STEREO; break;
760 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Input channel is not supported");
766 case VCP_AUDIO_TYPE_PCM_S16_LE: audio_type = AUDIO_SAMPLE_TYPE_S16_LE; break;
767 case VCP_AUDIO_TYPE_PCM_U8: audio_type = AUDIO_SAMPLE_TYPE_U8; break;
769 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Invalid Audio Type");
774 ret = audio_in_create(rate, audio_ch, audio_type, &g_audio_h);
775 if (AUDIO_IO_ERROR_NONE != ret) {
776 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to create audio handle : %d", ret);
777 g_is_valid_audio_in = false;
781 if (0 != audio_in_set_sound_stream_info(g_audio_h, g_stream_info_h)) {
782 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to set stream info");
787 g_audio_channel = channel;
789 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] audio type(%d) rate(%d) channel(%d)", g_audio_type, g_audio_rate, g_audio_channel);
795 int vcd_recorder_start()
800 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Enter, recorder state(%d)", g_recorder_state);
802 if (VCD_RECORDER_STATE_RECORDING == g_recorder_state) return 0;
804 bool started = false;
805 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] audio type : %s", g_current_audio_type);
807 if (NULL != g_current_audio_type) {
808 if (0 == strncmp(VCP_AUDIO_ID_BLUETOOTH, g_current_audio_type, strlen(VCP_AUDIO_ID_BLUETOOTH))) {
810 const unsigned char input_data[2] = {SMART_CONTROL_START_CMD, 0x00};
812 while (5 > bt_retry) {
813 ret = bt_hid_send_rc_command(NULL, input_data, sizeof(input_data));
814 if (BT_ERROR_NONE == ret) {
815 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Start bt audio recorder");
818 } else if (BT_ERROR_NOW_IN_PROGRESS == ret) {
819 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail bt_hid_send_rc_command : %d", ret);
826 if (false == started) {
827 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to start bt audio");
828 return VCD_ERROR_OPERATION_FAILED;
831 g_bt_extend_count = 0;
833 } else if (0 == strncmp(VCP_AUDIO_ID_MSF, g_current_audio_type, strlen(VCP_AUDIO_ID_MSF))) {
834 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] call RegisterMSFAudioCallback() function");
835 #ifdef TV_MSF_WIFI_MODE
837 ret = RegisterMSFAudioCallback(__msf_wifi_audio_data_receive_cb, NULL);
838 SLOG(LOG_ERROR, TAG_VCD, "[Recorder] ret = %d", ret);
839 if (MSFResult_OK == ret) {
842 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to start MSF(wifi) audio");
843 return VCD_ERROR_OPERATION_FAILED;
849 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] started = %d", started);
850 if (false == started) {
851 /* check audio format */
852 __check_audio_format();
854 ret = sound_manager_acquire_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_RECORDING, NULL);
855 if (SOUND_MANAGER_ERROR_NONE != ret) {
856 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to acquire focus : %d", ret);
858 ret = audio_in_set_sound_stream_info(g_audio_h, g_stream_info_h);
859 if (AUDIO_IO_ERROR_NONE != ret) {
860 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to set stream info : %d", ret);
865 ret = audio_in_prepare(g_audio_h);
866 if (AUDIO_IO_ERROR_NONE != ret) {
867 if (AUDIO_IO_ERROR_SOUND_POLICY == ret) {
868 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Audio is busy.");
869 return VCD_ERROR_RECORDER_BUSY;
871 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to start audio : %d", ret);
873 return VCD_ERROR_OPERATION_FAILED;
876 /* Add ecore timer to read audio data */
877 ecore_main_loop_thread_safe_call_async(__timer_read_normal_func, NULL);
878 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Start audio in recorder");
881 g_recorder_state = VCD_RECORDER_STATE_RECORDING;
884 char normal_file_name[128] = {'\0',};
888 snprintf(normal_file_name, sizeof(normal_file_name), "/tmp/vc_normal_%d_%d", getpid(), g_count);
889 ret = access(normal_file_name, 0);
892 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] File is already exist");
893 if (0 == remove(normal_file_name)) {
894 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Remove file");
904 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] File normal name : %s", normal_file_name);
907 g_normal_file = fopen(normal_file_name, "wb+x");
908 if (!g_normal_file) {
909 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] File not found!");
915 int vcd_recorder_stop()
919 if (VCD_RECORDER_STATE_READY == g_recorder_state)
922 g_recorder_state = VCD_RECORDER_STATE_READY;
925 fclose(g_normal_file);
930 if (NULL != g_current_audio_type) {
931 if (0 == strncmp(VCP_AUDIO_ID_BLUETOOTH, g_current_audio_type, strlen(VCP_AUDIO_ID_BLUETOOTH))) {
934 while (5 > bt_retry) {
935 ret = bt_hid_rc_stop_sending_voice(NULL);
936 if (BT_ERROR_NONE == ret) {
937 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] Stop bt audio recorder");
940 } else if (BT_ERROR_NOW_IN_PROGRESS == ret) {
941 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail bt_hid_rc_stop_sending_voice()");
948 if (NULL != g_current_audio_type &&
949 (!strncmp(g_current_audio_type, VCP_AUDIO_ID_BLUETOOTH, sizeof(VCP_AUDIO_ID_BLUETOOTH)) ||
950 !strncmp(g_current_audio_type, VCP_AUDIO_ID_MSF, sizeof(VCP_AUDIO_ID_MSF)))) {
951 SLOG(LOG_DEBUG, TAG_VCD, "[DEBUG] Recorder reset to NONE");
952 vcd_recorder_set(VCP_AUDIO_ID_NONE, g_audio_type, g_audio_rate, g_audio_channel);
955 if (false == stoped) {
956 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to stop bt audio");
957 return VCD_ERROR_OPERATION_FAILED;
960 } else if (0 == strncmp(VCP_AUDIO_ID_MSF, g_current_audio_type, strlen(VCP_AUDIO_ID_MSF))) {
961 #ifdef TV_MSF_WIFI_MODE
962 UnRegisterMSFAudioCallback();
963 if (NULL != g_current_audio_type &&
964 (!strncmp(g_current_audio_type, VCP_AUDIO_ID_BLUETOOTH, sizeof(VCP_AUDIO_ID_BLUETOOTH)) ||
965 !strncmp(g_current_audio_type, VCP_AUDIO_ID_MSF, sizeof(VCP_AUDIO_ID_MSF)))) {
966 SLOG(LOG_DEBUG, TAG_VCD, "[DEBUG] Recorder reset to NONE");
967 vcd_recorder_set(VCP_AUDIO_ID_NONE, g_audio_type, g_audio_rate, g_audio_channel);
972 SLOG(LOG_DEBUG, TAG_VCD, "[Recorder] current audio type is NONE");
976 if (false == stoped) {
977 ret = audio_in_unprepare(g_audio_h);
978 if (AUDIO_IO_ERROR_NONE != ret) {
979 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to stop audio : %d", ret);
980 return VCD_ERROR_OPERATION_FAILED;
984 ret = sound_manager_release_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_RECORDING, NULL);
985 if (SOUND_MANAGER_ERROR_NONE != ret) {
986 SLOG(LOG_ERROR, TAG_VCD, "[Recorder ERROR] Fail to release focus : %d", ret);
994 int vcd_recorder_get_state()
996 return g_recorder_state;