/*
-* Copyright (c) 2011-2014 Samsung Electronics Co., Ltd All Rights Reserved
+* Copyright (c) 2011-2014 Samsung Electronics Co., Ltd All Rights Reserved
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
#include <audio_io.h>
#include <Ecore.h>
#include <math.h>
+#include <pthread.h>
#include <sound_manager.h>
#include "stt_defs.h"
+#include "sttd_dbus.h"
#include "sttd_recorder.h"
#include "sttd_main.h"
#include "sttp.h"
#define FRAME_LENGTH 160
#define BUFFER_LENGTH FRAME_LENGTH * 2
+static pthread_mutex_t sttd_audio_in_handle_mutex = PTHREAD_MUTEX_INITIALIZER;
+
typedef enum {
STTD_RECORDER_STATE_NONE = -1,
STTD_RECORDER_STATE_READY = 0, /**< Recorder is ready to start */
typedef struct {
int engine_id;
+ int uid;
audio_in_h audio_h;
sttp_audio_type_e audio_type;
-}stt_recorder_s;
+} stt_recorder_s;
static GSList *g_recorder_list;
static sttd_recorder_state g_recorder_state = STTD_RECORDER_STATE_NONE;
-static FILE* g_pFile_vol;
-
static int g_buffer_count;
/* Sound buf save for test */
static FILE* g_pFile;
static int g_count = 1;
-#endif
+#endif
const char* __stt_get_session_interrupt_code(sound_session_interrupted_code_e code)
{
- switch(code) {
+ switch (code) {
case SOUND_SESSION_INTERRUPTED_COMPLETED: return "SOUND_SESSION_INTERRUPTED_COMPLETED";
case SOUND_SESSION_INTERRUPTED_BY_MEDIA: return "SOUND_SESSION_INTERRUPTED_BY_MEDIA";
case SOUND_SESSION_INTERRUPTED_BY_CALL: return "SOUND_SESSION_INTERRUPTED_BY_CALL";
void __sttd_recorder_sound_interrupted_cb(sound_session_interrupted_code_e code, void *user_data)
{
- SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Get the interrupt code from sound mgr : %s",
+ SLOG(LOG_DEBUG, TAG_STTD, "[Recorder] Get the interrupt code from sound mgr : %s",
__stt_get_session_interrupt_code(code));
if (SOUND_SESSION_INTERRUPTED_COMPLETED == code || SOUND_SESSION_INTERRUPTED_BY_EARJACK_UNPLUG == code)
SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Current state of recorder is recording");
return STTD_ERROR_INVALID_STATE;
}
-
+
+ if( 0 != pthread_mutex_init(&sttd_audio_in_handle_mutex, NULL)) {
+ SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to initialize audio in handle mutex.");
+ }
+
g_audio_cb = audio_cb;
g_interrupt_cb = interrupt_cb;
g_recorder_state = STTD_RECORDER_STATE_NONE;
int sttd_recorder_deinitialize()
{
+ if( 0 != pthread_mutex_destroy(&sttd_audio_in_handle_mutex)) {
+ SLOG(LOG_ERROR, TAG_STTD, "[Server ERROR] Fail to destroy audio in handle mutex.");
+ }
+
if (0 != sound_manager_unset_session_interrupted_cb()) {
SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to unset sound interrupt callback");
}
iter = g_slist_nth(g_recorder_list, 0);
}
- if (0 == access(STT_AUDIO_VOLUME_PATH, R_OK)) {
- if (0 != remove(STT_AUDIO_VOLUME_PATH)) {
- SLOG(LOG_WARN, TAG_STTD, "[Recorder WARN] Fail to remove volume file");
- }
- }
-
g_recorder_state = STTD_RECORDER_STATE_NONE;
return 0;
return 0;
}
-int sttd_recorder_create(int engine_id, sttp_audio_type_e type, int channel, unsigned int sample_rate)
+int sttd_recorder_create(int engine_id, int uid, sttp_audio_type_e type, int channel, unsigned int sample_rate)
{
/* Check engine id is valid */
if (NULL != __get_recorder(engine_id)) {
audio_sample_type_e audio_type;
audio_in_h temp_in_h;
- switch(channel) {
+ switch (channel) {
case 1: audio_ch = AUDIO_CHANNEL_MONO; break;
case 2: audio_ch = AUDIO_CHANNEL_STEREO; break;
default:
switch (type) {
case STTP_AUDIO_TYPE_PCM_S16_LE: audio_type = AUDIO_SAMPLE_TYPE_S16_LE; break;
case STTP_AUDIO_TYPE_PCM_U8: audio_type = AUDIO_SAMPLE_TYPE_U8; break;
- default:
- SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Invalid Audio Type");
+ default:
+ SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Invalid Audio Type");
return STTD_ERROR_OPERATION_FAILED;
break;
}
}
recorder->engine_id = engine_id;
+ recorder->uid = uid;
recorder->audio_h = temp_in_h;
recorder->audio_type = type;
-
+
g_recorder_list = g_slist_append(g_recorder_list, recorder);
g_recorder_state = STTD_RECORDER_STATE_READY;
int sttd_recorder_destroy(int engine_id)
{
+ // critical section required because this function can be called from stt engine thread context
+ SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Enter critical section");
+ pthread_mutex_lock(&sttd_audio_in_handle_mutex);
+
/* Check engine id is valid */
stt_recorder_s* recorder;
recorder = __get_recorder(engine_id);
if (NULL == recorder) {
SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Engine id is not valid");
+ pthread_mutex_unlock(&sttd_audio_in_handle_mutex);
return STTD_ERROR_INVALID_PARAMETER;
}
free(recorder);
+ pthread_mutex_unlock(&sttd_audio_in_handle_mutex);
+ SLOG(LOG_WARN, TAG_STTD, "[Recorder WARNING] Leave critical section");
+
return 0;
}
return EINA_FALSE;
}
- float vol_db = get_volume_decibel(g_buffer, BUFFER_LENGTH, recorder->audio_type);
-
- rewind(g_pFile_vol);
-
- fwrite((void*)(&vol_db), sizeof(vol_db), 1, g_pFile_vol);
+ if (0 == g_buffer_count % 30) {
+ float vol_db = get_volume_decibel(g_buffer, BUFFER_LENGTH, recorder->audio_type);
+ if (0 != sttdc_send_set_volume(recorder->uid, vol_db)) {
+ SLOG(LOG_ERROR, TAG_STTD, "[Recorder] Fail to send recording volume(%f)", vol_db);
+ }
+ }
/* Audio read log */
if (0 == g_buffer_count % 50) {
SLOG(LOG_DEBUG, TAG_STTD, "[Recorder][%d] Recording... : read_size(%d)", g_buffer_count, read_byte);
-
+
if (100000 == g_buffer_count) {
g_buffer_count = 0;
}
return STTD_ERROR_INVALID_PARAMETER;
}
- int ret = -1;
+ int ret = -1;
ret = audio_in_prepare(recorder->audio_h);
if (AUDIO_IO_ERROR_NONE != ret) {
SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to start audio : %d", ret);
g_recorder_state = STTD_RECORDER_STATE_RECORDING;
g_recording_engine_id = engine_id;
- g_pFile_vol = fopen(STT_AUDIO_VOLUME_PATH, "wb+");
- if (!g_pFile_vol) {
- SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to create Volume File");
- return -1;
- }
-
g_buffer_count = 0;
#ifdef BUF_SAVE_MODE
if (!g_pFile) {
SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] File not found!");
return -1;
- }
+ }
#endif
return 0;
return STTD_ERROR_INVALID_PARAMETER;
}
- int ret;
+ int ret;
ret = audio_in_unprepare(recorder->audio_h);
if (AUDIO_IO_ERROR_NONE != ret) {
SLOG(LOG_ERROR, TAG_STTD, "[Recorder ERROR] Fail to unprepare audioin : %d", ret);
g_recorder_state = STTD_RECORDER_STATE_READY;
g_recording_engine_id = -1;
- fclose(g_pFile_vol);
-
#ifdef BUF_SAVE_MODE
fclose(g_pFile);
-#endif
+#endif
return 0;
}