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 "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, get_tag(), "----- client list -----");
55 for (int i=0; i<vsize; i++) {
56 SECURE_SLOG(LOG_DEBUG, get_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, get_tag(), "No Client");
63 SLOG(LOG_DEBUG, get_tag(), "-----------------------");
65 return TTSD_ERROR_NONE;
68 int __data_show_sound_list(int index)
70 SLOG(LOG_DEBUG, get_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, get_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, get_tag(), "No Sound Data");
84 SLOG(LOG_DEBUG, get_tag(), "----------------------");
85 return TTSD_ERROR_NONE;
88 int __data_show_text_list(int index)
90 SLOG(LOG_DEBUG, get_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, get_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, get_tag(), "No Text Data");
104 SLOG(LOG_DEBUG, get_tag(), "---------------------");
105 return TTSD_ERROR_NONE;
108 int __data_show_used_voice_list(int index)
110 SLOG(LOG_DEBUG, get_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, get_tag(), "[%dth] lang(%s), vctype(%d)", i + 1, iter->lang, iter->vctype);
120 SLOG(LOG_DEBUG, get_tag(), "No Voice Data");
123 SLOG(LOG_DEBUG, get_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, get_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, get_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
163 if (0 != ttsd_data_clear_data(uid)) {
164 SLOG(LOG_ERROR, get_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, get_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, get_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, get_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, get_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, get_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
263 return TTSD_ERROR_INVALID_PARAMETER;
266 if (NULL == callback) {
267 SLOG(LOG_WARN, get_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, get_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, get_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, get_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, get_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
347 return TTSD_ERROR_INVALID_PARAMETER;
351 SLOG(LOG_ERROR, get_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, get_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, get_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)
408 index = ttsd_data_is_client(uid);
411 SECURE_SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
412 return TTSD_ERROR_INVALID_PARAMETER;
415 return g_app_list[index].m_wav_data.size();
418 int ttsd_data_clear_data(int uid)
422 index = ttsd_data_is_client(uid);
424 SECURE_SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
425 return TTSD_ERROR_INVALID_PARAMETER;
428 int removed_last_uttid = -1;
429 speak_data_s* temp_speak = NULL;
430 sound_data_s* temp_sound = NULL;
432 /* free allocated data */
434 if (0 != ttsd_data_get_speak_data(uid, &temp_speak)) {
438 if (NULL != temp_speak) {
439 SLOG(LOG_DEBUG, get_tag(), "[DEBUG] utt(%d), text(%s), lang(%s), vctype(%d) speed(%d)",
440 temp_speak->utt_id, temp_speak->text, temp_speak->lang, temp_speak->vctype, temp_speak->speed);
442 if (NULL != temp_speak->text) {
443 free(temp_speak->text);
444 temp_speak->text = NULL;
446 if (NULL != temp_speak->lang) {
447 free(temp_speak->lang);
448 temp_speak->lang = NULL;
450 removed_last_uttid = temp_speak->utt_id;
457 if (-1 != removed_last_uttid) {
458 g_app_list[index].utt_id_stopped = removed_last_uttid;
462 if (0 != ttsd_data_get_sound_data(uid, &temp_sound)) {
466 if (NULL != temp_sound) {
467 SLOG(LOG_ERROR, get_tag(), "[DEBUG][%p] uid(%d), event(%d) data(%p) size(%d) rate(%d) utt(%d)",
468 temp_sound, uid, temp_sound->event, temp_sound->data, temp_sound->data_size, temp_sound->rate, temp_sound->utt_id);
470 if (NULL != temp_sound->data) {
471 free(temp_sound->data);
472 temp_sound->data = NULL;
480 g_app_list[index].m_speak_data.clear();
481 g_app_list[index].m_wav_data.clear();
483 return TTSD_ERROR_NONE;
486 int ttsd_data_get_client_state(int uid, app_state_e* state)
490 index = ttsd_data_is_client(uid);
492 SECURE_SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
493 return TTSD_ERROR_INVALID_PARAMETER;
496 *state = g_app_list[index].state;
498 return TTSD_ERROR_NONE;
501 int ttsd_data_set_client_state(int uid, app_state_e state)
505 index = ttsd_data_is_client(uid);
507 SECURE_SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
508 return TTSD_ERROR_INVALID_PARAMETER;
511 /* The client of playing state of all clients is only one. need to check state. */
512 if (APP_STATE_PLAYING == state) {
513 int vsize = g_app_list.size();
514 for (int i=0 ; i<vsize ; i++) {
515 if(g_app_list[i].state == APP_STATE_PLAYING) {
516 SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] A playing client has already existed.");
522 g_app_list[index].state = state;
524 return TTSD_ERROR_NONE;
527 int ttsd_data_get_current_playing()
529 int vsize = g_app_list.size();
531 for (int i=0; i<vsize; i++) {
532 if (APP_STATE_PLAYING == g_app_list[i].state) {
533 SLOG(LOG_DEBUG, get_tag(), "[DATA] uid(%d) is playing", g_app_list[i].uid);
534 return g_app_list[i].uid;
541 int ttsd_data_foreach_clients(ttsd_data_get_client_cb callback, void* user_data)
543 if (NULL == callback) {
544 SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] input data is NULL!!");
553 vector<app_data_s> temp_app_list;
554 int vsize = g_app_list.size();
557 for (i = 0;i < vsize;i++) {
559 app.pid = g_app_list[i].pid;
560 app.uid = g_app_list[i].uid;
561 app.utt_id_stopped = 0;
562 app.state = g_app_list[i].state;
564 temp_app_list.insert(temp_app_list.end(), app);
567 for (i = 0;i < vsize;i++) {
568 SECURE_SLOG(LOG_DEBUG, get_tag(), "[%dth] pid(%d), uid(%d), state(%d)", i, temp_app_list[i].pid, temp_app_list[i].uid, temp_app_list[i].state);
569 if (false == callback(temp_app_list[i].pid, temp_app_list[i].uid, temp_app_list[i].state, user_data)) {
574 for (i = 0;i < vsize;i++) {
575 temp_app_list.erase(temp_app_list.begin());
581 bool ttsd_data_is_uttid_valid(int uid, int uttid)
585 index = ttsd_data_is_client(uid);
587 SECURE_SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
588 return TTSD_ERROR_INVALID_PARAMETER;
591 if (uttid < g_app_list[index].utt_id_stopped)
597 int ttsd_data_is_current_playing()
599 int vsize = g_app_list.size();
601 for (int i=0; i<vsize; i++) {
602 if(g_app_list[i].state == APP_STATE_PLAYING) {
603 return g_app_list[i].uid;
610 int ttsd_data_get_same_pid_client_count(int pid)
612 int vsize = g_app_list.size();
615 for (int i = 0;i < vsize;i++) {
616 if(g_app_list[i].pid == pid) {
624 int ttsd_data_save_error_log(int uid, FILE* fp)
629 pid = ttsd_data_get_pid(uid);
631 SLOG(LOG_ERROR, get_tag(), "[ERROR] Fail to get pid");
633 fprintf(fp, "pid - %d", pid);
637 ret = ttsd_data_get_client_state(uid, &state);
639 SLOG(LOG_ERROR, get_tag(), "[ERROR] Fail to get app state");
641 fprintf(fp, "app state - %d", state);
647 index = ttsd_data_is_client(uid);
649 SLOG(LOG_ERROR, get_tag(), "[ERROR] Invalid client");
654 fprintf(fp, "----- Sound list -----");
657 std::list<sound_data_s*>::iterator iter;
658 for (iter = g_app_list[index].m_wav_data.begin(); iter != g_app_list[index].m_wav_data.end(); iter++) {
659 SLOG(LOG_DEBUG, get_tag(), "[%dth][%p] data(%p) data size(%ld), uttid(%d), type(%d)",
660 i, *iter, (*iter)->data, (*iter)->data_size, (*iter)->utt_id, (*iter)->audio_type);
664 fprintf(fp, "----------------------");
667 fprintf(fp, "----- Text list -----");
670 std::list<speak_data_s*>::iterator iter_speak;
671 for (iter_speak = g_app_list[index].m_speak_data.begin(); iter_speak != g_app_list[index].m_speak_data.end(); iter_speak++) {
672 SLOG(LOG_DEBUG, get_tag(), "[%dth][%p] lang(%s), vctype(%d), speed(%d), uttid(%d), text(%s)",
673 i, *iter_speak, (*iter_speak)->lang, (*iter_speak)->vctype, (*iter_speak)->speed, (*iter_speak)->utt_id, (*iter_speak)->text);
676 fprintf(fp, "---------------------");