2 * Copyright (c) 2011 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"
20 static vector<app_data_s> g_app_list;
22 static vector<setting_app_data_s> g_setting_list;
24 static bool g_mutex_state = false;
30 int __data_show_list()
32 int vsize = g_app_list.size();
34 SLOG(LOG_DEBUG, TAG_TTSD, "----- client list -----");
36 for (int i=0; i<vsize; i++) {
37 SLOG(LOG_DEBUG, TAG_TTSD, "[%dth] pid(%d), uid(%d), state(%d) \n", i, g_app_list[i].pid, g_app_list[i].uid, g_app_list[i].state );
41 SLOG(LOG_DEBUG, TAG_TTSD, "No Client \n");
44 SLOG(LOG_DEBUG, TAG_TTSD, "-----------------------");
46 SLOG(LOG_DEBUG, TAG_TTSD, "----- setting client list -----");
48 vsize = g_setting_list.size();
50 for (int i=0; i<vsize; i++) {
51 SLOG(LOG_DEBUG, TAG_TTSD, "[%dth] pid(%d)", i, g_setting_list[i].pid );
55 SLOG(LOG_DEBUG, TAG_TTSD, "No Setting Client");
58 SLOG(LOG_DEBUG, TAG_TTSD, "--------------------------------");
60 return TTSD_ERROR_NONE;
63 int __data_show_sound_list(int index)
65 SLOG(LOG_DEBUG, TAG_TTSD, "----- Sound list -----");
68 for (i=0 ; i < g_app_list[index].m_wav_data.size() ; i++) {
69 SLOG(LOG_DEBUG, TAG_TTSD, "[%dth] data size(%ld), uttid(%d), type(%d) \n",
70 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 );
74 SLOG(LOG_DEBUG, TAG_TTSD, "No Sound Data \n");
77 SLOG(LOG_DEBUG, TAG_TTSD, "----------------------");
78 return TTSD_ERROR_NONE;
81 int __data_show_text_list(int index)
83 SLOG(LOG_DEBUG, TAG_TTSD, "----- Text list -----");
86 for (i=0 ; i< g_app_list[index].m_speak_data.size() ; i++) {
87 SLOG(LOG_DEBUG, TAG_TTSD, "[%dth] lang(%s), vctype(%d), speed(%d), uttid(%d), text(%s) \n",
88 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,
89 g_app_list[index].m_speak_data[i].utt_id, g_app_list[index].m_speak_data[i].text );
93 SLOG(LOG_DEBUG, TAG_TTSD, "No Text Data \n");
96 SLOG(LOG_DEBUG, TAG_TTSD, "---------------------");
97 return TTSD_ERROR_NONE;
102 * ttsd data functions
105 int ttsd_data_new_client(int pid, int uid)
107 if( -1 != ttsd_data_is_client(uid) ) {
108 SLOG(LOG_ERROR, TAG_TTSD, "[DATA ERROR] ttsd_data_new_client() : uid is not valid (%d)\n", uid);
109 return TTSD_ERROR_INVALID_PARAMETER;
115 app.utt_id_stopped = 0;
116 app.state = APP_STATE_READY;
118 g_app_list.insert( g_app_list.end(), app);
123 return TTSD_ERROR_NONE;
126 int ttsd_data_delete_client(int uid)
130 index = ttsd_data_is_client(uid);
133 SLOG(LOG_ERROR, TAG_TTSD, "[DATA ERROR] ttsd_data_delete_client() : uid is not valid (%d)\n", uid);
137 if (0 != ttsd_data_clear_data(uid)) {
138 SLOG(LOG_ERROR, TAG_TTSD, "[DATA ERROR] fail ttsd_data_clear_data()\n");
142 g_app_list.erase(g_app_list.begin()+index);
147 return TTSD_ERROR_NONE;
150 int ttsd_data_is_client(int uid)
152 int vsize = g_app_list.size();
154 for (int i=0; i<vsize; i++) {
155 if(g_app_list[i].uid == uid) {
163 int ttsd_data_get_client_count()
165 return g_app_list.size() + g_setting_list.size();
168 int ttsd_data_get_pid(int uid)
172 index = ttsd_data_is_client(uid);
175 SLOG(LOG_ERROR, TAG_TTSD, "[DATA ERROR] ttsd_data_delete_client() : uid is not valid (%d)\n", uid);
176 return TTSD_ERROR_INVALID_PARAMETER;
179 return g_app_list[index].pid;
182 int ttsd_data_get_speak_data_size(int uid)
185 index = ttsd_data_is_client(uid);
188 SLOG(LOG_ERROR, TAG_TTSD, "[DATA ERROR] ttsd_data_get_speak_data_size() : uid is not valid (%d)\n", uid);
189 return TTSD_ERROR_INVALID_PARAMETER;
192 int size = g_app_list[index].m_speak_data.size();
196 int ttsd_data_add_speak_data(int uid, speak_data_s data)
199 index = ttsd_data_is_client(uid);
202 SLOG(LOG_ERROR, TAG_TTSD, "[DATA ERROR] ttsd_data_add_speak_data() : uid is not valid (%d)\n", uid);
203 return TTSD_ERROR_INVALID_PARAMETER;
206 g_app_list[index].m_speak_data.insert(g_app_list[index].m_speak_data.end(), data);
208 if (1 == data.utt_id)
209 g_app_list[index].utt_id_stopped = 0;
212 __data_show_text_list(index);
214 return TTSD_ERROR_NONE;
217 int ttsd_data_get_speak_data(int uid, speak_data_s* data)
220 index = ttsd_data_is_client(uid);
223 SLOG(LOG_ERROR, TAG_TTSD, "[DATA ERROR] ttsd_data_get_speak_data() : uid is not valid(%d)\n", uid);
224 return TTSD_ERROR_INVALID_PARAMETER;
227 if (0 == g_app_list[index].m_speak_data.size()) {
228 SLOG(LOG_WARN, TAG_TTSD, "[DATA WARNING] There is no speak data\n");
232 data->lang = g_strdup(g_app_list[index].m_speak_data[0].lang);
233 data->vctype = g_app_list[index].m_speak_data[0].vctype;
234 data->speed = g_app_list[index].m_speak_data[0].speed;
236 data->text = g_app_list[index].m_speak_data[0].text;
237 data->utt_id = g_app_list[index].m_speak_data[0].utt_id;
239 g_app_list[index].m_speak_data.erase(g_app_list[index].m_speak_data.begin());
242 __data_show_text_list(index);
244 return TTSD_ERROR_NONE;
247 int ttsd_data_add_sound_data(int uid, sound_data_s data)
250 index = ttsd_data_is_client(uid);
253 SLOG(LOG_ERROR, TAG_TTSD, "[DATA ERROR] ttsd_data_add_sound_data() : uid is not valid (%d)\n", uid);
254 return TTSD_ERROR_INVALID_PARAMETER;
257 g_app_list[index].m_wav_data.insert(g_app_list[index].m_wav_data.end(), data);
260 __data_show_sound_list(index);
262 return TTSD_ERROR_NONE;
265 int ttsd_data_get_sound_data(int uid, sound_data_s* data)
268 index = ttsd_data_is_client(uid);
271 SLOG(LOG_ERROR, TAG_TTSD, "[DATA ERROR] ttsd_data_get_sound_data() : uid is not valid (%d)\n", uid);
272 return TTSD_ERROR_INVALID_PARAMETER;
275 if (0 == g_app_list[index].m_wav_data.size()) {
276 SLOG(LOG_WARN, TAG_TTSD, "[DATA WARNING] There is no wav data\n");
280 data->data = g_app_list[index].m_wav_data[0].data;
281 data->data_size = g_app_list[index].m_wav_data[0].data_size;
282 data->utt_id = g_app_list[index].m_wav_data[0].utt_id;
283 data->audio_type = g_app_list[index].m_wav_data[0].audio_type;
284 data->rate = g_app_list[index].m_wav_data[0].rate;
285 data->channels = g_app_list[index].m_wav_data[0].channels;
286 data->event = g_app_list[index].m_wav_data[0].event;
288 g_app_list[index].m_wav_data.erase(g_app_list[index].m_wav_data.begin());
291 __data_show_sound_list(index);
293 return TTSD_ERROR_NONE;
296 int ttsd_data_get_sound_data_size(int uid)
299 index = ttsd_data_is_client(uid);
302 SLOG(LOG_ERROR, TAG_TTSD, "[DATA ERROR] ttsd_data_get_sound_data_size() : uid is not valid (%d)\n", uid);
303 return TTSD_ERROR_INVALID_PARAMETER;
306 return g_app_list[index].m_wav_data.size();
309 int ttsd_data_clear_data(int uid)
313 index = ttsd_data_is_client(uid);
315 SLOG(LOG_ERROR, TAG_TTSD, "[DATA ERROR] ttsd_data_clear_data() : uid is not valid (%d)\n", uid);
316 return TTSD_ERROR_INVALID_PARAMETER;
319 int removed_last_uttid = -1;
320 /* free allocated data */
323 if (0 != ttsd_data_get_speak_data(uid, &temp)) {
327 if (NULL != temp.text) free(temp.text);
328 if (NULL != temp.lang) free(temp.lang);
330 removed_last_uttid = temp.utt_id;
333 if (-1 != removed_last_uttid) {
334 g_app_list[index].utt_id_stopped = removed_last_uttid;
339 if (0 != ttsd_data_get_sound_data(uid, &temp)) {
343 if (NULL != temp.data) free(temp.data);
346 g_app_list[index].m_speak_data.clear();
347 g_app_list[index].m_wav_data.clear();
349 return TTSD_ERROR_NONE;
352 int ttsd_data_get_client_state(int uid, app_state_e* state)
356 index = ttsd_data_is_client(uid);
358 SLOG(LOG_ERROR, TAG_TTSD, "[DATA ERROR] ttsd_data_get_client_state() : uid is not valid (%d)\n", uid);
359 return TTSD_ERROR_INVALID_PARAMETER;
362 *state = g_app_list[index].state;
364 return TTSD_ERROR_NONE;
367 int ttsd_data_set_client_state(int uid, app_state_e state)
371 index = ttsd_data_is_client(uid);
373 SLOG(LOG_ERROR, TAG_TTSD, "[DATA ERROR] ttsd_data_set_client_state() : uid is not valid (%d)\n", uid);
374 return TTSD_ERROR_INVALID_PARAMETER;
377 if (true == g_mutex_state) {
378 while(true == g_mutex_state) {
382 g_mutex_state = true;
384 /* The client of playing state of all clients is only one. need to check state. */
385 if (APP_STATE_PLAYING == state) {
386 int vsize = g_app_list.size();
387 for (int i=0 ; i<vsize ; i++) {
388 if(g_app_list[i].state == APP_STATE_PLAYING) {
389 SLOG(LOG_ERROR, TAG_TTSD, "[DATA ERROR] ttsd_data_set_client_state() : a playing client has already existed. \n");
390 g_mutex_state = false;
396 g_app_list[index].state = state;
398 g_mutex_state = false;
400 return TTSD_ERROR_NONE;
403 int ttsd_data_get_current_playing()
405 int vsize = g_app_list.size();
407 for (int i=0; i<vsize; i++) {
408 if (APP_STATE_PLAYING == g_app_list[i].state) {
409 return g_app_list[i].uid;
413 SLOG(LOG_DEBUG, TAG_TTSD, "[DATA] NO CURRENT PLAYING !!");
418 int ttsd_data_foreach_clients(ttsd_data_get_client_cb callback, void* user_data)
420 if (NULL == callback) {
421 SLOG(LOG_ERROR, TAG_TTSD, "[DATA ERROR] input data is NULL!!");
425 int vsize = g_app_list.size();
427 for (int i=0; i<vsize; i++) {
428 if (false == callback(g_app_list[i].pid, g_app_list[i].uid, g_app_list[i].state, user_data)) {
436 bool ttsd_data_is_uttid_valid(int uid, int uttid)
440 index = ttsd_data_is_client(uid);
442 SLOG(LOG_ERROR, TAG_TTSD, "[DATA ERROR] ttsd_data_set_client_state() : uid is not valid (%d)\n", uid);
443 return TTSD_ERROR_INVALID_PARAMETER;
446 if (uttid < g_app_list[index].utt_id_stopped)
452 int ttsd_data_is_current_playing()
454 int vsize = g_app_list.size();
456 for (int i=0; i<vsize; i++) {
457 if(g_app_list[i].state == APP_STATE_PLAYING) {
458 return g_app_list[i].uid;
469 int ttsd_setting_data_add(int pid)
471 if (-1 != ttsd_setting_data_is_setting(pid)) {
472 SLOG(LOG_ERROR, TAG_TTSD, "[DATA ERROR] pid(%d) is not valid", pid);
473 return TTSD_ERROR_INVALID_PARAMETER;
476 setting_app_data_s setting_app;
477 setting_app.pid = pid;
479 g_setting_list.insert(g_setting_list.end(), setting_app);
484 return TTSD_ERROR_NONE;
488 int ttsd_setting_data_delete(int pid)
492 index = ttsd_setting_data_is_setting(pid);
495 SLOG(LOG_ERROR, TAG_TTSD, "[DATA ERROR] uid is not valid (%d)", pid);
499 g_setting_list.erase(g_setting_list.begin()+index);
504 return TTSD_ERROR_NONE;
507 int ttsd_setting_data_is_setting(int pid)
509 int vsize = g_setting_list.size();
511 for (int i=0; i<vsize; i++) {
512 if(g_setting_list[i].pid == pid) {