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