2d59d888b28d30474554861a959c159466c75e92
[platform/core/uifw/tts.git] / server / ttsd_data.cpp
1 /*
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.
12 */
13
14
15 #include "ttsd_main.h"
16 #include "ttsd_data.h"
17
18 using namespace std;
19
20 static vector<app_data_s> g_app_list;
21
22 static bool g_mutex_state = false;
23
24 /*
25 * functions for debug
26 */
27
28 int __data_show_list()
29 {
30         int vsize = g_app_list.size();
31
32         SLOG(LOG_DEBUG, TAG_TTSD, "----- client list -----");
33
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 );
36         }
37
38         if (0 == vsize) {
39                 SLOG(LOG_DEBUG, TAG_TTSD, "No Client \n");
40         }
41
42         SLOG(LOG_DEBUG, TAG_TTSD, "-----------------------");
43         return TTSD_ERROR_NONE;
44 }
45
46 int __data_show_sound_list(const int index)
47 {
48         SLOG(LOG_DEBUG, TAG_TTSD, "----- Sound list -----");
49         
50         unsigned int i;
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 );
54         }
55
56         if (i == 0) {
57                 SLOG(LOG_DEBUG, TAG_TTSD, "No Sound Data \n");
58         }
59
60         SLOG(LOG_DEBUG, TAG_TTSD, "----------------------");
61         return TTSD_ERROR_NONE;
62 }
63
64 int __data_show_text_list(const int index)
65 {
66         SLOG(LOG_DEBUG, TAG_TTSD, "----- Text list -----");
67
68         unsigned int i;
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 );     
73         }
74
75         if (0 == i) {
76                 SLOG(LOG_DEBUG, TAG_TTSD, "No Text Data \n");
77         }
78
79         SLOG(LOG_DEBUG, TAG_TTSD, "---------------------");
80         return TTSD_ERROR_NONE;
81 }
82
83
84 /*
85 * ttsd data functions
86 */
87
88 int ttsd_data_new_client(const int pid, const int uid)
89 {
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;
93         }
94
95         app_data_s app;
96         app.pid = pid;
97         app.uid = uid;
98         app.utt_id_stopped = 0;
99         app.state = APP_STATE_READY;
100
101         g_app_list.insert( g_app_list.end(), app);
102
103 #ifdef DATA_DEBUG
104         __data_show_list();
105 #endif 
106         return TTSD_ERROR_NONE;
107 }
108
109 int ttsd_data_delete_client(const int uid)
110 {
111         int index = 0;
112
113         index = ttsd_data_is_client(uid);
114         
115         if (index < 0) {
116                 SLOG(LOG_ERROR, TAG_TTSD, "[DATA ERROR] ttsd_data_delete_client() : uid is not valid (%d)\n", uid);     
117                 return -1;
118         }
119
120         if (0 != ttsd_data_clear_data(uid)) {
121                 SLOG(LOG_ERROR, TAG_TTSD, "[DATA ERROR] fail ttsd_data_clear_data()\n");
122                 return -1;
123         }
124
125         g_app_list.erase(g_app_list.begin()+index);
126
127 #ifdef DATA_DEBUG
128         __data_show_list();
129 #endif 
130         return TTSD_ERROR_NONE;
131 }
132
133 int ttsd_data_is_client(const int uid)
134 {
135         int vsize = g_app_list.size();
136
137         for (int i=0; i<vsize; i++) {
138                 if(g_app_list[i].uid == uid) {
139                         return i;               
140                 }
141         }
142
143         return -1;
144 }
145
146 int ttsd_data_get_client_count()
147 {
148         return g_app_list.size();
149 }
150
151 int ttsd_data_get_pid(const int uid)
152 {
153         int index;
154
155         index = ttsd_data_is_client(uid);
156         
157         if (index < 0)  {
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;
160         }
161
162         return g_app_list[index].pid;
163 }
164
165 int ttsd_data_get_speak_data_size(const int uid)
166 {
167         int index = 0;
168         index = ttsd_data_is_client(uid);
169         
170         if (index < 0) {
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;
173         }
174
175         int size = g_app_list[index].m_speak_data.size();
176         return size;
177 }
178
179 int ttsd_data_add_speak_data(const int uid, const speak_data_s data)
180 {
181         int index = 0;
182         index = ttsd_data_is_client(uid);
183
184         if (index < 0) {
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;
187         }
188         
189         g_app_list[index].m_speak_data.insert(g_app_list[index].m_speak_data.end(), data);
190
191         if (1 == data.utt_id)
192                 g_app_list[index].utt_id_stopped = 0;
193
194 #ifdef DATA_DEBUG
195         __data_show_text_list(index);
196 #endif 
197         return TTSD_ERROR_NONE;
198 }
199
200 int ttsd_data_get_speak_data(const int uid, speak_data_s* data)
201 {
202         int index = 0;
203         index = ttsd_data_is_client(uid);
204
205         if (index < 0) {
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;
208         }
209
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"); 
212                 return -1;
213         }
214
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;
218
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;
221
222         g_app_list[index].m_speak_data.erase(g_app_list[index].m_speak_data.begin());
223
224 #ifdef DATA_DEBUG
225         __data_show_text_list(index);
226 #endif 
227         return TTSD_ERROR_NONE;
228 }
229
230 int ttsd_data_add_sound_data(const int uid, const sound_data_s data)
231 {
232         int index = 0;
233         index = ttsd_data_is_client(uid);
234
235         if(index < 0) {
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;
238         }
239
240         g_app_list[index].m_wav_data.insert(g_app_list[index].m_wav_data.end(), data);
241
242 #ifdef DATA_DEBUG
243         __data_show_sound_list(index);
244 #endif 
245         return TTSD_ERROR_NONE;
246 }
247
248 int ttsd_data_get_sound_data(const int uid, sound_data_s* data)
249 {
250         int index = 0;
251         index = ttsd_data_is_client(uid);
252
253         if (index < 0)  {
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;
256         }
257
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"); 
260                 return -1;
261         }
262
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;
270
271         g_app_list[index].m_wav_data.erase(g_app_list[index].m_wav_data.begin());
272
273 #ifdef DATA_DEBUG
274         __data_show_sound_list(index);
275 #endif 
276         return TTSD_ERROR_NONE;
277 }
278
279 int ttsd_data_get_sound_data_size(const int uid)
280 {
281         int index = 0;
282         index = ttsd_data_is_client(uid);
283
284         if (index < 0)  {
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;
287         }
288
289         return g_app_list[index].m_wav_data.size();
290 }
291
292 int ttsd_data_clear_data(const int uid)
293 {
294         int index = 0;
295
296         index = ttsd_data_is_client(uid);
297         if (index < 0) {
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;
300         }
301
302         int removed_last_uttid = -1;
303         /* free allocated data */
304         while(1) {
305                 speak_data_s temp;
306                 if (0 != ttsd_data_get_speak_data(uid, &temp)) {
307                         break;
308                 }
309
310                 if (NULL != temp.text)  free(temp.text);
311                 if (NULL != temp.lang)  free(temp.lang);
312
313                 removed_last_uttid = temp.utt_id;
314         }
315
316         if (-1 != removed_last_uttid) {
317                 g_app_list[index].utt_id_stopped = removed_last_uttid;
318         }
319
320         while(1) {
321                 sound_data_s temp;
322                 if (0 != ttsd_data_get_sound_data(uid, &temp)) {
323                         break;
324                 }
325
326                 if (NULL != temp.data)  free(temp.data);
327         }
328
329         g_app_list[index].m_speak_data.clear();
330         g_app_list[index].m_wav_data.clear();
331
332         return TTSD_ERROR_NONE;
333 }
334
335 int ttsd_data_get_client_state(const int uid, app_state_e* state)
336 {
337         int index = 0;
338
339         index = ttsd_data_is_client(uid);
340         if (index < 0)  {
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;
343         }
344
345         *state = g_app_list[index].state;
346
347         return TTSD_ERROR_NONE;
348 }
349
350 int ttsd_data_set_client_state(const int uid, const app_state_e state)
351 {
352         int index = 0;
353
354         index = ttsd_data_is_client(uid);
355         if (index < 0)  {
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;
358         }
359
360         if (true == g_mutex_state) {
361                 while(true == g_mutex_state)    {
362                 }
363         }
364         
365         g_mutex_state = true;
366
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;
374                                 return -1;
375                         }
376                 }
377         }
378
379         g_app_list[index].state = state;
380
381         g_mutex_state = false;
382
383         return TTSD_ERROR_NONE;
384 }
385
386 int ttsd_data_get_current_playing()
387 {
388         int vsize = g_app_list.size();
389
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;
393                 }
394         }
395
396         SLOG(LOG_DEBUG, TAG_TTSD, "[DATA] NO CURRENT PLAYING !!");      
397
398         return -1;
399 }
400
401 int ttsd_data_foreach_clients(ttsd_data_get_client_cb callback, void* user_data)
402 {
403         if (NULL == callback) {
404                 SLOG(LOG_ERROR, TAG_TTSD, "[DATA ERROR] input data is NULL!!");
405                 return -1;
406         }
407
408         int vsize = g_app_list.size();
409
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)) {
412                         break;
413                 }
414         }
415         
416         return 0;
417 }
418
419 bool ttsd_data_is_uttid_valid(int uid, int uttid)
420 {
421         int index = 0;
422
423         index = ttsd_data_is_client(uid);
424         if (index < 0)  {
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;
427         }
428
429         if (uttid < g_app_list[index].utt_id_stopped)
430                 return false;
431
432         return true;
433 }