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