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