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