#include "tts_internal.h"
#include "ttsd_server.h"
-/*
-* Internal data structure
-*/
-
-typedef struct {
- unsigned int uid; /** client id */
-} player_s;
#define SOUND_BUFFER_LENGTH 2048
/** player init info */
static bool g_player_init = false;
-/** Client list */
-static GList *g_player_list;
-
-/** current player information */
-static player_s* g_playing_info;
-
static bool g_is_set_policy;
/* CAUTION!
#define SND_MGR_DUCKING_DURATION 500
static pthread_mutex_t g_play_thread_mutex = PTHREAD_MUTEX_INITIALIZER;
-static pthread_mutex_t g_player_control_mutex = PTHREAD_MUTEX_INITIALIZER;
-
static pthread_cond_t g_play_thread_cond = PTHREAD_COND_INITIALIZER;
static BackgroundVolume* g_background_volume = nullptr;
}
#endif
-player_s* __player_get_item(unsigned int uid)
-{
- GList *iter = NULL;
- player_s *data = NULL;
-
- if (0 < g_list_length(g_player_list)) {
- /* Get a first item */
- iter = g_list_first(g_player_list);
-
- while (NULL != iter) {
- /* Get handle data from list */
- data = (player_s*)iter->data;
-
- /* compare uid */
- if (uid == data->uid)
- return data;
-
- /* Get next item */
- iter = g_list_next(iter);
- }
- }
-
- return NULL;
-}
-
static void __focus_release_callback()
{
unsigned int uid = g_player_thread->getCurrentUid();
*/
int ttsd_player_init()
{
- pthread_mutex_lock(&g_player_control_mutex);
- g_playing_info = NULL;
-
g_background_volume = new BackgroundVolume(SND_MGR_DUCKING_DURATION);
g_audio_stream = new AudioStream(__focus_release_callback);
g_player_thread = new PlayerThread(nullptr);
g_is_set_policy = false;
g_player_init = true;
- pthread_mutex_unlock(&g_player_control_mutex);
-
return 0;
}
#endif
__set_playing_status(false);
- pthread_mutex_lock(&g_player_control_mutex);
if (false == g_player_init) {
SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
- pthread_mutex_unlock(&g_player_control_mutex);
return TTSD_ERROR_OPERATION_FAILED;
}
thread_count = ecore_thread_active_get();
}
- SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Thread is released");
-
- /* clear g_player_list */
- g_playing_info = NULL;
g_player_init = false;
g_player_thread->requestStop();
pthread_cond_broadcast(&g_play_thread_cond);
+ SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Thread is released");
delete g_audio_stream;
g_audio_stream = nullptr;
delete g_player_thread;
g_player_thread = nullptr;
- pthread_mutex_unlock(&g_player_control_mutex);
-
- return 0;
-}
-
-int ttsd_player_create_instance(unsigned int uid)
-{
- if (false == g_player_init) {
- SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
- return TTSD_ERROR_OPERATION_FAILED;
- }
-
- /* Check uid is duplicated */
- if (NULL != __player_get_item(uid)) {
- SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%u) is already registered", uid);
- return -1;
- }
-
- player_s* new_client = (player_s*)calloc(1, sizeof(player_s));
- if (NULL == new_client) {
- SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to allocate memory");
- return TTSE_ERROR_OUT_OF_MEMORY;
- }
-
- new_client->uid = uid;
- ttsd_data_set_last_sound_result_event(uid, TTSE_RESULT_EVENT_FINISH);
- ttsd_data_set_paused_data_existing(uid, false);
-
- SECURE_SLOG(LOG_DEBUG, tts_tag(), "[Player] Create player : uid(%u)", uid);
-
- g_player_list = g_list_append(g_player_list, new_client);
-
- return 0;
-}
-
-int ttsd_player_destroy_instance(unsigned int uid)
-{
- pthread_mutex_lock(&g_player_control_mutex);
- if (false == g_player_init) {
- SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
- pthread_mutex_unlock(&g_player_control_mutex);
- return TTSD_ERROR_OPERATION_FAILED;
- }
-
- player_s* current;
- current = __player_get_item(uid);
- if (NULL == current) {
- SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%u) is not valid", uid);
- pthread_mutex_unlock(&g_player_control_mutex);
- return -1;
- }
-
- if (NULL != g_playing_info) {
- if (uid == g_playing_info->uid) {
- g_playing_info = NULL;
- }
- }
-
- GList *iter = NULL;
- player_s *data = NULL;
-
- if (0 < g_list_length(g_player_list)) {
- /* Get a first item */
- iter = g_list_first(g_player_list);
-
- while (NULL != iter) {
- /* Get handle data from list */
- data = (player_s*)iter->data;
-
- if (NULL != data) {
- /* compare uid */
- if (uid == data->uid) {
- g_player_list = g_list_remove_link(g_player_list, iter);
- free(data);
- data = NULL;
- g_list_free(iter);
- break;
- }
- }
-
- /* Get next item */
- iter = g_list_next(iter);
- }
- }
- pthread_mutex_unlock(&g_player_control_mutex);
-
- SLOG(LOG_DEBUG, tts_tag(), "[PLAYER Success] Destroy instance");
-
return 0;
}
int ttsd_player_play(unsigned int uid)
{
- pthread_mutex_lock(&g_player_control_mutex);
if (false == g_player_init) {
SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
- pthread_mutex_unlock(&g_player_control_mutex);
return TTSD_ERROR_OPERATION_FAILED;
}
if (0 > ttsd_data_is_client(uid)) {
SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%u) is not valid", uid);
- pthread_mutex_unlock(&g_player_control_mutex);
return -1;
}
if (0 < ttsd_data_is_client(currentUid)) {
if (uid == currentUid) {
SLOG(LOG_DEBUG, tts_tag(), "[Player] uid(%u) has already played", uid);
- pthread_mutex_unlock(&g_player_control_mutex);
return 0;
} else {
SLOG(LOG_WARN, tts_tag(), "[Player WARNING] stop old player (%u)", currentUid);
- pthread_mutex_unlock(&g_player_control_mutex);
ttsd_player_stop(currentUid);
- pthread_mutex_lock(&g_player_control_mutex);
}
}
g_player_thread->requestPlay(uid);
pthread_cond_broadcast(&g_play_thread_cond);
- pthread_mutex_unlock(&g_player_control_mutex);
return 0;
}
int ttsd_player_stop(unsigned int uid)
{
- pthread_mutex_lock(&g_player_control_mutex);
if (false == g_player_init) {
SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
- pthread_mutex_unlock(&g_player_control_mutex);
return TTSD_ERROR_OPERATION_FAILED;
}
SLOG(LOG_INFO, tts_tag(), "[Player SUCCESS] Stop player : uid(%u)", uid);
- pthread_mutex_unlock(&g_player_control_mutex);
- return 0;
-}
-
-int ttsd_player_clear(unsigned int uid)
-{
- if (false == g_player_init) {
- SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
- return TTSD_ERROR_OPERATION_FAILED;
- }
-
- /* Check uid */
- player_s* current;
- current = __player_get_item(uid);
- if (NULL == current) {
- SECURE_SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%u) is not valid", uid);
- return -1;
- }
-
- ttsd_data_set_last_sound_result_event(uid, TTSE_RESULT_EVENT_FINISH);
- ttsd_data_set_paused_data_existing(uid, false);
-
- SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Clear player : uid(%u)", uid);
-
return 0;
}
int ttsd_player_pause(unsigned int uid)
{
- pthread_mutex_lock(&g_player_control_mutex);
SLOG(LOG_DEBUG, tts_tag(), "[Player] pause player : uid(%u)", uid);
if (false == g_player_init) {
SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
- pthread_mutex_unlock(&g_player_control_mutex);
return TTSD_ERROR_OPERATION_FAILED;
}
if (0 > ttsd_data_is_client(uid)) {
SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] ttsd_player_pause() : uid(%u) is not valid", uid);
- pthread_mutex_unlock(&g_player_control_mutex);
return -1;
}
__set_playing_status(false);
SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Pause player : uid(%u)", uid);
-
- pthread_mutex_unlock(&g_player_control_mutex);
return 0;
}
int ttsd_player_resume(unsigned int uid)
{
- pthread_mutex_lock(&g_player_control_mutex);
SLOG(LOG_DEBUG, tts_tag(), "[Player] Resume player : uid(%u)", uid);
if (false == g_player_init) {
SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
- pthread_mutex_unlock(&g_player_control_mutex);
return TTSD_ERROR_OPERATION_FAILED;
}
if (0 > ttsd_data_is_client(uid)) {
SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%u) is not valid", uid);
- pthread_mutex_unlock(&g_player_control_mutex);
return -1;
}
g_player_thread->requestPlay(uid);
pthread_cond_broadcast(&g_play_thread_cond);
- pthread_mutex_unlock(&g_player_control_mutex);
return 0;
}
int ttsd_player_all_stop()
{
- pthread_mutex_lock(&g_player_control_mutex);
if (false == g_player_init) {
SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
- pthread_mutex_unlock(&g_player_control_mutex);
return TTSD_ERROR_OPERATION_FAILED;
}
g_player_thread->requestStop();
SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] player all stop!!");
-
- pthread_mutex_unlock(&g_player_control_mutex);
return 0;
}