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