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.
18 #include "ttsd_main.h"
19 #include "ttsd_data.h"
36 std::list<speak_data_s*> m_speak_data;
37 std::list<sound_data_s*> m_wav_data;
39 std::list<used_voice_s> m_used_voice;
42 static vector<app_data_s> g_app_list;
44 static pthread_mutex_t g_sound_data_mutex = PTHREAD_MUTEX_INITIALIZER;
49 int __data_show_list()
51 int vsize = g_app_list.size();
53 SLOG(LOG_DEBUG, tts_tag(), "----- client list -----");
55 for (int i=0; i < vsize; i++) {
56 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[%dth] pid(%d), uid(%d), state(%d)", i, g_app_list[i].pid, g_app_list[i].uid, g_app_list[i].state);
60 SLOG(LOG_DEBUG, tts_tag(), "No Client");
63 SLOG(LOG_DEBUG, tts_tag(), "-----------------------");
65 return TTSD_ERROR_NONE;
68 int __data_show_sound_list(int index)
70 SLOG(LOG_DEBUG, tts_tag(), "----- Sound list -----");
73 std::list<sound_data_s*>::iterator iter;
74 for (iter = g_app_list[index].m_wav_data.begin(); iter != g_app_list[index].m_wav_data.end(); iter++) {
75 SLOG(LOG_DEBUG, tts_tag(), "[%dth][%p] data(%p) data size(%ld), uttid(%d), type(%d)",
76 i, *iter, (*iter)->data, (*iter)->data_size, (*iter)->utt_id, (*iter)->audio_type);
81 SLOG(LOG_DEBUG, tts_tag(), "No Sound Data");
84 SLOG(LOG_DEBUG, tts_tag(), "----------------------");
85 return TTSD_ERROR_NONE;
88 int __data_show_text_list(int index)
90 SLOG(LOG_DEBUG, tts_tag(), "----- Text list -----");
93 std::list<speak_data_s*>::iterator iter;
94 for (iter = g_app_list[index].m_speak_data.begin(); iter != g_app_list[index].m_speak_data.end(); iter++) {
95 SLOG(LOG_DEBUG, tts_tag(), "[%dth][%p] lang(%s), vctype(%d), speed(%d), uttid(%d), text(%s)",
96 i + 1, *iter, (*iter)->lang, (*iter)->vctype, (*iter)->speed, (*iter)->utt_id, (*iter)->text);
101 SLOG(LOG_DEBUG, tts_tag(), "No Text Data");
104 SLOG(LOG_DEBUG, tts_tag(), "---------------------");
105 return TTSD_ERROR_NONE;
108 int __data_show_used_voice_list(int index)
110 SLOG(LOG_DEBUG, tts_tag(), "----- Used voice list -----");
113 std::list<used_voice_s>::iterator iter;
114 for (iter = g_app_list[index].m_used_voice.begin(); iter != g_app_list[index].m_used_voice.end(); iter++) {
115 SLOG(LOG_DEBUG, tts_tag(), "[%dth] lang(%s), vctype(%d)", i + 1, iter->lang, iter->vctype);
120 SLOG(LOG_DEBUG, tts_tag(), "No Voice Data");
123 SLOG(LOG_DEBUG, tts_tag(), "---------------------------");
124 return TTSD_ERROR_NONE;
128 * ttsd data functions
131 int ttsd_data_new_client(int pid, int uid)
133 if( -1 != ttsd_data_is_client(uid) ) {
134 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
135 return TTSD_ERROR_INVALID_PARAMETER;
141 app.utt_id_stopped = 0;
142 app.state = APP_STATE_READY;
144 g_app_list.insert(g_app_list.end(), app);
149 return TTSD_ERROR_NONE;
152 int ttsd_data_delete_client(int uid)
156 index = ttsd_data_is_client(uid);
159 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
163 if (0 != ttsd_data_clear_data(uid)) {
164 SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] Fail to clear data");
168 g_app_list.erase(g_app_list.begin()+index);
173 return TTSD_ERROR_NONE;
176 int ttsd_data_is_client(int uid)
178 int vsize = g_app_list.size();
180 for (int i = 0; i < vsize; i++) {
181 if(g_app_list[i].uid == uid) {
189 int ttsd_data_get_client_count()
191 return g_app_list.size();
194 int ttsd_data_get_pid(int uid)
198 index = ttsd_data_is_client(uid);
201 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
202 return TTSD_ERROR_INVALID_PARAMETER;
205 return g_app_list[index].pid;
208 int ttsd_data_get_speak_data_size(int uid)
211 index = ttsd_data_is_client(uid);
214 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
215 return TTSD_ERROR_INVALID_PARAMETER;
218 int size = g_app_list[index].m_speak_data.size();
222 int ttsd_data_set_used_voice(int uid, const char* lang, int type)
225 index = ttsd_data_is_client(uid);
228 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
229 return TTSD_ERROR_INVALID_PARAMETER;
233 std::list<used_voice_s>::iterator iter;
235 for (iter = g_app_list[index].m_used_voice.begin();iter != g_app_list[index].m_used_voice.end();iter++) {
236 if (0 == strcmp(lang, iter->lang) && type == iter->vctype) {
237 SLOG(LOG_DEBUG, tts_tag(), "[DATA] The voice is already registered (%s)(%d)", lang, type);
243 used_voice_s used_voice;
244 used_voice.lang = strdup(lang);
245 used_voice.vctype = type;
247 g_app_list[index].m_used_voice.insert(g_app_list[index].m_used_voice.end(), used_voice);
250 __data_show_used_voice_list(index);
253 return -1; /* Need to load voice*/
256 int ttsd_data_reset_used_voice(int uid, ttsd_used_voice_cb callback)
259 index = ttsd_data_is_client(uid);
262 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
263 return TTSD_ERROR_INVALID_PARAMETER;
266 if (NULL == callback) {
267 SLOG(LOG_WARN, tts_tag(), "[DATA WARNING] Used voice callback is NULL");
271 std::list<used_voice_s>::iterator iter;
273 for (iter = g_app_list[index].m_used_voice.begin(); iter != g_app_list[index].m_used_voice.end(); iter++) {
274 if (NULL != callback) {
275 callback(iter->lang, iter->vctype);
278 if (NULL != iter->lang) {
283 g_app_list[index].m_used_voice.clear();
286 __data_show_used_voice_list(index);
289 return TTSD_ERROR_NONE;
292 int ttsd_data_add_speak_data(int uid, speak_data_s* data)
295 index = ttsd_data_is_client(uid);
298 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
299 return TTSD_ERROR_INVALID_PARAMETER;
302 g_app_list[index].m_speak_data.insert(g_app_list[index].m_speak_data.end(), data);
304 if (1 == data->utt_id)
305 g_app_list[index].utt_id_stopped = 0;
308 __data_show_text_list(index);
310 return TTSD_ERROR_NONE;
313 int ttsd_data_get_speak_data(int uid, speak_data_s** data)
316 index = ttsd_data_is_client(uid);
319 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid(%d)", uid);
320 return TTSD_ERROR_INVALID_PARAMETER;
323 if (0 == g_app_list[index].m_speak_data.size()) {
325 SLOG(LOG_WARN, tts_tag(), "[DATA WARNING] There is no speak data");
330 std::list<speak_data_s*>::iterator iter = g_app_list[index].m_speak_data.begin();
332 g_app_list[index].m_speak_data.pop_front();
335 __data_show_text_list(index);
337 return TTSD_ERROR_NONE;
340 int ttsd_data_add_sound_data(int uid, sound_data_s* data)
343 index = ttsd_data_is_client(uid);
346 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
347 return TTSD_ERROR_INVALID_PARAMETER;
351 SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] sound data is NULL");
352 return TTSD_ERROR_INVALID_PARAMETER;
354 /* mutex is locked */
355 pthread_mutex_lock(&g_sound_data_mutex);
357 g_app_list[index].m_wav_data.insert(g_app_list[index].m_wav_data.end(), data);
360 __data_show_sound_list(index);
363 /* mutex is unlocked */
364 pthread_mutex_unlock(&g_sound_data_mutex);
366 return TTSD_ERROR_NONE;
369 int ttsd_data_get_sound_data(int uid, sound_data_s** data)
372 index = ttsd_data_is_client(uid);
375 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
376 return TTSD_ERROR_INVALID_PARAMETER;
379 /* mutex is locked */
380 pthread_mutex_lock(&g_sound_data_mutex);
382 if (0 == g_app_list[index].m_wav_data.size()) {
384 SLOG(LOG_DEBUG, tts_tag(), "[DATA] There is no wav data");
386 /* mutex is unlocked */
387 pthread_mutex_unlock(&g_sound_data_mutex);
391 std::list<sound_data_s*>::iterator iter = g_app_list[index].m_wav_data.begin();
393 g_app_list[index].m_wav_data.pop_front();
396 __data_show_sound_list(index);
399 /* mutex is unlocked */
400 pthread_mutex_unlock(&g_sound_data_mutex);
402 return TTSD_ERROR_NONE;
405 int ttsd_data_get_sound_data_size(int uid)
409 index = ttsd_data_is_client(uid);
412 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
413 return TTSD_ERROR_INVALID_PARAMETER;
416 /* mutex is locked */
417 pthread_mutex_lock(&g_sound_data_mutex);
418 data_size = g_app_list[index].m_wav_data.size();
420 /* mutex is unlocked */
421 pthread_mutex_unlock(&g_sound_data_mutex);
426 int ttsd_data_clear_data(int uid)
430 index = ttsd_data_is_client(uid);
432 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
433 return TTSD_ERROR_INVALID_PARAMETER;
436 int removed_last_uttid = -1;
437 speak_data_s* temp_speak = NULL;
438 sound_data_s* temp_sound = NULL;
440 /* free allocated data */
442 if (0 != ttsd_data_get_speak_data(uid, &temp_speak)) {
446 if (NULL != temp_speak) {
447 SLOG(LOG_DEBUG, tts_tag(), "[DEBUG] utt(%d), text(%s), lang(%s), vctype(%d) speed(%d)",
448 temp_speak->utt_id, temp_speak->text, temp_speak->lang, temp_speak->vctype, temp_speak->speed);
450 if (NULL != temp_speak->text) {
451 free(temp_speak->text);
452 temp_speak->text = NULL;
454 if (NULL != temp_speak->lang) {
455 free(temp_speak->lang);
456 temp_speak->lang = NULL;
458 removed_last_uttid = temp_speak->utt_id;
465 if (-1 != removed_last_uttid) {
466 g_app_list[index].utt_id_stopped = removed_last_uttid;
470 if (0 != ttsd_data_get_sound_data(uid, &temp_sound)) {
474 if (NULL != temp_sound) {
475 SLOG(LOG_ERROR, tts_tag(), "[DEBUG][%p] uid(%d), event(%d) data(%p) size(%d) rate(%d) utt(%d)",
476 temp_sound, uid, temp_sound->event, temp_sound->data, temp_sound->data_size, temp_sound->rate, temp_sound->utt_id);
478 if (NULL != temp_sound->data) {
479 free(temp_sound->data);
480 temp_sound->data = NULL;
488 g_app_list[index].m_speak_data.clear();
489 g_app_list[index].m_wav_data.clear();
491 return TTSD_ERROR_NONE;
494 int ttsd_data_get_client_state(int uid, app_state_e* state)
498 index = ttsd_data_is_client(uid);
500 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
501 return TTSD_ERROR_INVALID_PARAMETER;
504 *state = g_app_list[index].state;
506 return TTSD_ERROR_NONE;
509 int ttsd_data_set_client_state(int uid, app_state_e state)
513 index = ttsd_data_is_client(uid);
515 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
516 return TTSD_ERROR_INVALID_PARAMETER;
519 /* The client of playing state of all clients is only one. need to check state. */
520 if (APP_STATE_PLAYING == state) {
521 int vsize = g_app_list.size();
522 for (int i = 0; i < vsize; i++) {
523 if(g_app_list[i].state == APP_STATE_PLAYING) {
524 SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] A playing client has already existed.");
530 g_app_list[index].state = state;
532 return TTSD_ERROR_NONE;
535 int ttsd_data_get_current_playing()
537 int vsize = g_app_list.size();
539 for (int i = 0; i < vsize; i++) {
540 if (APP_STATE_PLAYING == g_app_list[i].state) {
541 SLOG(LOG_DEBUG, tts_tag(), "[DATA] uid(%d) is playing", g_app_list[i].uid);
542 return g_app_list[i].uid;
549 int ttsd_data_foreach_clients(ttsd_data_get_client_cb callback, void* user_data)
551 if (NULL == callback) {
552 SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] input data is NULL!!");
561 vector<app_data_s> temp_app_list;
562 int vsize = g_app_list.size();
565 for (i = 0;i < vsize;i++) {
567 app.pid = g_app_list[i].pid;
568 app.uid = g_app_list[i].uid;
569 app.utt_id_stopped = 0;
570 app.state = g_app_list[i].state;
572 temp_app_list.insert(temp_app_list.end(), app);
575 for (i = 0;i < vsize;i++) {
576 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[%dth] pid(%d), uid(%d), state(%d)", i, temp_app_list[i].pid, temp_app_list[i].uid, temp_app_list[i].state);
577 if (false == callback(temp_app_list[i].pid, temp_app_list[i].uid, temp_app_list[i].state, user_data)) {
582 for (i = 0;i < vsize;i++) {
583 temp_app_list.erase(temp_app_list.begin());
589 bool ttsd_data_is_uttid_valid(int uid, int uttid)
593 index = ttsd_data_is_client(uid);
595 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
596 return TTSD_ERROR_INVALID_PARAMETER;
599 if (uttid < g_app_list[index].utt_id_stopped)
605 int ttsd_data_is_current_playing()
607 int vsize = g_app_list.size();
609 for (int i = 0; i < vsize; i++) {
610 if(g_app_list[i].state == APP_STATE_PLAYING) {
611 return g_app_list[i].uid;
618 int ttsd_data_get_same_pid_client_count(int pid)
620 int vsize = g_app_list.size();
623 for (int i = 0;i < vsize;i++) {
624 if(g_app_list[i].pid == pid) {
632 int ttsd_data_save_error_log(int uid, FILE* fp)
637 pid = ttsd_data_get_pid(uid);
639 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to get pid");
641 fprintf(fp, "pid - %d", pid);
645 ret = ttsd_data_get_client_state(uid, &state);
647 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to get app state");
649 fprintf(fp, "app state - %d", state);
655 index = ttsd_data_is_client(uid);
657 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Invalid client");
662 fprintf(fp, "----- Sound list -----");
665 std::list<sound_data_s*>::iterator iter;
666 for (iter = g_app_list[index].m_wav_data.begin(); iter != g_app_list[index].m_wav_data.end(); iter++) {
667 SLOG(LOG_DEBUG, tts_tag(), "[%dth][%p] data(%p) data size(%ld), uttid(%d), type(%d)",
668 i, *iter, (*iter)->data, (*iter)->data_size, (*iter)->utt_id, (*iter)->audio_type);
672 fprintf(fp, "----------------------");
675 fprintf(fp, "----- Text list -----");
678 std::list<speak_data_s*>::iterator iter_speak;
679 for (iter_speak = g_app_list[index].m_speak_data.begin(); iter_speak != g_app_list[index].m_speak_data.end(); iter_speak++) {
680 SLOG(LOG_DEBUG, tts_tag(), "[%dth][%p] lang(%s), vctype(%d), speed(%d), uttid(%d), text(%s)",
681 i, *iter_speak, (*iter_speak)->lang, (*iter_speak)->vctype, (*iter_speak)->speed, (*iter_speak)->utt_id, (*iter_speak)->text);
684 fprintf(fp, "---------------------");