aa2fbccf22c981084a05a25d636ba2e3c584d9a7
[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 #include "tts_internal.h"
25 #include "ttsd_server.h"
26
27 /*
28 * Internal data structure
29 */
30
31 typedef enum {
32         AUDIO_STATE_NONE = 0,
33         AUDIO_STATE_READY,
34         AUDIO_STATE_PLAY
35 } audio_state_e;
36
37 typedef struct {
38         int                     uid;    /** client id */
39         app_tts_state_e         state;  /** client state */
40
41         /* Current utterance information */
42         ttse_result_event_e     event;  /** event of last utterance */
43
44         bool                    is_paused_data;
45         int                     idx;
46         sound_data_s*           paused_data;
47 } player_s;
48
49 #define SOUND_BUFFER_LENGTH     2048
50 #define FOCUS_SERVER_READY "/tmp/.sound_server_ready"
51
52 /** player init info */
53 static bool g_player_init = false;
54
55 /** Client list */
56 static GList *g_player_list;
57
58 /** current player information */
59 static player_s* g_playing_info;
60
61 /* player state */
62 static audio_state_e g_audio_state;
63
64 static ttse_audio_type_e g_audio_type;
65
66 static int g_sampling_rate;
67
68 static audio_out_h g_audio_h;
69
70 static sound_stream_info_h g_stream_info_h;
71
72 static int g_focus_watch_id;
73
74
75 /*
76 * Internal Interfaces
77 */
78
79 player_s* __player_get_item(int uid)
80 {
81         GList *iter = NULL;
82         player_s *data = NULL;
83
84         if (0 < g_list_length(g_player_list)) {
85                 /* Get a first item */
86                 iter = g_list_first(g_player_list);
87
88                 while (NULL != iter) {
89                         /* Get handle data from list */
90                         data = (player_s*)iter->data;
91
92                         /* compare uid */
93                         if (uid == data->uid)
94                                 return data;
95
96                         /* Get next item */
97                         iter = g_list_next(iter);
98                 }
99         }
100
101         return NULL;
102 }
103
104 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,
105                                                         sound_stream_focus_change_reason_e reason_for_change, int sound_behavior, const char *extra_info, void *user_data)
106 {
107         SLOG(LOG_DEBUG, tts_tag(), "@@@ Focus state changed cb");
108
109         if (stream_info != g_stream_info_h) {
110                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Invalid stream info handle");
111                 return;
112         }
113         SLOG(LOG_WARN, tts_tag(), "[Player] focus state changed to (%d) with reason(%d) and extra info(%s)", (int)focus_state, (int)reason_for_change, extra_info);
114
115         if (AUDIO_STATE_PLAY == g_audio_state && focus_mask == SOUND_STREAM_FOCUS_FOR_PLAYBACK && SOUND_STREAM_FOCUS_STATE_RELEASED == focus_state) {
116                 if (TTSD_MODE_DEFAULT == ttsd_get_mode()) {
117                         g_audio_state = AUDIO_STATE_READY;
118
119                         if (NULL == g_playing_info) {
120                                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] No current player");
121                                 return;
122                         }
123
124                         if (APP_STATE_PLAYING == g_playing_info->state) {
125                                 int uid = g_playing_info->uid;
126
127                                 if (0 != ttsd_player_pause(uid)) {
128                                         SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to pause the player");
129                                         return;
130                                 }
131
132                                 ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
133                                 int pid = ttsd_data_get_pid(uid);
134                                 /* send message to client about changing state */
135                                 ttsdc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
136                         }
137                 } else {
138                         SLOG(LOG_DEBUG, tts_tag(), "[Player] Ignore focus state cb - mode(%d)", ttsd_get_mode());
139                 }
140         }
141
142 /*      if (AUDIO_STATE_READY == g_audio_state && focus_mask == SOUND_STREAM_FOCUS_FOR_PLAYBACK && SOUND_STREAM_FOCUS_STATE_ACQUIRED == focus_state) {
143                 if (TTSD_MODE_DEFAULT == ttsd_get_mode()) {
144                         g_audio_state = AUDIO_STATE_PLAY;
145
146                         if (NULL == g_playing_info) {
147                                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] No current player");
148                                 return;
149                         }
150
151                         if (APP_STATE_PAUSED == g_playing_info->state) {
152                                 int uid = g_playing_info->uid;
153
154                                 g_audio_state = AUDIO_STATE_PLAY;
155                                 if (0 != ttsd_player_resume(uid)) {
156                                         SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to resume the player");
157                                         g_audio_state = AUDIO_STATE_READY;
158                                         return;
159                                 }
160
161                                 ttsd_data_set_client_state(uid, APP_STATE_PLAYING);
162                                 int pid = ttsd_data_get_pid(uid);
163                                 ttsdc_send_set_state_message(pid, uid, APP_STATE_PLAYING);
164                         }
165
166                 } else {
167                         SLOG(LOG_DEBUG, tts_tag(), "[Player] Ignore focus state cb - mode(%d)", ttsd_get_mode());
168                 }
169         }
170 */
171         SLOG(LOG_DEBUG, tts_tag(), "@@@");
172
173         return;
174 }
175
176 void __player_focus_state_watch_cb(int id, sound_stream_focus_mask_e focus_mask, sound_stream_focus_state_e focus_state, sound_stream_focus_change_reason_e reason,
177                                                                 const char *extra_info, void *user_data)
178 {
179         SLOG(LOG_DEBUG, tts_tag(), "@@@ Focus state watch cb");
180
181         ttsd_mode_e mode = ttsd_get_mode();
182
183         if (TTSD_MODE_SCREEN_READER != mode && TTSD_MODE_NOTIFICATION != mode) {
184                 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] This is not screen-reader mode and notification mode.");
185                 return;
186         }
187
188         if (AUDIO_STATE_PLAY == g_audio_state && SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_INFORMATION == reason && 
189                         NULL != extra_info && 0 == strncmp(extra_info, "TTSD_MODE_INTERRUPT", strlen(extra_info))) {
190                 /* If the focus is changed by "Interrupt" mode and current players of "SR" and "Noti" modes are on going, please stop the current players. */
191                 g_audio_state = AUDIO_STATE_READY;
192
193                 if (NULL == g_playing_info) {
194                         SLOG(LOG_WARN, tts_tag(), "[Player WARNING] No current player");
195                         return;
196                 }
197
198                 if (APP_STATE_PLAYING == g_playing_info->state) {
199                         int uid = g_playing_info->uid;
200
201                         if (0 != ttsd_server_stop(uid)) {
202                                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to stop TTS server");
203                                 return;
204                         }
205                         if (0 != ttsd_player_stop(uid)) {
206                                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to stop the player");
207                                 return;
208                         }
209
210                         ttsd_data_set_client_state(uid, APP_STATE_READY);
211                         int pid = ttsd_data_get_pid(uid);
212                         /* send message to client about changing state */
213                         ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
214                 } else {
215                         SLOG(LOG_DEBUG, tts_tag(), "[Player] Not playing state");
216                 }
217         } else {
218                 SLOG(LOG_DEBUG, tts_tag(), "[Player] This is not Interrupt mode or not playing state.");
219         }
220
221         return;
222 }
223
224 static int __create_audio_out(ttse_audio_type_e type, int rate)
225 {
226         int ret = -1;
227         audio_sample_type_e sample_type;
228
229         if (TTSE_AUDIO_TYPE_RAW_S16 == type) {
230                 sample_type = AUDIO_SAMPLE_TYPE_S16_LE;
231         } else {
232                 sample_type = AUDIO_SAMPLE_TYPE_U8;
233         }
234
235         ret = audio_out_create_new(rate, AUDIO_CHANNEL_MONO, sample_type, &g_audio_h);
236         if (AUDIO_IO_ERROR_NONE != ret) {
237                 g_audio_state = AUDIO_STATE_NONE;
238                 g_audio_h = NULL;
239                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to create audio");
240                 return -1;
241         } else {
242                 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Create audio");
243         }
244
245         g_audio_type = type;
246         g_sampling_rate = rate;
247
248         g_audio_state = AUDIO_STATE_READY;
249
250         return 0;
251 }
252
253 static int __destroy_audio_out()
254 {
255         if (NULL == g_audio_h) {
256                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Current handle is not valid");
257                 return -1;
258         }
259
260         int ret = -1;
261         ret = audio_out_destroy(g_audio_h);
262         if (AUDIO_IO_ERROR_NONE != ret) {
263                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to destroy audio");
264                 return -1;
265         } else {
266                 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Destroy audio");
267         }
268
269         g_audio_type = 0;
270         g_sampling_rate = 0;
271
272         g_audio_state = AUDIO_STATE_NONE;
273         g_audio_h = NULL;
274
275         return 0;
276 }
277
278 static void __end_play_thread(void *data, Ecore_Thread *thread)
279 {
280         SLOG(LOG_ERROR, tts_tag(), "@@@ End thread");
281 }
282
283 static void __set_policy_for_playing(int volume)
284 {
285         /* Set stream info */
286         int ret;
287         ttsd_mode_e mode = ttsd_get_mode();
288         if (TTSD_MODE_DEFAULT == mode) {
289                 ret = sound_manager_acquire_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, SOUND_BEHAVIOR_NONE, NULL);
290                 if (SOUND_MANAGER_ERROR_NONE != ret) {
291                         SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to acquire focus");
292                 } else {
293                         SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Success to acquire focus (default mode)");
294                 }
295         } else if (TTSD_MODE_INTERRUPT == mode) {
296                 ret = sound_manager_acquire_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, SOUND_BEHAVIOR_NONE, "TTSD_MODE_INTERRUPT");
297                 if (SOUND_MANAGER_ERROR_NONE != ret) {
298                         SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to acquire focus");
299                 } else {
300                         SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Success to acquire focus (interrupt mode)");
301                 }
302         }
303
304         ret = audio_out_set_sound_stream_info(g_audio_h, g_stream_info_h);
305         if (AUDIO_IO_ERROR_NONE != ret) {
306                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to set stream info");
307         }
308
309         SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] set policy for playing");
310
311         return;
312 }
313
314 static void __unset_policy_for_playing()
315 {
316         int ret;
317         ttsd_mode_e mode = ttsd_get_mode();
318         /* Unset stream info */
319         if (TTSD_MODE_DEFAULT == mode || TTSD_MODE_INTERRUPT == mode) {
320                 sound_stream_focus_state_e state_for_playing = SOUND_STREAM_FOCUS_STATE_ACQUIRED;
321                 ret = sound_manager_get_focus_state(g_stream_info_h, &state_for_playing, NULL);
322                 if (SOUND_MANAGER_ERROR_NONE != ret) {
323                         SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to get focus state: %d", ret);
324                 }
325
326                 if (SOUND_STREAM_FOCUS_STATE_ACQUIRED == state_for_playing) {
327                         if (TTSD_MODE_DEFAULT == mode || TTSD_MODE_INTERRUPT == mode) {
328                                 ret = sound_manager_release_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, SOUND_BEHAVIOR_NONE, NULL);
329                                 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] release focus (mode: %d)", mode);
330                         }
331
332                         if (SOUND_MANAGER_ERROR_NONE != ret) {
333                                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to release focus");
334                         }
335                 }
336         }
337
338         SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] unset policy for playing");
339
340         return;
341 }
342
343 int ttsd_player_check_current_playback_focus(bool *is_current_interrupt)
344 {
345         int ret;
346         ttsd_mode_e mode = ttsd_get_mode();
347
348         if (TTSD_MODE_INTERRUPT != mode) {
349                 /* check the current playback focus */
350                 sound_stream_focus_change_reason_e reason;
351                 int sound_behavior = 0;
352                 char *extra_info = NULL;
353
354                 ret = sound_manager_get_current_playback_focus(&reason, &sound_behavior, &extra_info);
355
356                 SLOG(LOG_DEBUG, tts_tag(), "[Player] current playback focus: extra_info(%s), reason(%d), sound_behavior(%d)", extra_info, reason, sound_behavior);
357
358                 if (SOUND_MANAGER_ERROR_NONE == ret && NULL != extra_info && 0 < strlen(extra_info)) {
359                         if (SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_INFORMATION == reason && 0 == strncmp(extra_info, "TTSD_MODE_INTERRUPT", strlen(extra_info))) {
360                                 SLOG(LOG_DEBUG, tts_tag(), "[Player] The current focus in Interrupt. Cannot play the requested sound data");
361                                 *is_current_interrupt = true;
362
363                                 free(extra_info);
364                                 extra_info = NULL;
365
366                                 return TTSD_ERROR_NONE;
367                         }
368                 }
369
370                 if (NULL != extra_info) {
371                         free(extra_info);
372                         extra_info = NULL;
373                 }
374         }
375
376         *is_current_interrupt = false;
377
378         return TTSD_ERROR_NONE;
379 }
380
381 static void __play_thread(void *data, Ecore_Thread *thread)
382 {
383         SLOG(LOG_DEBUG, tts_tag(), "@@@ Start thread");
384
385         if (NULL == g_playing_info) {
386                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] No current player");
387                 return;
388         }
389
390         player_s* player = g_playing_info;
391         sound_data_s* sound_data = NULL;
392
393         int ret = -1;
394         int len = SOUND_BUFFER_LENGTH;
395         int idx = 0;
396
397         /* set volume policy as 40% */
398         __set_policy_for_playing(40);
399         while (1) {
400                 if (true == player->is_paused_data) {
401                         /* Resume player */
402                         sound_data = player->paused_data;
403                         player->paused_data = NULL;
404
405                         idx = player->idx;
406
407                         player->is_paused_data = false;
408                         player->idx = 0;
409
410                         if (NULL == sound_data) {
411                                 /* Request unprepare */
412                                 ret = audio_out_unprepare(g_audio_h);
413                                 if (AUDIO_IO_ERROR_NONE != ret) {
414                                         SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
415                                 } else {
416                                         SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
417                                 }
418
419                                 g_audio_state = AUDIO_STATE_READY;
420
421                                 /* unset volume policy, volume will be 100% */
422                                 __unset_policy_for_playing();
423                                 return;
424                         }
425                         SLOG(LOG_INFO, tts_tag(), "[Player] Sound info : id(%d) data(%p) size(%d) audiotype(%d) rate(%d) event(%d)", 
426                                 sound_data->utt_id, sound_data->data, sound_data->data_size, sound_data->audio_type, sound_data->rate, sound_data->event);
427                 } else {
428                         sound_data = NULL;
429                         ret = ttsd_data_get_sound_data(player->uid, &sound_data);
430                         if (0 != ret || NULL == sound_data) {
431                                 /* empty queue */
432                                 SLOG(LOG_DEBUG, tts_tag(), "[Player] No sound data. Waiting mode");
433
434                                 /* wait for new audio data come */
435                                 while (1) {
436                                         usleep(10000);
437                                         if (NULL == g_playing_info) {
438                                                 /* current playing uid is replaced */
439                                                 SLOG(LOG_INFO, tts_tag(), "[Player] Finish thread");
440                                                 if (AUDIO_STATE_PLAY == g_audio_state) {
441                                                         /* release audio & recover session */
442                                                         ret = audio_out_unprepare(g_audio_h);
443                                                         if (AUDIO_IO_ERROR_NONE != ret) {
444                                                                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
445                                                         } else {
446                                                                 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
447                                                         }
448                                                         g_audio_state = AUDIO_STATE_READY;
449                                                 }
450                                                 /* unset volume policy, volume will be 100% */
451                                                 __unset_policy_for_playing();
452                                                 return;
453                                         } else if (0 < ttsd_data_get_sound_data_size(player->uid)) {
454                                                 /* new audio data come */
455                                                 SLOG(LOG_INFO, tts_tag(), "[Player] Resume thread");
456                                                 break;
457                                         }
458
459                                         /* If engine is not on processing */
460                                         if (TTSD_SYNTHESIS_CONTROL_DOING != ttsd_get_synth_control()) {
461                                                 if (AUDIO_STATE_PLAY == g_audio_state) {
462                                                         /* release audio & recover session */
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                                                         g_audio_state = AUDIO_STATE_READY;
470
471                                                         /* unset volume policy, volume will be 100% */
472                                                         __unset_policy_for_playing();
473                                                 }
474                                         }
475                                 }
476
477                                 SLOG(LOG_INFO, tts_tag(), "[Player] Finish to wait for new audio data come");
478
479                                 if (AUDIO_STATE_READY == g_audio_state) {
480                                         /* set volume policy as 40%, when resume play thread*/
481                                         __set_policy_for_playing(40);
482                                 }
483
484                                 /* resume play thread */
485                                 player->state = APP_STATE_PLAYING;
486                                 continue;
487                         }
488
489                         /* If wdata's event is 'start', current wdata is first data of engine for synthesis.
490                          * If wdata's event is 'finish', player should check previous event to know whether this wdata is first or not.
491                          * When previous wdata's event is 'finish' and current wdata's event is 'finish',
492                          * the player should send utt started event.
493                          */
494                         if (TTSE_RESULT_EVENT_START == sound_data->event ||
495                            (TTSE_RESULT_EVENT_FINISH == player->event && TTSE_RESULT_EVENT_FINISH == sound_data->event)) {
496                                 int pid = ttsd_data_get_pid(player->uid);
497
498                                 if (pid <= 0) {
499                                         SLOG(LOG_WARN, tts_tag(), "[Send WARNIING] Current player is not valid");
500                                         /* unset volume policy, volume will be 100% */
501                                         __unset_policy_for_playing();
502
503                                         return;
504                                 }
505
506                                 if (0 != ttsdc_send_utt_start_message(pid, player->uid, sound_data->utt_id)) {
507                                         SLOG(LOG_ERROR, tts_tag(), "[Send ERROR] Fail to send Utterance Start Signal : pid(%d), uid(%d), uttid(%d)", 
508                                                 pid, player->uid, sound_data->utt_id);
509                                 }
510                                 SLOG(LOG_INFO, tts_tag(), "[Player] Start utterance : uid(%d), uttid(%d)", player->uid, sound_data->utt_id);
511                         }
512
513                         /* Save last event to check utterance start */
514                         player->event = sound_data->event;
515                         idx = 0;
516
517                         if (NULL == sound_data->data || 0 >= sound_data->data_size) {
518                                 if (TTSE_RESULT_EVENT_FINISH == sound_data->event) {
519                                         SLOG(LOG_DEBUG, tts_tag(), "No sound data");
520                                         /* send utterence finish signal */
521                                         int pid = ttsd_data_get_pid(player->uid);
522
523                                         if (pid <= 0) {
524                                                 SLOG(LOG_WARN, tts_tag(), "[Send WARNIING] Current player is not valid");
525                                                 /* unset volume policy, volume will be 100% */
526                                                 __unset_policy_for_playing();
527                                                 return;
528                                         }
529
530                                         __unset_policy_for_playing();
531
532                                         if (0 != ttsdc_send_utt_finish_message(pid, player->uid, sound_data->utt_id)) {
533                                                 SLOG(LOG_ERROR, tts_tag(), "[Send ERROR] Fail to send Utterance Completed Signal : pid(%d), uid(%d), uttid(%d)", 
534                                                         pid, player->uid, sound_data->utt_id);
535                                         } else {
536                                                 SLOG(LOG_INFO, tts_tag(), "[Player] Finish utterance : uid(%d), uttid(%d)", player->uid, sound_data->utt_id);
537                                         }
538                                 }
539                                 SLOG(LOG_INFO, tts_tag(), "[Player] Event(%d) utterance : uid(%d), uttid(%d)", sound_data->event, player->uid, sound_data->utt_id);
540                                 ttsd_data_clear_sound_data(player->uid, &sound_data);
541                                 continue;
542                         }
543                 }
544
545                 if (g_sampling_rate != sound_data->rate || g_audio_type != sound_data->audio_type) {
546                         SLOG(LOG_INFO, tts_tag(), "[Player] Change audio handle : org type(%d) org rate(%d)", g_audio_type, g_sampling_rate);
547                         if (NULL != g_audio_h) {
548                                 __destroy_audio_out();
549                         }
550
551                         if (0 > __create_audio_out(sound_data->audio_type, sound_data->rate)) {
552                                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to create audio out");
553                                 /* unset volume policy, volume will be 100% */
554                                 __unset_policy_for_playing();
555
556                                 ttsd_data_clear_sound_data(player->uid, &sound_data);
557
558                                 return;
559                         }
560
561                         SLOG(LOG_INFO, tts_tag(), "[Player INFO] Success to destroy and recreate audio out");
562                         __set_policy_for_playing(40);
563                 }
564
565                 while (APP_STATE_PLAYING == player->state || APP_STATE_PAUSED == player->state) {
566                         if ((unsigned int)idx >= sound_data->data_size)
567                                 break;
568
569                         if ((unsigned int)idx + SOUND_BUFFER_LENGTH > sound_data->data_size) {
570                                 len = sound_data->data_size - idx;
571                         } else {
572                                 len = SOUND_BUFFER_LENGTH;
573                         }
574
575                         if (AUDIO_STATE_READY == g_audio_state) {
576                                 /* Request prepare */
577                                 ret = audio_out_prepare(g_audio_h);
578                                 if (AUDIO_IO_ERROR_NONE != ret) {
579                                         SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to prepare audio : %d", ret);
580                                         g_playing_info = NULL;
581                                         /* unset volume policy, volume will be 100% */
582                                         __unset_policy_for_playing();
583
584                                         ttsd_data_clear_sound_data(player->uid, &sound_data);
585
586                                         return;
587                                 }
588                                 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Prepare audio");
589                                 g_audio_state = AUDIO_STATE_PLAY;
590                         }
591
592                         char* temp_data = sound_data->data;
593                         SLOG(LOG_INFO, tts_tag(), "[Player INFO] Before audio_out_write. data(%p), data[%d](%p), uid(%d), utt_id(%d), len(%d)",
594                                         temp_data, idx, &temp_data[idx], player->uid, sound_data->utt_id, len);
595                         ret = audio_out_write(g_audio_h, &temp_data[idx], len);
596                         if (0 > ret) {
597                                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to audio write - %d", ret);
598                         } else {
599                                 idx += len;
600                                 SLOG(LOG_INFO, tts_tag(), "[Player INFO] After audio_out_write");
601                         }
602
603                         if (NULL == g_playing_info && APP_STATE_PAUSED != player->state) {
604                                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Current player is NULL");
605                                 g_audio_state = AUDIO_STATE_READY;
606                                 ret = audio_out_unprepare(g_audio_h);
607                                 if (AUDIO_IO_ERROR_NONE != ret) {
608                                         SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
609                                 }
610                                 /* unset volume policy, volume will be 100% */
611                                 __unset_policy_for_playing();
612
613                                 ttsd_data_clear_sound_data(player->uid, &sound_data);
614
615                                 return;
616                         }
617
618                         if (APP_STATE_PAUSED == player->state) {
619                                 /* Save data */
620                                 SLOG(LOG_DEBUG, tts_tag(), "[Player] player(%p)", player);
621                                 player->paused_data = sound_data;
622
623                                 player->is_paused_data = true;
624                                 player->idx = idx;
625
626                                 g_audio_state = AUDIO_STATE_READY;
627
628                                 SLOG(LOG_INFO, tts_tag(), "[Player] Stop player thread by pause");
629
630                                 /* Request prepare */
631                                 ret = audio_out_unprepare(g_audio_h);
632                                 if (AUDIO_IO_ERROR_NONE != ret) {
633                                         SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
634                                 } else {
635                                         SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
636                                 }
637                                 /* unset volume policy, volume will be 100% */
638                                 __unset_policy_for_playing();
639                                 return;
640                         }
641                 }
642
643                 if (NULL == g_playing_info && APP_STATE_READY == player->state) {
644                         /* player_stop */ 
645                         g_audio_state = AUDIO_STATE_READY;
646                         SLOG(LOG_DEBUG, tts_tag(), "[Player] Stop player thread");
647
648                         /* Request prepare */
649                         ret = audio_out_unprepare(g_audio_h);
650                         if (AUDIO_IO_ERROR_NONE != ret) {
651                                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
652                         } else {
653                                 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
654                         }
655
656                         ttsd_data_clear_sound_data(player->uid, &sound_data);
657                         /* unset volume policy, volume will be 100% */
658                         __unset_policy_for_playing();
659                         return;
660                 }
661
662                 if ((APP_STATE_PLAYING == player->state || APP_STATE_PAUSED == player->state) &&
663                         (TTSE_RESULT_EVENT_FINISH == sound_data->event)) {
664                         /* send utterence finish signal */
665                         int pid = ttsd_data_get_pid(player->uid);
666
667                         if (pid <= 0) {
668                                 SLOG(LOG_WARN, tts_tag(), "[Send WARNIING] Current player is not valid");
669                                 /* unset volume policy, volume will be 100% */
670                                 __unset_policy_for_playing();
671
672                                 return;
673                         }
674
675                         if (0 != ttsdc_send_utt_finish_message(pid, player->uid, sound_data->utt_id)) {
676                                 SLOG(LOG_ERROR, tts_tag(), "[Send ERROR] Fail to send Utterance Completed Signal : pid(%d), uid(%d), uttid(%d)", 
677                                         pid, player->uid, sound_data->utt_id);
678                                 /* unset volume policy, volume will be 100% */
679                                 __unset_policy_for_playing();
680
681                                 ttsd_data_clear_sound_data(player->uid, &sound_data);
682                                 return;
683                         }
684
685                         SLOG(LOG_INFO, tts_tag(), "[Player] Finish utterance : uid(%d), uttid(%d)", player->uid, sound_data->utt_id);
686                 }
687
688                 ttsd_data_clear_sound_data(player->uid, &sound_data);
689
690                 if (NULL == g_playing_info) {
691                         SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Current player is NULL");
692                         g_audio_state = AUDIO_STATE_READY;
693                         ret = audio_out_unprepare(g_audio_h);
694                         if (AUDIO_IO_ERROR_NONE != ret) {
695                                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
696                         }
697                         /* unset volume policy, volume will be 100% */
698                         __unset_policy_for_playing();
699
700                         return;
701                 }
702         }
703 }
704
705 /*
706 * Player Interfaces
707 */
708 int ttsd_player_init()
709 {
710         g_playing_info = NULL;
711         g_audio_state = AUDIO_STATE_NONE;
712         g_audio_h = NULL;
713
714         int ret;
715
716         ecore_thread_max_set(1);
717
718         int cnt = 0;
719         while (1) {
720                 if (0 == access(FOCUS_SERVER_READY, F_OK)) {
721                         SLOG(LOG_ERROR, tts_tag(), "[Player SUCCESS] focus server is available");
722                         break;
723                 } else {
724                         if (0 == cnt++ % 10)
725                                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] focus server is not available");
726                         usleep(50000);
727                 }
728         }
729
730         ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_VOICE_INFORMATION, __player_focus_state_cb, NULL, &g_stream_info_h);
731         if (SOUND_MANAGER_ERROR_NONE != ret) {
732                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to create stream info");
733                 return -1;
734         } else {
735                 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Create stream info");
736         }
737
738         /* add sound focus state watch callback */
739         ret = sound_manager_add_focus_state_watch_cb(SOUND_STREAM_FOCUS_FOR_PLAYBACK, __player_focus_state_watch_cb, NULL, &g_focus_watch_id);
740         if (SOUND_MANAGER_ERROR_NONE != ret) {
741                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to add sound focus watch callback");
742                 sound_manager_destroy_stream_information(g_stream_info_h);
743                 return -1;
744         } else {
745                 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Add sound focus watch callback");
746         }
747
748         ecore_thread_max_set(1);
749
750         ret = __create_audio_out(TTSE_AUDIO_TYPE_RAW_S16, 16000);
751         if (0 != ret) {
752                 sound_manager_destroy_stream_information(g_stream_info_h);
753                 sound_manager_remove_focus_state_watch_cb(g_focus_watch_id);
754                 return -1;
755         }
756
757         g_player_init = true;
758
759         return 0;
760 }
761
762 int ttsd_player_release(void)
763 {
764         if (false == g_player_init) {
765                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
766                 return TTSD_ERROR_OPERATION_FAILED;
767         }
768
769         int ret;
770
771         SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] @@@@@");
772         SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Active thread count : %d", ecore_thread_active_get());
773         SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] @@@@@");
774
775         /* The thread should be released */
776         int thread_count = ecore_thread_active_get();
777         int count = 0;
778         while (0 < thread_count) {
779                 usleep(10000);
780
781                 count++;
782                 if (20 == count) {
783                         SLOG(LOG_WARN, tts_tag(), "[Player WARNING!!] Thread is blocked. Player release continue.");
784                         break;
785                 }
786
787                 thread_count = ecore_thread_active_get();
788         }
789
790         SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Thread is released");
791
792         ret = __destroy_audio_out();
793         if (0 != ret)
794                 return -1;
795
796         ret = sound_manager_destroy_stream_information(g_stream_info_h);
797         if (SOUND_MANAGER_ERROR_NONE != ret) {
798                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to destroy stream info");
799         } else {
800                 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Destroy stream info");
801         }
802
803         /* remove focus state watch callback */
804         ret = sound_manager_remove_focus_state_watch_cb(g_focus_watch_id);
805         if (SOUND_MANAGER_ERROR_NONE != ret) {
806                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to remove the focus state watch cb");
807         } else {
808                 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Remove the focus state watch cb");
809         }
810
811         /* clear g_player_list */
812         g_playing_info = NULL;
813         g_player_init = false;
814
815         return 0;
816 }
817
818 int ttsd_player_create_instance(int uid)
819 {
820         if (false == g_player_init) {
821                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
822                 return -1;
823         }
824
825         /* Check uid is duplicated */
826         if (NULL != __player_get_item(uid)) {
827                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is already registered", uid);
828                 return -1;
829         }
830
831         player_s* new_client = (player_s*)calloc(1, sizeof(player_s));
832         if (NULL == new_client) {
833                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to allocate memory");
834                 return TTSE_ERROR_OUT_OF_MEMORY;
835         }
836
837         new_client->uid = uid;
838         new_client->event = TTSE_RESULT_EVENT_FINISH;
839         new_client->state = APP_STATE_READY;
840         new_client->is_paused_data = false;
841         new_client->idx = 0;
842         new_client->paused_data = NULL;
843         
844         SECURE_SLOG(LOG_DEBUG, tts_tag(), "[Player] Create player : uid(%d)", uid);
845
846         g_player_list = g_list_append(g_player_list, new_client);
847
848         return 0;
849 }
850
851 int ttsd_player_destroy_instance(int uid)
852 {
853         if (false == g_player_init) {
854                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
855                 return -1;
856         }
857
858         player_s* current;
859         current = __player_get_item(uid);
860         if (NULL == current) {
861                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
862                 return -1;
863         }
864
865         if (NULL != g_playing_info) {
866                 if (uid == g_playing_info->uid) {
867                         g_playing_info = NULL;
868                 }
869         }
870
871         GList *iter = NULL;
872         player_s *data = NULL;
873
874         if (0 < g_list_length(g_player_list)) {
875                 /* Get a first item */
876                 iter = g_list_first(g_player_list);
877
878                 while (NULL != iter) {
879                         /* Get handle data from list */
880                         data = (player_s*)iter->data;
881
882                         if (NULL != data) {
883                                 /* compare uid */
884                                 if (uid == data->uid) {
885                                         g_player_list = g_list_remove_link(g_player_list, iter);
886                                         free(data);
887                                         g_list_free(iter);              
888                                         break;
889                                 }
890                         }
891
892                         /* Get next item */
893                         iter = g_list_next(iter);
894                 }
895         }
896
897         SLOG(LOG_DEBUG, tts_tag(), "[PLAYER Success] Destroy instance");
898
899         return 0;
900 }
901
902 int ttsd_player_play(int uid)
903 {
904         if (false == g_player_init) {
905                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
906                 return -1;
907         }
908
909         if (NULL != g_playing_info) {
910                 if (uid == g_playing_info->uid) {
911                         SLOG(LOG_DEBUG, tts_tag(), "[Player] uid(%d) has already played", g_playing_info->uid);
912                         return 0;
913                 } else {
914                         SLOG(LOG_WARN, tts_tag(), "[Player WARNING] stop old player (%d)", g_playing_info->uid);
915                         ttsd_player_stop(g_playing_info->uid);
916                 }
917         }
918
919         SLOG(LOG_DEBUG, tts_tag(), "[Player] start play : uid(%d)", uid);
920
921         /* Check sound queue size */
922         if (0 == ttsd_data_get_sound_data_size(uid)) {
923                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] A sound queue of current player(%d) is empty", uid);
924                 return -1;
925         }
926
927         /* Check uid */
928         player_s* current;
929         current = __player_get_item(uid);
930         if (NULL == current) {
931                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
932                 return -1;
933         }
934
935         current->state = APP_STATE_PLAYING;
936
937         g_playing_info = current;
938
939         SLOG(LOG_INFO, tts_tag(), "[Player DEBUG] Active thread count : %d", ecore_thread_active_get());
940
941         if (0 < ttsd_data_get_sound_data_size(current->uid)) {
942                 SLOG(LOG_INFO, tts_tag(), "[Player] Run thread");
943                 ecore_thread_run(__play_thread, __end_play_thread, NULL, NULL);
944         }
945
946         return 0;
947 }
948
949 int ttsd_player_stop(int uid)
950 {
951         if (false == g_player_init) {
952                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
953                 return -1;
954         }
955
956         /* Check uid */
957         player_s* current;
958         current = __player_get_item(uid);
959         if (NULL == current) {
960                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
961                 return -1;
962         }
963
964         /* check whether uid is current playing or not */
965         if (NULL != g_playing_info) {
966                 if (uid == g_playing_info->uid) {
967                         /* release current playing info */
968                         g_playing_info = NULL;
969                 }
970         } else {
971                 SLOG(LOG_DEBUG, tts_tag(), "[Player] No current playing");
972         }
973
974         if (true == current->is_paused_data) {
975                 if (NULL != current->paused_data) {
976                         if (NULL != current->paused_data->data) {
977                                 free(current->paused_data->data);
978                                 current->paused_data->data = NULL;
979                         }
980
981                         free(current->paused_data);
982                         current->paused_data = NULL;
983                 }
984         }
985
986         current->event = TTSE_RESULT_EVENT_FINISH;
987         current->state = APP_STATE_READY;
988         current->is_paused_data = false;
989         current->idx = 0;
990
991         if (NULL == g_playing_info) {
992                 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
993                 SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
994                 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
995
996                 /* The thread should be released */
997                 int thread_count = ecore_thread_active_get();
998                 int count = 0;
999                 while (0 < thread_count) {
1000                         usleep(10000);
1001
1002                         count++;
1003                         if (30 == count) {
1004                                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING!!] Thread is blocked. Player release continue.");
1005                                 break;
1006                         }
1007
1008                         thread_count = ecore_thread_active_get();
1009                 }
1010
1011                 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1012                 SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
1013                 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1014         }
1015
1016         SLOG(LOG_INFO, tts_tag(), "[Player SUCCESS] Stop player : uid(%d)", uid);
1017
1018         return 0;
1019 }
1020
1021 int ttsd_player_clear(int uid)
1022 {
1023         if (false == g_player_init) {
1024                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1025                 return -1;
1026         }
1027
1028         /* Check uid */
1029         player_s* current;
1030         current = __player_get_item(uid);
1031         if (NULL == current) {
1032                 SECURE_SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid); 
1033                 return -1;
1034         }
1035
1036         if (true == current->is_paused_data) {
1037                 if (NULL != current->paused_data) {
1038                         if (NULL != current->paused_data->data) {
1039                                 free(current->paused_data->data);
1040                                 current->paused_data->data = NULL;
1041                         }
1042
1043                         free(current->paused_data);
1044                         current->paused_data = NULL;
1045                 }
1046         }
1047
1048         current->event = TTSE_RESULT_EVENT_FINISH;
1049         current->state = APP_STATE_READY;
1050         current->is_paused_data = false;
1051         current->idx = 0;
1052
1053         SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Clear player : uid(%d)", uid);
1054
1055         return 0;
1056 }
1057
1058 int ttsd_player_pause(int uid)
1059 {
1060         SLOG(LOG_DEBUG, tts_tag(), "[Player] pause player : uid(%d)", uid);
1061
1062         if (false == g_player_init) {
1063                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1064                 return -1;
1065         }
1066
1067         /* Check uid */
1068         player_s* current;
1069         current = __player_get_item(uid);
1070         if (NULL == current) {
1071                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] ttsd_player_pause() : uid(%d) is not valid", uid);
1072                 return -1;
1073         }
1074
1075         /* check whether uid is current playing or not */
1076         if (NULL != g_playing_info) {
1077                 if (uid == g_playing_info->uid) {
1078                         /* release current playing info */
1079                         SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] release current playing info (%d)", uid);
1080                         g_playing_info = NULL;
1081                 } else {
1082                         /* error case */
1083                 }
1084         }
1085
1086         SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] current player (%p), g_playing_info(%p)", current, g_playing_info);
1087
1088         current->state = APP_STATE_PAUSED;
1089
1090         if (NULL == g_playing_info) {
1091                 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1092                 SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
1093                 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1094
1095                 /* The thread should be released */
1096                 int thread_count = ecore_thread_active_get();
1097                 int count = 0;
1098                 while (0 < thread_count) {
1099                         usleep(10000);
1100
1101                         count++;
1102                         SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] current(%p), state(%d)", current, current->state);
1103
1104                         if (30 == count) {
1105                                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING!!] Thread is blocked. Player release continue. current(%p) current state(%d)", current, current->state);
1106                                 break;
1107                         }
1108
1109                         thread_count = ecore_thread_active_get();
1110                 }
1111
1112                 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1113                 SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
1114                 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1115         }
1116
1117
1118         SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Pause player : uid(%d)", uid);
1119
1120         return 0;
1121 }
1122
1123 int ttsd_player_resume(int uid)
1124 {
1125         SLOG(LOG_DEBUG, tts_tag(), "[Player] Resume player : uid(%d)", uid);
1126
1127         if (false == g_player_init) {
1128                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1129                 return -1;
1130         }
1131
1132         /* Check id */
1133         player_s* current;
1134         current = __player_get_item(uid);
1135         if (NULL == current) {
1136                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
1137                 return -1;
1138         }
1139
1140         /* check current player */
1141         if (NULL != g_playing_info)
1142                 g_playing_info = NULL;
1143
1144         current->state = APP_STATE_PLAYING;
1145         g_playing_info = current;
1146
1147         SLOG(LOG_INFO, tts_tag(), "[Player] Resume to run thread");
1148         ecore_thread_run(__play_thread, __end_play_thread, NULL, NULL);
1149
1150         return 0;
1151 }
1152
1153 int ttsd_player_all_stop()
1154 {
1155         if (false == g_player_init) {
1156                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1157                 return -1;
1158         }
1159
1160         g_playing_info = NULL;
1161
1162         GList *iter = NULL;
1163         player_s *data = NULL;
1164
1165         if (0 < g_list_length(g_player_list)) {
1166                 /* Get a first item */
1167                 iter = g_list_first(g_player_list);
1168
1169                 while (NULL != iter) {
1170                         /* Get handle data from list */
1171                         data = (player_s*)iter->data;
1172
1173                         app_tts_state_e state;
1174                         if (0 > ttsd_data_get_client_state(data->uid, &state)) {
1175                                 SLOG(LOG_ERROR, tts_tag(), "[player ERROR] uid(%d) is not valid", data->uid);
1176                                 ttsd_player_destroy_instance(data->uid);
1177                                 iter = g_list_next(iter);
1178                                 continue;
1179                         }
1180
1181                         if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1182                                 data->event = TTSE_RESULT_EVENT_FINISH;
1183                                 data->state = APP_STATE_READY;
1184
1185                                 if (true == data->is_paused_data) {
1186                                         if (NULL != data->paused_data) {
1187                                                 if (NULL != data->paused_data->data) {
1188                                                         free(data->paused_data->data);
1189                                                         data->paused_data->data = NULL;
1190                                                 }
1191
1192                                                 free(data->paused_data);
1193                                                 data->paused_data = NULL;
1194                                         }
1195                                 }
1196
1197                                 data->is_paused_data = false;
1198                                 data->idx = 0;
1199                         }
1200
1201                         /* Get next item */
1202                         iter = g_list_next(iter);
1203                 }
1204         }
1205
1206         SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] player all stop!!");
1207
1208         return 0;
1209 }
1210
1211 int ttsd_player_play_pcm(int uid)
1212 {
1213         if (false == g_player_init) {
1214                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1215                 return -1;
1216         }
1217
1218         if (NULL != g_playing_info) {
1219                 if (uid == g_playing_info->uid) {
1220                         SLOG(LOG_DEBUG, tts_tag(), "[Player] uid(%d) has already played", g_playing_info->uid);
1221                         return 0;
1222                 } else {
1223                         SLOG(LOG_WARN, tts_tag(), "[Player WARNING] stop old player (%d)", g_playing_info->uid);
1224                         ttsd_player_stop(g_playing_info->uid);
1225                 }
1226         }
1227
1228         SLOG(LOG_DEBUG, tts_tag(), "[Player] start play : uid(%d)", uid);
1229
1230         /* Check sound queue size */
1231         if (0 == ttsd_data_get_sound_data_size(uid)) {
1232                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] A sound queue of current player(%d) is empty", uid);
1233         }
1234
1235         /* Check uid */
1236         player_s* current;
1237         current = __player_get_item(uid);
1238         if (NULL == current) {
1239                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
1240                 return -1;
1241         }
1242
1243         current->state = APP_STATE_PLAYING;
1244
1245         g_playing_info = current;
1246
1247         SLOG(LOG_INFO, tts_tag(), "[Player DEBUG] Active thread count : %d", ecore_thread_active_get());
1248
1249         if (0 <= ttsd_data_get_sound_data_size(current->uid)) {
1250                 SLOG(LOG_INFO, tts_tag(), "[Player] Run thread");
1251                 ecore_thread_run(__play_thread, __end_play_thread, NULL, NULL);
1252         }
1253
1254         return 0;
1255 }
1256