Merge "Try to prepare when dbus call is requested earlier while a tts engine is reset...
[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                 if (SOUND_MANAGER_ERROR_NONE == ret && NULL != extra_info) {
355                         if (SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_INFORMATION == reason && 0 == strncmp(extra_info, "TTSD_MODE_INTERRUPT", strlen(extra_info))) {
356                                 SLOG(LOG_DEBUG, tts_tag(), "[Player] The current focus in Interrupt. Cannot play the requested sound data");
357                                 *is_current_interrupt = true;
358
359                                 free(extra_info);
360                                 extra_info = NULL;
361
362                                 return TTSD_ERROR_NONE;
363                         }
364                 }
365
366                 if (NULL != extra_info) {
367                         free(extra_info);
368                         extra_info = NULL;
369                 }
370         }
371
372         *is_current_interrupt = false;
373
374         return TTSD_ERROR_NONE;
375 }
376
377 static void __play_thread(void *data, Ecore_Thread *thread)
378 {
379         SLOG(LOG_DEBUG, tts_tag(), "@@@ Start thread");
380
381         if (NULL == g_playing_info) {
382                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] No current player");
383                 return;
384         }
385
386         player_s* player = g_playing_info;
387         sound_data_s* sound_data = NULL;
388
389         int ret = -1;
390         int len = SOUND_BUFFER_LENGTH;
391         int idx = 0;
392
393         /* set volume policy as 40% */
394         __set_policy_for_playing(40);
395         while (1) {
396                 if (true == player->is_paused_data) {
397                         /* Resume player */
398                         sound_data = player->paused_data;
399                         player->paused_data = NULL;
400
401                         idx = player->idx;
402
403                         player->is_paused_data = false;
404                         player->idx = 0;
405
406                         if (NULL == sound_data) {
407                                 /* Request unprepare */
408                                 ret = audio_out_unprepare(g_audio_h);
409                                 if (AUDIO_IO_ERROR_NONE != ret) {
410                                         SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
411                                 } else {
412                                         SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
413                                 }
414
415                                 g_audio_state = AUDIO_STATE_READY;
416
417                                 /* unset volume policy, volume will be 100% */
418                                 __unset_policy_for_playing();
419                                 return;
420                         }
421                         SLOG(LOG_INFO, tts_tag(), "[Player] Sound info : id(%d) data(%p) size(%d) audiotype(%d) rate(%d) event(%d)", 
422                                 sound_data->utt_id, sound_data->data, sound_data->data_size, sound_data->audio_type, sound_data->rate, sound_data->event);
423                 } else {
424                         sound_data = NULL;
425                         ret = ttsd_data_get_sound_data(player->uid, &sound_data);
426                         if (0 != ret || NULL == sound_data) {
427                                 /* empty queue */
428                                 SLOG(LOG_DEBUG, tts_tag(), "[Player] No sound data. Waiting mode");
429                                 /* release audio & recover session */
430                                 ret = audio_out_unprepare(g_audio_h);
431                                 if (AUDIO_IO_ERROR_NONE != ret) {
432                                         SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
433                                 } else {
434                                         SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
435                                 }
436                                 g_audio_state = AUDIO_STATE_READY;
437
438                                 /* unset volume policy, volume will be 100% */
439                                 __unset_policy_for_playing();
440
441                                 /* wait for new audio data come */
442                                 while (1) {
443                                         usleep(10000);
444                                         if (NULL == g_playing_info) {
445                                                 /* current playing uid is replaced */
446                                                 SLOG(LOG_INFO, tts_tag(), "[Player] Finish thread");
447                                                 return;
448                                         } else if (0 < ttsd_data_get_sound_data_size(player->uid)) {
449                                                 /* new audio data come */
450                                                 SLOG(LOG_INFO, tts_tag(), "[Player] Resume thread");
451                                                 break;
452                                         }
453                                 }
454
455                                 SLOG(LOG_INFO, tts_tag(), "[Player] Finish to wait for new audio data come");
456
457                                 /* set volume policy as 40%, when resume play thread*/
458                                 __set_policy_for_playing(40);
459
460                                 /* resume play thread */
461                                 player->state = APP_STATE_PLAYING;
462                                 continue;
463                         }
464
465                         /* If wdata's event is 'start', current wdata is first data of engine for synthesis.
466                          * If wdata's event is 'finish', player should check previous event to know whether this wdata is first or not.
467                          * When previous wdata's event is 'finish' and current wdata's event is 'finish',
468                          * the player should send utt started event.
469                          */
470                         if (TTSE_RESULT_EVENT_START == sound_data->event ||
471                            (TTSE_RESULT_EVENT_FINISH == player->event && TTSE_RESULT_EVENT_FINISH == sound_data->event)) {
472                                 int pid = ttsd_data_get_pid(player->uid);
473
474                                 if (pid <= 0) {
475                                         SLOG(LOG_WARN, tts_tag(), "[Send WARNIING] Current player is not valid");
476                                         /* unset volume policy, volume will be 100% */
477                                         __unset_policy_for_playing();
478
479                                         if (NULL != sound_data->data) {
480                                                 free(sound_data->data);
481                                                 sound_data->data = NULL;
482                                         }
483                                         free(sound_data);
484                                         sound_data = NULL;
485                                         return;
486                                 }
487
488                                 if (0 != ttsdc_send_utt_start_message(pid, player->uid, sound_data->utt_id)) {
489                                         SLOG(LOG_ERROR, tts_tag(), "[Send ERROR] Fail to send Utterance Start Signal : pid(%d), uid(%d), uttid(%d)", 
490                                                 pid, player->uid, sound_data->utt_id);
491                                 }
492                                 SLOG(LOG_INFO, tts_tag(), "[Player] Start utterance : uid(%d), uttid(%d)", player->uid, sound_data->utt_id);
493                         }
494
495                         /* Save last event to check utterance start */
496                         player->event = sound_data->event;
497                         idx = 0;
498
499                         if (NULL == sound_data->data || 0 >= sound_data->data_size) {
500                                 if (TTSE_RESULT_EVENT_FINISH == sound_data->event) {
501                                         SLOG(LOG_DEBUG, tts_tag(), "No sound data");
502                                         /* send utterence finish signal */
503                                         int pid = ttsd_data_get_pid(player->uid);
504
505                                         if (pid <= 0) {
506                                                 SLOG(LOG_WARN, tts_tag(), "[Send WARNIING] Current player is not valid");
507                                                 /* unset volume policy, volume will be 100% */
508                                                 __unset_policy_for_playing();
509                                                 if (NULL != sound_data->data) {
510                                                         free(sound_data->data);
511                                                         sound_data->data = NULL;
512                                                 }
513                                                 free(sound_data);
514                                                 sound_data = NULL;
515                                                 return;
516                                         }
517                                         if (0 != ttsdc_send_utt_finish_message(pid, player->uid, sound_data->utt_id)) {
518                                                 SLOG(LOG_ERROR, tts_tag(), "[Send ERROR] Fail to send Utterance Completed Signal : pid(%d), uid(%d), uttid(%d)", 
519                                                         pid, player->uid, sound_data->utt_id);
520                                         }
521                                 }
522                                 SLOG(LOG_INFO, tts_tag(), "[Player] Finish utterance : uid(%d), uttid(%d)", player->uid, sound_data->utt_id);
523                                 if (NULL != sound_data->data) {
524                                         free(sound_data->data);
525                                         sound_data->data = NULL;
526                                 }
527                                 free(sound_data);
528                                 sound_data = NULL;
529                                 continue;
530                         }
531                 }
532
533                 if (g_sampling_rate != sound_data->rate || g_audio_type != sound_data->audio_type) {
534                         SLOG(LOG_DEBUG, tts_tag(), "[Player] Change audio handle : org type(%d) org rate(%d)", g_audio_type, g_sampling_rate);
535                         if (NULL != g_audio_h) {
536                                 __destroy_audio_out();
537                         }
538
539                         if (0 > __create_audio_out(sound_data->audio_type, sound_data->rate)) {
540                                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to create audio out");
541                                 /* unset volume policy, volume will be 100% */
542                                 __unset_policy_for_playing();
543
544                                 if (NULL != sound_data->data) {
545                                         free(sound_data->data);
546                                         sound_data->data = NULL;
547                                 }
548                                 free(sound_data);
549                                 sound_data = NULL;
550
551                                 return;
552                         }
553
554                         __set_policy_for_playing(40);
555                 }
556
557                 while (APP_STATE_PLAYING == player->state || APP_STATE_PAUSED == player->state) {
558                         if ((unsigned int)idx >= sound_data->data_size)
559                                 break;
560
561                         if ((unsigned int)idx + SOUND_BUFFER_LENGTH > sound_data->data_size) {
562                                 len = sound_data->data_size - idx;
563                         } else {
564                                 len = SOUND_BUFFER_LENGTH;
565                         }
566
567                         if (AUDIO_STATE_READY == g_audio_state) {
568                                 /* Request prepare */
569                                 ret = audio_out_prepare(g_audio_h);
570                                 if (AUDIO_IO_ERROR_NONE != ret) {
571                                         SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to prepare audio : %d", ret);
572                                         g_playing_info = NULL;
573                                         /* unset volume policy, volume will be 100% */
574                                         __unset_policy_for_playing();
575
576                                         if (NULL != sound_data->data) {
577                                                 free(sound_data->data);
578                                                 sound_data->data = NULL;
579                                         }
580
581                                         free(sound_data);
582                                         sound_data = NULL;
583
584                                         return;
585                                 }
586                                 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Prepare audio");
587                                 g_audio_state = AUDIO_STATE_PLAY;
588                         }
589
590                         char* temp_data = sound_data->data;
591                         ret = audio_out_write(g_audio_h, &temp_data[idx], len);
592                         if (0 > ret) {
593                                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to audio write - %d", ret);
594                         } else {
595                                 idx += len;
596                         }
597
598                         if (NULL == g_playing_info && APP_STATE_PAUSED != player->state) {
599                                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Current player is NULL");
600                                 g_audio_state = AUDIO_STATE_READY;
601                                 ret = audio_out_unprepare(g_audio_h);
602                                 if (AUDIO_IO_ERROR_NONE != ret) {
603                                         SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
604                                 }
605                                 /* unset volume policy, volume will be 100% */
606                                 __unset_policy_for_playing();
607
608                                 if (NULL != sound_data) {
609                                         if (NULL != sound_data->data) {
610                                                 free(sound_data->data);
611                                                 sound_data->data = NULL;
612                                         }
613
614                                         free(sound_data);
615                                         sound_data = NULL;
616                                 }
617
618                                 return;
619                         }
620
621                         if (APP_STATE_PAUSED == player->state) {
622                                 /* Save data */
623                                 player->paused_data = sound_data;
624
625                                 player->is_paused_data = true;
626                                 player->idx = idx;
627
628                                 g_audio_state = AUDIO_STATE_READY;
629                                 SLOG(LOG_INFO, tts_tag(), "[Player] Stop player thread by pause");
630
631                                 /* Request prepare */
632                                 ret = audio_out_unprepare(g_audio_h);
633                                 if (AUDIO_IO_ERROR_NONE != ret) {
634                                         SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
635                                 } else {
636                                         SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
637                                 }
638                                 /* unset volume policy, volume will be 100% */
639                                 __unset_policy_for_playing();
640                                 return;
641                         }
642                 }
643
644                 if (NULL == g_playing_info && APP_STATE_READY == player->state) {
645                         /* player_stop */ 
646                         g_audio_state = AUDIO_STATE_READY;
647                         SLOG(LOG_DEBUG, tts_tag(), "[Player] Stop player thread");
648
649                         /* Request prepare */
650                         ret = audio_out_unprepare(g_audio_h);
651                         if (AUDIO_IO_ERROR_NONE != ret) {
652                                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
653                         } else {
654                                 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
655                         }
656
657                         if (NULL != sound_data) {
658                                 if (NULL != sound_data->data) {
659                                         free(sound_data->data);
660                                         sound_data->data = NULL;
661                                 }
662
663                                 free(sound_data);
664                                 sound_data = NULL;
665                         }
666                         /* unset volume policy, volume will be 100% */
667                         __unset_policy_for_playing();
668                         return;
669                 }
670
671                 if ((APP_STATE_PLAYING == player->state || APP_STATE_PAUSED == player->state) &&
672                         (TTSE_RESULT_EVENT_FINISH == sound_data->event)) {
673                         /* send utterence finish signal */
674                         int pid = ttsd_data_get_pid(player->uid);
675
676                         if (pid <= 0) {
677                                 SLOG(LOG_WARN, tts_tag(), "[Send WARNIING] Current player is not valid");
678                                 /* unset volume policy, volume will be 100% */
679                                 __unset_policy_for_playing();
680                                 if (NULL != sound_data->data) {
681                                         free(sound_data->data);
682                                         sound_data->data = NULL;
683                                 }
684
685                                 free(sound_data);
686                                 sound_data = NULL;
687                                 return;
688                         }
689
690                         if (0 != ttsdc_send_utt_finish_message(pid, player->uid, sound_data->utt_id)) {
691                                 SLOG(LOG_ERROR, tts_tag(), "[Send ERROR] Fail to send Utterance Completed Signal : pid(%d), uid(%d), uttid(%d)", 
692                                         pid, player->uid, sound_data->utt_id);
693                                 /* unset volume policy, volume will be 100% */
694                                 __unset_policy_for_playing();
695                                 if (NULL != sound_data->data) {
696                                         free(sound_data->data);
697                                         sound_data->data = NULL;
698                                 }
699
700                                 free(sound_data);
701                                 sound_data = NULL;
702                                 return;
703                         }
704
705                         SLOG(LOG_INFO, tts_tag(), "[Player] Finish utterance : uid(%d), uttid(%d)", player->uid, sound_data->utt_id);
706                 }
707
708                 if (NULL != sound_data) {
709                         if (NULL != sound_data->data) {
710                                 free(sound_data->data);
711                                 sound_data->data = NULL;
712                         }
713
714                         free(sound_data);
715                         sound_data = NULL;
716                 }
717
718                 if (NULL == g_playing_info) {
719                         SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Current player is NULL");
720                         g_audio_state = AUDIO_STATE_READY;
721                         ret = audio_out_unprepare(g_audio_h);
722                         if (AUDIO_IO_ERROR_NONE != ret) {
723                                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
724                         }
725                         /* unset volume policy, volume will be 100% */
726                         __unset_policy_for_playing();
727
728                         return;
729                 }
730         }
731 }
732
733 /*
734 * Player Interfaces
735 */
736 int ttsd_player_init()
737 {
738         g_playing_info = NULL;
739         g_audio_state = AUDIO_STATE_NONE;
740         g_audio_h = NULL;
741
742         int ret;
743
744         ecore_thread_max_set(1);
745
746         int cnt = 0;
747         while (1) {
748                 if (0 == access(FOCUS_SERVER_READY, F_OK)) {
749                         SLOG(LOG_ERROR, tts_tag(), "[Player SUCCESS] focus server is available");
750                         break;
751                 } else {
752                         if (0 == cnt++ % 10)
753                                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] focus server is not available");
754                         usleep(50000);
755                 }
756         }
757
758         ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_VOICE_INFORMATION, __player_focus_state_cb, NULL, &g_stream_info_h);
759         if (SOUND_MANAGER_ERROR_NONE != ret) {
760                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to create stream info");
761                 return -1;
762         } else {
763                 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Create stream info");
764         }
765
766         /* add sound focus state watch callback */
767         ret = sound_manager_add_focus_state_watch_cb(SOUND_STREAM_FOCUS_FOR_PLAYBACK, __player_focus_state_watch_cb, NULL, &g_focus_watch_id);
768         if (SOUND_MANAGER_ERROR_NONE != ret) {
769                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to add sound focus watch callback");
770                 return -1;
771         } else {
772                 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Add sound focus watch callback");
773         }
774
775         ecore_thread_max_set(1);
776
777         ret = __create_audio_out(TTSE_AUDIO_TYPE_RAW_S16, 16000);
778         if (0 != ret)
779                 return -1;
780
781         g_player_init = true;
782
783         return 0;
784 }
785
786 int ttsd_player_release(void)
787 {
788         if (false == g_player_init) {
789                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
790                 return TTSD_ERROR_OPERATION_FAILED;
791         }
792
793         int ret;
794
795         SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] @@@@@");
796         SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Active thread count : %d", ecore_thread_active_get());
797         SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] @@@@@");
798
799         /* The thread should be released */
800         int thread_count = ecore_thread_active_get();
801         int count = 0;
802         while (0 < thread_count) {
803                 usleep(10000);
804
805                 count++;
806                 if (20 == count) {
807                         SLOG(LOG_WARN, tts_tag(), "[Player WARNING!!] Thread is blocked. Player release continue.");
808                         break;
809                 }
810
811                 thread_count = ecore_thread_active_get();
812         }
813
814         SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Thread is released");
815
816         ret = __destroy_audio_out();
817         if (0 != ret)
818                 return -1;
819
820         ret = sound_manager_destroy_stream_information(g_stream_info_h);
821         if (SOUND_MANAGER_ERROR_NONE != ret) {
822                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to destroy stream info");
823         } else {
824                 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Destroy stream info");
825         }
826
827         /* remove focus state watch callback */
828         ret = sound_manager_remove_focus_state_watch_cb(g_focus_watch_id);
829         if (SOUND_MANAGER_ERROR_NONE != ret) {
830                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to remove the focus state watch cb");
831         } else {
832                 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Remove the focus state watch cb");
833         }
834
835         /* clear g_player_list */
836         g_playing_info = NULL;
837         g_player_init = false;
838
839         return 0;
840 }
841
842 int ttsd_player_create_instance(int uid)
843 {
844         if (false == g_player_init) {
845                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
846                 return -1;
847         }
848
849         /* Check uid is duplicated */
850         if (NULL != __player_get_item(uid)) {
851                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is already registered", uid);
852                 return -1;
853         }
854
855         player_s* new_client = (player_s*)calloc(1, sizeof(player_s));
856         if (NULL == new_client) {
857                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to allocate memory");
858                 return TTSE_ERROR_OUT_OF_MEMORY;
859         }
860
861         new_client->uid = uid;
862         new_client->event = TTSE_RESULT_EVENT_FINISH;
863         new_client->state = APP_STATE_READY;
864         new_client->is_paused_data = false;
865         new_client->idx = 0;
866         new_client->paused_data = NULL;
867         
868         SECURE_SLOG(LOG_DEBUG, tts_tag(), "[Player] Create player : uid(%d)", uid);
869
870         g_player_list = g_list_append(g_player_list, new_client);
871
872         return 0;
873 }
874
875 int ttsd_player_destroy_instance(int uid)
876 {
877         if (false == g_player_init) {
878                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
879                 return -1;
880         }
881
882         player_s* current;
883         current = __player_get_item(uid);
884         if (NULL == current) {
885                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
886                 return -1;
887         }
888
889         if (NULL != g_playing_info) {
890                 if (uid == g_playing_info->uid) {
891                         g_playing_info = NULL;
892                 }
893         }
894
895         GList *iter = NULL;
896         player_s *data = NULL;
897
898         if (0 < g_list_length(g_player_list)) {
899                 /* Get a first item */
900                 iter = g_list_first(g_player_list);
901
902                 while (NULL != iter) {
903                         /* Get handle data from list */
904                         data = (player_s*)iter->data;
905
906                         if (NULL != data) {
907                                 /* compare uid */
908                                 if (uid == data->uid) {
909                                         g_player_list = g_list_remove_link(g_player_list, iter);
910                                         free(data);
911                                         g_list_free(iter);              
912                                         break;
913                                 }
914                         }
915
916                         /* Get next item */
917                         iter = g_list_next(iter);
918                 }
919         }
920
921         SLOG(LOG_DEBUG, tts_tag(), "[PLAYER Success] Destroy instance");
922
923         return 0;
924 }
925
926 int ttsd_player_play(int uid)
927 {
928         if (false == g_player_init) {
929                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
930                 return -1;
931         }
932
933         if (NULL != g_playing_info) {
934                 if (uid == g_playing_info->uid) {
935                         SLOG(LOG_DEBUG, tts_tag(), "[Player] uid(%d) has already played", g_playing_info->uid);
936                         return 0;
937                 } else {
938                         SLOG(LOG_WARN, tts_tag(), "[Player WARNING] stop old player (%d)", g_playing_info->uid);
939                         ttsd_player_stop(g_playing_info->uid);
940                 }
941         }
942
943         SLOG(LOG_DEBUG, tts_tag(), "[Player] start play : uid(%d)", uid);
944
945         /* Check sound queue size */
946         if (0 == ttsd_data_get_sound_data_size(uid)) {
947                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] A sound queue of current player(%d) is empty", uid);
948                 return -1;
949         }
950
951         /* Check uid */
952         player_s* current;
953         current = __player_get_item(uid);
954         if (NULL == current) {
955                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
956                 return -1;
957         }
958
959         current->state = APP_STATE_PLAYING;
960
961         g_playing_info = current;
962
963         SLOG(LOG_INFO, tts_tag(), "[Player DEBUG] Active thread count : %d", ecore_thread_active_get());
964
965         if (0 < ttsd_data_get_sound_data_size(current->uid)) {
966                 SLOG(LOG_INFO, tts_tag(), "[Player] Run thread");
967                 ecore_thread_run(__play_thread, __end_play_thread, NULL, NULL);
968         }
969
970         return 0;
971 }
972
973 int ttsd_player_stop(int uid)
974 {
975         if (false == g_player_init) {
976                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
977                 return -1;
978         }
979
980         /* Check uid */
981         player_s* current;
982         current = __player_get_item(uid);
983         if (NULL == current) {
984                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
985                 return -1;
986         }
987
988         /* check whether uid is current playing or not */
989         if (NULL != g_playing_info) {
990                 if (uid == g_playing_info->uid) {
991                         /* release current playing info */
992                         g_playing_info = NULL;
993                 }
994         } else {
995                 SLOG(LOG_DEBUG, tts_tag(), "[Player] No current playing");
996         }
997
998         if (true == current->is_paused_data) {
999                 if (NULL != current->paused_data) {
1000                         if (NULL != current->paused_data->data) {
1001                                 free(current->paused_data->data);
1002                                 current->paused_data->data = NULL;
1003                         }
1004
1005                         free(current->paused_data);
1006                         current->paused_data = NULL;
1007                 }
1008         }
1009
1010         current->event = TTSE_RESULT_EVENT_FINISH;
1011         current->state = APP_STATE_READY;
1012         current->is_paused_data = false;
1013         current->idx = 0;
1014
1015         if (NULL == g_playing_info) {
1016                 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1017                 SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
1018                 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1019
1020                 /* The thread should be released */
1021                 int thread_count = ecore_thread_active_get();
1022                 int count = 0;
1023                 while (0 < thread_count) {
1024                         usleep(10000);
1025
1026                         count++;
1027                         if (30 == count) {
1028                                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING!!] Thread is blocked. Player release continue.");
1029                                 break;
1030                         }
1031
1032                         thread_count = ecore_thread_active_get();
1033                 }
1034
1035                 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1036                 SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
1037                 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1038         }
1039
1040         SLOG(LOG_INFO, tts_tag(), "[Player SUCCESS] Stop player : uid(%d)", uid);
1041
1042         return 0;
1043 }
1044
1045 int ttsd_player_clear(int uid)
1046 {
1047         if (false == g_player_init) {
1048                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1049                 return -1;
1050         }
1051
1052         /* Check uid */
1053         player_s* current;
1054         current = __player_get_item(uid);
1055         if (NULL == current) {
1056                 SECURE_SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid); 
1057                 return -1;
1058         }
1059
1060         if (true == current->is_paused_data) {
1061                 if (NULL != current->paused_data) {
1062                         if (NULL != current->paused_data->data) {
1063                                 free(current->paused_data->data);
1064                                 current->paused_data->data = NULL;
1065                         }
1066
1067                         free(current->paused_data);
1068                         current->paused_data = NULL;
1069                 }
1070         }
1071
1072         current->event = TTSE_RESULT_EVENT_FINISH;
1073         current->state = APP_STATE_READY;
1074         current->is_paused_data = false;
1075         current->idx = 0;
1076
1077         SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Clear player : uid(%d)", uid);
1078
1079         return 0;
1080 }
1081
1082 int ttsd_player_pause(int uid)
1083 {
1084         SLOG(LOG_DEBUG, tts_tag(), "[Player] pause player : uid(%d)", uid);
1085
1086         if (false == g_player_init) {
1087                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1088                 return -1;
1089         }
1090
1091         /* Check uid */
1092         player_s* current;
1093         current = __player_get_item(uid);
1094         if (NULL == current) {
1095                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] ttsd_player_pause() : uid(%d) is not valid", uid);
1096                 return -1;
1097         }
1098
1099         /* check whether uid is current playing or not */
1100         if (NULL != g_playing_info) {
1101                 if (uid == g_playing_info->uid) {
1102                         /* release current playing info */
1103                         g_playing_info = NULL;
1104                 } else {
1105                         /* error case */
1106                 }
1107         }
1108
1109         current->state = APP_STATE_PAUSED;
1110
1111         SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1112         SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
1113         SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1114
1115         /* The thread should be released */
1116         int thread_count = ecore_thread_active_get();
1117         int count = 0;
1118         while (0 < thread_count) {
1119                 usleep(10000);
1120
1121                 count++;
1122                 if (30 == count) {
1123                         SLOG(LOG_WARN, tts_tag(), "[Player WARNING!!] Thread is blocked. Player release continue.");
1124                         break;
1125                 }
1126
1127                 thread_count = ecore_thread_active_get();
1128         }
1129
1130         SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1131         SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
1132         SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1133
1134         SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Pause player : uid(%d)", uid);
1135
1136         return 0;
1137 }
1138
1139 int ttsd_player_resume(int uid)
1140 {
1141         SLOG(LOG_DEBUG, tts_tag(), "[Player] Resume player : uid(%d)", uid);
1142
1143         if (false == g_player_init) {
1144                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1145                 return -1;
1146         }
1147
1148         /* Check id */
1149         player_s* current;
1150         current = __player_get_item(uid);
1151         if (NULL == current) {
1152                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
1153                 return -1;
1154         }
1155
1156         /* check current player */
1157         if (NULL != g_playing_info)
1158                 g_playing_info = NULL;
1159
1160         current->state = APP_STATE_PLAYING;
1161         g_playing_info = current;
1162
1163         SLOG(LOG_INFO, tts_tag(), "[Player] Resume to run thread");
1164         ecore_thread_run(__play_thread, __end_play_thread, NULL, NULL);
1165
1166         return 0;
1167 }
1168
1169 int ttsd_player_all_stop()
1170 {
1171         if (false == g_player_init) {
1172                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1173                 return -1;
1174         }
1175
1176         g_playing_info = NULL;
1177
1178         GList *iter = NULL;
1179         player_s *data = NULL;
1180
1181         if (0 < g_list_length(g_player_list)) {
1182                 /* Get a first item */
1183                 iter = g_list_first(g_player_list);
1184
1185                 while (NULL != iter) {
1186                         /* Get handle data from list */
1187                         data = (player_s*)iter->data;
1188
1189                         app_tts_state_e state;
1190                         if (0 > ttsd_data_get_client_state(data->uid, &state)) {
1191                                 SLOG(LOG_ERROR, tts_tag(), "[player ERROR] uid(%d) is not valid", data->uid);
1192                                 ttsd_player_destroy_instance(data->uid);
1193                                 iter = g_list_next(iter);
1194                                 continue;
1195                         }
1196
1197                         if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1198                                 data->event = TTSE_RESULT_EVENT_FINISH;
1199                                 data->state = APP_STATE_READY;
1200
1201                                 if (true == data->is_paused_data) {
1202                                         if (NULL != data->paused_data) {
1203                                                 if (NULL != data->paused_data->data) {
1204                                                         free(data->paused_data->data);
1205                                                         data->paused_data->data = NULL;
1206                                                 }
1207
1208                                                 free(data->paused_data);
1209                                                 data->paused_data = NULL;
1210                                         }
1211                                 }
1212
1213                                 data->is_paused_data = false;
1214                                 data->idx = 0;
1215                         }
1216
1217                         /* Get next item */
1218                         iter = g_list_next(iter);
1219                 }
1220         }
1221
1222         SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] player all stop!!");
1223
1224         return 0;
1225 }
1226
1227 int ttsd_player_play_pcm(int uid)
1228 {
1229         if (false == g_player_init) {
1230                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1231                 return -1;
1232         }
1233
1234         if (NULL != g_playing_info) {
1235                 if (uid == g_playing_info->uid) {
1236                         SLOG(LOG_DEBUG, tts_tag(), "[Player] uid(%d) has already played", g_playing_info->uid);
1237                         return 0;
1238                 } else {
1239                         SLOG(LOG_WARN, tts_tag(), "[Player WARNING] stop old player (%d)", g_playing_info->uid);
1240                         ttsd_player_stop(g_playing_info->uid);
1241                 }
1242         }
1243
1244         SLOG(LOG_DEBUG, tts_tag(), "[Player] start play : uid(%d)", uid);
1245
1246         /* Check sound queue size */
1247         if (0 == ttsd_data_get_sound_data_size(uid)) {
1248                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] A sound queue of current player(%d) is empty", uid);
1249         }
1250
1251         /* Check uid */
1252         player_s* current;
1253         current = __player_get_item(uid);
1254         if (NULL == current) {
1255                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
1256                 return -1;
1257         }
1258
1259         current->state = APP_STATE_PLAYING;
1260
1261         g_playing_info = current;
1262
1263         SLOG(LOG_INFO, tts_tag(), "[Player DEBUG] Active thread count : %d", ecore_thread_active_get());
1264
1265         if (0 <= ttsd_data_get_sound_data_size(current->uid)) {
1266                 SLOG(LOG_INFO, tts_tag(), "[Player] Run thread");
1267                 ecore_thread_run(__play_thread, __end_play_thread, NULL, NULL);
1268         }
1269
1270         return 0;
1271 }
1272