2 * Copyright (c) 2012, 2013 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"
27 std::vector<speak_data_s> m_speak_data;
28 std::vector<sound_data_s> m_wav_data;
35 static vector<app_data_s> g_app_list;
37 static vector<setting_app_data_s> g_setting_list;
39 static bool g_mutex_state = false;
45 int __data_show_list()
47 int vsize = g_app_list.size();
49 SLOG(LOG_DEBUG, get_tag(), "----- client list -----");
51 for (int i=0; i<vsize; i++) {
52 SLOG(LOG_DEBUG, get_tag(), "[%dth] pid(%d), uid(%d), state(%d) \n", i, g_app_list[i].pid, g_app_list[i].uid, g_app_list[i].state );
56 SLOG(LOG_DEBUG, get_tag(), "No Client \n");
59 SLOG(LOG_DEBUG, get_tag(), "-----------------------");
61 SLOG(LOG_DEBUG, get_tag(), "----- setting client list -----");
63 vsize = g_setting_list.size();
65 for (int i=0; i<vsize; i++) {
66 SLOG(LOG_DEBUG, get_tag(), "[%dth] pid(%d)", i, g_setting_list[i].pid );
70 SLOG(LOG_DEBUG, get_tag(), "No Setting Client");
73 SLOG(LOG_DEBUG, get_tag(), "--------------------------------");
75 return TTSD_ERROR_NONE;
78 int __data_show_sound_list(int index)
80 SLOG(LOG_DEBUG, get_tag(), "----- Sound list -----");
83 for (i=0 ; i < g_app_list[index].m_wav_data.size() ; i++) {
84 SLOG(LOG_DEBUG, get_tag(), "[%dth] data size(%ld), uttid(%d), type(%d) \n",
85 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 );
89 SLOG(LOG_DEBUG, get_tag(), "No Sound Data \n");
92 SLOG(LOG_DEBUG, get_tag(), "----------------------");
93 return TTSD_ERROR_NONE;
96 int __data_show_text_list(int index)
98 SLOG(LOG_DEBUG, get_tag(), "----- Text list -----");
101 for (i=0 ; i< g_app_list[index].m_speak_data.size() ; i++) {
102 SLOG(LOG_DEBUG, get_tag(), "[%dth] lang(%s), vctype(%d), speed(%d), uttid(%d), text(%s) \n",
103 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,
104 g_app_list[index].m_speak_data[i].utt_id, g_app_list[index].m_speak_data[i].text );
108 SLOG(LOG_DEBUG, get_tag(), "No Text Data \n");
111 SLOG(LOG_DEBUG, get_tag(), "---------------------");
112 return TTSD_ERROR_NONE;
117 * ttsd data functions
120 int ttsd_data_new_client(int pid, int uid)
122 if( -1 != ttsd_data_is_client(uid) ) {
123 SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] ttsd_data_new_client() : uid is not valid (%d)\n", uid);
124 return TTSD_ERROR_INVALID_PARAMETER;
130 app.utt_id_stopped = 0;
131 app.state = APP_STATE_READY;
133 g_app_list.insert( g_app_list.end(), app);
138 return TTSD_ERROR_NONE;
141 int ttsd_data_delete_client(int uid)
145 index = ttsd_data_is_client(uid);
148 SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] ttsd_data_delete_client() : uid is not valid (%d)\n", uid);
152 if (0 != ttsd_data_clear_data(uid)) {
153 SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] fail ttsd_data_clear_data()\n");
157 g_app_list.erase(g_app_list.begin()+index);
162 return TTSD_ERROR_NONE;
165 int ttsd_data_is_client(int uid)
167 int vsize = g_app_list.size();
169 for (int i=0; i<vsize; i++) {
170 if(g_app_list[i].uid == uid) {
178 int ttsd_data_get_client_count()
180 return g_app_list.size() + g_setting_list.size();
183 int ttsd_data_get_pid(int uid)
187 index = ttsd_data_is_client(uid);
190 SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] ttsd_data_delete_client() : uid is not valid (%d)\n", uid);
191 return TTSD_ERROR_INVALID_PARAMETER;
194 return g_app_list[index].pid;
197 int ttsd_data_get_speak_data_size(int uid)
200 index = ttsd_data_is_client(uid);
203 SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] ttsd_data_get_speak_data_size() : uid is not valid (%d)\n", uid);
204 return TTSD_ERROR_INVALID_PARAMETER;
207 int size = g_app_list[index].m_speak_data.size();
211 int ttsd_data_add_speak_data(int uid, speak_data_s data)
214 index = ttsd_data_is_client(uid);
217 SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] ttsd_data_add_speak_data() : uid is not valid (%d)\n", uid);
218 return TTSD_ERROR_INVALID_PARAMETER;
221 g_app_list[index].m_speak_data.insert(g_app_list[index].m_speak_data.end(), data);
223 if (1 == data.utt_id)
224 g_app_list[index].utt_id_stopped = 0;
227 __data_show_text_list(index);
229 return TTSD_ERROR_NONE;
232 int ttsd_data_get_speak_data(int uid, speak_data_s* data)
235 index = ttsd_data_is_client(uid);
238 SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] ttsd_data_get_speak_data() : uid is not valid(%d)\n", uid);
239 return TTSD_ERROR_INVALID_PARAMETER;
242 if (0 == g_app_list[index].m_speak_data.size()) {
243 SLOG(LOG_WARN, get_tag(), "[DATA WARNING] There is no speak data\n");
247 data->lang = g_strdup(g_app_list[index].m_speak_data[0].lang);
248 data->vctype = g_app_list[index].m_speak_data[0].vctype;
249 data->speed = g_app_list[index].m_speak_data[0].speed;
251 data->text = g_app_list[index].m_speak_data[0].text;
252 data->utt_id = g_app_list[index].m_speak_data[0].utt_id;
254 g_app_list[index].m_speak_data.erase(g_app_list[index].m_speak_data.begin());
257 __data_show_text_list(index);
259 return TTSD_ERROR_NONE;
262 int ttsd_data_add_sound_data(int uid, sound_data_s data)
265 index = ttsd_data_is_client(uid);
268 SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] ttsd_data_add_sound_data() : uid is not valid (%d)\n", uid);
269 return TTSD_ERROR_INVALID_PARAMETER;
272 g_app_list[index].m_wav_data.insert(g_app_list[index].m_wav_data.end(), data);
275 __data_show_sound_list(index);
277 return TTSD_ERROR_NONE;
280 int ttsd_data_get_sound_data(int uid, sound_data_s* data)
283 index = ttsd_data_is_client(uid);
286 SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] ttsd_data_get_sound_data() : uid is not valid (%d)\n", uid);
287 return TTSD_ERROR_INVALID_PARAMETER;
290 if (0 == g_app_list[index].m_wav_data.size()) {
291 SLOG(LOG_WARN, get_tag(), "[DATA WARNING] There is no wav data\n");
295 data->data = g_app_list[index].m_wav_data[0].data;
296 data->data_size = g_app_list[index].m_wav_data[0].data_size;
297 data->utt_id = g_app_list[index].m_wav_data[0].utt_id;
298 data->audio_type = g_app_list[index].m_wav_data[0].audio_type;
299 data->rate = g_app_list[index].m_wav_data[0].rate;
300 data->channels = g_app_list[index].m_wav_data[0].channels;
301 data->event = g_app_list[index].m_wav_data[0].event;
303 g_app_list[index].m_wav_data.erase(g_app_list[index].m_wav_data.begin());
306 __data_show_sound_list(index);
308 return TTSD_ERROR_NONE;
311 int ttsd_data_get_sound_data_size(int uid)
314 index = ttsd_data_is_client(uid);
317 SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] ttsd_data_get_sound_data_size() : uid is not valid (%d)\n", uid);
318 return TTSD_ERROR_INVALID_PARAMETER;
321 return g_app_list[index].m_wav_data.size();
324 int ttsd_data_clear_data(int uid)
328 index = ttsd_data_is_client(uid);
330 SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] ttsd_data_clear_data() : uid is not valid (%d)\n", uid);
331 return TTSD_ERROR_INVALID_PARAMETER;
334 int removed_last_uttid = -1;
335 /* free allocated data */
338 if (0 != ttsd_data_get_speak_data(uid, &temp)) {
342 if (NULL != temp.text) free(temp.text);
343 if (NULL != temp.lang) free(temp.lang);
345 removed_last_uttid = temp.utt_id;
348 if (-1 != removed_last_uttid) {
349 g_app_list[index].utt_id_stopped = removed_last_uttid;
354 if (0 != ttsd_data_get_sound_data(uid, &temp)) {
358 if (NULL != temp.data) free(temp.data);
361 g_app_list[index].m_speak_data.clear();
362 g_app_list[index].m_wav_data.clear();
364 return TTSD_ERROR_NONE;
367 int ttsd_data_get_client_state(int uid, app_state_e* state)
371 index = ttsd_data_is_client(uid);
373 SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] ttsd_data_get_client_state() : uid is not valid (%d)\n", uid);
374 return TTSD_ERROR_INVALID_PARAMETER;
377 *state = g_app_list[index].state;
379 return TTSD_ERROR_NONE;
382 int ttsd_data_set_client_state(int uid, app_state_e state)
386 index = ttsd_data_is_client(uid);
388 SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] ttsd_data_set_client_state() : uid is not valid (%d)\n", uid);
389 return TTSD_ERROR_INVALID_PARAMETER;
392 if (true == g_mutex_state) {
393 while(true == g_mutex_state) {
397 g_mutex_state = true;
399 /* The client of playing state of all clients is only one. need to check state. */
400 if (APP_STATE_PLAYING == state) {
401 int vsize = g_app_list.size();
402 for (int i=0 ; i<vsize ; i++) {
403 if(g_app_list[i].state == APP_STATE_PLAYING) {
404 SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] ttsd_data_set_client_state() : a playing client has already existed. \n");
405 g_mutex_state = false;
411 g_app_list[index].state = state;
413 g_mutex_state = false;
415 return TTSD_ERROR_NONE;
418 int ttsd_data_get_current_playing()
420 int vsize = g_app_list.size();
422 for (int i=0; i<vsize; i++) {
423 if (APP_STATE_PLAYING == g_app_list[i].state) {
424 return g_app_list[i].uid;
428 SLOG(LOG_DEBUG, get_tag(), "[DATA] NO CURRENT PLAYING !!");
433 int ttsd_data_foreach_clients(ttsd_data_get_client_cb callback, void* user_data)
435 if (NULL == callback) {
436 SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] input data is NULL!!");
440 int vsize = g_app_list.size();
442 for (int i=0; i<vsize; i++) {
443 if (false == callback(g_app_list[i].pid, g_app_list[i].uid, g_app_list[i].state, user_data)) {
451 bool ttsd_data_is_uttid_valid(int uid, int uttid)
455 index = ttsd_data_is_client(uid);
457 SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] ttsd_data_set_client_state() : uid is not valid (%d)\n", uid);
458 return TTSD_ERROR_INVALID_PARAMETER;
461 if (uttid < g_app_list[index].utt_id_stopped)
467 int ttsd_data_is_current_playing()
469 int vsize = g_app_list.size();
471 for (int i=0; i<vsize; i++) {
472 if(g_app_list[i].state == APP_STATE_PLAYING) {
473 return g_app_list[i].uid;
484 int ttsd_setting_data_add(int pid)
486 if (-1 != ttsd_setting_data_is_setting(pid)) {
487 SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] pid(%d) is not valid", pid);
488 return TTSD_ERROR_INVALID_PARAMETER;
491 setting_app_data_s setting_app;
492 setting_app.pid = pid;
494 g_setting_list.insert(g_setting_list.end(), setting_app);
499 return TTSD_ERROR_NONE;
503 int ttsd_setting_data_delete(int pid)
507 index = ttsd_setting_data_is_setting(pid);
510 SLOG(LOG_ERROR, get_tag(), "[DATA ERROR] uid is not valid (%d)", pid);
514 g_setting_list.erase(g_setting_list.begin()+index);
519 return TTSD_ERROR_NONE;
522 int ttsd_setting_data_is_setting(int pid)
524 int vsize = g_setting_list.size();
525 for (int i=0; i<vsize; i++) {
526 if(g_setting_list[i].pid == pid) {
534 int ttsd_data_save_error_log(int uid, FILE* fp)
539 pid = ttsd_data_get_pid(uid);
541 SLOG(LOG_ERROR, get_tag(), "[ERROR] Fail to get pid");
543 fprintf(fp, "pid - %d\n", pid);
547 ret = ttsd_data_get_client_state(uid, &state);
549 SLOG(LOG_ERROR, get_tag(), "[ERROR] Fail to get app state");
551 fprintf(fp, "app state - %d\n", state);
557 index = ttsd_data_is_client(uid);
560 fprintf(fp, "----- Sound list -----\n");
562 for (i=0 ; i < g_app_list[index].m_wav_data.size() ; i++) {
563 fprintf(fp, "[%dth] data size(%ld), uttid(%d), type(%d) \n",
564 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 );
566 fprintf(fp, "----------------------\n");
569 fprintf(fp, "----- Text list -----\n");
571 for (i=0 ; i< g_app_list[index].m_speak_data.size() ; i++) {
572 fprintf(fp, "[%dth] lang(%s), vctype(%d), speed(%d), uttid(%d), text(%s) \n",
573 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,
574 g_app_list[index].m_speak_data[i].utt_id, g_app_list[index].m_speak_data[i].text );
576 fprintf(fp, "---------------------");