2 * Copyright (c) 2011-2016 Samsung Electronics Co., Ltd All Rights Reserved
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 * http://www.apache.org/licenses/LICENSE-2.0
7 * Unless required by applicable law or agreed to in writing, software
8 * distributed under the License is distributed on an "AS IS" BASIS,
9 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 * See the License for the specific language governing permissions and
11 * limitations under the License.
22 #include <sound_manager.h>
24 #include <bluetooth.h>
28 #include "sttd_dbus.h"
29 #include "sttd_recorder.h"
30 #include "sttd_main.h"
33 #define FRAME_LENGTH 160
34 #define BUFFER_LENGTH FRAME_LENGTH * 2
36 static pthread_mutex_t sttd_audio_in_handle_mutex = PTHREAD_MUTEX_INITIALIZER;
39 STTD_RECORDER_STATE_NONE = -1,
40 STTD_RECORDER_STATE_READY = 0, /**< Recorder is ready to start */
41 STTD_RECORDER_STATE_RECORDING /**< In the middle of recording */
42 } sttd_recorder_state;
47 stte_audio_type_e audio_type;
50 static stt_recorder_s* g_recorder = NULL;
52 static stt_recorder_audio_cb g_audio_cb;
54 static sound_stream_info_h g_stream_info_h;
56 static stt_recorder_interrupt_cb g_interrupt_cb;
58 static sttd_recorder_state g_recorder_state = STTD_RECORDER_STATE_NONE;
60 static int g_buffer_count;
62 /* Sound buf save for test */
67 static char g_temp_file_name[128] = {'\0',};
71 static int g_count = 1;
75 static float get_volume_decibel(char* data, int size, stte_audio_type_e type);
77 //static stt_recorder_s* __get_recorder(int engine_id);
79 static int g_bt_extend_count;
81 #define SMART_CONTROL_EXTEND_CMD 0x03
82 #define SMART_CONTROL_START_CMD 0x04
84 static void _bt_cb_hid_state_changed(int result, bool connected, const char *remote_address, void *user_data)
86 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Bluetooth Event [%d] Received address [%s]", result, remote_address);
89 static void _bt_hid_audio_data_receive_cb(bt_hid_voice_data_s *voice_data, void *user_data)
91 if (STTD_RECORDER_STATE_RECORDING != g_recorder_state) {
92 /* SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Exit audio reading normal func"); */
96 if (NULL != g_audio_cb) {
97 if (0 != g_audio_cb((void*)voice_data->audio_buf, (unsigned int)voice_data->length)) {
98 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to read audio");
102 if (NULL == g_recorder) {
106 float vol_db = get_volume_decibel((char*)voice_data->audio_buf, (int)voice_data->length, g_recorder->audio_type);
107 if (0 != sttdc_send_set_volume(g_recorder->uid, vol_db)) {
108 SLOG(LOG_ERROR, TAG_STTD, "[Recorder] Fail to send recording volume(%f)", vol_db);
112 if (0 == g_buffer_count || 0 == g_buffer_count % 50) {
113 SLOG(LOG_WARN, TAG_STTD, "[Recorder][%d] Recording... : read_size(%d)", g_buffer_count, voice_data->length);
115 if (0 == g_bt_extend_count % 5 && 0 != g_buffer_count) {
116 const unsigned char input_data[2] = {SMART_CONTROL_EXTEND_CMD, 0x10 };
117 if (BT_ERROR_NONE != bt_hid_send_rc_command(NULL, input_data, sizeof(input_data))) {
118 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail bt_hid_send_rc_command(NULL, %s, %d)", input_data, sizeof(input_data));
120 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Extend bt audio recorder");
125 if (100000 == g_buffer_count) {
133 /* write pcm buffer */
134 fwrite(data, 1, len, g_pFile);
141 const char* __stt_get_focus_changed_reason_code(sound_stream_focus_change_reason_e reason)
144 case SOUND_STREAM_FOCUS_CHANGED_BY_MEDIA: return "SOUND_STREAM_FOCUS_CHANGED_BY_MEDIA";
145 case SOUND_STREAM_FOCUS_CHANGED_BY_SYSTEM: return "SOUND_STREAM_FOCUS_CHANGED_BY_SYSTEM";
146 case SOUND_STREAM_FOCUS_CHANGED_BY_ALARM: return "SOUND_STREAM_FOCUS_CHANGED_BY_ALARM";
147 case SOUND_STREAM_FOCUS_CHANGED_BY_NOTIFICATION: return "SOUND_STREAM_FOCUS_CHANGED_BY_NOTIFICATION";
148 case SOUND_STREAM_FOCUS_CHANGED_BY_EMERGENCY: return "SOUND_STREAM_FOCUS_CHANGED_BY_EMERGENCY";
149 case SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_INFORMATION: return "SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_INFORMATION";
150 case SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_RECOGNITION: return "SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_RECOGNITION";
151 case SOUND_STREAM_FOCUS_CHANGED_BY_RINGTONE: return "SOUND_STREAM_FOCUS_CHANGED_BY_RINGTONE";
152 case SOUND_STREAM_FOCUS_CHANGED_BY_VOIP: return "SOUND_STREAM_FOCUS_CHANGED_BY_VOIP";
153 case SOUND_STREAM_FOCUS_CHANGED_BY_CALL: return "SOUND_STREAM_FOCUS_CHANGED_BY_CALL";
154 case SOUND_STREAM_FOCUS_CHANGED_BY_MEDIA_EXTERNAL_ONLY: return "SOUND_STREAM_FOCUS_CHANGED_BY_MEDIA_EXTERNAL_ONLY";
155 default: return "Undefined reason code";
159 void __recorder_focus_state_cb(sound_stream_info_h stream_info, sound_stream_focus_change_reason_e reason, const char *extra_info, void *user_data)
161 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Focus state changed cb");
163 if (stream_info != g_stream_info_h) {
164 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Invalid stream info handle");
169 sound_stream_focus_state_e state_for_recording;
170 ret = sound_manager_get_focus_state(g_stream_info_h, NULL, &state_for_recording);
171 if (SOUND_MANAGER_ERROR_NONE != ret) {
172 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to get focus state");
176 SLOG(LOG_WARN, TAG_STTD, "[Recorder] focus state changed to (%d) with reason(%s)", (int)state_for_recording, __stt_get_focus_changed_reason_code(reason));
178 if (STTD_RECORDER_STATE_RECORDING == g_recorder_state && SOUND_STREAM_FOCUS_STATE_RELEASED == state_for_recording) {
179 SLOG(LOG_WARN, TAG_STTD, "[Recorder] Focus released as interrupt");
180 if (NULL != g_interrupt_cb) {
187 int sttd_recorder_initialize(stt_recorder_audio_cb audio_cb, stt_recorder_interrupt_cb interrupt_cb)
189 if (NULL == audio_cb || NULL == interrupt_cb) {
190 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Input param is NOT valid");
191 return STTD_ERROR_INVALID_PARAMETER;
194 if (STTD_RECORDER_STATE_NONE != g_recorder_state) {
195 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Current state of recorder is recording");
196 return STTD_ERROR_INVALID_STATE;
199 if (0 != pthread_mutex_init(&sttd_audio_in_handle_mutex, NULL)) {
200 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to initialize audio in handle mutex.");
203 g_audio_cb = audio_cb;
204 g_interrupt_cb = interrupt_cb;
205 g_recorder_state = STTD_RECORDER_STATE_NONE;
208 if (0 != sound_manager_create_stream_information(SOUND_STREAM_TYPE_VOICE_RECOGNITION, __recorder_focus_state_cb, NULL, &g_stream_info_h)) {
209 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to create stream info");
213 if (BT_ERROR_NONE != bt_initialize()) {
214 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to init bt");
215 return STTD_ERROR_OPERATION_FAILED;
218 if (BT_ERROR_NONE != bt_hid_host_initialize(_bt_cb_hid_state_changed, NULL)) {
219 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail bt_hid_host_initialize()");
220 return STTD_ERROR_OPERATION_FAILED;
227 int sttd_recorder_deinitialize()
229 if (0 != pthread_mutex_destroy(&sttd_audio_in_handle_mutex)) {
230 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy audio in handle mutex.");
233 if (0 != sound_manager_destroy_stream_information(g_stream_info_h)) {
234 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to destroy stream info");
237 if (NULL != g_recorder) {
238 audio_in_destroy(g_recorder->audio_h);
239 g_recorder->audio_h = NULL;
244 #ifdef __UNUSED_CODES__
245 /* Remove all recorder */
247 stt_recorder_s *recorder = NULL;
249 iter = g_slist_nth(g_recorder_list, 0);
251 while (NULL != iter) {
252 recorder = iter->data;
254 if (NULL != recorder) {
255 g_recorder_list = g_slist_remove(g_recorder_list, recorder);
256 if (recorder->audio_h) {
257 audio_in_destroy(recorder->audio_h);
258 recorder->audio_h = NULL;
263 iter = g_slist_nth(g_recorder_list, 0);
268 bt_hid_host_deinitialize();
273 g_recorder_state = STTD_RECORDER_STATE_NONE;
278 int sttd_recorder_set_audio_session()
283 int sttd_recorder_unset_audio_session()
288 int sttd_recorder_create(stte_audio_type_e type, int channel, unsigned int sample_rate)
290 /* Check engine id is valid */
291 if (NULL != g_recorder) {
292 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is already registered");
293 return STTD_ERROR_INVALID_PARAMETER;
296 audio_in_h temp_in_h = NULL;
299 audio_channel_e audio_ch;
300 audio_sample_type_e audio_type;
303 case 1: audio_ch = AUDIO_CHANNEL_MONO; break;
304 case 2: audio_ch = AUDIO_CHANNEL_STEREO; break;
306 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Input channel is not supported");
307 return STTD_ERROR_OPERATION_FAILED;
312 case STTE_AUDIO_TYPE_PCM_S16_LE: audio_type = AUDIO_SAMPLE_TYPE_S16_LE; break;
313 case STTE_AUDIO_TYPE_PCM_U8: audio_type = AUDIO_SAMPLE_TYPE_U8; break;
315 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Invalid Audio Type");
316 return STTD_ERROR_OPERATION_FAILED;
321 ret = audio_in_create(sample_rate, audio_ch, audio_type, &temp_in_h);
322 if (AUDIO_IO_ERROR_NONE != ret) {
323 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to create audio handle : %d", ret);
324 return STTD_ERROR_OPERATION_FAILED;
327 if (BT_ERROR_NONE != bt_hid_set_audio_data_receive_cb(_bt_hid_audio_data_receive_cb, NULL)) {
328 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail bt_hid_set_audio_data_receive_cb()");
329 return STTD_ERROR_OPERATION_FAILED;
333 stt_recorder_s* recorder;
334 recorder = (stt_recorder_s*)calloc(1, sizeof(stt_recorder_s));
335 if (NULL == recorder) {
338 audio_in_destroy(temp_in_h);
342 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to allocate memory");
343 return STTD_ERROR_OUT_OF_MEMORY;
347 recorder->audio_h = temp_in_h;
348 recorder->audio_type = type;
350 g_recorder = recorder;
352 g_recorder_state = STTD_RECORDER_STATE_READY;
357 int sttd_recorder_destroy()
359 // critical section required because this function can be called from stt engine thread context
360 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Enter critical section");
361 pthread_mutex_lock(&sttd_audio_in_handle_mutex);
363 /* Check engine id is valid */
364 if (NULL == g_recorder) {
365 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
366 pthread_mutex_unlock(&sttd_audio_in_handle_mutex);
367 return STTD_ERROR_INVALID_PARAMETER;
372 if (STTD_RECORDER_STATE_RECORDING == g_recorder_state) {
373 if (g_recorder->audio_h) {
374 ret = audio_in_unprepare(g_recorder->audio_h);
375 if (AUDIO_IO_ERROR_NONE != ret) {
376 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to unprepare audioin : %d", ret);
380 g_recorder_state = STTD_RECORDER_STATE_READY;
383 if (g_recorder->audio_h) {
384 ret = audio_in_destroy(g_recorder->audio_h);
385 if (AUDIO_IO_ERROR_NONE != ret) {
386 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to destroy audioin : %d", ret);
388 g_recorder->audio_h = NULL;
392 if (STTD_RECORDER_STATE_RECORDING == g_recorder_state) {
393 g_recorder_state = STTD_RECORDER_STATE_READY;
396 bt_hid_unset_audio_data_receive_cb();
402 pthread_mutex_unlock(&sttd_audio_in_handle_mutex);
403 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Leave critical section");
408 static float get_volume_decibel(char* data, int size, stte_audio_type_e type)
410 #define MAX_AMPLITUDE_MEAN_16 32768
411 #define MAX_AMPLITUDE_MEAN_08 128
418 unsigned long long square_sum = 0;
420 if (type == STTE_AUDIO_TYPE_PCM_S16_LE)
425 for (i = 0; i < size; i += (depthByte<<1)) {
426 if (depthByte == 2) {
428 memcpy(&pcm16, data + i, sizeof(short));
429 square_sum += pcm16 * pcm16;
432 memcpy(&pcm8, data + i, sizeof(char));
433 square_sum += pcm8 * pcm8;
441 rms = sqrt(square_sum/count);
444 db = 20 * log10(rms/MAX_AMPLITUDE_MEAN_16);
446 db = 20 * log10(rms/MAX_AMPLITUDE_MEAN_08);
452 Eina_Bool __read_audio_func(void *data)
455 static char g_buffer[BUFFER_LENGTH];
457 /* Check engine id is valid */
458 if (NULL == g_recorder) {
462 if (STTD_RECORDER_STATE_READY == g_recorder_state) {
463 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Exit audio reading func");
467 read_byte = audio_in_read(g_recorder->audio_h, g_buffer, BUFFER_LENGTH);
469 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Fail to read audio : %d", read_byte);
470 g_recorder_state = STTD_RECORDER_STATE_READY;
474 if (0 != g_audio_cb(g_buffer, read_byte)) {
475 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Fail audio callback");
476 sttd_recorder_stop();
480 if (0 == g_buffer_count % 30) {
481 float vol_db = get_volume_decibel(g_buffer, BUFFER_LENGTH, g_recorder->audio_type);
482 if (0 != sttdc_send_set_volume(g_recorder->uid, vol_db)) {
483 SLOG(LOG_ERROR, TAG_STTD, "[Recorder] Fail to send recording volume(%f)", vol_db);
488 if (0 == g_buffer_count % 50) {
489 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder][%d] Recording... : read_size(%d)", g_buffer_count, read_byte);
491 if (100000 == g_buffer_count) {
499 /* write pcm buffer */
500 fwrite(g_buffer, 1, BUFFER_LENGTH, g_pFile);
507 int sttd_recorder_start(int uid)
509 if (STTD_RECORDER_STATE_RECORDING == g_recorder_state)
514 /* Check engine id is valid */
515 if (NULL == g_recorder) {
516 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
517 return STTD_ERROR_INVALID_PARAMETER;
520 ret = sound_manager_acquire_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_RECORDING, NULL);
521 if (SOUND_MANAGER_ERROR_NONE != ret) {
522 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to acquire focus : %d", ret);
524 ret = audio_in_set_stream_info(g_recorder->audio_h, g_stream_info_h);
525 if (AUDIO_IO_ERROR_NONE != ret) {
526 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to set stream info");
530 ret = audio_in_prepare(g_recorder->audio_h);
531 if (AUDIO_IO_ERROR_NONE != ret) {
532 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to start audio : %d", ret);
533 return STTD_ERROR_RECORDER_BUSY;
536 /* Add ecore timer to read audio data */
537 ecore_timer_add(0, __read_audio_func, NULL);
540 g_bt_extend_count = 0;
541 const unsigned char input_data[2] = {SMART_CONTROL_START_CMD, 0x00 };
543 bool started = false;
544 while (5 > bt_retry) {
545 ret = bt_hid_send_rc_command(NULL, input_data, sizeof(input_data));
546 if (BT_ERROR_NONE == ret) {
547 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Start bt audio recorder");
550 } else if (BT_ERROR_NOW_IN_PROGRESS == ret) {
551 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail bt_hid_send_rc_command(NULL, %s, %d)", input_data, sizeof(input_data));
558 if (false == started) {
559 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to start bt audio");
560 return STTD_ERROR_OPERATION_FAILED;
563 g_recorder_state = STTD_RECORDER_STATE_RECORDING;
564 g_recorder->uid = uid;
571 snprintf(g_temp_file_name, sizeof(g_temp_file_name), "/tmp/stt_temp_%d_%d", getpid(), g_count);
572 SECURE_SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Temp file name=[%s]", g_temp_file_name);
575 g_pFile = fopen(g_temp_file_name, "wb+");
577 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] File not found!");
585 int sttd_recorder_stop()
587 if (STTD_RECORDER_STATE_READY == g_recorder_state)
590 /* Check engine id is valid */
591 if (NULL == g_recorder) {
592 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
593 return STTD_ERROR_INVALID_PARAMETER;
596 g_recorder->uid = -1;
600 ret = audio_in_unprepare(g_recorder->audio_h);
601 if (AUDIO_IO_ERROR_NONE != ret) {
602 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to unprepare audioin : %d", ret);
606 bool stopped = false;
607 while (5 > bt_retry) {
608 ret = bt_hid_rc_stop_sending_voice(NULL);
609 if (BT_ERROR_NONE == ret) {
610 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Stop bt audio");
613 } else if (BT_ERROR_NOW_IN_PROGRESS == ret) {
614 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail bt_hid_rc_stop_sending_voice()");
621 if (false == stopped) {
622 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to stop bt audio");
623 return STTD_ERROR_OPERATION_FAILED;
627 g_recorder_state = STTD_RECORDER_STATE_READY;
629 ret = sound_manager_release_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_RECORDING, NULL);
630 if (SOUND_MANAGER_ERROR_NONE != ret) {
631 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to release focus :%d", ret);