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 if (!g_app_list[index].m_speak_data.empty())
349 g_app_list[index].m_speak_data.pop_front();
352 __data_show_text_list(index);
354 pthread_mutex_unlock(&g_speak_data_mutex);
356 return TTSD_ERROR_NONE;
359 int ttsd_data_add_sound_data(int uid, sound_data_s* data)
362 index = ttsd_data_is_client(uid);
365 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
366 return TTSD_ERROR_INVALID_PARAMETER;
370 SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] sound data is NULL");
371 return TTSD_ERROR_INVALID_PARAMETER;
373 /* mutex is locked */
374 pthread_mutex_lock(&g_sound_data_mutex);
376 g_app_list[index].m_wav_data.insert(g_app_list[index].m_wav_data.end(), data);
379 __data_show_sound_list(index);
382 /* mutex is unlocked */
383 pthread_mutex_unlock(&g_sound_data_mutex);
385 return TTSD_ERROR_NONE;
388 int ttsd_data_get_sound_data(int uid, sound_data_s** data)
391 index = ttsd_data_is_client(uid);
394 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
395 return TTSD_ERROR_INVALID_PARAMETER;
398 /* mutex is locked */
399 pthread_mutex_lock(&g_sound_data_mutex);
401 if (0 == g_app_list[index].m_wav_data.size()) {
403 SLOG(LOG_DEBUG, tts_tag(), "[DATA] There is no wav data");
405 /* mutex is unlocked */
406 pthread_mutex_unlock(&g_sound_data_mutex);
410 std::list<sound_data_s*>::iterator iter = g_app_list[index].m_wav_data.begin();
412 if (!g_app_list[index].m_wav_data.empty())
413 g_app_list[index].m_wav_data.pop_front();
416 __data_show_sound_list(index);
419 /* mutex is unlocked */
420 pthread_mutex_unlock(&g_sound_data_mutex);
422 return TTSD_ERROR_NONE;
425 int ttsd_data_get_sound_data_size(int uid)
429 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 /* mutex is locked */
437 pthread_mutex_lock(&g_sound_data_mutex);
438 data_size = g_app_list[index].m_wav_data.size();
440 /* mutex is unlocked */
441 pthread_mutex_unlock(&g_sound_data_mutex);
446 int ttsd_data_clear_data(int uid)
450 index = ttsd_data_is_client(uid);
452 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
453 return TTSD_ERROR_INVALID_PARAMETER;
456 int removed_last_uttid = -1;
457 speak_data_s* temp_speak = NULL;
458 sound_data_s* temp_sound = NULL;
460 /* free allocated data */
462 if (0 != ttsd_data_get_speak_data(uid, &temp_speak)) {
466 if (NULL != temp_speak) {
467 SLOG(LOG_DEBUG, tts_tag(), "[DEBUG] utt(%d), text(%s), lang(%s), vctype(%d) speed(%d)",
468 temp_speak->utt_id, temp_speak->text, temp_speak->lang, temp_speak->vctype, temp_speak->speed);
470 if (NULL != temp_speak->text) {
471 free(temp_speak->text);
472 temp_speak->text = NULL;
474 if (NULL != temp_speak->lang) {
475 free(temp_speak->lang);
476 temp_speak->lang = NULL;
478 removed_last_uttid = temp_speak->utt_id;
485 if (-1 != removed_last_uttid) {
486 g_app_list[index].utt_id_stopped = removed_last_uttid;
490 if (0 != ttsd_data_get_sound_data(uid, &temp_sound)) {
494 if (NULL != temp_sound) {
495 SLOG(LOG_ERROR, tts_tag(), "[DEBUG][%p] uid(%d), event(%d) data(%p) size(%d) rate(%d) utt(%d)",
496 temp_sound, uid, temp_sound->event, temp_sound->data, temp_sound->data_size, temp_sound->rate, temp_sound->utt_id);
498 if (NULL != temp_sound->data) {
499 free(temp_sound->data);
500 temp_sound->data = NULL;
508 pthread_mutex_lock(&g_speak_data_mutex);
509 g_app_list[index].m_speak_data.clear();
510 pthread_mutex_unlock(&g_speak_data_mutex);
512 pthread_mutex_lock(&g_sound_data_mutex);
513 g_app_list[index].m_wav_data.clear();
514 pthread_mutex_unlock(&g_sound_data_mutex);
516 return TTSD_ERROR_NONE;
519 int ttsd_data_get_client_state(int uid, app_state_e* state)
523 index = ttsd_data_is_client(uid);
525 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
526 return TTSD_ERROR_INVALID_PARAMETER;
529 *state = g_app_list[index].state;
531 return TTSD_ERROR_NONE;
534 int ttsd_data_set_client_state(int uid, app_state_e state)
538 index = ttsd_data_is_client(uid);
540 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
541 return TTSD_ERROR_INVALID_PARAMETER;
544 /* The client of playing state of all clients is only one. need to check state. */
545 if (APP_STATE_PLAYING == state) {
546 int vsize = g_app_list.size();
547 for (int i = 0; i < vsize; i++) {
548 if(g_app_list[i].state == APP_STATE_PLAYING) {
549 SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] A playing client has already existed.");
555 g_app_list[index].state = state;
557 return TTSD_ERROR_NONE;
560 int ttsd_data_get_current_playing()
562 int vsize = g_app_list.size();
564 for (int i = 0; i < vsize; i++) {
565 if (APP_STATE_PLAYING == g_app_list[i].state) {
566 SLOG(LOG_DEBUG, tts_tag(), "[DATA] uid(%d) is playing", g_app_list[i].uid);
567 return g_app_list[i].uid;
574 int ttsd_data_foreach_clients(ttsd_data_get_client_cb callback, void* user_data)
576 if (NULL == callback) {
577 SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] input data is NULL!!");
586 vector<app_data_s> temp_app_list;
587 int vsize = g_app_list.size();
590 for (i = 0;i < vsize;i++) {
592 app.pid = g_app_list[i].pid;
593 app.uid = g_app_list[i].uid;
594 app.utt_id_stopped = 0;
595 app.state = g_app_list[i].state;
597 temp_app_list.insert(temp_app_list.end(), app);
600 for (i = 0;i < vsize;i++) {
601 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);
602 if (false == callback(temp_app_list[i].pid, temp_app_list[i].uid, temp_app_list[i].state, user_data)) {
607 for (i = 0;i < vsize;i++) {
608 temp_app_list.erase(temp_app_list.begin());
614 bool ttsd_data_is_uttid_valid(int uid, int uttid)
618 index = ttsd_data_is_client(uid);
620 SECURE_SLOG(LOG_ERROR, tts_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
621 return TTSD_ERROR_INVALID_PARAMETER;
624 if (uttid < g_app_list[index].utt_id_stopped)
630 int ttsd_data_is_current_playing()
632 int vsize = g_app_list.size();
634 for (int i = 0; i < vsize; i++) {
635 if(g_app_list[i].state == APP_STATE_PLAYING) {
636 return g_app_list[i].uid;
643 int ttsd_data_get_same_pid_client_count(int pid)
645 int vsize = g_app_list.size();
648 for (int i = 0;i < vsize;i++) {
649 if(g_app_list[i].pid == pid) {
657 int ttsd_data_save_error_log(int uid, FILE* fp)
662 pid = ttsd_data_get_pid(uid);
664 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to get pid");
666 fprintf(fp, "pid - %d", pid);
670 ret = ttsd_data_get_client_state(uid, &state);
672 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to get app state");
674 fprintf(fp, "app state - %d", state);
680 index = ttsd_data_is_client(uid);
682 SLOG(LOG_ERROR, tts_tag(), "[ERROR] Invalid client");
687 fprintf(fp, "----- Sound list -----");
690 std::list<sound_data_s*>::iterator iter;
691 for (iter = g_app_list[index].m_wav_data.begin(); iter != g_app_list[index].m_wav_data.end(); iter++) {
692 SLOG(LOG_DEBUG, tts_tag(), "[%dth][%p] data(%p) data size(%ld), uttid(%d), type(%d)",
693 i, *iter, (*iter)->data, (*iter)->data_size, (*iter)->utt_id, (*iter)->audio_type);
697 fprintf(fp, "----------------------");
700 fprintf(fp, "----- Text list -----");
703 std::list<speak_data_s*>::iterator iter_speak;
704 for (iter_speak = g_app_list[index].m_speak_data.begin(); iter_speak != g_app_list[index].m_speak_data.end(); iter_speak++) {
705 SLOG(LOG_DEBUG, tts_tag(), "[%dth][%p] lang(%s), vctype(%d), speed(%d), uttid(%d), text(%s)",
706 i, *iter_speak, (*iter_speak)->lang, (*iter_speak)->vctype, (*iter_speak)->speed, (*iter_speak)->utt_id, (*iter_speak)->text);
709 fprintf(fp, "---------------------");