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.
18 #include <sound_manager.h>
21 #include "sttd_dbus.h"
22 #include "sttd_recorder.h"
23 #include "sttd_main.h"
27 #define FRAME_LENGTH 160
28 #define BUFFER_LENGTH FRAME_LENGTH * 2
30 static pthread_mutex_t sttd_audio_in_handle_mutex = PTHREAD_MUTEX_INITIALIZER;
33 STTD_RECORDER_STATE_NONE = -1,
34 STTD_RECORDER_STATE_READY = 0, /**< Recorder is ready to start */
35 STTD_RECORDER_STATE_RECORDING /**< In the middle of recording */
36 } sttd_recorder_state;
42 sttp_audio_type_e audio_type;
45 static GSList *g_recorder_list;
47 static int g_recording_engine_id;
49 static stt_recorder_audio_cb g_audio_cb;
51 static stt_recorder_interrupt_cb g_interrupt_cb;
53 static sttd_recorder_state g_recorder_state = STTD_RECORDER_STATE_NONE;
55 static int g_buffer_count;
57 /* Sound buf save for test */
62 static char g_temp_file_name[128] = {'\0',};
66 static int g_count = 1;
69 const char* __stt_get_session_interrupt_code(sound_session_interrupted_code_e code)
72 case SOUND_SESSION_INTERRUPTED_COMPLETED: return "SOUND_SESSION_INTERRUPTED_COMPLETED";
73 case SOUND_SESSION_INTERRUPTED_BY_MEDIA: return "SOUND_SESSION_INTERRUPTED_BY_MEDIA";
74 case SOUND_SESSION_INTERRUPTED_BY_CALL: return "SOUND_SESSION_INTERRUPTED_BY_CALL";
75 case SOUND_SESSION_INTERRUPTED_BY_EARJACK_UNPLUG: return "SOUND_SESSION_INTERRUPTED_BY_EARJACK_UNPLUG";
76 case SOUND_SESSION_INTERRUPTED_BY_RESOURCE_CONFLICT: return "SOUND_SESSION_INTERRUPTED_BY_RESOURCE_CONFLICT";
77 case SOUND_SESSION_INTERRUPTED_BY_ALARM: return "SOUND_SESSION_INTERRUPTED_BY_ALARM";
78 case SOUND_SESSION_INTERRUPTED_BY_EMERGENCY: return "SOUND_SESSION_INTERRUPTED_BY_EMERGENCY";
79 case SOUND_SESSION_INTERRUPTED_BY_NOTIFICATION: return "SOUND_SESSION_INTERRUPTED_BY_NOTIFICATION";
81 return "Undefined error code";
85 void __sttd_recorder_sound_interrupted_cb(sound_session_interrupted_code_e code, void *user_data)
87 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Get the interrupt code from sound mgr : %s",
88 __stt_get_session_interrupt_code(code));
90 if (SOUND_SESSION_INTERRUPTED_COMPLETED == code || SOUND_SESSION_INTERRUPTED_BY_EARJACK_UNPLUG == code)
93 if (NULL != g_interrupt_cb) {
99 int sttd_recorder_initialize(stt_recorder_audio_cb audio_cb, stt_recorder_interrupt_cb interrupt_cb)
101 if (NULL == audio_cb || NULL == interrupt_cb) {
102 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Input param is NOT valid");
103 return STTD_ERROR_INVALID_PARAMETER;
106 if (STTD_RECORDER_STATE_NONE != g_recorder_state) {
107 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Current state of recorder is recording");
108 return STTD_ERROR_INVALID_STATE;
111 if( 0 != pthread_mutex_init(&sttd_audio_in_handle_mutex, NULL)) {
112 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to initialize audio in handle mutex.");
115 g_audio_cb = audio_cb;
116 g_interrupt_cb = interrupt_cb;
117 g_recorder_state = STTD_RECORDER_STATE_NONE;
118 g_recording_engine_id = -1;
120 if (0 != sound_manager_set_session_type(SOUND_SESSION_TYPE_MEDIA)) {
121 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to set exclusive session");
124 if (0 != sound_manager_set_session_interrupted_cb(__sttd_recorder_sound_interrupted_cb, NULL)) {
125 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to set sound interrupt callback");
131 int sttd_recorder_deinitialize()
133 if( 0 != pthread_mutex_destroy(&sttd_audio_in_handle_mutex)) {
134 SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy audio in handle mutex.");
137 if (0 != sound_manager_unset_session_interrupted_cb()) {
138 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to unset sound interrupt callback");
141 /* Remove all recorder */
143 stt_recorder_s *recorder = NULL;
145 iter = g_slist_nth(g_recorder_list, 0);
147 while (NULL != iter) {
148 recorder = iter->data;
150 if (NULL != recorder) {
151 g_recorder_list = g_slist_remove(g_recorder_list, recorder);
152 audio_in_destroy(recorder->audio_h);
157 iter = g_slist_nth(g_recorder_list, 0);
160 g_recorder_state = STTD_RECORDER_STATE_NONE;
165 static stt_recorder_s* __get_recorder(int engine_id)
168 stt_recorder_s *recorder = NULL;
170 iter = g_slist_nth(g_recorder_list, 0);
172 while (NULL != iter) {
173 recorder = iter->data;
175 if (recorder->engine_id == engine_id) {
179 iter = g_slist_next(iter);
185 int sttd_recorder_set_audio_session()
190 int sttd_recorder_unset_audio_session()
195 int sttd_recorder_create(int engine_id, int uid, sttp_audio_type_e type, int channel, unsigned int sample_rate)
197 /* Check engine id is valid */
198 if (NULL != __get_recorder(engine_id)) {
199 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is already registered");
200 return STTD_ERROR_INVALID_PARAMETER;
203 audio_channel_e audio_ch;
204 audio_sample_type_e audio_type;
205 audio_in_h temp_in_h;
208 case 1: audio_ch = AUDIO_CHANNEL_MONO; break;
209 case 2: audio_ch = AUDIO_CHANNEL_STEREO; break;
211 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Input channel is not supported");
212 return STTD_ERROR_OPERATION_FAILED;
217 case STTP_AUDIO_TYPE_PCM_S16_LE: audio_type = AUDIO_SAMPLE_TYPE_S16_LE; break;
218 case STTP_AUDIO_TYPE_PCM_U8: audio_type = AUDIO_SAMPLE_TYPE_U8; break;
220 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Invalid Audio Type");
221 return STTD_ERROR_OPERATION_FAILED;
226 ret = audio_in_create(sample_rate, audio_ch, audio_type, &temp_in_h);
227 if (AUDIO_IO_ERROR_NONE != ret) {
228 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to create audio handle : %d", ret);
229 return STTD_ERROR_OPERATION_FAILED;
232 stt_recorder_s* recorder;
233 recorder = (stt_recorder_s*)calloc(1, sizeof(stt_recorder_s));
234 if (NULL == recorder) {
235 audio_in_destroy(temp_in_h);
236 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to allocate memory");
237 return STTD_ERROR_OUT_OF_MEMORY;
240 recorder->engine_id = engine_id;
242 recorder->audio_h = temp_in_h;
243 recorder->audio_type = type;
245 g_recorder_list = g_slist_append(g_recorder_list, recorder);
247 g_recorder_state = STTD_RECORDER_STATE_READY;
252 int sttd_recorder_destroy(int engine_id)
254 // critical section required because this function can be called from stt engine thread context
255 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Enter critical section");
256 pthread_mutex_lock(&sttd_audio_in_handle_mutex);
258 /* Check engine id is valid */
259 stt_recorder_s* recorder;
260 recorder = __get_recorder(engine_id);
261 if (NULL == recorder) {
262 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
263 pthread_mutex_unlock(&sttd_audio_in_handle_mutex);
264 return STTD_ERROR_INVALID_PARAMETER;
268 if (STTD_RECORDER_STATE_RECORDING == g_recorder_state) {
269 ret = audio_in_unprepare(recorder->audio_h);
270 if (AUDIO_IO_ERROR_NONE != ret) {
271 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to unprepare audioin : %d", ret);
274 g_recorder_state = STTD_RECORDER_STATE_READY;
277 ret = audio_in_destroy(recorder->audio_h);
278 if (AUDIO_IO_ERROR_NONE != ret) {
279 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to destroy audioin : %d", ret);
282 g_recorder_list = g_slist_remove(g_recorder_list, recorder);
286 pthread_mutex_unlock(&sttd_audio_in_handle_mutex);
287 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Leave critical section");
292 static float get_volume_decibel(char* data, int size, sttp_audio_type_e type)
294 #define MAX_AMPLITUDE_MEAN_16 32768
295 #define MAX_AMPLITUDE_MEAN_08 128
302 unsigned long long square_sum = 0;
304 if (type == STTP_AUDIO_TYPE_PCM_S16_LE)
309 for (i = 0; i < size; i += (depthByte<<1)) {
310 if (depthByte == 2) {
312 memcpy(&pcm16, data + i, sizeof(short));
313 square_sum += pcm16 * pcm16;
316 memcpy(&pcm8, data + i, sizeof(char));
317 square_sum += pcm8 * pcm8;
325 rms = sqrt(square_sum/count);
328 db = 20 * log10(rms/MAX_AMPLITUDE_MEAN_16);
330 db = 20 * log10(rms/MAX_AMPLITUDE_MEAN_08);
335 Eina_Bool __read_audio_func(void *data)
338 static char g_buffer[BUFFER_LENGTH];
340 /* Check engine id is valid */
341 stt_recorder_s* recorder;
342 recorder = __get_recorder(g_recording_engine_id);
343 if (NULL == recorder) {
347 if (STTD_RECORDER_STATE_READY == g_recorder_state) {
348 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Exit audio reading func");
352 read_byte = audio_in_read(recorder->audio_h, g_buffer, BUFFER_LENGTH);
354 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Fail to read audio : %d", read_byte);
355 g_recorder_state = STTD_RECORDER_STATE_READY;
359 if (0 != g_audio_cb(g_buffer, read_byte)) {
360 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Fail audio callback");
361 sttd_recorder_stop(g_recording_engine_id);
365 if (0 == g_buffer_count % 30) {
366 float vol_db = get_volume_decibel(g_buffer, BUFFER_LENGTH, recorder->audio_type);
367 if (0 != sttdc_send_set_volume(recorder->uid, vol_db)) {
368 SLOG(LOG_ERROR, TAG_STTD, "[Recorder] Fail to send recording volume(%f)", vol_db);
373 if (0 == g_buffer_count % 50) {
374 SLOG(LOG_DEBUG, TAG_STTD, "[Recorder][%d] Recording... : read_size(%d)", g_buffer_count, read_byte);
376 if (100000 == g_buffer_count) {
384 /* write pcm buffer */
385 fwrite(g_buffer, 1, BUFFER_LENGTH, g_pFile);
391 int sttd_recorder_start(int engine_id)
393 if (STTD_RECORDER_STATE_RECORDING == g_recorder_state)
396 /* Check engine id is valid */
397 stt_recorder_s* recorder;
398 recorder = __get_recorder(engine_id);
399 if (NULL == recorder) {
400 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
401 return STTD_ERROR_INVALID_PARAMETER;
405 ret = audio_in_prepare(recorder->audio_h);
406 if (AUDIO_IO_ERROR_NONE != ret) {
407 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to start audio : %d", ret);
408 return STTD_ERROR_RECORDER_BUSY;
411 /* Add ecore timer to read audio data */
412 ecore_timer_add(0, __read_audio_func, NULL);
414 g_recorder_state = STTD_RECORDER_STATE_RECORDING;
415 g_recording_engine_id = engine_id;
422 snprintf(g_temp_file_name, sizeof(g_temp_file_name), "/tmp/stt_temp_%d_%d", getpid(), g_count);
423 SECURE_SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Temp file name=[%s]", g_temp_file_name);
426 g_pFile = fopen(g_temp_file_name, "wb+");
428 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] File not found!");
436 int sttd_recorder_stop(int engine_id)
438 if (STTD_RECORDER_STATE_READY == g_recorder_state)
441 /* Check engine id is valid */
442 stt_recorder_s* recorder;
443 recorder = __get_recorder(engine_id);
444 if (NULL == recorder) {
445 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
446 return STTD_ERROR_INVALID_PARAMETER;
450 ret = audio_in_unprepare(recorder->audio_h);
451 if (AUDIO_IO_ERROR_NONE != ret) {
452 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to unprepare audioin : %d", ret);
455 g_recorder_state = STTD_RECORDER_STATE_READY;
456 g_recording_engine_id = -1;
465 int sttd_recorder_set_ignore_session(int engine_id)
467 if (STTD_RECORDER_STATE_READY != g_recorder_state) {
468 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Record is working.");
472 /* Check engine id is valid */
473 stt_recorder_s* recorder;
474 recorder = __get_recorder(engine_id);
475 if (NULL == recorder) {
476 SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
477 return STTD_ERROR_INVALID_PARAMETER;
480 int ret = audio_in_ignore_session(recorder->audio_h);
481 if (AUDIO_IO_ERROR_NONE != ret) {
482 SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to ignore session : %d", ret);
483 return STTD_ERROR_OPERATION_FAILED;