Apply audio stream policy and add null checker
[platform/core/uifw/tts.git] / server / ttsd_player.c
1 /*
2 *  Copyright (c) 2011-2016 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 #include <audio_io.h>
15 #include <Ecore.h>
16 #include <sound_manager.h>
17 #include <sound_manager_internal.h>
18
19 #include "ttsd_main.h"
20 #include "ttsd_player.h"
21 #include "ttsd_data.h"
22 #include "ttsd_dbus.h"
23
24 /*
25 * Internal data structure
26 */
27
28 typedef enum {
29         AUDIO_STATE_NONE = 0,
30         AUDIO_STATE_READY,
31         AUDIO_STATE_PLAY
32 } audio_state_e;
33
34 typedef struct {
35         int                     uid;    /** client id */
36         app_state_e             state;  /** client state */
37
38         /* Current utterance information */
39         ttse_result_event_e     event;  /** event of last utterance */
40
41         bool                    is_paused_data;
42         int                     idx;
43         sound_data_s*           paused_data;
44 } player_s;
45
46 #define SOUND_BUFFER_LENGTH     2048
47
48 /** player init info */
49 static bool g_player_init = false;
50
51 /** Client list */
52 static GList *g_player_list;
53
54 /** current player information */
55 static player_s* g_playing_info;
56
57 /* player state */
58 static audio_state_e g_audio_state;
59
60 static ttse_audio_type_e g_audio_type;
61
62 static int g_sampling_rate;
63
64 static audio_out_h g_audio_h;
65
66 static sound_stream_info_h g_stream_info_h;
67
68 /*
69 * Internal Interfaces
70 */
71
72 player_s* __player_get_item(int uid)
73 {
74         GList *iter = NULL;
75         player_s *data = NULL;
76
77         if (0 < g_list_length(g_player_list)) {
78                 /* Get a first item */
79                 iter = g_list_first(g_player_list);
80
81                 while (NULL != iter) {
82                         /* Get handle data from list */
83                         data = (player_s*)iter->data;
84
85                         /* compare uid */
86                         if (uid == data->uid)
87                                 return data;
88
89                         /* Get next item */
90                         iter = g_list_next(iter);
91                 }
92         }
93
94         return NULL;
95 }
96
97 void __player_focus_state_cb(sound_stream_info_h stream_info, sound_stream_focus_change_reason_e reason_for_change, const char *extra_info, void *user_data)
98 {
99         SLOG(LOG_DEBUG, tts_tag(), "===== Focus state changed cb");
100
101         if (stream_info != g_stream_info_h) {
102                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Invalid stream info handle");
103                 return;
104         }
105
106         int ret;
107         sound_stream_focus_state_e state_for_playback ;
108         ret = sound_manager_get_focus_state(g_stream_info_h, &state_for_playback, NULL);
109         if (SOUND_MANAGER_ERROR_NONE != ret) {
110                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to get focus state");
111                 return;
112         }
113
114         SLOG(LOG_WARN, tts_tag(), "[Player] focus state changed to (%d) with reason(%d)", (int)state_for_playback, (int)reason_for_change);
115
116         if (AUDIO_STATE_PLAY == g_audio_state && SOUND_STREAM_FOCUS_STATE_RELEASED == state_for_playback) {
117                 if (TTSD_MODE_DEFAULT == ttsd_get_mode()) {
118                         g_audio_state = AUDIO_STATE_READY;
119         
120                         if (NULL == g_playing_info) {
121                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] No current player");
122                                 return;
123                         }
124
125                         if (APP_STATE_PLAYING == g_playing_info->state) {
126                                 g_playing_info->state = APP_STATE_PAUSED;
127         
128                                 ttsd_data_set_client_state(g_playing_info->uid, APP_STATE_PAUSED);
129
130                                 int pid = ttsd_data_get_pid(g_playing_info->uid);
131
132                                 /* send message to client about changing state */
133                                 ttsdc_send_set_state_message(pid, g_playing_info->uid, APP_STATE_PAUSED);
134                         }
135                 } else {
136                         SLOG(LOG_DEBUG, tts_tag(), "[Player] Ignore focus state cb - mode(%d)", ttsd_get_mode());
137                 }
138         }
139
140         SLOG(LOG_DEBUG, tts_tag(), "=====");
141         SLOG(LOG_DEBUG, tts_tag(), "");
142
143         return;
144 }
145
146 static int __create_audio_out(ttse_audio_type_e type, int rate)
147 {
148         int ret = -1;
149         audio_sample_type_e sample_type;
150
151         if (TTSE_AUDIO_TYPE_RAW_S16 == type) {
152                 sample_type = AUDIO_SAMPLE_TYPE_S16_LE;
153         } else {
154                 sample_type = AUDIO_SAMPLE_TYPE_U8;
155         }
156
157         ret = audio_out_create_new(rate, AUDIO_CHANNEL_MONO, sample_type, &g_audio_h);
158         if (AUDIO_IO_ERROR_NONE != ret) {
159                 g_audio_state = AUDIO_STATE_NONE;
160                 g_audio_h = NULL;
161                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to create audio");
162                 return -1;
163         } else {
164                 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Create audio");
165         }
166
167         g_audio_type = type;
168         g_sampling_rate = rate;
169
170         g_audio_state = AUDIO_STATE_READY;
171
172         return 0;
173 }
174
175 static int __destroy_audio_out()
176 {
177         if (NULL == g_audio_h) {
178                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Current handle is not valid");
179                 return -1;
180         }
181
182         int ret = -1;
183         ret = audio_out_destroy(g_audio_h);
184         if (AUDIO_IO_ERROR_NONE != ret) {
185                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to destroy audio");
186                 return -1;
187         } else {
188                 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Destroy audio");
189         }
190
191         g_audio_type = 0;
192         g_sampling_rate = 0;
193
194         g_audio_state = AUDIO_STATE_NONE;
195         g_audio_h = NULL;
196
197         return 0;
198 }
199
200 static void __end_play_thread(void *data, Ecore_Thread *thread)
201 {
202         SLOG(LOG_ERROR, tts_tag(), "===== End thread");
203 }
204
205 static void __set_policy_for_playing(int volume)
206 {
207         /* Set stream info */
208         int ret;
209         if (TTSD_MODE_DEFAULT == ttsd_get_mode()) {
210                 ret = sound_manager_acquire_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, NULL);
211                 if (SOUND_MANAGER_ERROR_NONE != ret) {
212                         SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to acquire focus");
213                 }
214         }
215         ret = audio_out_set_stream_info(g_audio_h, g_stream_info_h);
216         if (AUDIO_IO_ERROR_NONE != ret) {
217                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to set stream info");
218         }
219
220         return;
221 }
222
223 static void __unset_policy_for_playing()
224 {
225         int ret;
226         /* Unset stream info */
227         if (TTSD_MODE_DEFAULT == ttsd_get_mode()) {
228                 ret = sound_manager_release_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, NULL);
229                 if (SOUND_MANAGER_ERROR_NONE != ret) {
230                         SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to release focus");
231                 }
232         }
233
234         return;
235 }
236
237 static void __play_thread(void *data, Ecore_Thread *thread)
238 {
239         SLOG(LOG_DEBUG, tts_tag(), "===== Start thread");
240
241         if (NULL == g_playing_info) {
242                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] No current player");
243                 return;
244         }
245
246         player_s* player = g_playing_info;
247         sound_data_s* sound_data = NULL;
248
249         int ret = -1;
250         int len = SOUND_BUFFER_LENGTH;
251         int idx = 0;
252
253         /* set volume policy as 40% */
254         __set_policy_for_playing(40);
255         while (1) {
256                 if (true == player->is_paused_data) {
257                         /* Resume player */
258                         sound_data = player->paused_data;
259                         player->paused_data = NULL;
260
261                         idx = player->idx;
262
263                         player->is_paused_data = false;
264                         player->idx = 0;
265
266                         if (NULL == sound_data) {
267                                 /* Request unprepare */
268                                 ret = audio_out_unprepare(g_audio_h);
269                                 if (AUDIO_IO_ERROR_NONE != ret) {
270                                         SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
271                                 } else {
272                                         SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
273                                 }
274
275                                 g_audio_state = AUDIO_STATE_READY;
276
277                                 /* unset volume policy, volume will be 100% */
278                                 __unset_policy_for_playing();
279                                 return;
280                         }
281                         SLOG(LOG_INFO, tts_tag(), "[Player] Sound info : id(%d) data(%p) size(%d) audiotype(%d) rate(%d) event(%d)", 
282                                 sound_data->utt_id, sound_data->data, sound_data->data_size, sound_data->audio_type, sound_data->rate, sound_data->event);
283                 } else {
284                         sound_data = NULL;
285                         ret = ttsd_data_get_sound_data(player->uid, &sound_data);
286                         if (0 != ret || NULL == sound_data) {
287                                 /* empty queue */
288                                 SLOG(LOG_DEBUG, tts_tag(), "[Player] No sound data. Waiting mode");
289                                 /* release audio & recover session */
290                                 ret = audio_out_unprepare(g_audio_h);
291                                 if (AUDIO_IO_ERROR_NONE != ret) {
292                                         SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
293                                 } else {
294                                         SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
295                                 }
296                                 g_audio_state = AUDIO_STATE_READY;
297
298                                 /* unset volume policy, volume will be 100% */
299                                 __unset_policy_for_playing();
300
301                                 /* wait for new audio data come */
302                                 while (1) {
303                                         usleep(10000);
304                                         if (NULL == g_playing_info) {
305                                                 /* current playing uid is replaced */
306                                                 SLOG(LOG_DEBUG, tts_tag(), "[Player] Finish thread");
307                                                 return;
308                                         } else if (0 < ttsd_data_get_sound_data_size(player->uid)) {
309                                                 /* new audio data come */
310                                                 SLOG(LOG_DEBUG, tts_tag(), "[Player] Resume thread");
311                                                 break;
312                                         }
313                                 }
314
315                                 /* set volume policy as 40%, when resume play thread*/
316                                 __set_policy_for_playing(40);
317
318                                 /* resume play thread */
319                                 player->state = APP_STATE_PLAYING;
320                                 continue;
321                         }
322
323                         /* If wdata's event is 'start', current wdata is first data of engine for synthesis.
324                          * If wdata's event is 'finish', player should check previous event to know whether this wdata is first or not.
325                          * When previous wdata's event is 'finish' and current wdata's event is 'finish',
326                          * the player should send utt started event.
327                          */
328                         if (TTSE_RESULT_EVENT_START == sound_data->event ||
329                            (TTSE_RESULT_EVENT_FINISH == player->event && TTSE_RESULT_EVENT_FINISH == sound_data->event)) {
330                                 int pid = ttsd_data_get_pid(player->uid);
331
332                                 if (pid <= 0) {
333                                         SLOG(LOG_WARN, tts_tag(), "[Send WARNIING] Current player is not valid");
334                                         /* unset volume policy, volume will be 100% */
335                                         __unset_policy_for_playing();
336                                         return;
337                                 }
338
339                                 if (0 != ttsdc_send_utt_start_message(pid, player->uid, sound_data->utt_id)) {
340                                         SLOG(LOG_ERROR, tts_tag(), "[Send ERROR] Fail to send Utterance Start Signal : pid(%d), uid(%d), uttid(%d)", 
341                                                 pid, player->uid, sound_data->utt_id);
342                                 }
343                                 SLOG(LOG_DEBUG, tts_tag(), "[Player] Start utterance : uid(%d), uttid(%d)", player->uid, sound_data->utt_id);
344                         }
345
346                         /* Save last event to check utterance start */
347                         player->event = sound_data->event;
348                         idx = 0;
349
350                         if (NULL == sound_data->data || 0 >= sound_data->data_size) {
351                                 if (TTSE_RESULT_EVENT_FINISH == sound_data->event) {
352                                         SLOG(LOG_DEBUG, tts_tag(), "No sound data");
353                                         /* send utterence finish signal */
354                                         int pid = ttsd_data_get_pid(player->uid);
355
356                                         if (pid <= 0) {
357                                                 SLOG(LOG_WARN, tts_tag(), "[Send WARNIING] Current player is not valid");
358                                                 /* unset volume policy, volume will be 100% */
359                                                 __unset_policy_for_playing();
360                                                 return;
361                                         }
362                                         if (0 != ttsdc_send_utt_finish_message(pid, player->uid, sound_data->utt_id)) {
363                                                 SLOG(LOG_ERROR, tts_tag(), "[Send ERROR] Fail to send Utterance Completed Signal : pid(%d), uid(%d), uttid(%d)", 
364                                                         pid, player->uid, sound_data->utt_id);
365                                         }
366                                 }
367                                 SLOG(LOG_DEBUG, tts_tag(), "[Player] Finish utterance : uid(%d), uttid(%d)", player->uid, sound_data->utt_id);
368                                 continue;
369                         }
370                 }
371
372                 if (g_sampling_rate != sound_data->rate || g_audio_type != sound_data->audio_type) {
373                         SLOG(LOG_DEBUG, tts_tag(), "[Player] Change audio handle : org type(%d) org rate(%d)", g_audio_type, g_sampling_rate);
374                         if (NULL != g_audio_h) {
375                                 __destroy_audio_out();
376                         }
377
378                         if (0 > __create_audio_out(sound_data->audio_type, sound_data->rate)) {
379                                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to create audio out");
380                                 /* unset volume policy, volume will be 100% */
381                                 __unset_policy_for_playing();
382                                 return;
383                         }
384                 }
385
386                 while (APP_STATE_PLAYING == player->state || APP_STATE_PAUSED == player->state) {
387                         if ((unsigned int)idx >= sound_data->data_size)
388                                 break;
389
390                         if ((unsigned int)idx + SOUND_BUFFER_LENGTH > sound_data->data_size) {
391                                 len = sound_data->data_size - idx;
392                         } else {
393                                 len = SOUND_BUFFER_LENGTH;
394                         }
395
396                         if (AUDIO_STATE_READY == g_audio_state) {
397                                 /* Request prepare */
398                                 ret = audio_out_prepare(g_audio_h);
399                                 if (AUDIO_IO_ERROR_NONE != ret) {
400                                         SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to prepare audio : %d", ret);
401                                         g_playing_info = NULL;
402                                         /* unset volume policy, volume will be 100% */
403                                         __unset_policy_for_playing();
404                                         return;
405                                 }
406                                 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Prepare audio");
407                                 g_audio_state = AUDIO_STATE_PLAY;
408                         }
409
410                         char* temp_data = sound_data->data;
411                         ret = audio_out_write(g_audio_h, &temp_data[idx], len);
412                         if (0 > ret) {
413                                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to audio write - %d", ret);
414                         } else {
415                                 idx += len;
416                         }
417
418                         if (NULL == g_playing_info && APP_STATE_PAUSED != player->state) {
419                                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Current player is NULL");
420                                 g_audio_state = AUDIO_STATE_READY;
421                                 ret = audio_out_unprepare(g_audio_h);
422                                 if (AUDIO_IO_ERROR_NONE != ret) {
423                                         SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
424                                 }
425                                 /* unset volume policy, volume will be 100% */
426                                 __unset_policy_for_playing();
427
428                                 if (NULL != sound_data) {
429                                         if (NULL != sound_data->data) {
430                                                 free(sound_data->data);
431                                                 sound_data->data = NULL;
432                                         }
433
434                                         free(sound_data);
435                                         sound_data = NULL;
436                                 }
437
438                                 return;
439                         }
440
441                         if (APP_STATE_PAUSED == player->state) {
442                                 /* Save data */
443                                 player->paused_data = sound_data;
444
445                                 player->is_paused_data = true;
446                                 player->idx = idx;
447
448                                 g_audio_state = AUDIO_STATE_READY;
449                                 SLOG(LOG_DEBUG, tts_tag(), "[Player] Stop player thread by pause");
450
451                                 /* Request prepare */
452                                 ret = audio_out_unprepare(g_audio_h);
453                                 if (AUDIO_IO_ERROR_NONE != ret) {
454                                         SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
455                                 } else {
456                                         SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
457                                 }
458                                 /* unset volume policy, volume will be 100% */
459                                 __unset_policy_for_playing();
460                                 return;
461                         }
462                 }
463
464                 if (NULL == g_playing_info && APP_STATE_READY == player->state) {
465                         /* player_stop */ 
466                         g_audio_state = AUDIO_STATE_READY;
467                         SLOG(LOG_DEBUG, tts_tag(), "[Player] Stop player thread");
468
469                         /* Request prepare */
470                         ret = audio_out_unprepare(g_audio_h);
471                         if (AUDIO_IO_ERROR_NONE != ret) {
472                                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
473                         } else {
474                                 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
475                         }
476
477                         if (NULL != sound_data) {
478                                 if (NULL != sound_data->data) {
479                                         free(sound_data->data);
480                                         sound_data->data = NULL;
481                                 }
482
483                                 free(sound_data);
484                                 sound_data = NULL;
485                         }
486                         /* unset volume policy, volume will be 100% */
487                         __unset_policy_for_playing();
488                         return;
489                 }
490
491                 if ((APP_STATE_PLAYING == player->state || APP_STATE_PAUSED == player->state) &&
492                         (TTSE_RESULT_EVENT_FINISH == sound_data->event)) {
493                         /* send utterence finish signal */
494                         int pid = ttsd_data_get_pid(player->uid);
495
496                         if (pid <= 0) {
497                                 SLOG(LOG_WARN, tts_tag(), "[Send WARNIING] Current player is not valid");
498                                 /* unset volume policy, volume will be 100% */
499                                 __unset_policy_for_playing();
500                                 return;
501                         }
502
503                         if (0 != ttsdc_send_utt_finish_message(pid, player->uid, sound_data->utt_id)) {
504                                 SLOG(LOG_ERROR, tts_tag(), "[Send ERROR] Fail to send Utterance Completed Signal : pid(%d), uid(%d), uttid(%d)", 
505                                         pid, player->uid, sound_data->utt_id);
506                                 /* unset volume policy, volume will be 100% */
507                                 __unset_policy_for_playing();
508                                 return;
509                         }
510
511                         SLOG(LOG_DEBUG, tts_tag(), "[Player] Finish utterance : uid(%d), uttid(%d)", player->uid, sound_data->utt_id);
512                 }
513
514                 if (NULL != sound_data) {
515                         if (NULL != sound_data->data) {
516                                 free(sound_data->data);
517                                 sound_data->data = NULL;
518                         }
519
520                         free(sound_data);
521                         sound_data = NULL;
522                 }
523
524                 if (NULL == g_playing_info) {
525                         SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Current player is NULL");
526                         g_audio_state = AUDIO_STATE_READY;
527                         ret = audio_out_unprepare(g_audio_h);
528                         if (AUDIO_IO_ERROR_NONE != ret) {
529                                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
530                         }
531                         /* unset volume policy, volume will be 100% */
532                         __unset_policy_for_playing();
533
534                         return;
535                 }
536         }
537 }
538
539 /*
540 * Player Interfaces
541 */
542 int ttsd_player_init()
543 {
544         g_playing_info = NULL;
545         g_audio_state = AUDIO_STATE_NONE;
546         g_audio_h = NULL;
547
548         int ret;
549
550         ecore_thread_max_set(1);
551
552         ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_VOICE_INFORMATION, __player_focus_state_cb, NULL, &g_stream_info_h);
553         if (SOUND_MANAGER_ERROR_NONE != ret) {
554                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to create stream info");
555                 return -1;
556         } else {
557                 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Create stream info");
558         }
559
560         ecore_thread_max_set(1);
561
562         ret = __create_audio_out(TTSE_AUDIO_TYPE_RAW_S16, 16000);
563         if (0 != ret)
564                 return -1;
565
566         g_player_init = true;
567
568         return 0;
569 }
570
571 int ttsd_player_release(void)
572 {
573         if (false == g_player_init) {
574                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
575                 return TTSD_ERROR_OPERATION_FAILED;
576         }
577
578         int ret;
579
580         SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] ==========================");
581         SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Active thread count : %d", ecore_thread_active_get());
582         SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] ==========================");
583
584         /* The thread should be released */
585         int thread_count = ecore_thread_active_get();
586         int count = 0;
587         while (0 < thread_count) {
588                 usleep(10000);
589
590                 count++;
591                 if (20 == count) {
592                         SLOG(LOG_WARN, tts_tag(), "[Player WARNING!!] Thread is blocked. Player release continue.");
593                         break;
594                 }
595
596                 thread_count = ecore_thread_active_get();
597         }
598
599         SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Thread is released");
600
601         ret = __destroy_audio_out();
602         if (0 != ret)
603                 return -1;
604
605         ret = sound_manager_destroy_stream_information(g_stream_info_h);
606         if (SOUND_MANAGER_ERROR_NONE != ret) {
607                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to destroy stream info");
608         } else {
609                 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Destroy stream info");
610         }
611
612         /* clear g_player_list */
613         g_playing_info = NULL;
614         g_player_init = false;
615
616         return 0;
617 }
618
619 int ttsd_player_create_instance(int uid)
620 {
621         if (false == g_player_init) {
622                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
623                 return -1;
624         }
625
626         /* Check uid is duplicated */
627         if (NULL != __player_get_item(uid)) {
628                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is already registered", uid);
629                 return -1;
630         }
631
632         player_s* new_client = (player_s*)calloc(1, sizeof(player_s));
633         if (NULL == new_client) {
634                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to allocate memory");
635                 return TTSE_ERROR_OUT_OF_MEMORY;
636         }
637
638         new_client->uid = uid;
639         new_client->event = TTSE_RESULT_EVENT_FINISH;
640         new_client->state = APP_STATE_READY;
641         new_client->is_paused_data = false;
642         new_client->idx = 0;
643         new_client->paused_data = NULL;
644         
645         SECURE_SLOG(LOG_DEBUG, tts_tag(), "[Player] Create player : uid(%d)", uid);
646
647         g_player_list = g_list_append(g_player_list, new_client);
648
649         return 0;
650 }
651
652 int ttsd_player_destroy_instance(int uid)
653 {
654         if (false == g_player_init) {
655                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
656                 return -1;
657         }
658
659         player_s* current;
660         current = __player_get_item(uid);
661         if (NULL == current) {
662                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
663                 return -1;
664         }
665
666         if (NULL != g_playing_info) {
667                 if (uid == g_playing_info->uid) {
668                         g_playing_info = NULL;
669                 }
670         }
671
672         GList *iter = NULL;
673         player_s *data = NULL;
674
675         if (0 < g_list_length(g_player_list)) {
676                 /* Get a first item */
677                 iter = g_list_first(g_player_list);
678
679                 while (NULL != iter) {
680                         /* Get handle data from list */
681                         data = (player_s*)iter->data;
682
683                         if (NULL != data) {
684                                 /* compare uid */
685                                 if (uid == data->uid) {
686                                         g_player_list = g_list_remove_link(g_player_list, iter);
687                                         free(data);
688                                         g_list_free(iter);              
689                                         break;
690                                 }
691                         }
692
693                         /* Get next item */
694                         iter = g_list_next(iter);
695                 }
696         }
697
698         SLOG(LOG_DEBUG, tts_tag(), "[PLAYER Success] Destroy instance");
699
700         return 0;
701 }
702
703 int ttsd_player_play(int uid)
704 {
705         if (false == g_player_init) {
706                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
707                 return -1;
708         }
709
710         if (NULL != g_playing_info) {
711                 if (uid == g_playing_info->uid) {
712                         SLOG(LOG_DEBUG, tts_tag(), "[Player] uid(%d) has already played", g_playing_info->uid);
713                         return 0;
714                 } else {
715                         SLOG(LOG_WARN, tts_tag(), "[Player WARNING] stop old player (%d)", g_playing_info->uid);
716                         ttsd_player_stop(g_playing_info->uid);
717                 }
718         }
719
720         SLOG(LOG_DEBUG, tts_tag(), "[Player] start play : uid(%d)", uid);
721
722         /* Check sound queue size */
723         if (0 == ttsd_data_get_sound_data_size(uid)) {
724                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] A sound queue of current player(%d) is empty", uid);
725                 return -1;
726         }
727
728         /* Check uid */
729         player_s* current;
730         current = __player_get_item(uid);
731         if (NULL == current) {
732                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
733                 return -1;
734         }
735
736         current->state = APP_STATE_PLAYING;
737
738         g_playing_info = current;
739
740         SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Active thread count : %d", ecore_thread_active_get());
741
742         if (0 < ttsd_data_get_sound_data_size(current->uid)) {
743                 SLOG(LOG_DEBUG, tts_tag(), "[Player] Run thread");
744                 ecore_thread_run(__play_thread, __end_play_thread, NULL, NULL);
745         }
746
747         return 0;
748 }
749
750 int ttsd_player_stop(int uid)
751 {
752         if (false == g_player_init) {
753                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
754                 return -1;
755         }
756
757         /* Check uid */
758         player_s* current;
759         current = __player_get_item(uid);
760         if (NULL == current) {
761                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
762                 return -1;
763         }
764
765         /* check whether uid is current playing or not */
766         if (NULL != g_playing_info) {
767                 if (uid == g_playing_info->uid) {
768                         /* release current playing info */
769                         g_playing_info = NULL;
770                 }
771         } else {
772                 SLOG(LOG_DEBUG, tts_tag(), "[Player] No current playing");
773         }
774
775         if (true == current->is_paused_data) {
776                 if (NULL != current->paused_data) {
777                         if (NULL != current->paused_data->data) {
778                                 free(current->paused_data->data);
779                                 current->paused_data->data = NULL;
780                         }
781
782                         free(current->paused_data);
783                         current->paused_data = NULL;
784                 }
785         }
786
787         current->event = TTSE_RESULT_EVENT_FINISH;
788         current->state = APP_STATE_READY;
789         current->is_paused_data = false;
790         current->idx = 0;
791
792         if (NULL == g_playing_info) {
793                 SLOG(LOG_DEBUG, tts_tag(), "[Player] ==========================");
794                 SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
795                 SLOG(LOG_DEBUG, tts_tag(), "[Player] ==========================");
796
797                 /* The thread should be released */
798                 int thread_count = ecore_thread_active_get();
799                 int count = 0;
800                 while (0 < thread_count) {
801                         usleep(10000);
802
803                         count++;
804                         if (30 == count) {
805                                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING!!] Thread is blocked. Player release continue.");
806                                 break;
807                         }
808
809                         thread_count = ecore_thread_active_get();
810                 }
811
812                 SLOG(LOG_DEBUG, tts_tag(), "[Player] ==========================");
813                 SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
814                 SLOG(LOG_DEBUG, tts_tag(), "[Player] ==========================");
815         }
816
817         SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Stop player : uid(%d)", uid);
818
819         return 0;
820 }
821
822 int ttsd_player_clear(int uid)
823 {
824         if (false == g_player_init) {
825                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
826                 return -1;
827         }
828
829         /* Check uid */
830         player_s* current;
831         current = __player_get_item(uid);
832         if (NULL == current) {
833                 SECURE_SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid); 
834                 return -1;
835         }
836
837         if (true == current->is_paused_data) {
838                 if (NULL != current->paused_data) {
839                         if (NULL != current->paused_data->data) {
840                                 free(current->paused_data->data);
841                                 current->paused_data->data = NULL;
842                         }
843
844                         free(current->paused_data);
845                         current->paused_data = NULL;
846                 }
847         }
848
849         current->event = TTSE_RESULT_EVENT_FINISH;
850         current->state = APP_STATE_READY;
851         current->is_paused_data = false;
852         current->idx = 0;
853
854         SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Clear player : uid(%d)", uid);
855
856         return 0;
857 }
858
859 int ttsd_player_pause(int uid)
860 {
861         SLOG(LOG_DEBUG, tts_tag(), "[Player] pause player : uid(%d)", uid);
862
863         if (false == g_player_init) {
864                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
865                 return -1;
866         }
867
868         /* Check uid */
869         player_s* current;
870         current = __player_get_item(uid);
871         if (NULL == current) {
872                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] ttsd_player_pause() : uid(%d) is not valid", uid);
873                 return -1;
874         }
875
876         /* check whether uid is current playing or not */
877         if (NULL != g_playing_info) {
878                 if (uid == g_playing_info->uid) {
879                         /* release current playing info */
880                         g_playing_info = NULL;
881                 } else {
882                         /* error case */
883                 }
884         }
885
886         current->state = APP_STATE_PAUSED;
887         
888         SLOG(LOG_DEBUG, tts_tag(), "[Player] ==========================");
889         SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
890         SLOG(LOG_DEBUG, tts_tag(), "[Player] ==========================");
891
892         /* The thread should be released */
893         int thread_count = ecore_thread_active_get();
894         int count = 0;
895         while (0 < thread_count) {
896                 usleep(10000);
897
898                 count++;
899                 if (30 == count) {
900                         SLOG(LOG_WARN, tts_tag(), "[Player WARNING!!] Thread is blocked. Player release continue.");
901                         break;
902                 }
903
904                 thread_count = ecore_thread_active_get();
905         }
906
907         SLOG(LOG_DEBUG, tts_tag(), "[Player] ==========================");
908         SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
909         SLOG(LOG_DEBUG, tts_tag(), "[Player] ==========================");
910
911         SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Pause player : uid(%d)", uid);
912
913         return 0;
914 }
915
916 int ttsd_player_resume(int uid)
917 {
918         SLOG(LOG_DEBUG, tts_tag(), "[Player] Resume player : uid(%d)", uid);
919
920         if (false == g_player_init) {
921                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
922                 return -1;
923         }
924
925         /* Check id */
926         player_s* current;
927         current = __player_get_item(uid);
928         if (NULL == current) {
929                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
930                 return -1;
931         }
932
933         /* check current player */
934         if (NULL != g_playing_info)
935                 g_playing_info = NULL;
936
937         current->state = APP_STATE_PLAYING;
938         g_playing_info = current;
939
940         SLOG(LOG_DEBUG, tts_tag(), "[Player] Run thread");
941         ecore_thread_run(__play_thread, __end_play_thread, NULL, NULL);
942
943         return 0;
944 }
945
946 int ttsd_player_all_stop()
947 {
948         if (false == g_player_init) {
949                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
950                 return -1;
951         }
952
953         g_playing_info = NULL;
954
955         GList *iter = NULL;
956         player_s *data = NULL;
957
958         if (0 < g_list_length(g_player_list)) {
959                 /* Get a first item */
960                 iter = g_list_first(g_player_list);
961
962                 while (NULL != iter) {
963                         /* Get handle data from list */
964                         data = (player_s*)iter->data;
965
966                         app_state_e state;
967                         if (0 > ttsd_data_get_client_state(data->uid, &state)) {
968                                 SLOG(LOG_ERROR, tts_tag(), "[player ERROR] uid(%d) is not valid", data->uid);
969                                 ttsd_player_destroy_instance(data->uid);
970                                 iter = g_list_next(iter);
971                                 continue;
972                         }
973
974                         if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
975                                 data->event = TTSE_RESULT_EVENT_FINISH;
976                                 data->state = APP_STATE_READY;
977
978                                 if (true == data->is_paused_data) {
979                                         if (NULL != data->paused_data) {
980                                                 if (NULL != data->paused_data->data) {
981                                                         free(data->paused_data->data);
982                                                         data->paused_data->data = NULL;
983                                                 }
984
985                                                 free(data->paused_data);
986                                                 data->paused_data = NULL;
987                                         }
988                                 }
989
990                                 data->is_paused_data = false;
991                                 data->idx = 0;
992                         }
993
994                         /* Get next item */
995                         iter = g_list_next(iter);
996                 }
997         }
998
999         SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] player all stop!!");
1000
1001         return 0;
1002 }