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 bool g_mutex_state = false;
28 int __data_show_list()
30 int vsize = g_app_list.size();
32 SLOG(LOG_DEBUG, TAG_TTSD, "----- client list -----");
34 for (int i=0; i<vsize; i++) {
35 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 );
39 SLOG(LOG_DEBUG, TAG_TTSD, "No Client \n");
42 SLOG(LOG_DEBUG, TAG_TTSD, "-----------------------");
43 return TTSD_ERROR_NONE;
46 int __data_show_sound_list(const int index)
48 SLOG(LOG_DEBUG, TAG_TTSD, "----- Sound list -----");
51 for (i=0 ; i < g_app_list[index].m_wav_data.size() ; i++) {
52 SLOG(LOG_DEBUG, TAG_TTSD, "[%dth] data size(%ld), uttid(%d), type(%d) \n",
53 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 );
57 SLOG(LOG_DEBUG, TAG_TTSD, "No Sound Data \n");
60 SLOG(LOG_DEBUG, TAG_TTSD, "----------------------");
61 return TTSD_ERROR_NONE;
64 int __data_show_text_list(const int index)
66 SLOG(LOG_DEBUG, TAG_TTSD, "----- Text list -----");
69 for (i=0 ; i< g_app_list[index].m_speak_data.size() ; i++) {
70 SLOG(LOG_DEBUG, TAG_TTSD, "[%dth] lang(%s), vctype(%d), speed(%d), uttid(%d), text(%s) \n",
71 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,
72 g_app_list[index].m_speak_data[i].utt_id, g_app_list[index].m_speak_data[i].text );
76 SLOG(LOG_DEBUG, TAG_TTSD, "No Text Data \n");
79 SLOG(LOG_DEBUG, TAG_TTSD, "---------------------");
80 return TTSD_ERROR_NONE;
88 int ttsd_data_new_client(const int pid, const int uid)
90 if( -1 != ttsd_data_is_client(uid) ) {
91 SLOG(LOG_ERROR, TAG_TTSD, "[DATA ERROR] ttsd_data_new_client() : uid is not valid (%d)\n", uid);
92 return TTSD_ERROR_INVALID_PARAMETER;
98 app.utt_id_stopped = 0;
99 app.state = APP_STATE_READY;
101 g_app_list.insert( g_app_list.end(), app);
106 return TTSD_ERROR_NONE;
109 int ttsd_data_delete_client(const int uid)
113 index = ttsd_data_is_client(uid);
116 SLOG(LOG_ERROR, TAG_TTSD, "[DATA ERROR] ttsd_data_delete_client() : uid is not valid (%d)\n", uid);
120 if (0 != ttsd_data_clear_data(uid)) {
121 SLOG(LOG_ERROR, TAG_TTSD, "[DATA ERROR] fail ttsd_data_clear_data()\n");
125 g_app_list.erase(g_app_list.begin()+index);
130 return TTSD_ERROR_NONE;
133 int ttsd_data_is_client(const int uid)
135 int vsize = g_app_list.size();
137 for (int i=0; i<vsize; i++) {
138 if(g_app_list[i].uid == uid) {
146 int ttsd_data_get_client_count()
148 return g_app_list.size();
151 int ttsd_data_get_pid(const int uid)
155 index = ttsd_data_is_client(uid);
158 SLOG(LOG_ERROR, TAG_TTSD, "[DATA ERROR] ttsd_data_delete_client() : uid is not valid (%d)\n", uid);
159 return TTSD_ERROR_INVALID_PARAMETER;
162 return g_app_list[index].pid;
165 int ttsd_data_get_speak_data_size(const int uid)
168 index = ttsd_data_is_client(uid);
171 SLOG(LOG_ERROR, TAG_TTSD, "[DATA ERROR] ttsd_data_get_speak_data_size() : uid is not valid (%d)\n", uid);
172 return TTSD_ERROR_INVALID_PARAMETER;
175 int size = g_app_list[index].m_speak_data.size();
179 int ttsd_data_add_speak_data(const int uid, const speak_data_s data)
182 index = ttsd_data_is_client(uid);
185 SLOG(LOG_ERROR, TAG_TTSD, "[DATA ERROR] ttsd_data_add_speak_data() : uid is not valid (%d)\n", uid);
186 return TTSD_ERROR_INVALID_PARAMETER;
189 g_app_list[index].m_speak_data.insert(g_app_list[index].m_speak_data.end(), data);
191 if (1 == data.utt_id)
192 g_app_list[index].utt_id_stopped = 0;
195 __data_show_text_list(index);
197 return TTSD_ERROR_NONE;
200 int ttsd_data_get_speak_data(const int uid, speak_data_s* data)
203 index = ttsd_data_is_client(uid);
206 SLOG(LOG_ERROR, TAG_TTSD, "[DATA ERROR] ttsd_data_get_speak_data() : uid is not valid(%d)\n", uid);
207 return TTSD_ERROR_INVALID_PARAMETER;
210 if (0 == g_app_list[index].m_speak_data.size()) {
211 SLOG(LOG_WARN, TAG_TTSD, "[DATA WARNING] There is no speak data\n");
215 data->lang = g_strdup(g_app_list[index].m_speak_data[0].lang);
216 data->vctype = g_app_list[index].m_speak_data[0].vctype;
217 data->speed = g_app_list[index].m_speak_data[0].speed;
219 data->text = g_app_list[index].m_speak_data[0].text;
220 data->utt_id = g_app_list[index].m_speak_data[0].utt_id;
222 g_app_list[index].m_speak_data.erase(g_app_list[index].m_speak_data.begin());
225 __data_show_text_list(index);
227 return TTSD_ERROR_NONE;
230 int ttsd_data_add_sound_data(const int uid, const sound_data_s data)
233 index = ttsd_data_is_client(uid);
236 SLOG(LOG_ERROR, TAG_TTSD, "[DATA ERROR] ttsd_data_add_sound_data() : uid is not valid (%d)\n", uid);
237 return TTSD_ERROR_INVALID_PARAMETER;
240 g_app_list[index].m_wav_data.insert(g_app_list[index].m_wav_data.end(), data);
243 __data_show_sound_list(index);
245 return TTSD_ERROR_NONE;
248 int ttsd_data_get_sound_data(const int uid, sound_data_s* data)
251 index = ttsd_data_is_client(uid);
254 SLOG(LOG_ERROR, TAG_TTSD, "[DATA ERROR] ttsd_data_get_sound_data() : uid is not valid (%d)\n", uid);
255 return TTSD_ERROR_INVALID_PARAMETER;
258 if (0 == g_app_list[index].m_wav_data.size()) {
259 SLOG(LOG_WARN, TAG_TTSD, "[DATA WARNING] There is no wav data\n");
263 data->data = g_app_list[index].m_wav_data[0].data;
264 data->data_size = g_app_list[index].m_wav_data[0].data_size;
265 data->utt_id = g_app_list[index].m_wav_data[0].utt_id;
266 data->audio_type = g_app_list[index].m_wav_data[0].audio_type;
267 data->rate = g_app_list[index].m_wav_data[0].rate;
268 data->channels = g_app_list[index].m_wav_data[0].channels;
269 data->event = g_app_list[index].m_wav_data[0].event;
271 g_app_list[index].m_wav_data.erase(g_app_list[index].m_wav_data.begin());
274 __data_show_sound_list(index);
276 return TTSD_ERROR_NONE;
279 int ttsd_data_get_sound_data_size(const int uid)
282 index = ttsd_data_is_client(uid);
285 SLOG(LOG_ERROR, TAG_TTSD, "[DATA ERROR] ttsd_data_get_sound_data_size() : uid is not valid (%d)\n", uid);
286 return TTSD_ERROR_INVALID_PARAMETER;
289 return g_app_list[index].m_wav_data.size();
292 int ttsd_data_clear_data(const int uid)
296 index = ttsd_data_is_client(uid);
298 SLOG(LOG_ERROR, TAG_TTSD, "[DATA ERROR] ttsd_data_clear_data() : uid is not valid (%d)\n", uid);
299 return TTSD_ERROR_INVALID_PARAMETER;
302 int removed_last_uttid = -1;
303 /* free allocated data */
306 if (0 != ttsd_data_get_speak_data(uid, &temp)) {
310 if (NULL != temp.text) free(temp.text);
311 if (NULL != temp.lang) free(temp.lang);
313 removed_last_uttid = temp.utt_id;
316 if (-1 != removed_last_uttid) {
317 g_app_list[index].utt_id_stopped = removed_last_uttid;
322 if (0 != ttsd_data_get_sound_data(uid, &temp)) {
326 if (NULL != temp.data) free(temp.data);
329 g_app_list[index].m_speak_data.clear();
330 g_app_list[index].m_wav_data.clear();
332 return TTSD_ERROR_NONE;
335 int ttsd_data_get_client_state(const int uid, app_state_e* state)
339 index = ttsd_data_is_client(uid);
341 SLOG(LOG_ERROR, TAG_TTSD, "[DATA ERROR] ttsd_data_get_client_state() : uid is not valid (%d)\n", uid);
342 return TTSD_ERROR_INVALID_PARAMETER;
345 *state = g_app_list[index].state;
347 return TTSD_ERROR_NONE;
350 int ttsd_data_set_client_state(const int uid, const app_state_e state)
354 index = ttsd_data_is_client(uid);
356 SLOG(LOG_ERROR, TAG_TTSD, "[DATA ERROR] ttsd_data_set_client_state() : uid is not valid (%d)\n", uid);
357 return TTSD_ERROR_INVALID_PARAMETER;
360 if (true == g_mutex_state) {
361 while(true == g_mutex_state) {
365 g_mutex_state = true;
367 /* The client of playing state of all clients is only one. need to check state. */
368 if (APP_STATE_PLAYING == state) {
369 int vsize = g_app_list.size();
370 for (int i=0 ; i<vsize ; i++) {
371 if(g_app_list[i].state == APP_STATE_PLAYING) {
372 SLOG(LOG_ERROR, TAG_TTSD, "[DATA ERROR] ttsd_data_set_client_state() : a playing client has already existed. \n");
373 g_mutex_state = false;
379 g_app_list[index].state = state;
381 g_mutex_state = false;
383 return TTSD_ERROR_NONE;
386 int ttsd_data_get_current_playing()
388 int vsize = g_app_list.size();
390 for (int i=0; i<vsize; i++) {
391 if (APP_STATE_PLAYING == g_app_list[i].state) {
392 return g_app_list[i].uid;
396 SLOG(LOG_DEBUG, TAG_TTSD, "[DATA] NO CURRENT PLAYING !!");
401 int ttsd_data_foreach_clients(ttsd_data_get_client_cb callback, void* user_data)
403 if (NULL == callback) {
404 SLOG(LOG_ERROR, TAG_TTSD, "[DATA ERROR] input data is NULL!!");
408 int vsize = g_app_list.size();
410 for (int i=0; i<vsize; i++) {
411 if (false == callback(g_app_list[i].pid, g_app_list[i].uid, g_app_list[i].state, user_data)) {
419 bool ttsd_data_is_uttid_valid(int uid, int uttid)
423 index = ttsd_data_is_client(uid);
425 SLOG(LOG_ERROR, TAG_TTSD, "[DATA ERROR] ttsd_data_set_client_state() : uid is not valid (%d)\n", uid);
426 return TTSD_ERROR_INVALID_PARAMETER;
429 if (uttid < g_app_list[index].utt_id_stopped)