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_speak_data_mutex = PTHREAD_MUTEX_INITIALIZER;
45 static pthread_mutex_t g_sound_data_mutex = PTHREAD_MUTEX_INITIALIZER;
50 int __data_show_list()
52 int vsize = g_app_list.size();
54 SLOG(LOG_DEBUG, tts_tag(), "----- client list -----");
56 for (int i=0; i < vsize; i++) {
57 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);
61 SLOG(LOG_DEBUG, tts_tag(), "No Client");
64 SLOG(LOG_DEBUG, tts_tag(), "-----------------------");
66 return TTSD_ERROR_NONE;
69 int __data_show_sound_list(int index)
71 SLOG(LOG_DEBUG, tts_tag(), "----- Sound list -----");
74 std::list<sound_data_s*>::iterator iter;
75 for (iter = g_app_list[index].m_wav_data.begin(); iter != g_app_list[index].m_wav_data.end(); iter++) {
76 SLOG(LOG_DEBUG, tts_tag(), "[%dth][%p] data(%p) data size(%ld), uttid(%d), type(%d)",
77 i, *iter, (*iter)->data, (*iter)->data_size, (*iter)->utt_id, (*iter)->audio_type);
82 SLOG(LOG_DEBUG, tts_tag(), "No Sound Data");
85 SLOG(LOG_DEBUG, tts_tag(), "----------------------");
86 return TTSD_ERROR_NONE;
89 int __data_show_text_list(int index)
91 SLOG(LOG_DEBUG, tts_tag(), "----- Text list -----");
94 std::list<speak_data_s*>::iterator iter;
95 for (iter = g_app_list[index].m_speak_data.begin(); iter != g_app_list[index].m_speak_data.end(); iter++) {
96 SLOG(LOG_DEBUG, tts_tag(), "[%dth][%p] lang(%s), vctype(%d), speed(%d), uttid(%d), text(%s)",
97 i + 1, *iter, (*iter)->lang, (*iter)->vctype, (*iter)->speed, (*iter)->utt_id, (*iter)->text);
102 SLOG(LOG_DEBUG, tts_tag(), "No Text Data");
105 SLOG(LOG_DEBUG, tts_tag(), "---------------------");
106 return TTSD_ERROR_NONE;
109 int __data_show_used_voice_list(int index)
111 SLOG(LOG_DEBUG, tts_tag(), "----- Used voice list -----");
114 std::list<used_voice_s>::iterator iter;
115 for (iter = g_app_list[index].m_used_voice.begin(); iter != g_app_list[index].m_used_voice.end(); iter++) {
116 SLOG(LOG_DEBUG, tts_tag(), "[%dth] lang(%s), vctype(%d)", i + 1, iter->lang, iter->vctype);
121 SLOG(LOG_DEBUG, tts_tag(), "No Voice Data");
124 SLOG(LOG_DEBUG, tts_tag(), "---------------------------");
125 return TTSD_ERROR_NONE;
129 * ttsd data functions
132 int ttsd_data_new_client(int pid, int uid)
134 if( -1 != ttsd_data_is_client(uid) ) {
135 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
136 return TTSD_ERROR_INVALID_PARAMETER;
142 app.utt_id_stopped = 0;
143 app.state = APP_STATE_READY;
145 g_app_list.insert(g_app_list.end(), app);
150 return TTSD_ERROR_NONE;
153 int ttsd_data_delete_client(int uid)
157 index = ttsd_data_is_client(uid);
160 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
164 if (0 != ttsd_data_clear_data(uid)) {
165 SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] Fail to clear data");
169 g_app_list.erase(g_app_list.begin()+index);
174 return TTSD_ERROR_NONE;
177 int ttsd_data_is_client(int uid)
179 int vsize = g_app_list.size();
181 for (int i = 0; i < vsize; i++) {
182 if(g_app_list[i].uid == uid) {
190 int ttsd_data_get_client_count()
192 return g_app_list.size();
195 int ttsd_data_get_pid(int uid)
199 index = ttsd_data_is_client(uid);
202 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
203 return TTSD_ERROR_INVALID_PARAMETER;
206 return g_app_list[index].pid;
209 int ttsd_data_get_speak_data_size(int uid)
212 index = ttsd_data_is_client(uid);
215 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
216 return TTSD_ERROR_INVALID_PARAMETER;
219 /* mutex is locked */
220 pthread_mutex_lock(&g_speak_data_mutex);
221 int size = g_app_list[index].m_speak_data.size();
223 /* mutex is unlocked */
224 pthread_mutex_unlock(&g_speak_data_mutex);
229 int ttsd_data_set_used_voice(int uid, const char* lang, int type)
232 index = ttsd_data_is_client(uid);
235 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
236 return TTSD_ERROR_INVALID_PARAMETER;
240 std::list<used_voice_s>::iterator iter;
242 for (iter = g_app_list[index].m_used_voice.begin();iter != g_app_list[index].m_used_voice.end();iter++) {
243 if (0 == strcmp(lang, iter->lang) && type == iter->vctype) {
244 SLOG(LOG_DEBUG, tts_tag(), "[DATA] The voice is already registered (%s)(%d)", lang, type);
250 used_voice_s used_voice;
251 used_voice.lang = strdup(lang);
252 used_voice.vctype = type;
254 g_app_list[index].m_used_voice.insert(g_app_list[index].m_used_voice.end(), used_voice);
257 __data_show_used_voice_list(index);
260 return -1; /* Need to load voice*/
263 int ttsd_data_reset_used_voice(int uid, ttsd_used_voice_cb callback)
266 index = ttsd_data_is_client(uid);
269 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
270 return TTSD_ERROR_INVALID_PARAMETER;
273 if (NULL == callback) {
274 SLOG(LOG_WARN, tts_tag(), "[DATA WARNING] Used voice callback is NULL");
278 std::list<used_voice_s>::iterator iter;
280 for (iter = g_app_list[index].m_used_voice.begin(); iter != g_app_list[index].m_used_voice.end(); iter++) {
281 if (NULL != callback) {
282 callback(iter->lang, iter->vctype);
285 if (NULL != iter->lang) {
290 g_app_list[index].m_used_voice.clear();
293 __data_show_used_voice_list(index);
296 return TTSD_ERROR_NONE;
299 int ttsd_data_add_speak_data(int uid, speak_data_s* data)
302 index = ttsd_data_is_client(uid);
305 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
306 return TTSD_ERROR_INVALID_PARAMETER;
309 /* mutex is locked */
310 pthread_mutex_lock(&g_speak_data_mutex);
312 g_app_list[index].m_speak_data.insert(g_app_list[index].m_speak_data.end(), data);
314 if (1 == data->utt_id)
315 g_app_list[index].utt_id_stopped = 0;
318 __data_show_text_list(index);
320 pthread_mutex_unlock(&g_speak_data_mutex);
322 return TTSD_ERROR_NONE;
325 int ttsd_data_get_speak_data(int uid, speak_data_s** data)
328 index = ttsd_data_is_client(uid);
331 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid(%d)", uid);
332 return TTSD_ERROR_INVALID_PARAMETER;
335 /* mutex is locked */
336 pthread_mutex_lock(&g_speak_data_mutex);
338 if (0 == g_app_list[index].m_speak_data.size()) {
340 SLOG(LOG_WARN, tts_tag(), "[DATA WARNING] There is no speak data");
342 pthread_mutex_unlock(&g_speak_data_mutex);
346 std::list<speak_data_s*>::iterator iter = g_app_list[index].m_speak_data.begin();
348 g_app_list[index].m_speak_data.pop_front();
351 __data_show_text_list(index);
353 pthread_mutex_unlock(&g_speak_data_mutex);
355 return TTSD_ERROR_NONE;
358 int ttsd_data_add_sound_data(int uid, sound_data_s* data)
361 index = ttsd_data_is_client(uid);
364 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
365 return TTSD_ERROR_INVALID_PARAMETER;
369 SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] sound data is NULL");
370 return TTSD_ERROR_INVALID_PARAMETER;
372 /* mutex is locked */
373 pthread_mutex_lock(&g_sound_data_mutex);
375 g_app_list[index].m_wav_data.insert(g_app_list[index].m_wav_data.end(), data);
378 __data_show_sound_list(index);
381 /* mutex is unlocked */
382 pthread_mutex_unlock(&g_sound_data_mutex);
384 return TTSD_ERROR_NONE;
387 int ttsd_data_get_sound_data(int uid, sound_data_s** data)
390 index = ttsd_data_is_client(uid);
393 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
394 return TTSD_ERROR_INVALID_PARAMETER;
397 /* mutex is locked */
398 pthread_mutex_lock(&g_sound_data_mutex);
400 if (0 == g_app_list[index].m_wav_data.size()) {
402 SLOG(LOG_DEBUG, tts_tag(), "[DATA] There is no wav data");
404 /* mutex is unlocked */
405 pthread_mutex_unlock(&g_sound_data_mutex);
409 std::list<sound_data_s*>::iterator iter = g_app_list[index].m_wav_data.begin();
411 g_app_list[index].m_wav_data.pop_front();
414 __data_show_sound_list(index);
417 /* mutex is unlocked */
418 pthread_mutex_unlock(&g_sound_data_mutex);
420 return TTSD_ERROR_NONE;
423 int ttsd_data_get_sound_data_size(int uid)
427 index = ttsd_data_is_client(uid);
430 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
431 return TTSD_ERROR_INVALID_PARAMETER;
434 /* mutex is locked */
435 pthread_mutex_lock(&g_sound_data_mutex);
436 data_size = g_app_list[index].m_wav_data.size();
438 /* mutex is unlocked */
439 pthread_mutex_unlock(&g_sound_data_mutex);
444 int ttsd_data_clear_data(int uid)
448 index = ttsd_data_is_client(uid);
450 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
451 return TTSD_ERROR_INVALID_PARAMETER;
454 int removed_last_uttid = -1;
455 speak_data_s* temp_speak = NULL;
456 sound_data_s* temp_sound = NULL;
458 /* free allocated data */
460 if (0 != ttsd_data_get_speak_data(uid, &temp_speak)) {
464 if (NULL != temp_speak) {
465 SLOG(LOG_DEBUG, tts_tag(), "[DEBUG] utt(%d), text(%s), lang(%s), vctype(%d) speed(%d)",
466 temp_speak->utt_id, temp_speak->text, temp_speak->lang, temp_speak->vctype, temp_speak->speed);
468 if (NULL != temp_speak->text) {
469 free(temp_speak->text);
470 temp_speak->text = NULL;
472 if (NULL != temp_speak->lang) {
473 free(temp_speak->lang);
474 temp_speak->lang = NULL;
476 removed_last_uttid = temp_speak->utt_id;
483 if (-1 != removed_last_uttid) {
484 g_app_list[index].utt_id_stopped = removed_last_uttid;
488 if (0 != ttsd_data_get_sound_data(uid, &temp_sound)) {
492 if (NULL != temp_sound) {
493 SLOG(LOG_ERROR, tts_tag(), "[DEBUG][%p] uid(%d), event(%d) data(%p) size(%d) rate(%d) utt(%d)",
494 temp_sound, uid, temp_sound->event, temp_sound->data, temp_sound->data_size, temp_sound->rate, temp_sound->utt_id);
496 if (NULL != temp_sound->data) {
497 free(temp_sound->data);
498 temp_sound->data = NULL;
506 /* mutex is locked */
507 pthread_mutex_lock(&g_sound_data_mutex);
509 g_app_list[index].m_speak_data.clear();
510 g_app_list[index].m_wav_data.clear();
512 /* mutex is unlocked */
513 pthread_mutex_unlock(&g_sound_data_mutex);
515 return TTSD_ERROR_NONE;
518 int ttsd_data_get_client_state(int uid, app_state_e* state)
522 index = ttsd_data_is_client(uid);
524 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
525 return TTSD_ERROR_INVALID_PARAMETER;
528 *state = g_app_list[index].state;
530 return TTSD_ERROR_NONE;
533 int ttsd_data_set_client_state(int uid, app_state_e state)
537 index = ttsd_data_is_client(uid);
539 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
540 return TTSD_ERROR_INVALID_PARAMETER;
543 /* The client of playing state of all clients is only one. need to check state. */
544 if (APP_STATE_PLAYING == state) {
545 int vsize = g_app_list.size();
546 for (int i = 0; i < vsize; i++) {
547 if(g_app_list[i].state == APP_STATE_PLAYING) {
548 SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] A playing client has already existed.");
554 g_app_list[index].state = state;
556 return TTSD_ERROR_NONE;
559 int ttsd_data_get_current_playing()
561 int vsize = g_app_list.size();
563 for (int i = 0; i < vsize; i++) {
564 if (APP_STATE_PLAYING == g_app_list[i].state) {
565 SLOG(LOG_DEBUG, tts_tag(), "[DATA] uid(%d) is playing", g_app_list[i].uid);
566 return g_app_list[i].uid;
573 int ttsd_data_foreach_clients(ttsd_data_get_client_cb callback, void* user_data)
575 if (NULL == callback) {
576 SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] input data is NULL!!");
585 vector<app_data_s> temp_app_list;
586 int vsize = g_app_list.size();
589 for (i = 0;i < vsize;i++) {
591 app.pid = g_app_list[i].pid;
592 app.uid = g_app_list[i].uid;
593 app.utt_id_stopped = 0;
594 app.state = g_app_list[i].state;
596 temp_app_list.insert(temp_app_list.end(), app);
599 for (i = 0;i < vsize;i++) {
600 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);
601 if (false == callback(temp_app_list[i].pid, temp_app_list[i].uid, temp_app_list[i].state, user_data)) {
606 for (i = 0;i < vsize;i++) {
607 temp_app_list.erase(temp_app_list.begin());
613 bool ttsd_data_is_uttid_valid(int uid, int uttid)
617 index = ttsd_data_is_client(uid);
619 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
620 return TTSD_ERROR_INVALID_PARAMETER;
623 if (uttid < g_app_list[index].utt_id_stopped)
629 int ttsd_data_is_current_playing()
631 int vsize = g_app_list.size();
633 for (int i = 0; i < vsize; i++) {
634 if(g_app_list[i].state == APP_STATE_PLAYING) {
635 return g_app_list[i].uid;
642 int ttsd_data_get_same_pid_client_count(int pid)
644 int vsize = g_app_list.size();
647 for (int i = 0;i < vsize;i++) {
648 if(g_app_list[i].pid == pid) {
656 int ttsd_data_save_error_log(int uid, FILE* fp)
661 pid = ttsd_data_get_pid(uid);
663 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to get pid");
665 fprintf(fp, "pid - %d", pid);
669 ret = ttsd_data_get_client_state(uid, &state);
671 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to get app state");
673 fprintf(fp, "app state - %d", state);
679 index = ttsd_data_is_client(uid);
681 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Invalid client");
686 fprintf(fp, "----- Sound list -----");
689 std::list<sound_data_s*>::iterator iter;
690 for (iter = g_app_list[index].m_wav_data.begin(); iter != g_app_list[index].m_wav_data.end(); iter++) {
691 SLOG(LOG_DEBUG, tts_tag(), "[%dth][%p] data(%p) data size(%ld), uttid(%d), type(%d)",
692 i, *iter, (*iter)->data, (*iter)->data_size, (*iter)->utt_id, (*iter)->audio_type);
696 fprintf(fp, "----------------------");
699 fprintf(fp, "----- Text list -----");
702 std::list<speak_data_s*>::iterator iter_speak;
703 for (iter_speak = g_app_list[index].m_speak_data.begin(); iter_speak != g_app_list[index].m_speak_data.end(); iter_speak++) {
704 SLOG(LOG_DEBUG, tts_tag(), "[%dth][%p] lang(%s), vctype(%d), speed(%d), uttid(%d), text(%s)",
705 i, *iter_speak, (*iter_speak)->lang, (*iter_speak)->vctype, (*iter_speak)->speed, (*iter_speak)->utt_id, (*iter_speak)->text);
708 fprintf(fp, "---------------------");