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) {
291 g_app_list[index].m_used_voice.clear();
294 __data_show_used_voice_list(index);
297 return TTSD_ERROR_NONE;
300 int ttsd_data_add_speak_data(int uid, speak_data_s* data)
303 index = ttsd_data_is_client(uid);
306 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
307 return TTSD_ERROR_INVALID_PARAMETER;
310 /* mutex is locked */
311 pthread_mutex_lock(&g_speak_data_mutex);
313 g_app_list[index].m_speak_data.insert(g_app_list[index].m_speak_data.end(), data);
315 if (1 == data->utt_id)
316 g_app_list[index].utt_id_stopped = 0;
319 __data_show_text_list(index);
321 pthread_mutex_unlock(&g_speak_data_mutex);
323 return TTSD_ERROR_NONE;
326 int ttsd_data_get_speak_data(int uid, speak_data_s** data)
329 index = ttsd_data_is_client(uid);
332 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid(%d)", uid);
333 return TTSD_ERROR_INVALID_PARAMETER;
336 /* mutex is locked */
337 pthread_mutex_lock(&g_speak_data_mutex);
339 if (0 == g_app_list[index].m_speak_data.size()) {
341 SLOG(LOG_WARN, tts_tag(), "[DATA WARNING] There is no speak data");
343 pthread_mutex_unlock(&g_speak_data_mutex);
347 std::list<speak_data_s*>::iterator iter = g_app_list[index].m_speak_data.begin();
349 if (!g_app_list[index].m_speak_data.empty())
350 g_app_list[index].m_speak_data.pop_front();
353 __data_show_text_list(index);
355 pthread_mutex_unlock(&g_speak_data_mutex);
357 return TTSD_ERROR_NONE;
360 int ttsd_data_add_sound_data(int uid, sound_data_s* data)
363 index = ttsd_data_is_client(uid);
366 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
367 return TTSD_ERROR_INVALID_PARAMETER;
371 SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] sound data is NULL");
372 return TTSD_ERROR_INVALID_PARAMETER;
374 /* mutex is locked */
375 pthread_mutex_lock(&g_sound_data_mutex);
377 g_app_list[index].m_wav_data.insert(g_app_list[index].m_wav_data.end(), data);
380 __data_show_sound_list(index);
383 /* mutex is unlocked */
384 pthread_mutex_unlock(&g_sound_data_mutex);
386 return TTSD_ERROR_NONE;
389 int ttsd_data_get_sound_data(int uid, sound_data_s** data)
392 index = ttsd_data_is_client(uid);
395 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
396 return TTSD_ERROR_INVALID_PARAMETER;
399 /* mutex is locked */
400 pthread_mutex_lock(&g_sound_data_mutex);
402 if (0 == g_app_list[index].m_wav_data.size()) {
404 SLOG(LOG_DEBUG, tts_tag(), "[DATA] There is no wav data");
406 /* mutex is unlocked */
407 pthread_mutex_unlock(&g_sound_data_mutex);
411 std::list<sound_data_s*>::iterator iter = g_app_list[index].m_wav_data.begin();
413 if (!g_app_list[index].m_wav_data.empty())
414 g_app_list[index].m_wav_data.pop_front();
417 __data_show_sound_list(index);
420 /* mutex is unlocked */
421 pthread_mutex_unlock(&g_sound_data_mutex);
423 return TTSD_ERROR_NONE;
426 int ttsd_data_get_sound_data_size(int uid)
430 index = ttsd_data_is_client(uid);
433 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
434 return TTSD_ERROR_INVALID_PARAMETER;
437 /* mutex is locked */
438 pthread_mutex_lock(&g_sound_data_mutex);
439 data_size = g_app_list[index].m_wav_data.size();
441 /* mutex is unlocked */
442 pthread_mutex_unlock(&g_sound_data_mutex);
447 int ttsd_data_clear_data(int uid)
451 index = ttsd_data_is_client(uid);
453 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
454 return TTSD_ERROR_INVALID_PARAMETER;
457 int removed_last_uttid = -1;
458 speak_data_s* temp_speak = NULL;
459 sound_data_s* temp_sound = NULL;
461 /* free allocated data */
463 if (0 != ttsd_data_get_speak_data(uid, &temp_speak)) {
467 if (NULL != temp_speak) {
468 SLOG(LOG_DEBUG, tts_tag(), "[DEBUG] utt(%d), text(%s), lang(%s), vctype(%d) speed(%d)",
469 temp_speak->utt_id, temp_speak->text, temp_speak->lang, temp_speak->vctype, temp_speak->speed);
471 if (NULL != temp_speak->text) {
472 free(temp_speak->text);
473 temp_speak->text = NULL;
475 if (NULL != temp_speak->lang) {
476 free(temp_speak->lang);
477 temp_speak->lang = NULL;
479 removed_last_uttid = temp_speak->utt_id;
486 if (-1 != removed_last_uttid) {
487 g_app_list[index].utt_id_stopped = removed_last_uttid;
491 if (0 != ttsd_data_get_sound_data(uid, &temp_sound)) {
495 if (NULL != temp_sound) {
496 SLOG(LOG_ERROR, tts_tag(), "[DEBUG][%p] uid(%d), event(%d) data(%p) size(%d) rate(%d) utt(%d)",
497 temp_sound, uid, temp_sound->event, temp_sound->data, temp_sound->data_size, temp_sound->rate, temp_sound->utt_id);
499 if (NULL != temp_sound->data) {
500 free(temp_sound->data);
501 temp_sound->data = NULL;
509 pthread_mutex_lock(&g_speak_data_mutex);
510 g_app_list[index].m_speak_data.clear();
511 pthread_mutex_unlock(&g_speak_data_mutex);
513 pthread_mutex_lock(&g_sound_data_mutex);
514 g_app_list[index].m_wav_data.clear();
515 pthread_mutex_unlock(&g_sound_data_mutex);
517 return TTSD_ERROR_NONE;
520 int ttsd_data_get_client_state(int uid, app_state_e* state)
524 index = ttsd_data_is_client(uid);
526 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
527 return TTSD_ERROR_INVALID_PARAMETER;
530 *state = g_app_list[index].state;
532 return TTSD_ERROR_NONE;
535 int ttsd_data_set_client_state(int uid, app_state_e state)
539 index = ttsd_data_is_client(uid);
541 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
542 return TTSD_ERROR_INVALID_PARAMETER;
545 /* The client of playing state of all clients is only one. need to check state. */
546 if (APP_STATE_PLAYING == state) {
547 int vsize = g_app_list.size();
548 for (int i = 0; i < vsize; i++) {
549 if(g_app_list[i].state == APP_STATE_PLAYING) {
550 SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] A playing client has already existed.");
556 g_app_list[index].state = state;
558 return TTSD_ERROR_NONE;
561 int ttsd_data_get_current_playing()
563 int vsize = g_app_list.size();
565 for (int i = 0; i < vsize; i++) {
566 if (APP_STATE_PLAYING == g_app_list[i].state) {
567 SLOG(LOG_DEBUG, tts_tag(), "[DATA] uid(%d) is playing", g_app_list[i].uid);
568 return g_app_list[i].uid;
575 int ttsd_data_foreach_clients(ttsd_data_get_client_cb callback, void* user_data)
577 if (NULL == callback) {
578 SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] input data is NULL!!");
587 vector<app_data_s> temp_app_list;
588 int vsize = g_app_list.size();
591 for (i = 0;i < vsize;i++) {
593 app.pid = g_app_list[i].pid;
594 app.uid = g_app_list[i].uid;
595 app.utt_id_stopped = 0;
596 app.state = g_app_list[i].state;
598 temp_app_list.insert(temp_app_list.end(), app);
601 for (i = 0;i < vsize;i++) {
602 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);
603 if (false == callback(temp_app_list[i].pid, temp_app_list[i].uid, temp_app_list[i].state, user_data)) {
608 for (i = 0;i < vsize;i++) {
609 temp_app_list.erase(temp_app_list.begin());
615 bool ttsd_data_is_uttid_valid(int uid, int uttid)
619 index = ttsd_data_is_client(uid);
621 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
622 return TTSD_ERROR_INVALID_PARAMETER;
625 if (uttid < g_app_list[index].utt_id_stopped)
631 int ttsd_data_is_current_playing()
633 int vsize = g_app_list.size();
635 for (int i = 0; i < vsize; i++) {
636 if(g_app_list[i].state == APP_STATE_PLAYING) {
637 return g_app_list[i].uid;
644 int ttsd_data_get_same_pid_client_count(int pid)
646 int vsize = g_app_list.size();
649 for (int i = 0;i < vsize;i++) {
650 if(g_app_list[i].pid == pid) {
658 int ttsd_data_save_error_log(int uid, FILE* fp)
663 pid = ttsd_data_get_pid(uid);
665 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to get pid");
667 fprintf(fp, "pid - %d", pid);
671 ret = ttsd_data_get_client_state(uid, &state);
673 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to get app state");
675 fprintf(fp, "app state - %d", state);
681 index = ttsd_data_is_client(uid);
683 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Invalid client");
688 fprintf(fp, "----- Sound list -----");
691 std::list<sound_data_s*>::iterator iter;
692 for (iter = g_app_list[index].m_wav_data.begin(); iter != g_app_list[index].m_wav_data.end(); iter++) {
693 SLOG(LOG_DEBUG, tts_tag(), "[%dth][%p] data(%p) data size(%ld), uttid(%d), type(%d)",
694 i, *iter, (*iter)->data, (*iter)->data_size, (*iter)->utt_id, (*iter)->audio_type);
698 fprintf(fp, "----------------------");
701 fprintf(fp, "----- Text list -----");
704 std::list<speak_data_s*>::iterator iter_speak;
705 for (iter_speak = g_app_list[index].m_speak_data.begin(); iter_speak != g_app_list[index].m_speak_data.end(); iter_speak++) {
706 SLOG(LOG_DEBUG, tts_tag(), "[%dth][%p] lang(%s), vctype(%d), speed(%d), uttid(%d), text(%s)",
707 i, *iter_speak, (*iter_speak)->lang, (*iter_speak)->vctype, (*iter_speak)->speed, (*iter_speak)->utt_id, (*iter_speak)->text);
710 fprintf(fp, "---------------------");