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