2 * Copyright (c) 2011-2014 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"
34 #define FRAME_LENGTH 160
35 #define BUFFER_LENGTH FRAME_LENGTH * 2
37 static pthread_mutex_t sttd_audio_in_handle_mutex = PTHREAD_MUTEX_INITIALIZER;
40 STTD_RECORDER_STATE_NONE = -1,
41 STTD_RECORDER_STATE_READY = 0, /**< Recorder is ready to start */
42 STTD_RECORDER_STATE_RECORDING /**< In the middle of recording */
43 } sttd_recorder_state;
49 sttp_audio_type_e audio_type;
52 static GSList *g_recorder_list;
54 static int g_recording_engine_id;
56 static stt_recorder_audio_cb g_audio_cb;
58 static sound_stream_info_h g_stream_info_h;
60 static stt_recorder_interrupt_cb g_interrupt_cb;
62 static sttd_recorder_state g_recorder_state = STTD_RECORDER_STATE_NONE;
64 static int g_buffer_count;
66 /* Sound buf save for test */
71 static char g_temp_file_name[128] = {'\0',};
75 static int g_count = 1;
79 static float get_volume_decibel(char* data, int size, sttp_audio_type_e type);
81 static stt_recorder_s* __get_recorder(int engine_id);
83 static int g_bt_extend_count;
85 #define SMART_CONTROL_EXTEND_CMD 0x03
86 #define SMART_CONTROL_START_CMD 0x04
88 static void _bt_cb_hid_state_changed(int result, bool connected, const char *remote_address, void *user_data)
90 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Bluetooth Event [%d] Received address [%s]", result, remote_address);
93 static void _bt_hid_audio_data_receive_cb(bt_hid_voice_data_s *voice_data, void *user_data)
95 if (STTD_RECORDER_STATE_RECORDING != g_recorder_state) {
96 /* SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Exit audio reading normal func"); */
100 if (NULL != g_audio_cb) {
101 if (0 != g_audio_cb((void*)voice_data->audio_buf, (unsigned int)voice_data->length)) {
102 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to read audio");
103 sttd_recorder_stop(g_recording_engine_id);
106 stt_recorder_s* recorder;
107 recorder = __get_recorder(g_recording_engine_id);
108 if (NULL == recorder) {
112 float vol_db = get_volume_decibel((char*)voice_data->audio_buf, (int)voice_data->length, recorder->audio_type);
113 if (0 != sttdc_send_set_volume(recorder->uid, vol_db)) {
114 SLOG(LOG_ERROR, TAG_STTD, "[Recorder] Fail to send recording volume(%f)", vol_db);
118 if (0 == g_buffer_count || 0 == g_buffer_count % 50) {
119 SLOG(LOG_WARN, TAG_STTD, "[Recorder][%d] Recording... : read_size(%d)", g_buffer_count, voice_data->length);
121 if (0 == g_bt_extend_count % 5 && 0 != g_buffer_count) {
122 const unsigned char input_data[2] = {SMART_CONTROL_EXTEND_CMD, 0x10 };
123 if (BT_ERROR_NONE != bt_hid_send_rc_command(NULL, input_data, sizeof(input_data))) {
124 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail bt_hid_send_rc_command(NULL, %s, %d)", input_data, sizeof(input_data));
126 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Extend bt audio recorder");
131 if (100000 == g_buffer_count) {
139 /* write pcm buffer */
140 fwrite(data, 1, len, g_pFile);
147 const char* __stt_get_focus_changed_reason_code(sound_stream_focus_change_reason_e reason)
150 case SOUND_STREAM_FOCUS_CHANGED_BY_MEDIA: return "SOUND_STREAM_FOCUS_CHANGED_BY_MEDIA";
151 case SOUND_STREAM_FOCUS_CHANGED_BY_SYSTEM: return "SOUND_STREAM_FOCUS_CHANGED_BY_SYSTEM";
152 case SOUND_STREAM_FOCUS_CHANGED_BY_ALARM: return "SOUND_STREAM_FOCUS_CHANGED_BY_ALARM";
153 case SOUND_STREAM_FOCUS_CHANGED_BY_NOTIFICATION: return "SOUND_STREAM_FOCUS_CHANGED_BY_NOTIFICATION";
154 case SOUND_STREAM_FOCUS_CHANGED_BY_EMERGENCY: return "SOUND_STREAM_FOCUS_CHANGED_BY_EMERGENCY";
155 case SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_INFORMATION: return "SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_INFORMATION";
156 case SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_RECOGNITION: return "SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_RECOGNITION";
157 case SOUND_STREAM_FOCUS_CHANGED_BY_RINGTONE: return "SOUND_STREAM_FOCUS_CHANGED_BY_RINGTONE";
158 case SOUND_STREAM_FOCUS_CHANGED_BY_VOIP: return "SOUND_STREAM_FOCUS_CHANGED_BY_VOIP";
159 case SOUND_STREAM_FOCUS_CHANGED_BY_CALL: return "SOUND_STREAM_FOCUS_CHANGED_BY_CALL";
160 case SOUND_STREAM_FOCUS_CHANGED_BY_MEDIA_EXTERNAL_ONLY: return "SOUND_STREAM_FOCUS_CHANGED_BY_MEDIA_EXTERNAL_ONLY";
161 default: return "Undefined reason code";
165 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)
167 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Focus state changed cb");
169 if (stream_info != g_stream_info_h) {
170 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Invalid stream info handle");
175 sound_stream_focus_state_e state_for_recording;
176 ret = sound_manager_get_focus_state(g_stream_info_h, NULL, &state_for_recording);
177 if (SOUND_MANAGER_ERROR_NONE != ret) {
178 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to get focus state");
182 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));
184 if (STTD_RECORDER_STATE_RECORDING == g_recorder_state && SOUND_STREAM_FOCUS_STATE_RELEASED == state_for_recording) {
185 SLOG(LOG_WARN, TAG_STTD, "[Recorder] Focus released as interrupt");
186 if (NULL != g_interrupt_cb) {
193 int sttd_recorder_initialize(stt_recorder_audio_cb audio_cb, stt_recorder_interrupt_cb interrupt_cb)
195 if (NULL == audio_cb || NULL == interrupt_cb) {
196 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Input param is NOT valid");
197 return STTD_ERROR_INVALID_PARAMETER;
200 if (STTD_RECORDER_STATE_NONE != g_recorder_state) {
201 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Current state of recorder is recording");
202 return STTD_ERROR_INVALID_STATE;
205 if (0 != pthread_mutex_init(&sttd_audio_in_handle_mutex, NULL)) {
206 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to initialize audio in handle mutex.");
209 g_audio_cb = audio_cb;
210 g_interrupt_cb = interrupt_cb;
211 g_recorder_state = STTD_RECORDER_STATE_NONE;
212 g_recording_engine_id = -1;
214 if (0 != sound_manager_create_stream_information(SOUND_STREAM_TYPE_VOICE_RECOGNITION, __recorder_focus_state_cb, NULL, &g_stream_info_h)) {
215 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to create stream info");
219 if (BT_ERROR_NONE != bt_initialize()) {
220 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to init bt");
221 return STTD_ERROR_OPERATION_FAILED;
224 if (BT_ERROR_NONE != bt_hid_host_initialize(_bt_cb_hid_state_changed, NULL)) {
225 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail bt_hid_host_initialize()");
226 return STTD_ERROR_OPERATION_FAILED;
233 int sttd_recorder_deinitialize()
235 if (0 != pthread_mutex_destroy(&sttd_audio_in_handle_mutex)) {
236 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy audio in handle mutex.");
239 if (0 != sound_manager_destroy_stream_information(g_stream_info_h)) {
240 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to destroy stream info");
243 /* Remove all recorder */
245 stt_recorder_s *recorder = NULL;
247 iter = g_slist_nth(g_recorder_list, 0);
249 while (NULL != iter) {
250 recorder = iter->data;
252 if (NULL != recorder) {
253 g_recorder_list = g_slist_remove(g_recorder_list, recorder);
254 audio_in_destroy(recorder->audio_h);
259 iter = g_slist_nth(g_recorder_list, 0);
263 bt_hid_host_deinitialize ();
268 g_recorder_state = STTD_RECORDER_STATE_NONE;
273 static stt_recorder_s* __get_recorder(int engine_id)
276 stt_recorder_s *recorder = NULL;
278 iter = g_slist_nth(g_recorder_list, 0);
280 while (NULL != iter) {
281 recorder = iter->data;
283 if (recorder->engine_id == engine_id) {
287 iter = g_slist_next(iter);
293 int sttd_recorder_set_audio_session()
298 int sttd_recorder_unset_audio_session()
303 int sttd_recorder_create(int engine_id, int uid, sttp_audio_type_e type, int channel, unsigned int sample_rate)
305 /* Check engine id is valid */
306 if (NULL != __get_recorder(engine_id)) {
307 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is already registered");
308 return STTD_ERROR_INVALID_PARAMETER;
311 audio_in_h temp_in_h = NULL;
314 audio_channel_e audio_ch;
315 audio_sample_type_e audio_type;
318 case 1: audio_ch = AUDIO_CHANNEL_MONO; break;
319 case 2: audio_ch = AUDIO_CHANNEL_STEREO; break;
321 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Input channel is not supported");
322 return STTD_ERROR_OPERATION_FAILED;
327 case STTP_AUDIO_TYPE_PCM_S16_LE: audio_type = AUDIO_SAMPLE_TYPE_S16_LE; break;
328 case STTP_AUDIO_TYPE_PCM_U8: audio_type = AUDIO_SAMPLE_TYPE_U8; break;
330 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Invalid Audio Type");
331 return STTD_ERROR_OPERATION_FAILED;
336 ret = audio_in_create(sample_rate, audio_ch, audio_type, &temp_in_h);
337 if (AUDIO_IO_ERROR_NONE != ret) {
338 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to create audio handle : %d", ret);
339 return STTD_ERROR_OPERATION_FAILED;
342 if (BT_ERROR_NONE != bt_hid_set_audio_data_receive_cb(_bt_hid_audio_data_receive_cb, NULL)) {
343 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail bt_hid_set_audio_data_receive_cb()");
344 return STTD_ERROR_OPERATION_FAILED;
348 stt_recorder_s* recorder;
349 recorder = (stt_recorder_s*)calloc(1, sizeof(stt_recorder_s));
350 if (NULL == recorder) {
352 audio_in_destroy(temp_in_h);
354 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to allocate memory");
355 return STTD_ERROR_OUT_OF_MEMORY;
358 recorder->engine_id = engine_id;
360 recorder->audio_h = temp_in_h;
361 recorder->audio_type = type;
363 g_recorder_list = g_slist_append(g_recorder_list, recorder);
365 g_recorder_state = STTD_RECORDER_STATE_READY;
370 int sttd_recorder_destroy(int engine_id)
372 // critical section required because this function can be called from stt engine thread context
373 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Enter critical section");
374 pthread_mutex_lock(&sttd_audio_in_handle_mutex);
376 /* Check engine id is valid */
377 stt_recorder_s* recorder;
378 recorder = __get_recorder(engine_id);
379 if (NULL == recorder) {
380 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
381 pthread_mutex_unlock(&sttd_audio_in_handle_mutex);
382 return STTD_ERROR_INVALID_PARAMETER;
387 if (STTD_RECORDER_STATE_RECORDING == g_recorder_state) {
388 ret = audio_in_unprepare(recorder->audio_h);
389 if (AUDIO_IO_ERROR_NONE != ret) {
390 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to unprepare audioin : %d", ret);
393 g_recorder_state = STTD_RECORDER_STATE_READY;
396 ret = audio_in_destroy(recorder->audio_h);
397 if (AUDIO_IO_ERROR_NONE != ret) {
398 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to destroy audioin : %d", ret);
401 if (STTD_RECORDER_STATE_RECORDING == g_recorder_state) {
402 g_recorder_state = STTD_RECORDER_STATE_READY;
405 bt_hid_unset_audio_data_receive_cb();
408 g_recorder_list = g_slist_remove(g_recorder_list, recorder);
412 pthread_mutex_unlock(&sttd_audio_in_handle_mutex);
413 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Leave critical section");
418 static float get_volume_decibel(char* data, int size, sttp_audio_type_e type)
420 #define MAX_AMPLITUDE_MEAN_16 32768
421 #define MAX_AMPLITUDE_MEAN_08 128
428 unsigned long long square_sum = 0;
430 if (type == STTP_AUDIO_TYPE_PCM_S16_LE)
435 for (i = 0; i < size; i += (depthByte<<1)) {
436 if (depthByte == 2) {
438 memcpy(&pcm16, data + i, sizeof(short));
439 square_sum += pcm16 * pcm16;
442 memcpy(&pcm8, data + i, sizeof(char));
443 square_sum += pcm8 * pcm8;
451 rms = sqrt(square_sum/count);
454 db = 20 * log10(rms/MAX_AMPLITUDE_MEAN_16);
456 db = 20 * log10(rms/MAX_AMPLITUDE_MEAN_08);
462 Eina_Bool __read_audio_func(void *data)
465 static char g_buffer[BUFFER_LENGTH];
467 /* Check engine id is valid */
468 stt_recorder_s* recorder;
469 recorder = __get_recorder(g_recording_engine_id);
470 if (NULL == recorder) {
474 if (STTD_RECORDER_STATE_READY == g_recorder_state) {
475 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Exit audio reading func");
479 read_byte = audio_in_read(recorder->audio_h, g_buffer, BUFFER_LENGTH);
481 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Fail to read audio : %d", read_byte);
482 g_recorder_state = STTD_RECORDER_STATE_READY;
486 if (0 != g_audio_cb(g_buffer, read_byte)) {
487 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Fail audio callback");
488 sttd_recorder_stop(g_recording_engine_id);
492 if (0 == g_buffer_count % 30) {
493 float vol_db = get_volume_decibel(g_buffer, BUFFER_LENGTH, recorder->audio_type);
494 if (0 != sttdc_send_set_volume(recorder->uid, vol_db)) {
495 SLOG(LOG_ERROR, TAG_STTD, "[Recorder] Fail to send recording volume(%f)", vol_db);
500 if (0 == g_buffer_count % 50) {
501 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder][%d] Recording... : read_size(%d)", g_buffer_count, read_byte);
503 if (100000 == g_buffer_count) {
511 /* write pcm buffer */
512 fwrite(g_buffer, 1, BUFFER_LENGTH, g_pFile);
519 int sttd_recorder_start(int engine_id)
521 if (STTD_RECORDER_STATE_RECORDING == g_recorder_state)
526 /* Check engine id is valid */
527 stt_recorder_s* recorder;
528 recorder = __get_recorder(engine_id);
529 if (NULL == recorder) {
530 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
531 return STTD_ERROR_INVALID_PARAMETER;
534 ret = sound_manager_acquire_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_RECORDING, NULL);
535 if (SOUND_MANAGER_ERROR_NONE != ret) {
536 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to acquire focus : %d", ret);
538 ret = audio_in_set_stream_info(recorder->audio_h, g_stream_info_h);
539 if (AUDIO_IO_ERROR_NONE != ret) {
540 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to set stream info");
544 ret = audio_in_prepare(recorder->audio_h);
545 if (AUDIO_IO_ERROR_NONE != ret) {
546 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to start audio : %d", ret);
547 return STTD_ERROR_RECORDER_BUSY;
550 /* Add ecore timer to read audio data */
551 ecore_timer_add(0, __read_audio_func, NULL);
554 g_bt_extend_count = 0;
555 const unsigned char input_data[2] = {SMART_CONTROL_START_CMD, 0x00 };
557 bool started = false;
558 while (5 > bt_retry) {
559 ret = bt_hid_send_rc_command(NULL, input_data, sizeof(input_data));
560 if (BT_ERROR_NONE == ret) {
561 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Start bt audio recorder");
564 } else if (BT_ERROR_NOW_IN_PROGRESS == ret) {
565 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail bt_hid_send_rc_command(NULL, %s, %d)", input_data, sizeof(input_data));
572 if (false == started) {
573 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to start bt audio");
574 return STTD_ERROR_OPERATION_FAILED;
577 g_recorder_state = STTD_RECORDER_STATE_RECORDING;
578 g_recording_engine_id = engine_id;
585 snprintf(g_temp_file_name, sizeof(g_temp_file_name), "/tmp/stt_temp_%d_%d", getpid(), g_count);
586 SECURE_SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Temp file name=[%s]", g_temp_file_name);
589 g_pFile = fopen(g_temp_file_name, "wb+");
591 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] File not found!");
599 int sttd_recorder_stop(int engine_id)
601 if (STTD_RECORDER_STATE_READY == g_recorder_state)
604 /* Check engine id is valid */
605 stt_recorder_s* recorder;
606 recorder = __get_recorder(engine_id);
607 if (NULL == recorder) {
608 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
609 return STTD_ERROR_INVALID_PARAMETER;
614 ret = audio_in_unprepare(recorder->audio_h);
615 if (AUDIO_IO_ERROR_NONE != ret) {
616 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to unprepare audioin : %d", ret);
620 bool stopped = false;
621 while (5 > bt_retry) {
622 ret = bt_hid_rc_stop_sending_voice(NULL);
623 if (BT_ERROR_NONE == ret) {
624 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Stop bt audio");
627 } else if (BT_ERROR_NOW_IN_PROGRESS == ret) {
628 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail bt_hid_rc_stop_sending_voice()");
635 if (false == stopped) {
636 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to stop bt audio");
637 return STTD_ERROR_OPERATION_FAILED;
641 g_recorder_state = STTD_RECORDER_STATE_READY;
642 g_recording_engine_id = -1;
644 ret = sound_manager_release_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_RECORDING, NULL);
645 if (SOUND_MANAGER_ERROR_NONE != ret) {
646 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to release focus :%d", ret);