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.
15 #include "ttsd_main.h"
16 #include "ttsd_data.h"
33 std::vector<speak_data_s> m_speak_data;
34 std::vector<sound_data_s> m_wav_data;
36 std::vector<used_voice_s> m_used_voice;
39 static vector<app_data_s> g_app_list;
46 int __data_show_list()
48 int vsize = g_app_list.size();
50 SLOG(LOG_DEBUG, get_tag(), "----- client list -----");
52 for (int i=0; i<vsize; i++) {
53 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);
57 SLOG(LOG_DEBUG, get_tag(), "No Client");
60 SLOG(LOG_DEBUG, get_tag(), "-----------------------");
62 return TTSD_ERROR_NONE;
65 int __data_show_sound_list(int index)
67 SLOG(LOG_DEBUG, get_tag(), "----- Sound list -----");
70 for (i=0 ; i < g_app_list[index].m_wav_data.size() ; i++) {
71 SECURE_SLOG(LOG_DEBUG, get_tag(), "[%dth] data size(%ld), uttid(%d), type(%d)",
72 i+1, g_app_list[index].m_wav_data[i].data_size, g_app_list[index].m_wav_data[i].utt_id, g_app_list[index].m_wav_data[i].audio_type);
76 SLOG(LOG_DEBUG, get_tag(), "No Sound Data");
79 SLOG(LOG_DEBUG, get_tag(), "----------------------");
80 return TTSD_ERROR_NONE;
83 int __data_show_text_list(int index)
85 SLOG(LOG_DEBUG, get_tag(), "----- Text list -----");
88 for (i=0 ; i< g_app_list[index].m_speak_data.size() ; i++) {
89 SECURE_SLOG(LOG_DEBUG, get_tag(), "[%dth] lang(%s), vctype(%d), speed(%d), uttid(%d), text(%s)",
90 i+1, g_app_list[index].m_speak_data[i].lang, g_app_list[index].m_speak_data[i].vctype, g_app_list[index].m_speak_data[i].speed,
91 g_app_list[index].m_speak_data[i].utt_id, g_app_list[index].m_speak_data[i].text);
95 SLOG(LOG_DEBUG, get_tag(), "No Text Data");
98 SLOG(LOG_DEBUG, get_tag(), "---------------------");
99 return TTSD_ERROR_NONE;
102 int __data_show_used_voice_list(int index)
104 SLOG(LOG_DEBUG, get_tag(), "----- Voice list -----");
107 for (i=0 ; i< g_app_list[index].m_speak_data.size() ; i++) {
108 SECURE_SLOG(LOG_DEBUG, get_tag(), "[%dth] lang(%s), vctype(%d)",
109 i+1, g_app_list[index].m_used_voice[i].lang, g_app_list[index].m_used_voice[i].vctype);
113 SLOG(LOG_DEBUG, get_tag(), "No Voice Data");
116 SLOG(LOG_DEBUG, get_tag(), "---------------------");
117 return TTSD_ERROR_NONE;
121 * ttsd data functions
124 int ttsd_data_new_client(int pid, int uid)
126 if( -1 != ttsd_data_is_client(uid) ) {
127 SECURE_SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
128 return TTSD_ERROR_INVALID_PARAMETER;
134 app.utt_id_stopped = 0;
135 app.state = APP_STATE_READY;
137 g_app_list.insert( g_app_list.end(), app);
142 return TTSD_ERROR_NONE;
145 int ttsd_data_delete_client(int uid)
149 index = ttsd_data_is_client(uid);
152 SECURE_SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
156 if (0 != ttsd_data_clear_data(uid)) {
157 SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] Fail to clear data");
161 g_app_list.erase(g_app_list.begin()+index);
166 return TTSD_ERROR_NONE;
169 int ttsd_data_is_client(int uid)
171 int vsize = g_app_list.size();
173 for (int i=0; i<vsize; i++) {
174 if(g_app_list[i].uid == uid) {
182 int ttsd_data_get_client_count()
184 return g_app_list.size();
187 int ttsd_data_get_pid(int uid)
191 index = ttsd_data_is_client(uid);
194 SECURE_SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
195 return TTSD_ERROR_INVALID_PARAMETER;
198 return g_app_list[index].pid;
201 int ttsd_data_get_speak_data_size(int uid)
204 index = ttsd_data_is_client(uid);
207 SECURE_SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
208 return TTSD_ERROR_INVALID_PARAMETER;
211 int size = g_app_list[index].m_speak_data.size();
215 int ttsd_data_set_used_voice(int uid, const char* lang, int type)
218 index = ttsd_data_is_client(uid);
221 SECURE_SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
222 return TTSD_ERROR_INVALID_PARAMETER;
226 int vsize = g_app_list[index].m_used_voice.size();
229 for (i = 0;i < vsize;i++) {
230 if (0 == strcmp(lang, g_app_list[index].m_used_voice[i].lang) &&
231 type == g_app_list[index].m_used_voice[i].vctype) {
232 SLOG(LOG_DEBUG, get_tag(), "[DATA] The voice is already registered (%s)(%d)", lang, type);
238 used_voice_s used_voice;
239 used_voice.lang = strdup(lang);
240 used_voice.vctype = type;
242 g_app_list[index].m_used_voice.insert(g_app_list[index].m_used_voice.end(), used_voice);
245 __data_show_used_voice_list(index);
248 return -1; /* Need to load voice*/
251 int ttsd_data_reset_used_voice(int uid, ttsd_used_voice_cb callback)
254 index = ttsd_data_is_client(uid);
257 SECURE_SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
258 return TTSD_ERROR_INVALID_PARAMETER;
262 int vsize = g_app_list[index].m_used_voice.size();
265 for (i = 0;i < vsize;i++) {
266 if (NULL != callback) {
267 callback(g_app_list[index].m_used_voice[i].lang, g_app_list[index].m_used_voice[i].vctype);
269 SECURE_SLOG(LOG_WARN, get_tag(), "[DATA WARNING] Used voice callback is NULL");
272 if (NULL != g_app_list[index].m_used_voice[i].lang) {
273 free(g_app_list[index].m_used_voice[i].lang);
277 g_app_list[index].m_used_voice.clear();
279 return TTSD_ERROR_NONE;
282 int ttsd_data_add_speak_data(int uid, speak_data_s data)
285 index = ttsd_data_is_client(uid);
288 SECURE_SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
289 return TTSD_ERROR_INVALID_PARAMETER;
292 g_app_list[index].m_speak_data.insert(g_app_list[index].m_speak_data.end(), data);
294 if (1 == data.utt_id)
295 g_app_list[index].utt_id_stopped = 0;
298 __data_show_text_list(index);
300 return TTSD_ERROR_NONE;
303 int ttsd_data_get_speak_data(int uid, speak_data_s* data)
306 index = ttsd_data_is_client(uid);
309 SECURE_SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] uid is not valid(%d)", uid);
310 return TTSD_ERROR_INVALID_PARAMETER;
313 if (0 == g_app_list[index].m_speak_data.size()) {
315 SLOG(LOG_WARN, get_tag(), "[DATA WARNING] There is no speak data");
320 data->lang = g_strdup(g_app_list[index].m_speak_data[0].lang);
321 data->vctype = g_app_list[index].m_speak_data[0].vctype;
322 data->speed = g_app_list[index].m_speak_data[0].speed;
324 data->text = g_app_list[index].m_speak_data[0].text;
325 data->utt_id = g_app_list[index].m_speak_data[0].utt_id;
327 g_app_list[index].m_speak_data.erase(g_app_list[index].m_speak_data.begin());
330 __data_show_text_list(index);
332 return TTSD_ERROR_NONE;
335 int ttsd_data_add_sound_data(int uid, sound_data_s data)
338 index = ttsd_data_is_client(uid);
341 SECURE_SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
342 return TTSD_ERROR_INVALID_PARAMETER;
345 g_app_list[index].m_wav_data.insert(g_app_list[index].m_wav_data.end(), data);
348 __data_show_sound_list(index);
350 return TTSD_ERROR_NONE;
353 int ttsd_data_get_sound_data(int uid, sound_data_s* data)
356 index = ttsd_data_is_client(uid);
359 SECURE_SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
360 return TTSD_ERROR_INVALID_PARAMETER;
363 if (0 == g_app_list[index].m_wav_data.size()) {
365 SLOG(LOG_DEBUG, get_tag(), "[DATA] There is no wav data");
370 data->data = g_app_list[index].m_wav_data[0].data;
371 data->data_size = g_app_list[index].m_wav_data[0].data_size;
372 data->utt_id = g_app_list[index].m_wav_data[0].utt_id;
373 data->audio_type = g_app_list[index].m_wav_data[0].audio_type;
374 data->rate = g_app_list[index].m_wav_data[0].rate;
375 data->channels = g_app_list[index].m_wav_data[0].channels;
376 data->event = g_app_list[index].m_wav_data[0].event;
378 g_app_list[index].m_wav_data.erase(g_app_list[index].m_wav_data.begin());
381 __data_show_sound_list(index);
383 return TTSD_ERROR_NONE;
386 int ttsd_data_get_sound_data_size(int uid)
389 index = ttsd_data_is_client(uid);
392 SECURE_SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
393 return TTSD_ERROR_INVALID_PARAMETER;
396 return g_app_list[index].m_wav_data.size();
399 int ttsd_data_clear_data(int uid)
403 index = ttsd_data_is_client(uid);
405 SECURE_SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
406 return TTSD_ERROR_INVALID_PARAMETER;
409 int removed_last_uttid = -1;
410 /* free allocated data */
413 if (0 != ttsd_data_get_speak_data(uid, &temp)) {
417 if (NULL != temp.text) free(temp.text);
418 if (NULL != temp.lang) free(temp.lang);
420 removed_last_uttid = temp.utt_id;
423 if (-1 != removed_last_uttid) {
424 g_app_list[index].utt_id_stopped = removed_last_uttid;
429 if (0 != ttsd_data_get_sound_data(uid, &temp)) {
433 if (0 < temp.data_size) {
434 if (NULL != temp.data) free(temp.data);
436 SLOG(LOG_ERROR, get_tag(), "Sound data is NULL");
440 g_app_list[index].m_speak_data.clear();
441 g_app_list[index].m_wav_data.clear();
443 return TTSD_ERROR_NONE;
446 int ttsd_data_get_client_state(int uid, app_state_e* state)
450 index = ttsd_data_is_client(uid);
452 SECURE_SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
453 return TTSD_ERROR_INVALID_PARAMETER;
456 *state = g_app_list[index].state;
458 return TTSD_ERROR_NONE;
461 int ttsd_data_set_client_state(int uid, app_state_e state)
465 index = ttsd_data_is_client(uid);
467 SECURE_SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
468 return TTSD_ERROR_INVALID_PARAMETER;
471 /* The client of playing state of all clients is only one. need to check state. */
472 if (APP_STATE_PLAYING == state) {
473 int vsize = g_app_list.size();
474 for (int i=0 ; i<vsize ; i++) {
475 if(g_app_list[i].state == APP_STATE_PLAYING) {
476 SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] A playing client has already existed.");
482 g_app_list[index].state = state;
484 return TTSD_ERROR_NONE;
487 int ttsd_data_get_current_playing()
489 int vsize = g_app_list.size();
491 for (int i=0; i<vsize; i++) {
492 if (APP_STATE_PLAYING == g_app_list[i].state) {
493 SLOG(LOG_DEBUG, get_tag(), "[DATA] uid(%d) is playing", g_app_list[i].uid);
494 return g_app_list[i].uid;
501 int ttsd_data_foreach_clients(ttsd_data_get_client_cb callback, void* user_data)
503 if (NULL == callback) {
504 SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] input data is NULL!!");
513 vector<app_data_s> temp_app_list;
514 int vsize = g_app_list.size();
517 for (i = 0;i < vsize;i++) {
519 app.pid = g_app_list[i].pid;
520 app.uid = g_app_list[i].uid;
521 app.utt_id_stopped = 0;
522 app.state = g_app_list[i].state;
524 temp_app_list.insert(temp_app_list.end(), app);
527 for (i = 0;i < vsize;i++) {
528 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);
529 if (false == callback(temp_app_list[i].pid, temp_app_list[i].uid, temp_app_list[i].state, user_data)) {
534 for (i = 0;i < vsize;i++) {
535 temp_app_list.erase(temp_app_list.begin());
541 bool ttsd_data_is_uttid_valid(int uid, int uttid)
545 index = ttsd_data_is_client(uid);
547 SECURE_SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] uid is not valid (%d)", uid);
548 return TTSD_ERROR_INVALID_PARAMETER;
551 if (uttid < g_app_list[index].utt_id_stopped)
557 int ttsd_data_is_current_playing()
559 int vsize = g_app_list.size();
561 for (int i=0; i<vsize; i++) {
562 if(g_app_list[i].state == APP_STATE_PLAYING) {
563 return g_app_list[i].uid;
570 int ttsd_data_get_same_pid_client_count(int pid)
572 int vsize = g_app_list.size();
575 for (int i = 0;i < vsize;i++) {
576 if(g_app_list[i].pid == pid) {
584 int ttsd_data_save_error_log(int uid, FILE* fp)
589 pid = ttsd_data_get_pid(uid);
591 SLOG(LOG_ERROR, get_tag(), "[ERROR] Fail to get pid");
593 fprintf(fp, "pid - %d", pid);
597 ret = ttsd_data_get_client_state(uid, &state);
599 SLOG(LOG_ERROR, get_tag(), "[ERROR] Fail to get app state");
601 fprintf(fp, "app state - %d", state);
607 index = ttsd_data_is_client(uid);
610 fprintf(fp, "----- Sound list -----");
612 for (i=0 ; i < g_app_list[index].m_wav_data.size() ; i++) {
613 fprintf(fp, "[%dth] data size(%d), uttid(%d), type(%d)",
614 i+1, g_app_list[index].m_wav_data[i].data_size, g_app_list[index].m_wav_data[i].utt_id, g_app_list[index].m_wav_data[i].audio_type );
616 fprintf(fp, "----------------------");
619 fprintf(fp, "----- Text list -----");
621 for (i=0 ; i< g_app_list[index].m_speak_data.size() ; i++) {
622 fprintf(fp, "[%dth] lang(%s), vctype(%d), speed(%d), uttid(%d), text(%s)",
623 i+1, g_app_list[index].m_speak_data[i].lang, g_app_list[index].m_speak_data[i].vctype, g_app_list[index].m_speak_data[i].speed,
624 g_app_list[index].m_speak_data[i].utt_id, g_app_list[index].m_speak_data[i].text );
626 fprintf(fp, "---------------------");