Add audio stream set when audio handle is re-created
[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                         __set_policy_for_playing(40);
385                 }
386
387                 while (APP_STATE_PLAYING == player->state || APP_STATE_PAUSED == player->state) {
388                         if ((unsigned int)idx >= sound_data->data_size)
389                                 break;
390
391                         if ((unsigned int)idx + SOUND_BUFFER_LENGTH > sound_data->data_size) {
392                                 len = sound_data->data_size - idx;
393                         } else {
394                                 len = SOUND_BUFFER_LENGTH;
395                         }
396
397                         if (AUDIO_STATE_READY == g_audio_state) {
398                                 /* Request prepare */
399                                 ret = audio_out_prepare(g_audio_h);
400                                 if (AUDIO_IO_ERROR_NONE != ret) {
401                                         SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to prepare audio : %d", ret);
402                                         g_playing_info = NULL;
403                                         /* unset volume policy, volume will be 100% */
404                                         __unset_policy_for_playing();
405                                         return;
406                                 }
407                                 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Prepare audio");
408                                 g_audio_state = AUDIO_STATE_PLAY;
409                         }
410
411                         char* temp_data = sound_data->data;
412                         ret = audio_out_write(g_audio_h, &temp_data[idx], len);
413                         if (0 > ret) {
414                                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to audio write - %d", ret);
415                         } else {
416                                 idx += len;
417                         }
418
419                         if (NULL == g_playing_info && APP_STATE_PAUSED != player->state) {
420                                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Current player is NULL");
421                                 g_audio_state = AUDIO_STATE_READY;
422                                 ret = audio_out_unprepare(g_audio_h);
423                                 if (AUDIO_IO_ERROR_NONE != ret) {
424                                         SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
425                                 }
426                                 /* unset volume policy, volume will be 100% */
427                                 __unset_policy_for_playing();
428
429                                 if (NULL != sound_data) {
430                                         if (NULL != sound_data->data) {
431                                                 free(sound_data->data);
432                                                 sound_data->data = NULL;
433                                         }
434
435                                         free(sound_data);
436                                         sound_data = NULL;
437                                 }
438
439                                 return;
440                         }
441
442                         if (APP_STATE_PAUSED == player->state) {
443                                 /* Save data */
444                                 player->paused_data = sound_data;
445
446                                 player->is_paused_data = true;
447                                 player->idx = idx;
448
449                                 g_audio_state = AUDIO_STATE_READY;
450                                 SLOG(LOG_DEBUG, tts_tag(), "[Player] Stop player thread by pause");
451
452                                 /* Request prepare */
453                                 ret = audio_out_unprepare(g_audio_h);
454                                 if (AUDIO_IO_ERROR_NONE != ret) {
455                                         SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
456                                 } else {
457                                         SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
458                                 }
459                                 /* unset volume policy, volume will be 100% */
460                                 __unset_policy_for_playing();
461                                 return;
462                         }
463                 }
464
465                 if (NULL == g_playing_info && APP_STATE_READY == player->state) {
466                         /* player_stop */ 
467                         g_audio_state = AUDIO_STATE_READY;
468                         SLOG(LOG_DEBUG, tts_tag(), "[Player] Stop player thread");
469
470                         /* Request prepare */
471                         ret = audio_out_unprepare(g_audio_h);
472                         if (AUDIO_IO_ERROR_NONE != ret) {
473                                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
474                         } else {
475                                 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
476                         }
477
478                         if (NULL != sound_data) {
479                                 if (NULL != sound_data->data) {
480                                         free(sound_data->data);
481                                         sound_data->data = NULL;
482                                 }
483
484                                 free(sound_data);
485                                 sound_data = NULL;
486                         }
487                         /* unset volume policy, volume will be 100% */
488                         __unset_policy_for_playing();
489                         return;
490                 }
491
492                 if ((APP_STATE_PLAYING == player->state || APP_STATE_PAUSED == player->state) &&
493                         (TTSE_RESULT_EVENT_FINISH == sound_data->event)) {
494                         /* send utterence finish signal */
495                         int pid = ttsd_data_get_pid(player->uid);
496
497                         if (pid <= 0) {
498                                 SLOG(LOG_WARN, tts_tag(), "[Send WARNIING] Current player is not valid");
499                                 /* unset volume policy, volume will be 100% */
500                                 __unset_policy_for_playing();
501                                 return;
502                         }
503
504                         if (0 != ttsdc_send_utt_finish_message(pid, player->uid, sound_data->utt_id)) {
505                                 SLOG(LOG_ERROR, tts_tag(), "[Send ERROR] Fail to send Utterance Completed Signal : pid(%d), uid(%d), uttid(%d)", 
506                                         pid, player->uid, sound_data->utt_id);
507                                 /* unset volume policy, volume will be 100% */
508                                 __unset_policy_for_playing();
509                                 return;
510                         }
511
512                         SLOG(LOG_DEBUG, tts_tag(), "[Player] Finish utterance : uid(%d), uttid(%d)", player->uid, sound_data->utt_id);
513                 }
514
515                 if (NULL != sound_data) {
516                         if (NULL != sound_data->data) {
517                                 free(sound_data->data);
518                                 sound_data->data = NULL;
519                         }
520
521                         free(sound_data);
522                         sound_data = NULL;
523                 }
524
525                 if (NULL == g_playing_info) {
526                         SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Current player is NULL");
527                         g_audio_state = AUDIO_STATE_READY;
528                         ret = audio_out_unprepare(g_audio_h);
529                         if (AUDIO_IO_ERROR_NONE != ret) {
530                                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
531                         }
532                         /* unset volume policy, volume will be 100% */
533                         __unset_policy_for_playing();
534
535                         return;
536                 }
537         }
538 }
539
540 /*
541 * Player Interfaces
542 */
543 int ttsd_player_init()
544 {
545         g_playing_info = NULL;
546         g_audio_state = AUDIO_STATE_NONE;
547         g_audio_h = NULL;
548
549         int ret;
550
551         ecore_thread_max_set(1);
552
553         ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_VOICE_INFORMATION, __player_focus_state_cb, NULL, &g_stream_info_h);
554         if (SOUND_MANAGER_ERROR_NONE != ret) {
555                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to create stream info");
556                 return -1;
557         } else {
558                 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Create stream info");
559         }
560
561         ecore_thread_max_set(1);
562
563         ret = __create_audio_out(TTSE_AUDIO_TYPE_RAW_S16, 16000);
564         if (0 != ret)
565                 return -1;
566
567         g_player_init = true;
568
569         return 0;
570 }
571
572 int ttsd_player_release(void)
573 {
574         if (false == g_player_init) {
575                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
576                 return TTSD_ERROR_OPERATION_FAILED;
577         }
578
579         int ret;
580
581         SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] ==========================");
582         SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Active thread count : %d", ecore_thread_active_get());
583         SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] ==========================");
584
585         /* The thread should be released */
586         int thread_count = ecore_thread_active_get();
587         int count = 0;
588         while (0 < thread_count) {
589                 usleep(10000);
590
591                 count++;
592                 if (20 == count) {
593                         SLOG(LOG_WARN, tts_tag(), "[Player WARNING!!] Thread is blocked. Player release continue.");
594                         break;
595                 }
596
597                 thread_count = ecore_thread_active_get();
598         }
599
600         SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Thread is released");
601
602         ret = __destroy_audio_out();
603         if (0 != ret)
604                 return -1;
605
606         ret = sound_manager_destroy_stream_information(g_stream_info_h);
607         if (SOUND_MANAGER_ERROR_NONE != ret) {
608                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to destroy stream info");
609         } else {
610                 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Destroy stream info");
611         }
612
613         /* clear g_player_list */
614         g_playing_info = NULL;
615         g_player_init = false;
616
617         return 0;
618 }
619
620 int ttsd_player_create_instance(int uid)
621 {
622         if (false == g_player_init) {
623                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
624                 return -1;
625         }
626
627         /* Check uid is duplicated */
628         if (NULL != __player_get_item(uid)) {
629                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is already registered", uid);
630                 return -1;
631         }
632
633         player_s* new_client = (player_s*)calloc(1, sizeof(player_s));
634         if (NULL == new_client) {
635                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to allocate memory");
636                 return TTSE_ERROR_OUT_OF_MEMORY;
637         }
638
639         new_client->uid = uid;
640         new_client->event = TTSE_RESULT_EVENT_FINISH;
641         new_client->state = APP_STATE_READY;
642         new_client->is_paused_data = false;
643         new_client->idx = 0;
644         new_client->paused_data = NULL;
645         
646         SECURE_SLOG(LOG_DEBUG, tts_tag(), "[Player] Create player : uid(%d)", uid);
647
648         g_player_list = g_list_append(g_player_list, new_client);
649
650         return 0;
651 }
652
653 int ttsd_player_destroy_instance(int uid)
654 {
655         if (false == g_player_init) {
656                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
657                 return -1;
658         }
659
660         player_s* current;
661         current = __player_get_item(uid);
662         if (NULL == current) {
663                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
664                 return -1;
665         }
666
667         if (NULL != g_playing_info) {
668                 if (uid == g_playing_info->uid) {
669                         g_playing_info = NULL;
670                 }
671         }
672
673         GList *iter = NULL;
674         player_s *data = NULL;
675
676         if (0 < g_list_length(g_player_list)) {
677                 /* Get a first item */
678                 iter = g_list_first(g_player_list);
679
680                 while (NULL != iter) {
681                         /* Get handle data from list */
682                         data = (player_s*)iter->data;
683
684                         if (NULL != data) {
685                                 /* compare uid */
686                                 if (uid == data->uid) {
687                                         g_player_list = g_list_remove_link(g_player_list, iter);
688                                         free(data);
689                                         g_list_free(iter);              
690                                         break;
691                                 }
692                         }
693
694                         /* Get next item */
695                         iter = g_list_next(iter);
696                 }
697         }
698
699         SLOG(LOG_DEBUG, tts_tag(), "[PLAYER Success] Destroy instance");
700
701         return 0;
702 }
703
704 int ttsd_player_play(int uid)
705 {
706         if (false == g_player_init) {
707                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
708                 return -1;
709         }
710
711         if (NULL != g_playing_info) {
712                 if (uid == g_playing_info->uid) {
713                         SLOG(LOG_DEBUG, tts_tag(), "[Player] uid(%d) has already played", g_playing_info->uid);
714                         return 0;
715                 } else {
716                         SLOG(LOG_WARN, tts_tag(), "[Player WARNING] stop old player (%d)", g_playing_info->uid);
717                         ttsd_player_stop(g_playing_info->uid);
718                 }
719         }
720
721         SLOG(LOG_DEBUG, tts_tag(), "[Player] start play : uid(%d)", uid);
722
723         /* Check sound queue size */
724         if (0 == ttsd_data_get_sound_data_size(uid)) {
725                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] A sound queue of current player(%d) is empty", uid);
726                 return -1;
727         }
728
729         /* Check uid */
730         player_s* current;
731         current = __player_get_item(uid);
732         if (NULL == current) {
733                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
734                 return -1;
735         }
736
737         current->state = APP_STATE_PLAYING;
738
739         g_playing_info = current;
740
741         SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Active thread count : %d", ecore_thread_active_get());
742
743         if (0 < ttsd_data_get_sound_data_size(current->uid)) {
744                 SLOG(LOG_DEBUG, tts_tag(), "[Player] Run thread");
745                 ecore_thread_run(__play_thread, __end_play_thread, NULL, NULL);
746         }
747
748         return 0;
749 }
750
751 int ttsd_player_stop(int uid)
752 {
753         if (false == g_player_init) {
754                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
755                 return -1;
756         }
757
758         /* Check uid */
759         player_s* current;
760         current = __player_get_item(uid);
761         if (NULL == current) {
762                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
763                 return -1;
764         }
765
766         /* check whether uid is current playing or not */
767         if (NULL != g_playing_info) {
768                 if (uid == g_playing_info->uid) {
769                         /* release current playing info */
770                         g_playing_info = NULL;
771                 }
772         } else {
773                 SLOG(LOG_DEBUG, tts_tag(), "[Player] No current playing");
774         }
775
776         if (true == current->is_paused_data) {
777                 if (NULL != current->paused_data) {
778                         if (NULL != current->paused_data->data) {
779                                 free(current->paused_data->data);
780                                 current->paused_data->data = NULL;
781                         }
782
783                         free(current->paused_data);
784                         current->paused_data = NULL;
785                 }
786         }
787
788         current->event = TTSE_RESULT_EVENT_FINISH;
789         current->state = APP_STATE_READY;
790         current->is_paused_data = false;
791         current->idx = 0;
792
793         if (NULL == g_playing_info) {
794                 SLOG(LOG_DEBUG, tts_tag(), "[Player] ==========================");
795                 SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
796                 SLOG(LOG_DEBUG, tts_tag(), "[Player] ==========================");
797
798                 /* The thread should be released */
799                 int thread_count = ecore_thread_active_get();
800                 int count = 0;
801                 while (0 < thread_count) {
802                         usleep(10000);
803
804                         count++;
805                         if (30 == count) {
806                                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING!!] Thread is blocked. Player release continue.");
807                                 break;
808                         }
809
810                         thread_count = ecore_thread_active_get();
811                 }
812
813                 SLOG(LOG_DEBUG, tts_tag(), "[Player] ==========================");
814                 SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
815                 SLOG(LOG_DEBUG, tts_tag(), "[Player] ==========================");
816         }
817
818         SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Stop player : uid(%d)", uid);
819
820         return 0;
821 }
822
823 int ttsd_player_clear(int uid)
824 {
825         if (false == g_player_init) {
826                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
827                 return -1;
828         }
829
830         /* Check uid */
831         player_s* current;
832         current = __player_get_item(uid);
833         if (NULL == current) {
834                 SECURE_SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid); 
835                 return -1;
836         }
837
838         if (true == current->is_paused_data) {
839                 if (NULL != current->paused_data) {
840                         if (NULL != current->paused_data->data) {
841                                 free(current->paused_data->data);
842                                 current->paused_data->data = NULL;
843                         }
844
845                         free(current->paused_data);
846                         current->paused_data = NULL;
847                 }
848         }
849
850         current->event = TTSE_RESULT_EVENT_FINISH;
851         current->state = APP_STATE_READY;
852         current->is_paused_data = false;
853         current->idx = 0;
854
855         SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Clear player : uid(%d)", uid);
856
857         return 0;
858 }
859
860 int ttsd_player_pause(int uid)
861 {
862         SLOG(LOG_DEBUG, tts_tag(), "[Player] pause player : uid(%d)", uid);
863
864         if (false == g_player_init) {
865                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
866                 return -1;
867         }
868
869         /* Check uid */
870         player_s* current;
871         current = __player_get_item(uid);
872         if (NULL == current) {
873                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] ttsd_player_pause() : uid(%d) is not valid", uid);
874                 return -1;
875         }
876
877         /* check whether uid is current playing or not */
878         if (NULL != g_playing_info) {
879                 if (uid == g_playing_info->uid) {
880                         /* release current playing info */
881                         g_playing_info = NULL;
882                 } else {
883                         /* error case */
884                 }
885         }
886
887         current->state = APP_STATE_PAUSED;
888         
889         SLOG(LOG_DEBUG, tts_tag(), "[Player] ==========================");
890         SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
891         SLOG(LOG_DEBUG, tts_tag(), "[Player] ==========================");
892
893         /* The thread should be released */
894         int thread_count = ecore_thread_active_get();
895         int count = 0;
896         while (0 < thread_count) {
897                 usleep(10000);
898
899                 count++;
900                 if (30 == count) {
901                         SLOG(LOG_WARN, tts_tag(), "[Player WARNING!!] Thread is blocked. Player release continue.");
902                         break;
903                 }
904
905                 thread_count = ecore_thread_active_get();
906         }
907
908         SLOG(LOG_DEBUG, tts_tag(), "[Player] ==========================");
909         SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
910         SLOG(LOG_DEBUG, tts_tag(), "[Player] ==========================");
911
912         SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Pause player : uid(%d)", uid);
913
914         return 0;
915 }
916
917 int ttsd_player_resume(int uid)
918 {
919         SLOG(LOG_DEBUG, tts_tag(), "[Player] Resume player : uid(%d)", uid);
920
921         if (false == g_player_init) {
922                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
923                 return -1;
924         }
925
926         /* Check id */
927         player_s* current;
928         current = __player_get_item(uid);
929         if (NULL == current) {
930                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
931                 return -1;
932         }
933
934         /* check current player */
935         if (NULL != g_playing_info)
936                 g_playing_info = NULL;
937
938         current->state = APP_STATE_PLAYING;
939         g_playing_info = current;
940
941         SLOG(LOG_DEBUG, tts_tag(), "[Player] Run thread");
942         ecore_thread_run(__play_thread, __end_play_thread, NULL, NULL);
943
944         return 0;
945 }
946
947 int ttsd_player_all_stop()
948 {
949         if (false == g_player_init) {
950                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
951                 return -1;
952         }
953
954         g_playing_info = NULL;
955
956         GList *iter = NULL;
957         player_s *data = NULL;
958
959         if (0 < g_list_length(g_player_list)) {
960                 /* Get a first item */
961                 iter = g_list_first(g_player_list);
962
963                 while (NULL != iter) {
964                         /* Get handle data from list */
965                         data = (player_s*)iter->data;
966
967                         app_state_e state;
968                         if (0 > ttsd_data_get_client_state(data->uid, &state)) {
969                                 SLOG(LOG_ERROR, tts_tag(), "[player ERROR] uid(%d) is not valid", data->uid);
970                                 ttsd_player_destroy_instance(data->uid);
971                                 iter = g_list_next(iter);
972                                 continue;
973                         }
974
975                         if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
976                                 data->event = TTSE_RESULT_EVENT_FINISH;
977                                 data->state = APP_STATE_READY;
978
979                                 if (true == data->is_paused_data) {
980                                         if (NULL != data->paused_data) {
981                                                 if (NULL != data->paused_data->data) {
982                                                         free(data->paused_data->data);
983                                                         data->paused_data->data = NULL;
984                                                 }
985
986                                                 free(data->paused_data);
987                                                 data->paused_data = NULL;
988                                         }
989                                 }
990
991                                 data->is_paused_data = false;
992                                 data->idx = 0;
993                         }
994
995                         /* Get next item */
996                         iter = g_list_next(iter);
997                 }
998         }
999
1000         SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] player all stop!!");
1001
1002         return 0;
1003 }