Fix potential defects on ttsd_player
[platform/core/uifw/tts.git] / server / ttsd_player.cpp
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 #include <pthread.h>
19 #include <time.h>
20
21 #include "ttsd_main.h"
22 #include "ttsd_player.h"
23 #include "ttsd_data.h"
24 #include "ttsd_dbus.h"
25 #include "ttsd_ipc.h"
26
27 #include "tts_internal.h"
28 #include "ttsd_server.h"
29
30 /*
31 * Internal data structure
32 */
33
34 typedef enum {
35         AUDIO_STATE_NONE = 0,
36         AUDIO_STATE_READY,
37         AUDIO_STATE_WAIT_FOR_PLAYING,
38         AUDIO_STATE_PLAY
39 } audio_state_e;
40
41 typedef struct {
42         unsigned int            uid;    /** client id */
43         app_tts_state_e         state;  /** client state */
44
45         /* Current utterance information */
46         ttse_result_event_e     event;  /** event of last utterance */
47
48         bool                    is_paused_data;
49         int                     idx;
50         sound_data_s*           paused_data;
51 } player_s;
52
53 #define SOUND_BUFFER_LENGTH     2048
54 #define FOCUS_SERVER_READY "/tmp/.sound_server_ready"
55
56 static const intptr_t CHECK_TIMER_DELETE = 1;
57 static const int EXTRA_INFO_LENGTH = 20;
58
59 /* Sound buf save for test */
60 /*
61 #define BUF_SAVE_MODE
62 */
63
64 #ifdef BUF_SAVE_MODE
65 static char g_temp_file_name[128] = {'\0',};
66 static FILE* g_pFile;
67 static int g_count = 0;
68 static pthread_mutex_t g_buf_save_mutex = PTHREAD_MUTEX_INITIALIZER;
69 #endif
70
71 /** player init info */
72 static bool g_player_init = false;
73
74 /** Client list */
75 static GList *g_player_list;
76
77 /** current player information */
78 static player_s* g_playing_info;
79
80 /* player state */
81 static audio_state_e g_audio_state;
82
83 static ttse_audio_type_e g_audio_type;
84
85 static int g_sampling_rate;
86
87 static audio_out_h g_audio_h;
88
89 static sound_stream_info_h g_stream_info_h;
90
91 static sound_stream_ducking_h g_media_stream_ducking;
92 //static sound_stream_ducking_h g_system_stream_ducking;
93 static sound_stream_ducking_h g_notification_stream_ducking;
94 static sound_stream_ducking_h g_alarm_stream_ducking;
95
96 static bool g_is_set_policy;
97
98 // static bool ducking_flag;
99
100 /* CAUTION!
101 If you change this constant value. Please check the function '__set_timer_for_delay_recover()'.
102 If you choose too big value, it may cause integer overflow issue.
103 */
104 #define SND_MGR_DUCKING_DURATION 500
105
106 static struct timespec g_policy_set_time;
107 static Ecore_Timer* g_delayed_unset_policy_timer = NULL;
108 static Ecore_Timer* g_modify_background_volume = NULL;
109
110 static double g_bg_volume_ratio;
111
112 static pthread_mutex_t g_play_thread_mutex = PTHREAD_MUTEX_INITIALIZER;
113 static pthread_mutex_t g_player_control_mutex = PTHREAD_MUTEX_INITIALIZER;
114
115 static pthread_cond_t g_play_thread_cond = PTHREAD_COND_INITIALIZER;
116 /*
117 * Internal Interfaces
118 */
119 static void __set_playing_status(bool is_playing)
120 {
121         int ret = vconf_set_bool(TTS_PLAYING_STATUS_KEY, is_playing ? 1 : 0);
122         SLOG(LOG_INFO, tts_tag(), "[Player] Set playing status (%s). ret(%d)", is_playing ? "True" : "False", ret);
123 }
124
125 #ifdef BUF_SAVE_MODE
126 static void __open_buffer_dump_file()
127 {
128         pthread_mutex_lock(&g_buf_save_mutex);
129         if (g_pFile) {
130                 SLOG(LOG_ERROR, tts_tag(), "[Buffer Dump] File is already opened(%s)", g_temp_file_name);
131                 pthread_mutex_unlock(&g_buf_save_mutex);
132                 return;
133         }
134
135         g_count++;
136         while (1) {
137                 snprintf(g_temp_file_name, sizeof(g_temp_file_name), "/tmp/tts_temp_%d_%d", getpid(), g_count);
138                 int ret = access(g_temp_file_name, 0);
139
140                 if (0 == ret) {
141                         SLOG(LOG_ERROR, tts_tag(), "[Recorder ERROR] File is already exist");
142                         if (0 == remove(g_temp_file_name)) {
143                                 SLOG(LOG_DEBUG, tts_tag(), "[Recorder] Remove file");
144                                 break;
145                         } else {
146                                 g_count++;
147                         }
148                 } else {
149                         break;
150                 }
151         }
152
153         SECURE_SLOG(LOG_DEBUG, tts_tag(), "[Recorder] Temp file name=[%s]", g_temp_file_name);
154
155         /* open test file */
156         g_pFile = fopen(g_temp_file_name, "wb+x");
157         if (NULL == g_pFile) {
158                 SLOG(LOG_ERROR, tts_tag(), "[Recorder ERROR] File not found!");
159         }
160
161         pthread_mutex_unlock(&g_buf_save_mutex);
162 }
163
164 static void __close_buffer_dump_file()
165 {
166         pthread_mutex_lock(&g_buf_save_mutex);
167
168         if (g_pFile) {
169                 fclose(g_pFile);
170                 g_pFile = NULL;
171         }
172
173         pthread_mutex_unlock(&g_buf_save_mutex);
174 }
175
176 static void __write_buffer_dump_file(const void* buffer, size_t length)
177 {
178         pthread_mutex_lock(&g_buf_save_mutex);
179
180         if (g_pFile) {
181                 size_t ret = fwrite(buffer, 1, length, g_pFile);
182                 SLOG(LOG_DEBUG, tts_tag(), "[Buffer Dump] Stored size(%zu / %zu)", ret, length);
183         } else {
184                 SLOG(LOG_ERROR, tts_tag(), "[Buffer Dump] File is not opened. Please check the file open");
185         }
186
187         pthread_mutex_unlock(&g_buf_save_mutex);
188 }
189 #endif
190
191 static bool __is_player_valid(player_s* player)
192 {
193         if (NULL == player || NULL == g_playing_info) {
194                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] player is NULL");
195                 return false;
196         }
197
198         if (g_playing_info != player || g_playing_info->uid != player->uid) {
199                 SLOG(LOG_ERROR, tts_tag(), "[ERROR] player is not current player");
200                 return false;
201         }
202
203         return true;
204 }
205
206 player_s* __player_get_item(unsigned int uid)
207 {
208         GList *iter = NULL;
209         player_s *data = NULL;
210
211         if (0 < g_list_length(g_player_list)) {
212                 /* Get a first item */
213                 iter = g_list_first(g_player_list);
214
215                 while (NULL != iter) {
216                         /* Get handle data from list */
217                         data = (player_s*)iter->data;
218
219                         /* compare uid */
220                         if (uid == data->uid)
221                                 return data;
222
223                         /* Get next item */
224                         iter = g_list_next(iter);
225                 }
226         }
227
228         return NULL;
229 }
230
231 static bool __is_focus_released_on_playing(sound_stream_focus_mask_e focus_mask, sound_stream_focus_state_e focus_state)
232 {
233         if (NULL == g_playing_info) {
234                 SLOG(LOG_INFO, tts_tag(), "[Player] No current player");
235                 return false;
236         }
237
238         if (APP_STATE_PLAYING != g_playing_info->state || AUDIO_STATE_NONE == g_audio_state || AUDIO_STATE_READY == g_audio_state) {
239                 SLOG(LOG_INFO, tts_tag(), "[Player] Audio is not played");
240                 return false;
241         }
242
243         if (SOUND_STREAM_FOCUS_FOR_PLAYBACK != focus_mask || SOUND_STREAM_FOCUS_STATE_RELEASED != focus_state) {
244                 SLOG(LOG_INFO, tts_tag(), "[Player] Playback focus is not released");
245                 return false;
246         }
247
248         return true;
249 }
250
251 static 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,
252                                                         sound_stream_focus_change_reason_e reason_for_change, int sound_behavior, const char *extra_info, void *user_data)
253 {
254         SLOG(LOG_DEBUG, tts_tag(), "@@@ Focus state changed cb");
255         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);
256
257         if (stream_info != g_stream_info_h) {
258                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Invalid stream info handle");
259                 return;
260         }
261
262         if (false == __is_focus_released_on_playing(focus_mask, focus_state)) {
263                 SLOG(LOG_INFO, tts_tag(), "[Player INFO] Playback focus is not released on playing");
264                 return;
265         }
266
267         unsigned int uid = g_playing_info->uid;
268         ttsd_mode_e mode = ttsd_data_get_mode(uid);
269
270         switch (mode) {
271         case TTSD_MODE_DEFAULT:
272                 {
273                         SLOG(LOG_DEBUG, tts_tag(), "[Player] Pause current player - mode(%d)", mode);
274                         g_audio_state = AUDIO_STATE_READY;
275
276                         if (0 != ttsd_player_pause(uid)) {
277                                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to pause the player");
278                                 break;
279                         }
280
281                         ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
282                         int pid = ttsd_data_get_pid(uid);
283                         if (pid <= 0) {
284                                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to get pid. uid(%u)", uid);
285                         } else {
286                                 /* send message to client about changing state */
287                                 SLOG(LOG_INFO, tts_tag(), "[Player INFO] Player paused. pid(%d), uid(%u)", pid, uid);
288                                 ttsdc_ipc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
289                         }
290                         break;
291                 }
292
293         case TTSD_MODE_NOTIFICATION:
294         case TTSD_MODE_SCREEN_READER:
295                 {
296                         SLOG(LOG_DEBUG, tts_tag(), "[Player] Stop current player - mode(%d)", mode);
297                         g_audio_state = AUDIO_STATE_READY;
298                         ttsd_send_all_stop();
299                         break;
300                 }
301
302         case TTSD_MODE_INTERRUPT:
303                 SLOG(LOG_DEBUG, tts_tag(), "[Player] Ignore focus release - mode(%d)", mode);
304                 break;
305
306         default:
307                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Invalid mode - mode(%d)", mode);
308                 break;
309         }
310
311         SLOG(LOG_DEBUG, tts_tag(), "@@@");
312
313         return;
314 }
315
316 void __sound_stream_ducking_state_changed_cb(sound_stream_ducking_h stream_ducking, bool is_ducked, void *user_data)
317 {
318         SLOG(LOG_DEBUG, tts_tag(), "@@@ ducking state changed cb");
319         SLOG(LOG_ERROR, tts_tag(), "[Player] ducking_h(%p) is ducked : %d", stream_ducking, is_ducked);
320         // ducking_flag = true;
321         return;
322 }
323
324 static const char* __get_ducking_stream(sound_stream_type_e stream_type)
325 {
326         const char* type = nullptr;
327         if (SOUND_STREAM_TYPE_MEDIA == stream_type)
328                 type = "Media stream";
329         else if (SOUND_STREAM_TYPE_SYSTEM == stream_type)
330                 type = "System stream";
331         else if (SOUND_STREAM_TYPE_NOTIFICATION == stream_type)
332                 type = "Notification stream";
333         else if (SOUND_STREAM_TYPE_ALARM == stream_type)
334                 type = "Alarm stream";
335         else
336                 type = "Non matched stream";
337
338         return type;
339 }
340
341 static int __activate_ducking_sound_stream(sound_stream_type_e stream_type, sound_stream_ducking_h stream_ducking_h, unsigned int duration)
342 {
343         bool is_ducked = false;
344         int ret = sound_manager_is_ducked(stream_ducking_h, &is_ducked);
345         if (is_ducked) {
346                 SLOG(LOG_DEBUG, tts_tag(), "[Player] The %s is already ducked", __get_ducking_stream(stream_type));
347         } else {
348                 ret = sound_manager_activate_ducking(stream_ducking_h, duration, g_bg_volume_ratio);
349                 if (SOUND_MANAGER_ERROR_NONE != ret) {
350                         SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to activate ducking for %s", __get_ducking_stream(stream_type));
351                 } else {
352                         SLOG(LOG_INFO, tts_tag(), "[Player SUCCESS] Activate ducking for %s", __get_ducking_stream(stream_type));
353                 }
354         }
355         return ret;
356 }
357
358 static void __change_background_volume(unsigned int duration)
359 {
360         SLOG(LOG_INFO, tts_tag(), "[BG] Change background volume");
361         SLOG(LOG_INFO, tts_tag(), "[Player] volume ratio(%lf)", g_bg_volume_ratio);
362         if (1.0 > g_bg_volume_ratio) {
363                 __activate_ducking_sound_stream(SOUND_STREAM_TYPE_MEDIA, g_media_stream_ducking, duration);
364 //              __activate_ducking_sound_stream(SOUND_STREAM_TYPE_SYSTEM, g_system_stream_ducking, duration);
365                 __activate_ducking_sound_stream(SOUND_STREAM_TYPE_NOTIFICATION, g_notification_stream_ducking, duration);
366                 __activate_ducking_sound_stream(SOUND_STREAM_TYPE_ALARM, g_alarm_stream_ducking, duration);
367         }
368 }
369
370 static void __change_background_volume_async(void* data)
371 {
372         __change_background_volume(SND_MGR_DUCKING_DURATION);
373 }
374
375 static int __deactivate_ducking_sound_stream(sound_stream_type_e stream_type, sound_stream_ducking_h stream_ducking_h)
376 {
377         bool is_ducked = false;
378         int ret = sound_manager_is_ducked(stream_ducking_h, &is_ducked);
379         if (!is_ducked) {
380                 SLOG(LOG_DEBUG, tts_tag(), "[Player] The %s is already recovered from ducking", __get_ducking_stream(stream_type));
381         } else {
382                 ret = sound_manager_deactivate_ducking(stream_ducking_h);
383                 if (SOUND_MANAGER_ERROR_NONE != ret) {
384                         SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to deactivate ducking for %s", __get_ducking_stream(stream_type));
385                 } else {
386                         SLOG(LOG_INFO, tts_tag(), "[Player SUCCESS] Deactivate ducking for %s", __get_ducking_stream(stream_type));
387                 }
388         }
389         return ret;
390 }
391
392 static void __recover_background_volume()
393 {
394         __deactivate_ducking_sound_stream(SOUND_STREAM_TYPE_MEDIA, g_media_stream_ducking);
395 //      __deactivate_ducking_sound_stream(SOUND_STREAM_TYPE_SYSTEM, g_system_stream_ducking);
396         __deactivate_ducking_sound_stream(SOUND_STREAM_TYPE_NOTIFICATION, g_notification_stream_ducking);
397         __deactivate_ducking_sound_stream(SOUND_STREAM_TYPE_ALARM, g_alarm_stream_ducking);
398 }
399
400 static int __create_audio_out(ttse_audio_type_e type, int rate)
401 {
402         int ret = -1;
403         audio_sample_type_e sample_type;
404
405         if (TTSE_AUDIO_TYPE_RAW_S16 == type) {
406                 sample_type = AUDIO_SAMPLE_TYPE_S16_LE;
407         } else {
408                 sample_type = AUDIO_SAMPLE_TYPE_U8;
409         }
410
411         ret = audio_out_create_new(rate, AUDIO_CHANNEL_MONO, sample_type, &g_audio_h);
412         if (AUDIO_IO_ERROR_NONE != ret) {
413                 g_audio_state = AUDIO_STATE_NONE;
414                 g_audio_h = NULL;
415                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to create audio");
416                 return -1;
417         } else {
418                 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Create audio");
419         }
420
421         g_audio_type = type;
422         g_sampling_rate = rate;
423
424         g_audio_state = AUDIO_STATE_READY;
425
426         return 0;
427 }
428
429 static int __destroy_audio_out()
430 {
431         if (NULL == g_audio_h) {
432                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Current handle is not valid");
433                 return -1;
434         }
435
436         int ret = -1;
437         ret = audio_out_destroy(g_audio_h);
438         if (AUDIO_IO_ERROR_NONE != ret) {
439                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to destroy audio");
440                 return -1;
441         } else {
442                 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Destroy audio");
443         }
444
445         g_audio_type = TTSE_AUDIO_TYPE_RAW_S16;
446         g_sampling_rate = 0;
447
448         g_audio_state = AUDIO_STATE_NONE;
449         g_audio_h = NULL;
450
451         return 0;
452 }
453
454 static void __end_play_thread(void *data, Ecore_Thread *thread)
455 {
456         SLOG(LOG_ERROR, tts_tag(), "@@@ End thread");
457 }
458
459 static void __del_timer_for_delayed_recover(void* data)
460 {
461         if (NULL != g_delayed_unset_policy_timer) {
462                 int result = (intptr_t)ecore_timer_del(g_delayed_unset_policy_timer);
463                 g_delayed_unset_policy_timer = NULL;
464                 SLOG(LOG_ERROR, tts_tag(), "[BG] Remove timer (%d)", result);
465         }
466 }
467
468 static void __set_policy_for_playing(void)
469 {
470         ecore_main_loop_thread_safe_call_async(__del_timer_for_delayed_recover, NULL);
471
472         /* Set stream info */
473         const char* extra_info = NULL;
474         if (TTSD_MODE_INTERRUPT == ttsd_get_mode()) {
475                 extra_info = "TTSD_MODE_INTERRUPT";
476         }
477
478         int ret = sound_manager_acquire_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, SOUND_BEHAVIOR_NONE, extra_info);
479         if (SOUND_MANAGER_ERROR_NONE != ret) {
480                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to acquire focus");
481         } else {
482                 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Success to acquire focus");
483         }
484
485         ret = audio_out_set_sound_stream_info(g_audio_h, g_stream_info_h);
486         if (AUDIO_IO_ERROR_NONE != ret) {
487                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to set stream info");
488         }
489
490         ecore_main_loop_thread_safe_call_async(__change_background_volume_async, NULL);
491
492         g_is_set_policy = true;
493         SLOG(LOG_ERROR, tts_tag(), "[BG] g_is_set_policy(%d)", g_is_set_policy);
494
495         clock_gettime(CLOCK_MONOTONIC, &g_policy_set_time);
496
497         SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] set policy for playing");
498
499         return;
500 }
501
502 static Eina_Bool __delay_recover_background_volume(void* data)
503 {
504         __recover_background_volume();
505         SLOG(LOG_INFO, tts_tag(), "[BG] Delayed unset policy success");
506
507         g_delayed_unset_policy_timer = NULL;
508         return EINA_FALSE;
509 }
510
511 static long long int __get_duration_from_last_volume_change()
512 {
513         struct timespec current_time;
514         clock_gettime(CLOCK_MONOTONIC, &current_time);
515
516         long long int diff = ((long long int)current_time.tv_sec - (long long int)g_policy_set_time.tv_sec) * 1000
517                         + ((long long int)current_time.tv_nsec - (long long int)g_policy_set_time.tv_nsec) / 1000000;
518         SLOG(LOG_INFO, tts_tag(), "[BG] Time Diff(%lld)", diff);
519
520         return diff;
521 }
522
523 static void __set_timer_for_delay_recover(void* data)
524 {
525         if (NULL != g_delayed_unset_policy_timer) {
526                 return;
527         }
528
529         long long int diff = __get_duration_from_last_volume_change();
530         if (diff > SND_MGR_DUCKING_DURATION) {
531                 SLOG(LOG_INFO, tts_tag(), "[BG] Direct unset policy");
532                 __recover_background_volume();
533         } else {
534                 double delay = (double)(SND_MGR_DUCKING_DURATION - diff) / 1000.0;
535                 g_delayed_unset_policy_timer = ecore_timer_add(delay, __delay_recover_background_volume, (void*)CHECK_TIMER_DELETE);
536                 SLOG(LOG_INFO, tts_tag(), "[BG] Delayed unset policy (%p), delay(%f)", g_delayed_unset_policy_timer, delay);
537         }
538 }
539
540 static void __unset_policy_for_playing()
541 {
542         /* Unset stream info */
543         sound_stream_focus_state_e state_for_playing = SOUND_STREAM_FOCUS_STATE_ACQUIRED;
544         int ret = sound_manager_get_focus_state(g_stream_info_h, &state_for_playing, NULL);
545         if (SOUND_MANAGER_ERROR_NONE != ret) {
546                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to get focus state: %d", ret);
547         }
548
549         if (SOUND_STREAM_FOCUS_STATE_ACQUIRED == state_for_playing) {
550                 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] release focus (mode: %d)", ttsd_get_mode());
551                 ret = sound_manager_release_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, SOUND_BEHAVIOR_NONE, NULL);
552                 if (SOUND_MANAGER_ERROR_NONE != ret) {
553                         SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to release focus: %d", ret);
554                 }
555         }
556
557         ecore_main_loop_thread_safe_call_async(__set_timer_for_delay_recover, NULL);
558
559         g_is_set_policy = false;
560         SLOG(LOG_ERROR, tts_tag(), "[BG] g_is_set_policy(%d)", g_is_set_policy);
561         SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] unset policy for playing");
562
563         return;
564 }
565
566 static bool __does_interrupt_have_focus(sound_stream_focus_change_reason_e reason, int sound_behavior, char *extra_info)
567 {
568         SLOG(LOG_DEBUG, tts_tag(), "[Player] current Playback focus: extra_info(%s), reason(%d), sound_behavior(%d)", extra_info, reason, sound_behavior);
569         if (SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_INFORMATION != reason) {
570                 return false;
571         }
572
573         if (NULL == extra_info || 0 >= strlen(extra_info) || 0 != strncmp(extra_info, "TTSD_MODE_INTERRUPT", EXTRA_INFO_LENGTH)) {
574                 return false;
575         }
576
577         return true;
578 }
579
580 bool ttsd_player_does_interrupt_have_playback_focus()
581 {
582         sound_stream_focus_change_reason_e reason;
583         int sound_behavior = 0;
584         char *extra_info = NULL;
585         if (SOUND_MANAGER_ERROR_NONE != sound_manager_get_current_playback_focus(&reason, &sound_behavior, &extra_info)) {
586                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to get focus information");
587                 return false;
588         }
589
590         bool result = __does_interrupt_have_focus(reason, sound_behavior, extra_info);
591         free(extra_info);
592         return result;
593 }
594
595 static void __play_thread_old(void *data, Ecore_Thread *thread)
596 {
597         SLOG(LOG_DEBUG, tts_tag(), "@@@ Start thread");
598
599         if (NULL == g_playing_info) {
600                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] No current player");
601                 return;
602         }
603
604         player_s* player = g_playing_info;
605         sound_data_s* sound_data = NULL;
606
607         int ret = -1;
608         int len = SOUND_BUFFER_LENGTH;
609         int idx = 0;
610
611         /* set volume policy as 40% */
612         __set_policy_for_playing();
613         while (1) { // 1st while(1)
614                 /* check g_playing_info one more time */
615                 if (false == __is_player_valid(player)) {
616                         SLOG(LOG_INFO, tts_tag(), "[Player INFO] Player is not valid");
617                         g_audio_state = AUDIO_STATE_READY;
618                         ret = audio_out_unprepare(g_audio_h);
619                         if (AUDIO_IO_ERROR_NONE != ret) {
620                                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
621                         }
622                         __unset_policy_for_playing();
623                         return;
624                 }
625
626                 if (true == player->is_paused_data && NULL != player->paused_data) {
627                         /* Resume player */
628                         sound_data_s* paused_data = player->paused_data;
629                         player->paused_data = NULL;
630
631                         ttsd_data_destroy_sound_data(sound_data);
632                         sound_data = ttsd_data_create_sound_data(paused_data->utt_id, paused_data->data, paused_data->data_size,
633                                         paused_data->event, paused_data->audio_type, paused_data->rate, paused_data->channels);
634                         if (NULL == sound_data || paused_data->data_size <= 0) {
635                                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Out of memory OR paused_data is empty");
636                                 ttsd_data_destroy_sound_data(sound_data);
637                                 sound_data = paused_data;
638                         } else { // NULL != sound_data && NULL != temp && player->paused_data->data_size > 0
639                                 ttsd_data_destroy_sound_data(paused_data);
640                         }
641
642                         idx = player->idx;
643
644                         player->is_paused_data = false;
645                         player->idx = 0;
646
647                         if (NULL == sound_data) {
648                                 /* Request unprepare */
649                                 ret = audio_out_unprepare(g_audio_h);
650                                 if (AUDIO_IO_ERROR_NONE != ret) {
651                                         SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
652                                 } else {
653                                         SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
654                                 }
655
656                                 g_audio_state = AUDIO_STATE_READY;
657
658                                 /* unset volume policy, volume will be 100% */
659                                 __unset_policy_for_playing();
660                                 return;
661                         }
662                         __set_playing_status(true);
663
664                         SLOG(LOG_INFO, tts_tag(), "[Player] Sound info : id(%d) data(%p) size(%d) audiotype(%d) rate(%d) event(%d)",
665                                 sound_data->utt_id, sound_data->data, sound_data->data_size, sound_data->audio_type, sound_data->rate, sound_data->event);
666                 } else { // NO player->is_paused_data
667                         sound_data = NULL;
668                         ret = ttsd_data_get_sound_data(player->uid, &sound_data);
669                         if (0 != ret || NULL == sound_data) {
670                                 /* empty queue */
671                                 SLOG(LOG_ERROR, tts_tag(), "[Player] No sound data. Waiting mode");
672
673                                 /* wait for new audio data come */
674                                 while (1) { // 2nd while(1)
675                                         usleep(10000);
676                                         if (false == __is_player_valid(player)) {
677                                                 /* current playing uid is replaced */
678                                                 SLOG(LOG_INFO, tts_tag(), "[Player] Finish thread");
679                                                 if (AUDIO_STATE_PLAY == g_audio_state) {
680                                                         /* release audio & recover session */
681                                                         ret = audio_out_unprepare(g_audio_h);
682                                                         if (AUDIO_IO_ERROR_NONE != ret) {
683                                                                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
684                                                         } else {
685                                                                 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
686                                                         }
687                                                         g_audio_state = AUDIO_STATE_READY;
688                                                 }
689                                                 /* unset volume policy, volume will be 100% */
690                                                 __unset_policy_for_playing();
691                                                 return;
692                                         } else if (0 < ttsd_data_get_sound_data_size(player->uid)) {
693                                                 /* new audio data come */
694                                                 SLOG(LOG_INFO, tts_tag(), "[Player] Resume thread");
695                                                 break; // exit from 2nd while(1)
696                                         }
697
698                                         /* If engine is not on processing */
699                                         ttsd_synthesis_control_e synth_control = ttsd_get_synth_control();
700                                         if (TTSD_SYNTHESIS_CONTROL_DOING != synth_control) {
701                                                 SLOG(LOG_INFO, tts_tag(), "[Server INFO] synth_control(%d)", synth_control);
702                                                 if (AUDIO_STATE_PLAY == g_audio_state) {
703                                                         /* release audio & recover session */
704                                                         ret = audio_out_unprepare(g_audio_h);
705                                                         if (AUDIO_IO_ERROR_NONE != ret) {
706                                                                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
707                                                         } else {
708                                                                 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
709                                                         }
710                                                         g_audio_state = AUDIO_STATE_READY;
711
712                                                         /* unset volume policy, volume will be 100% */
713                                                         __unset_policy_for_playing();
714                                                 }
715                                         }
716                                 } // end of 2nd while(1). waiting for new audio data come
717
718                                 SLOG(LOG_INFO, tts_tag(), "[Player] Finish to wait for new audio data come");
719
720                                 if (AUDIO_STATE_READY == g_audio_state || AUDIO_STATE_WAIT_FOR_PLAYING == g_audio_state) {
721                                         /* set volume policy as 40%, when resume play thread*/
722                                         __set_policy_for_playing();
723                                 }
724
725                                 /* resume play thread */
726                                 player->state = APP_STATE_PLAYING;
727                                 continue;
728                         } // NULL == sound_data
729
730                         /* If wdata's event is 'start', current wdata is first data of engine for synthesis.
731                          * If wdata's event is 'finish', player should check previous event to know whether this wdata is first or not.
732                          * When previous wdata's event is 'finish' and current wdata's event is 'finish',
733                          * the player should send utt started event.
734                          */
735                         if (TTSE_RESULT_EVENT_START == sound_data->event ||
736                            (TTSE_RESULT_EVENT_FINISH == player->event && TTSE_RESULT_EVENT_FINISH == sound_data->event)) {
737                                 int pid = ttsd_data_get_pid(player->uid);
738                                 if (pid <= 0) {
739                                         SLOG(LOG_WARN, tts_tag(), "[Send WARNIING] Current player is not valid. uid(%u)", player->uid);
740                                         /* unset volume policy, volume will be 100% */
741                                         __unset_policy_for_playing();
742                                         ttsd_data_destroy_sound_data(sound_data);
743                                         sound_data = NULL;
744                                         return;
745                                 }
746
747 #ifdef BUF_SAVE_MODE
748                                 __open_buffer_dump_file();
749 #endif
750
751                                 __set_playing_status(true);
752                                 if (0 != ttsdc_ipc_send_utt_start_message(pid, player->uid, sound_data->utt_id)) {
753                                         SLOG(LOG_ERROR, tts_tag(), "[Send ERROR] Fail to send Utterance Start Signal : pid(%d), uid(%u), uttid(%d)",
754                                                 pid, player->uid, sound_data->utt_id);
755                                 }
756                                 SLOG(LOG_INFO, tts_tag(), "[Player] Start utterance : uid(%u), uttid(%d)", player->uid, sound_data->utt_id);
757                         } // (TTSE_RESULT_EVENT_START == sound_data->event || (TTSE_RESULT_EVENT_FINISH == player->event && TTSE_RESULT_EVENT_FINISH == sound_data->event))
758
759                         /* Save last event to check utterance start */
760                         player->event = sound_data->event;
761                         idx = 0;
762
763                         if (NULL == sound_data->data || 0 >= sound_data->data_size) {
764                                 if (TTSE_RESULT_EVENT_FINISH == sound_data->event) {
765                                         SLOG(LOG_DEBUG, tts_tag(), "No sound data");
766                                         /* send utterence finish signal */
767                                         int pid = ttsd_data_get_pid(player->uid);
768
769                                         if (pid <= 0) {
770                                                 SLOG(LOG_WARN, tts_tag(), "[Send WARNIING] Current player is not valid. uid(%u)", player->uid);
771                                                 /* unset volume policy, volume will be 100% */
772                                                 __unset_policy_for_playing();
773                                                 ttsd_data_destroy_sound_data(sound_data);
774                                                 sound_data = NULL;
775                                                 return;
776                                         }
777
778                                         __unset_policy_for_playing();
779
780 #ifdef BUF_SAVE_MODE
781                                         __close_buffer_dump_file();
782 #endif
783
784                                         __set_playing_status(false);
785                                         if (0 != ttsdc_ipc_send_utt_finish_message(pid, player->uid, sound_data->utt_id)) {
786                                                 SLOG(LOG_ERROR, tts_tag(), "[Send ERROR] Fail to send Utterance Completed Signal : pid(%d), uid(%u), uttid(%d)",
787                                                         pid, player->uid, sound_data->utt_id);
788                                         } else {
789                                                 SLOG(LOG_INFO, tts_tag(), "[Player] Finish utterance : uid(%u), uttid(%d)", player->uid, sound_data->utt_id);
790                                         }
791                                 } // TTSE_RESULT_EVENT_FINISH == sound_data->event
792                                 SLOG(LOG_INFO, tts_tag(), "[Player] Event(%d) utterance : uid(%u), uttid(%d)", sound_data->event, player->uid, sound_data->utt_id);
793                                 ttsd_data_destroy_sound_data(sound_data);
794                                 sound_data = NULL;
795                                 continue;
796                         } // (NULL == sound_data->data || 0 >= sound_data->data_size)
797                 } // NO player->is_paused_data
798
799                 // If there is any change in audio format, recreate audio handle
800                 if (g_sampling_rate != sound_data->rate || g_audio_type != sound_data->audio_type) {
801                         SLOG(LOG_INFO, tts_tag(), "[Player] Change audio handle : org type(%d) org rate(%d)", g_audio_type, g_sampling_rate);
802                         if (NULL != g_audio_h) {
803                                 __destroy_audio_out();
804                         }
805
806                         if (0 > __create_audio_out(sound_data->audio_type, sound_data->rate)) {
807                                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to create audio out");
808                                 /* unset volume policy, volume will be 100% */
809                                 __unset_policy_for_playing();
810
811                                 ttsd_data_destroy_sound_data(sound_data);
812                                 sound_data = NULL;
813                                 return;
814                         }
815
816                         SLOG(LOG_INFO, tts_tag(), "[Player INFO] Success to destroy and recreate audio out");
817                         __set_policy_for_playing();
818                 }
819
820                 while (APP_STATE_PLAYING == player->state || APP_STATE_PAUSED == player->state) {
821                         if ((unsigned int)idx >= sound_data->data_size)
822                                 break;
823
824                         if ((unsigned int)idx + SOUND_BUFFER_LENGTH > sound_data->data_size) {
825                                 len = sound_data->data_size - idx;
826                         } else {
827                                 len = SOUND_BUFFER_LENGTH;
828                         }
829
830                         // Check whether set_policy is done or not
831                         if (false == g_is_set_policy) {
832                                 SLOG(LOG_INFO, tts_tag(), "[Player INFO] Set policy");
833                                 __set_policy_for_playing();
834                         }
835
836                         if (AUDIO_STATE_READY == g_audio_state || AUDIO_STATE_WAIT_FOR_PLAYING == g_audio_state) {
837                                 /* Request prepare */
838                                 ret = audio_out_prepare(g_audio_h);
839                                 if (AUDIO_IO_ERROR_NONE != ret) {
840                                         SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to prepare audio : %d", ret);
841                                         /* unset volume policy, volume will be 100% */
842                                         __unset_policy_for_playing();
843
844                                         ttsd_data_destroy_sound_data(sound_data);
845                                         sound_data = NULL;
846                                         return;
847                                 }
848                                 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Prepare audio");
849                                 g_audio_state = AUDIO_STATE_PLAY;
850                         } // (AUDIO_STATE_READY == g_audio_state || AUDIO_STATE_WAIT_FOR_PLAYING == g_audio_state)
851
852                         char* temp_data = sound_data->data;
853                         SLOG(LOG_INFO, tts_tag(), "[Player INFO] Before audio_out_write. data(%p), data[%d](%p), uid(%u), utt_id(%d), len(%d)",
854                                         temp_data, idx, &temp_data[idx], player->uid, sound_data->utt_id, len);
855 #ifdef BUF_SAVE_MODE
856                         __write_buffer_dump_file(&temp_data[idx], len);
857 #endif
858                         ret = audio_out_write(g_audio_h, &temp_data[idx], len);
859                         if (0 > ret) {
860                                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to audio write - %d", ret);
861                         } else {
862                                 idx += len;
863                                 SLOG(LOG_INFO, tts_tag(), "[Player INFO] After audio_out_write");
864                         }
865
866                         if (NULL == g_playing_info && APP_STATE_PAUSED != player->state) {
867                                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Current player is NULL");
868                                 g_audio_state = AUDIO_STATE_READY;
869                                 ret = audio_out_unprepare(g_audio_h);
870                                 if (AUDIO_IO_ERROR_NONE != ret) {
871                                         SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
872                                 }
873                                 /* unset volume policy, volume will be 100% */
874                                 __unset_policy_for_playing();
875
876                                 ttsd_data_destroy_sound_data(sound_data);
877                                 sound_data = NULL;
878                                 return;
879                         } // (NULL == g_playing_info && APP_STATE_PAUSED != player->state)
880
881                         if (APP_STATE_PAUSED == player->state) {
882                                 /* Save data */
883                                 SLOG(LOG_DEBUG, tts_tag(), "[Player] player(%p)", player);
884                                 ttsd_data_destroy_sound_data(player->paused_data);
885                                 player->paused_data = sound_data;
886
887                                 player->is_paused_data = true;
888                                 player->idx = idx;
889
890                                 g_audio_state = AUDIO_STATE_READY;
891
892                                 SLOG(LOG_INFO, tts_tag(), "[Player] Stop player thread by pause");
893
894                                 /* Request prepare */
895                                 ret = audio_out_unprepare(g_audio_h);
896                                 if (AUDIO_IO_ERROR_NONE != ret) {
897                                         SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
898                                 } else {
899                                         SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
900                                 }
901                                 /* unset volume policy, volume will be 100% */
902                                 __unset_policy_for_playing();
903                                 return;
904                         } // (APP_STATE_PAUSED == player->state)
905                 } // while (APP_STATE_PLAYING == player->state || APP_STATE_PAUSED == player->state)
906
907                 if (NULL == g_playing_info && APP_STATE_READY == player->state) {
908                         /* player_stop */
909                         g_audio_state = AUDIO_STATE_READY;
910                         SLOG(LOG_DEBUG, tts_tag(), "[Player] Stop player thread");
911
912                         /* Request prepare */
913                         ret = audio_out_unprepare(g_audio_h);
914                         if (AUDIO_IO_ERROR_NONE != ret) {
915                                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
916                         } else {
917                                 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
918                         }
919
920                         /* unset volume policy, volume will be 100% */
921                         __unset_policy_for_playing();
922                         ttsd_data_destroy_sound_data(sound_data);
923                         sound_data = NULL;
924                         return;
925                 } // (NULL == g_playing_info && APP_STATE_READY == player->state)
926
927                 if ((APP_STATE_PLAYING == player->state || APP_STATE_PAUSED == player->state) &&
928                         (TTSE_RESULT_EVENT_FINISH == sound_data->event)) {
929                         /* send utterence finish signal */
930                         int pid = ttsd_data_get_pid(player->uid);
931
932                         if (pid <= 0) {
933                                 SLOG(LOG_WARN, tts_tag(), "[Send WARNIING] Current player is not valid. uid(%u)", player->uid);
934                                 /* unset volume policy, volume will be 100% */
935                                 __unset_policy_for_playing();
936                                 ttsd_data_destroy_sound_data(sound_data);
937                                 sound_data = NULL;
938                                 return;
939                         }
940
941 #ifdef BUF_SAVE_MODE
942                         __close_buffer_dump_file();
943 #endif
944                         __set_playing_status(false);
945                         if (0 != ttsdc_ipc_send_utt_finish_message(pid, player->uid, sound_data->utt_id)) {
946                                 SLOG(LOG_ERROR, tts_tag(), "[Send ERROR] Fail to send Utterance Completed Signal : pid(%d), uid(%u), uttid(%d)",
947                                         pid, player->uid, sound_data->utt_id);
948                                 /* unset volume policy, volume will be 100% */
949                                 __unset_policy_for_playing();
950                                 ttsd_data_destroy_sound_data(sound_data);
951                                 sound_data = NULL;
952                                 return;
953                         }
954
955                         SLOG(LOG_INFO, tts_tag(), "[Player] Finish utterance : uid(%u), uttid(%d)", player->uid, sound_data->utt_id);
956                 } // ((APP_STATE_PLAYING == player->state || APP_STATE_PAUSED == player->state) && (TTSE_RESULT_EVENT_FINISH == sound_data->event))
957
958                 ttsd_data_destroy_sound_data(sound_data);
959                 sound_data = NULL;
960
961                 if (NULL == g_playing_info) {
962                         SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Current player is NULL");
963                         g_audio_state = AUDIO_STATE_READY;
964                         ret = audio_out_unprepare(g_audio_h);
965                         if (AUDIO_IO_ERROR_NONE != ret) {
966                                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
967                         }
968                         /* unset volume policy, volume will be 100% */
969                         __unset_policy_for_playing();
970                         ttsd_data_destroy_sound_data(sound_data);
971                         sound_data = NULL;
972                         return;
973                 }
974         } // end of 1st while(1)
975 }
976
977 static void __play_thread(void *data, Ecore_Thread *thread)
978 {
979         SLOG(LOG_INFO, tts_tag(), "[Player] play thread is on");
980
981         while (g_player_init) {
982                 SLOG(LOG_INFO, tts_tag(), "[Player] Wait play request...");
983                 pthread_mutex_lock(&g_play_thread_mutex);
984                 pthread_cond_wait(&g_play_thread_cond, &g_play_thread_mutex);
985                 if (false == g_player_init) {
986                         SLOG(LOG_INFO, tts_tag(), "[Player] Player is released");
987                         pthread_mutex_unlock(&g_play_thread_mutex);
988                         break;
989                 }
990
991                 __play_thread_old(data, thread);
992                 pthread_mutex_unlock(&g_play_thread_mutex);
993                 pthread_mutex_lock(&g_player_control_mutex);
994                 g_playing_info = NULL;
995                 pthread_mutex_unlock(&g_player_control_mutex);
996         }
997 }
998
999 int __create_ducking_handle(void)
1000 {
1001         int ret = -1;
1002         if (NULL == g_media_stream_ducking) {
1003                 ret = sound_manager_create_stream_ducking(SOUND_STREAM_TYPE_MEDIA, __sound_stream_ducking_state_changed_cb, NULL, &g_media_stream_ducking);
1004                 if (SOUND_MANAGER_ERROR_NONE != ret)
1005                         SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to create stream ducking for type media, ret(%d)", ret);
1006         } else {
1007                 SLOG(LOG_INFO, tts_tag(), "[Player INFO] Ducking handle for media stream is already created");
1008         }
1009
1010 /*      if (NULL == g_system_stream_ducking) {
1011                 ret = sound_manager_create_stream_ducking(SOUND_STREAM_TYPE_SYSTEM, __sound_stream_ducking_state_changed_cb, NULL, &g_system_stream_ducking);
1012                 if (SOUND_MANAGER_ERROR_NONE != ret)
1013                         SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to create stream ducking for system type, ret(%d)", ret);
1014         } else {
1015                 SLOG(LOG_INFO, tts_tag(), "[Player INFO] Ducking handle for system stream is already created");
1016         }
1017 */
1018         if (NULL == g_notification_stream_ducking) {
1019                 ret = sound_manager_create_stream_ducking(SOUND_STREAM_TYPE_NOTIFICATION, __sound_stream_ducking_state_changed_cb, NULL, &g_notification_stream_ducking);
1020                 if (SOUND_MANAGER_ERROR_NONE != ret)
1021                         SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to create stream ducking for notification type, ret(%d)", ret);
1022         } else {
1023                 SLOG(LOG_INFO, tts_tag(), "[Player INFO] Ducking handle for notification stream is already created");
1024         }
1025
1026         if (NULL == g_alarm_stream_ducking) {
1027                 ret = sound_manager_create_stream_ducking(SOUND_STREAM_TYPE_ALARM, __sound_stream_ducking_state_changed_cb, NULL, &g_alarm_stream_ducking);
1028                 if (SOUND_MANAGER_ERROR_NONE != ret)
1029                         SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to create stream ducking for alarm type, ret(%d)", ret);
1030         } else {
1031                 SLOG(LOG_INFO, tts_tag(), "[Player INFO] Ducking handle for alarm stream is already created");
1032         }
1033
1034         return ret;
1035 }
1036
1037 /*
1038 * Player Interfaces
1039 */
1040 int ttsd_player_init()
1041 {
1042         pthread_mutex_lock(&g_player_control_mutex);
1043         g_playing_info = NULL;
1044         g_audio_state = AUDIO_STATE_NONE;
1045         g_audio_h = NULL;
1046
1047         int ret;
1048
1049         ecore_thread_max_set(1);
1050
1051         int cnt = 0;
1052         while (1) {
1053                 if (0 == access(FOCUS_SERVER_READY, F_OK)) {
1054                         SLOG(LOG_ERROR, tts_tag(), "[Player SUCCESS] focus server is available");
1055                         break;
1056                 } else {
1057                         if (0 == cnt++ % 10)
1058                                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] focus server is not available");
1059                         usleep(50000);
1060                 }
1061         }
1062
1063         ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_VOICE_INFORMATION, __player_focus_state_cb, NULL, &g_stream_info_h);
1064         if (SOUND_MANAGER_ERROR_NONE != ret) {
1065                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to create stream info");
1066                 pthread_mutex_unlock(&g_player_control_mutex);
1067                 return -1;
1068         } else {
1069                 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Create stream info");
1070         }
1071
1072         ret = __create_ducking_handle();
1073         if (SOUND_MANAGER_ERROR_NONE != ret) {
1074                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to create ducking handle, ret(%d)", ret);
1075         } else {
1076                 SLOG(LOG_INFO, tts_tag(), "[Player SUCCESS] Create ducking handle");
1077         }
1078
1079         ecore_thread_max_set(1);
1080
1081         ret = __create_audio_out(TTSE_AUDIO_TYPE_RAW_S16, 16000);
1082         if (0 != ret) {
1083                 sound_manager_destroy_stream_information(g_stream_info_h);
1084                 g_stream_info_h = NULL;
1085                 pthread_mutex_unlock(&g_player_control_mutex);
1086
1087                 return -1;
1088         }
1089
1090         ecore_thread_run(__play_thread, __end_play_thread, NULL, NULL);
1091
1092         g_player_init = true;
1093
1094         pthread_mutex_unlock(&g_player_control_mutex);
1095
1096         return 0;
1097 }
1098
1099 static void __destroy_all_ducking_handles()
1100 {
1101         SLOG(LOG_INFO, tts_tag(), "[Player] Destroy stream ducking");
1102         if (__get_duration_from_last_volume_change() <= SND_MGR_DUCKING_DURATION) {
1103                 usleep(500000);
1104         }
1105
1106         __recover_background_volume();
1107
1108         int ret = -1;
1109         ret = sound_manager_destroy_stream_ducking(g_media_stream_ducking);
1110         if (SOUND_MANAGER_ERROR_NONE != ret) {
1111                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to destroy ducking handle, ret(%d)", ret);
1112         }
1113         g_media_stream_ducking = NULL;
1114
1115 /*      ret = sound_manager_destroy_stream_ducking(g_system_stream_ducking);
1116         if (SOUND_MANAGER_ERROR_NONE != ret) {
1117                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to destroy ducking handle, ret(%d)", ret);
1118         }
1119         g_system_stream_ducking = NULL;
1120 */
1121         ret = sound_manager_destroy_stream_ducking(g_notification_stream_ducking);
1122         if (SOUND_MANAGER_ERROR_NONE != ret) {
1123                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to destroy ducking handle, ret(%d)", ret);
1124         }
1125         g_notification_stream_ducking = NULL;
1126
1127         ret = sound_manager_destroy_stream_ducking(g_alarm_stream_ducking);
1128         if (SOUND_MANAGER_ERROR_NONE != ret) {
1129                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to destroy ducking handle, ret(%d)", ret);
1130         }
1131         g_alarm_stream_ducking = NULL;
1132 }
1133
1134 int ttsd_player_release(void)
1135 {
1136 #ifdef BUF_SAVE_MODE
1137         __close_buffer_dump_file();
1138 #endif
1139
1140         __set_playing_status(false);
1141         pthread_mutex_lock(&g_player_control_mutex);
1142         if (false == g_player_init) {
1143                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1144                 pthread_mutex_unlock(&g_player_control_mutex);
1145                 return TTSD_ERROR_OPERATION_FAILED;
1146         }
1147
1148         int ret;
1149
1150         SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] @@@@@");
1151         SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Active thread count : %d", ecore_thread_active_get());
1152         SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] @@@@@");
1153
1154         /* The thread should be released */
1155         int thread_count = ecore_thread_active_get();
1156         int count = 0;
1157         while (0 < thread_count) {
1158                 usleep(10000);
1159
1160                 count++;
1161                 if (20 == count) {
1162                         SLOG(LOG_WARN, tts_tag(), "[Player WARNING!!] Thread is blocked. Player release continue.");
1163                         break;
1164                 }
1165
1166                 thread_count = ecore_thread_active_get();
1167         }
1168
1169         SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Thread is released");
1170
1171         ret = __destroy_audio_out();
1172         if (0 != ret) {
1173                 pthread_mutex_unlock(&g_player_control_mutex);
1174                 return -1;
1175         }
1176
1177         ret = sound_manager_destroy_stream_information(g_stream_info_h);
1178         if (SOUND_MANAGER_ERROR_NONE != ret) {
1179                 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to destroy stream info");
1180         } else {
1181                 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Destroy stream info");
1182         }
1183
1184         __destroy_all_ducking_handles();
1185
1186         /* clear g_player_list */
1187         g_playing_info = NULL;
1188         g_player_init = false;
1189         pthread_cond_broadcast(&g_play_thread_cond);
1190
1191         g_stream_info_h = NULL;
1192
1193         pthread_mutex_unlock(&g_player_control_mutex);
1194
1195         return 0;
1196 }
1197
1198 int ttsd_player_create_instance(unsigned int uid)
1199 {
1200         if (false == g_player_init) {
1201                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1202                 return TTSD_ERROR_OPERATION_FAILED;
1203         }
1204
1205         /* Check uid is duplicated */
1206         if (NULL != __player_get_item(uid)) {
1207                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%u) is already registered", uid);
1208                 return -1;
1209         }
1210
1211         player_s* new_client = (player_s*)calloc(1, sizeof(player_s));
1212         if (NULL == new_client) {
1213                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to allocate memory");
1214                 return TTSE_ERROR_OUT_OF_MEMORY;
1215         }
1216
1217         new_client->uid = uid;
1218         new_client->event = TTSE_RESULT_EVENT_FINISH;
1219         new_client->state = APP_STATE_READY;
1220         new_client->is_paused_data = false;
1221         new_client->idx = 0;
1222         new_client->paused_data = NULL;
1223
1224         SECURE_SLOG(LOG_DEBUG, tts_tag(), "[Player] Create player : uid(%u)", uid);
1225
1226         g_player_list = g_list_append(g_player_list, new_client);
1227
1228         return 0;
1229 }
1230
1231 int ttsd_player_destroy_instance(unsigned int uid)
1232 {
1233         pthread_mutex_lock(&g_player_control_mutex);
1234         if (false == g_player_init) {
1235                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1236                 pthread_mutex_unlock(&g_player_control_mutex);
1237                 return TTSD_ERROR_OPERATION_FAILED;
1238         }
1239
1240         player_s* current;
1241         current = __player_get_item(uid);
1242         if (NULL == current) {
1243                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%u) is not valid", uid);
1244                 pthread_mutex_unlock(&g_player_control_mutex);
1245                 return -1;
1246         }
1247
1248         if (NULL != g_playing_info) {
1249                 if (uid == g_playing_info->uid) {
1250                         g_playing_info = NULL;
1251                 }
1252         }
1253
1254         GList *iter = NULL;
1255         player_s *data = NULL;
1256
1257         if (0 < g_list_length(g_player_list)) {
1258                 /* Get a first item */
1259                 iter = g_list_first(g_player_list);
1260
1261                 while (NULL != iter) {
1262                         /* Get handle data from list */
1263                         data = (player_s*)iter->data;
1264
1265                         if (NULL != data) {
1266                                 /* compare uid */
1267                                 if (uid == data->uid) {
1268                                         g_player_list = g_list_remove_link(g_player_list, iter);
1269                                         free(data);
1270                                         data = NULL;
1271                                         g_list_free(iter);
1272                                         break;
1273                                 }
1274                         }
1275
1276                         /* Get next item */
1277                         iter = g_list_next(iter);
1278                 }
1279         }
1280         pthread_mutex_unlock(&g_player_control_mutex);
1281
1282         SLOG(LOG_DEBUG, tts_tag(), "[PLAYER Success] Destroy instance");
1283
1284         return 0;
1285 }
1286
1287 int ttsd_player_play(unsigned int uid)
1288 {
1289         pthread_mutex_lock(&g_player_control_mutex);
1290         if (false == g_player_init) {
1291                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1292                 pthread_mutex_unlock(&g_player_control_mutex);
1293                 return TTSD_ERROR_OPERATION_FAILED;
1294         }
1295
1296         if (NULL != g_playing_info) {
1297                 if (uid == g_playing_info->uid) {
1298                         SLOG(LOG_DEBUG, tts_tag(), "[Player] uid(%u) has already played", g_playing_info->uid);
1299                         pthread_mutex_unlock(&g_player_control_mutex);
1300                         return 0;
1301                 } else {
1302                         SLOG(LOG_WARN, tts_tag(), "[Player WARNING] stop old player (%u)", g_playing_info->uid);
1303                         pthread_mutex_unlock(&g_player_control_mutex);
1304                         ttsd_player_stop(g_playing_info->uid);
1305                         pthread_mutex_lock(&g_player_control_mutex);
1306                 }
1307         }
1308
1309         SLOG(LOG_DEBUG, tts_tag(), "[Player] start play : uid(%u)", uid);
1310
1311         /* Check uid */
1312         player_s* current;
1313         current = __player_get_item(uid);
1314         if (NULL == current) {
1315                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%u) is not valid", uid);
1316                 pthread_mutex_unlock(&g_player_control_mutex);
1317                 return -1;
1318         }
1319
1320         current->state = APP_STATE_PLAYING;
1321         g_playing_info = current;
1322
1323         SLOG(LOG_INFO, tts_tag(), "[Player] Run thread");
1324         pthread_cond_broadcast(&g_play_thread_cond);
1325
1326         pthread_mutex_unlock(&g_player_control_mutex);
1327         return 0;
1328 }
1329
1330 int ttsd_player_stop(unsigned int uid)
1331 {
1332         pthread_mutex_lock(&g_player_control_mutex);
1333         if (false == g_player_init) {
1334                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1335                 pthread_mutex_unlock(&g_player_control_mutex);
1336                 return TTSD_ERROR_OPERATION_FAILED;
1337         }
1338
1339         /* check whether uid is current playing or not */
1340         if (NULL != g_playing_info) {
1341                 if (uid == g_playing_info->uid) {
1342                         /* release current playing info */
1343                         g_playing_info = NULL;
1344                 }
1345         } else {
1346                 SLOG(LOG_DEBUG, tts_tag(), "[Player] No current playing");
1347         }
1348
1349         if (NULL == g_playing_info) {
1350                 pthread_mutex_lock(&g_play_thread_mutex);
1351                 SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
1352                 pthread_mutex_unlock(&g_play_thread_mutex);
1353         }
1354
1355 #ifdef BUF_SAVE_MODE
1356         __close_buffer_dump_file();
1357 #endif
1358
1359         __set_playing_status(false);
1360         int ret = ttsd_player_clear(uid);
1361         if (0 != ret) {
1362                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to stop player, ret(%d)", ret);
1363                 pthread_mutex_unlock(&g_player_control_mutex);
1364                 return ret;
1365         }
1366
1367         SLOG(LOG_INFO, tts_tag(), "[Player SUCCESS] Stop player : uid(%u)", uid);
1368
1369         pthread_mutex_unlock(&g_player_control_mutex);
1370         return 0;
1371 }
1372
1373 int ttsd_player_clear(unsigned int uid)
1374 {
1375         if (false == g_player_init) {
1376                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1377                 return TTSD_ERROR_OPERATION_FAILED;
1378         }
1379
1380         /* Check uid */
1381         player_s* current;
1382         current = __player_get_item(uid);
1383         if (NULL == current) {
1384                 SECURE_SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%u) is not valid", uid);
1385                 return -1;
1386         }
1387
1388         if (true == current->is_paused_data) {
1389                 SLOG(LOG_INFO, tts_tag(), "[Player INFO] Clear paused data");
1390                 ttsd_data_destroy_sound_data(current->paused_data);
1391                 current->paused_data = NULL;
1392         }
1393
1394         current->event = TTSE_RESULT_EVENT_FINISH;
1395         current->state = APP_STATE_READY;
1396         current->is_paused_data = false;
1397         current->idx = 0;
1398
1399         SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Clear player : uid(%u)", uid);
1400
1401         return 0;
1402 }
1403
1404 int ttsd_player_pause(unsigned int uid)
1405 {
1406         pthread_mutex_lock(&g_player_control_mutex);
1407         SLOG(LOG_DEBUG, tts_tag(), "[Player] pause player : uid(%u)", uid);
1408
1409         if (false == g_player_init) {
1410                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1411                 pthread_mutex_unlock(&g_player_control_mutex);
1412                 return TTSD_ERROR_OPERATION_FAILED;
1413         }
1414
1415         /* Check uid */
1416         player_s* current;
1417         current = __player_get_item(uid);
1418         if (NULL == current) {
1419                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] ttsd_player_pause() : uid(%u) is not valid", uid);
1420                 pthread_mutex_unlock(&g_player_control_mutex);
1421                 return -1;
1422         }
1423
1424         /* check whether uid is current playing or not */
1425         if (NULL != g_playing_info) {
1426                 if (uid == g_playing_info->uid) {
1427                         /* release current playing info */
1428                         SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] release current playing info (%u)", uid);
1429                         g_playing_info = NULL;
1430                 } else {
1431                         /* error case */
1432                 }
1433         }
1434
1435         SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] current player (%p), g_playing_info(%p)", current, g_playing_info);
1436
1437         current->state = APP_STATE_PAUSED;
1438
1439         if (NULL == g_playing_info) {
1440                 pthread_mutex_lock(&g_play_thread_mutex);
1441                 SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
1442                 pthread_mutex_unlock(&g_play_thread_mutex);
1443         }
1444
1445 #ifdef BUF_SAVE_MODE
1446         __close_buffer_dump_file();
1447 #endif
1448
1449         __set_playing_status(false);
1450         SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Pause player : uid(%u)", uid);
1451
1452         pthread_mutex_unlock(&g_player_control_mutex);
1453         return 0;
1454 }
1455
1456 int ttsd_player_resume(unsigned int uid)
1457 {
1458         pthread_mutex_lock(&g_player_control_mutex);
1459         SLOG(LOG_DEBUG, tts_tag(), "[Player] Resume player : uid(%u)", uid);
1460
1461         if (false == g_player_init) {
1462                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1463                 pthread_mutex_unlock(&g_player_control_mutex);
1464                 return TTSD_ERROR_OPERATION_FAILED;
1465         }
1466
1467         /* Check id */
1468         player_s* current;
1469         current = __player_get_item(uid);
1470         if (NULL == current) {
1471                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%u) is not valid", uid);
1472                 pthread_mutex_unlock(&g_player_control_mutex);
1473                 return -1;
1474         }
1475
1476         /* check current player */
1477         if (NULL != g_playing_info)
1478                 g_playing_info = NULL;
1479
1480         current->state = APP_STATE_PLAYING;
1481         g_playing_info = current;
1482
1483         SLOG(LOG_INFO, tts_tag(), "[Player] Resume to run thread");
1484         pthread_cond_broadcast(&g_play_thread_cond);
1485
1486         pthread_mutex_unlock(&g_player_control_mutex);
1487         return 0;
1488 }
1489
1490 int ttsd_player_all_stop()
1491 {
1492         pthread_mutex_lock(&g_player_control_mutex);
1493         if (false == g_player_init) {
1494                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1495                 pthread_mutex_unlock(&g_player_control_mutex);
1496                 return TTSD_ERROR_OPERATION_FAILED;
1497         }
1498
1499         g_playing_info = NULL;
1500
1501         GList *iter = NULL;
1502         player_s *data = NULL;
1503
1504         if (0 < g_list_length(g_player_list)) {
1505                 /* Get a first item */
1506                 iter = g_list_first(g_player_list);
1507
1508                 while (NULL != iter) {
1509                         /* Get handle data from list */
1510                         data = (player_s*)iter->data;
1511
1512                         app_tts_state_e state;
1513                         if (0 > ttsd_data_get_client_state(data->uid, &state)) {
1514                                 SLOG(LOG_ERROR, tts_tag(), "[player ERROR] uid(%u) is not valid", data->uid);
1515                                 iter = g_list_next(iter);
1516
1517                                 pthread_mutex_unlock(&g_player_control_mutex);
1518                                 ttsd_player_destroy_instance(data->uid);
1519                                 pthread_mutex_lock(&g_player_control_mutex);
1520                                 continue;
1521                         }
1522
1523                         if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1524                                 data->event = TTSE_RESULT_EVENT_FINISH;
1525                                 data->state = APP_STATE_READY;
1526
1527                                 if (true == data->is_paused_data) {
1528                                         SLOG(LOG_INFO, tts_tag(), "[Player INFO] Clear paused data");
1529                                         ttsd_data_destroy_sound_data(data->paused_data);
1530                                         data->paused_data = NULL;
1531                                 }
1532
1533                                 data->is_paused_data = false;
1534                                 data->idx = 0;
1535                         }
1536
1537                         /* Get next item */
1538                         iter = g_list_next(iter);
1539                 }
1540         }
1541
1542         SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] player all stop!!");
1543
1544         pthread_mutex_unlock(&g_player_control_mutex);
1545         return 0;
1546 }
1547
1548 int ttsd_player_get_background_volume_ratio(double* ratio)
1549 {
1550         if (false == g_player_init) {
1551                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1552                 return TTSD_ERROR_OPERATION_FAILED;
1553         }
1554
1555         if (NULL == ratio)
1556                 return -1;
1557         else
1558                 *ratio = g_bg_volume_ratio;
1559
1560         return TTSD_ERROR_NONE;
1561 }
1562
1563 static Eina_Bool __modify_background_volume_async(void* data)
1564 {
1565         __recover_background_volume();
1566         __change_background_volume(0);
1567         SLOG(LOG_INFO, tts_tag(), "[BG] Modify background volume with delay");
1568
1569         g_modify_background_volume = NULL;
1570         return EINA_FALSE;
1571 }
1572
1573 static void __modify_background_volume(void* data)
1574 {
1575         if (NULL != g_delayed_unset_policy_timer) {
1576                 SLOG(LOG_INFO, tts_tag(), "[BG] Background volume is going to recover soon. Skip modification");
1577                 return;
1578         }
1579
1580         if (NULL != g_modify_background_volume) {
1581                 int result = (intptr_t)ecore_timer_del(g_modify_background_volume);
1582                 g_modify_background_volume = NULL;
1583                 SLOG(LOG_ERROR, tts_tag(), "[BG] Remove modify background volume timer (%d)", result);
1584         }
1585
1586         long long int diff = __get_duration_from_last_volume_change();
1587         if (diff > SND_MGR_DUCKING_DURATION) {
1588                 __recover_background_volume();
1589                 __change_background_volume(0);
1590                 SLOG(LOG_INFO, tts_tag(), "[BG] Direct modify background volume");
1591         } else {
1592                 double delay = (double)(SND_MGR_DUCKING_DURATION - diff) / 1000.0;
1593                 g_modify_background_volume = ecore_timer_add(delay, __modify_background_volume_async, (void*)CHECK_TIMER_DELETE);
1594                 SLOG(LOG_INFO, tts_tag(), "[BG] Delay modifying background volume (%p), delay(%f)", g_modify_background_volume, delay);
1595         }
1596 }
1597
1598 int ttsd_player_set_background_volume_ratio(double ratio)
1599 {
1600         if (false == g_player_init) {
1601                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1602                 return TTSD_ERROR_OPERATION_FAILED;
1603         }
1604
1605         SLOG(LOG_INFO, tts_tag(), "[Player DEBUG] ttsd_player_set_background_volume_ratio : %lf", ratio);
1606
1607         double prev_ratio = g_bg_volume_ratio;
1608         g_bg_volume_ratio = ratio;
1609
1610         SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Check whether sound is ducked and Change volume. as-is(%lf), to-be(%lf)", prev_ratio, g_bg_volume_ratio);
1611         if (prev_ratio == g_bg_volume_ratio) {
1612                 return TTSD_ERROR_NONE;
1613         }
1614
1615         if (g_is_set_policy) {
1616                 SLOG(LOG_INFO, tts_tag(), "[BG] Direct modify background volume");
1617                 ecore_main_loop_thread_safe_call_async(__modify_background_volume, NULL);
1618         }
1619
1620         return TTSD_ERROR_NONE;
1621 }
1622
1623 int ttsd_player_wait_to_play(unsigned int uid)
1624 {
1625         if (false == g_player_init) {
1626                 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1627                 return TTSD_ERROR_OPERATION_FAILED;
1628         }
1629
1630         SLOG(LOG_INFO, tts_tag(), "[Player INFO] wait to play (%u)", uid);
1631
1632         g_audio_state = AUDIO_STATE_WAIT_FOR_PLAYING;
1633
1634         return TTSD_ERROR_NONE;
1635 }