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.
16 #include <sound_manager.h>
17 #include <sound_manager_internal.h>
19 #include "ttsd_main.h"
20 #include "ttsd_player.h"
21 #include "ttsd_data.h"
22 #include "ttsd_dbus.h"
25 * Internal data structure
35 int uid; /** client id */
36 app_state_e state; /** client state */
38 /* Current utterance information */
39 ttse_result_event_e event; /** event of last utterance */
43 sound_data_s* paused_data;
46 #define SOUND_BUFFER_LENGTH 2048
47 #define FOCUS_SERVER_READY "/tmp/.sound_server_ready"
49 /** player init info */
50 static bool g_player_init = false;
53 static GList *g_player_list;
55 /** current player information */
56 static player_s* g_playing_info;
59 static audio_state_e g_audio_state;
61 static ttse_audio_type_e g_audio_type;
63 static int g_sampling_rate;
65 static audio_out_h g_audio_h;
67 static sound_stream_info_h g_stream_info_h;
73 player_s* __player_get_item(int uid)
76 player_s *data = NULL;
78 if (0 < g_list_length(g_player_list)) {
79 /* Get a first item */
80 iter = g_list_first(g_player_list);
82 while (NULL != iter) {
83 /* Get handle data from list */
84 data = (player_s*)iter->data;
91 iter = g_list_next(iter);
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)
101 SLOG(LOG_DEBUG, tts_tag(), "===== Focus state changed cb");
103 if (stream_info != g_stream_info_h) {
104 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Invalid stream info handle");
107 SLOG(LOG_WARN, tts_tag(), "[Player] focus state changed to (%d) with reason(%d)", (int)focus_state, (int)reason_for_change);
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;
113 if (NULL == g_playing_info) {
114 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] No current player");
118 if (APP_STATE_PLAYING == g_playing_info->state) {
119 int uid = g_playing_info->uid;
121 if (0 != ttsd_player_pause(uid)) {
122 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to pause the player");
126 ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
127 int pid = ttsd_data_get_pid(uid);
128 /* send message to client about changing state */
129 ttsdc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
132 SLOG(LOG_DEBUG, tts_tag(), "[Player] Ignore focus state cb - mode(%d)", ttsd_get_mode());
136 SLOG(LOG_DEBUG, tts_tag(), "=====");
137 SLOG(LOG_DEBUG, tts_tag(), "");
142 static int __create_audio_out(ttse_audio_type_e type, int rate)
145 audio_sample_type_e sample_type;
147 if (TTSE_AUDIO_TYPE_RAW_S16 == type) {
148 sample_type = AUDIO_SAMPLE_TYPE_S16_LE;
150 sample_type = AUDIO_SAMPLE_TYPE_U8;
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;
157 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to create audio");
160 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Create audio");
164 g_sampling_rate = rate;
166 g_audio_state = AUDIO_STATE_READY;
171 static int __destroy_audio_out()
173 if (NULL == g_audio_h) {
174 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Current handle is not valid");
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");
184 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Destroy audio");
190 g_audio_state = AUDIO_STATE_NONE;
196 static void __end_play_thread(void *data, Ecore_Thread *thread)
198 SLOG(LOG_ERROR, tts_tag(), "===== End thread");
201 static void __set_policy_for_playing(int volume)
203 /* Set stream info */
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");
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");
219 static void __unset_policy_for_playing()
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);
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");
241 static void __play_thread(void *data, Ecore_Thread *thread)
243 SLOG(LOG_DEBUG, tts_tag(), "===== Start thread");
245 if (NULL == g_playing_info) {
246 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] No current player");
250 player_s* player = g_playing_info;
251 sound_data_s* sound_data = NULL;
254 int len = SOUND_BUFFER_LENGTH;
257 /* set volume policy as 40% */
258 __set_policy_for_playing(40);
260 if (true == player->is_paused_data) {
262 sound_data = player->paused_data;
263 player->paused_data = NULL;
267 player->is_paused_data = false;
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);
276 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
279 g_audio_state = AUDIO_STATE_READY;
281 /* unset volume policy, volume will be 100% */
282 __unset_policy_for_playing();
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);
289 ret = ttsd_data_get_sound_data(player->uid, &sound_data);
290 if (0 != ret || NULL == sound_data) {
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);
298 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
300 g_audio_state = AUDIO_STATE_READY;
302 /* unset volume policy, volume will be 100% */
303 __unset_policy_for_playing();
305 /* wait for new audio data come */
308 if (NULL == g_playing_info) {
309 /* current playing uid is replaced */
310 SLOG(LOG_INFO, tts_tag(), "[Player] Finish thread");
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");
319 SLOG(LOG_INFO, tts_tag(), "[Player] Finish to wait for new audio data come");
321 /* set volume policy as 40%, when resume play thread*/
322 __set_policy_for_playing(40);
324 /* resume play thread */
325 player->state = APP_STATE_PLAYING;
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.
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);
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();
343 if (NULL != sound_data->data) {
344 free(sound_data->data);
345 sound_data->data = NULL;
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);
356 SLOG(LOG_INFO, tts_tag(), "[Player] Start utterance : uid(%d), uttid(%d)", player->uid, sound_data->utt_id);
359 /* Save last event to check utterance start */
360 player->event = sound_data->event;
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);
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;
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);
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;
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();
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();
408 if (NULL != sound_data->data) {
409 free(sound_data->data);
410 sound_data->data = NULL;
417 __set_policy_for_playing(40);
420 while (APP_STATE_PLAYING == player->state || APP_STATE_PAUSED == player->state) {
421 if ((unsigned int)idx >= sound_data->data_size)
424 if ((unsigned int)idx + SOUND_BUFFER_LENGTH > sound_data->data_size) {
425 len = sound_data->data_size - idx;
427 len = SOUND_BUFFER_LENGTH;
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();
439 if (NULL != sound_data->data) {
440 free(sound_data->data);
441 sound_data->data = NULL;
449 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Prepare audio");
450 g_audio_state = AUDIO_STATE_PLAY;
453 char* temp_data = sound_data->data;
454 ret = audio_out_write(g_audio_h, &temp_data[idx], len);
456 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to audio write - %d", ret);
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);
468 /* unset volume policy, volume will be 100% */
469 __unset_policy_for_playing();
471 if (NULL != sound_data) {
472 if (NULL != sound_data->data) {
473 free(sound_data->data);
474 sound_data->data = NULL;
484 if (APP_STATE_PAUSED == player->state) {
486 player->paused_data = sound_data;
488 player->is_paused_data = true;
491 g_audio_state = AUDIO_STATE_READY;
492 SLOG(LOG_INFO, tts_tag(), "[Player] Stop player thread by pause");
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);
499 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
501 /* unset volume policy, volume will be 100% */
502 __unset_policy_for_playing();
507 if (NULL == g_playing_info && APP_STATE_READY == player->state) {
509 g_audio_state = AUDIO_STATE_READY;
510 SLOG(LOG_DEBUG, tts_tag(), "[Player] Stop player thread");
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);
517 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
520 if (NULL != sound_data) {
521 if (NULL != sound_data->data) {
522 free(sound_data->data);
523 sound_data->data = NULL;
529 /* unset volume policy, volume will be 100% */
530 __unset_policy_for_playing();
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);
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;
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;
568 SLOG(LOG_INFO, tts_tag(), "[Player] Finish utterance : uid(%d), uttid(%d)", player->uid, sound_data->utt_id);
571 if (NULL != sound_data) {
572 if (NULL != sound_data->data) {
573 free(sound_data->data);
574 sound_data->data = NULL;
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);
588 /* unset volume policy, volume will be 100% */
589 __unset_policy_for_playing();
599 int ttsd_player_init()
601 g_playing_info = NULL;
602 g_audio_state = AUDIO_STATE_NONE;
607 ecore_thread_max_set(1);
611 if (0 == access(FOCUS_SERVER_READY, F_OK)) {
612 SLOG(LOG_ERROR, tts_tag(), "[Player SUCCESS] focus server is available");
616 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] focus server is not available");
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");
626 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Create stream info");
629 ecore_thread_max_set(1);
631 ret = __create_audio_out(TTSE_AUDIO_TYPE_RAW_S16, 16000);
635 g_player_init = true;
640 int ttsd_player_release(void)
642 if (false == g_player_init) {
643 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
644 return TTSD_ERROR_OPERATION_FAILED;
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] ==========================");
653 /* The thread should be released */
654 int thread_count = ecore_thread_active_get();
656 while (0 < thread_count) {
661 SLOG(LOG_WARN, tts_tag(), "[Player WARNING!!] Thread is blocked. Player release continue.");
665 thread_count = ecore_thread_active_get();
668 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Thread is released");
670 ret = __destroy_audio_out();
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");
678 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Destroy stream info");
681 /* clear g_player_list */
682 g_playing_info = NULL;
683 g_player_init = false;
688 int ttsd_player_create_instance(int uid)
690 if (false == g_player_init) {
691 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
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);
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;
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;
712 new_client->paused_data = NULL;
714 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[Player] Create player : uid(%d)", uid);
716 g_player_list = g_list_append(g_player_list, new_client);
721 int ttsd_player_destroy_instance(int uid)
723 if (false == g_player_init) {
724 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
729 current = __player_get_item(uid);
730 if (NULL == current) {
731 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
735 if (NULL != g_playing_info) {
736 if (uid == g_playing_info->uid) {
737 g_playing_info = NULL;
742 player_s *data = NULL;
744 if (0 < g_list_length(g_player_list)) {
745 /* Get a first item */
746 iter = g_list_first(g_player_list);
748 while (NULL != iter) {
749 /* Get handle data from list */
750 data = (player_s*)iter->data;
754 if (uid == data->uid) {
755 g_player_list = g_list_remove_link(g_player_list, iter);
763 iter = g_list_next(iter);
767 SLOG(LOG_DEBUG, tts_tag(), "[PLAYER Success] Destroy instance");
772 int ttsd_player_play(int uid)
774 if (false == g_player_init) {
775 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
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);
784 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] stop old player (%d)", g_playing_info->uid);
785 ttsd_player_stop(g_playing_info->uid);
789 SLOG(LOG_DEBUG, tts_tag(), "[Player] start play : uid(%d)", uid);
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);
799 current = __player_get_item(uid);
800 if (NULL == current) {
801 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
805 current->state = APP_STATE_PLAYING;
807 g_playing_info = current;
809 SLOG(LOG_INFO, tts_tag(), "[Player DEBUG] Active thread count : %d", ecore_thread_active_get());
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);
819 int ttsd_player_stop(int uid)
821 if (false == g_player_init) {
822 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
828 current = __player_get_item(uid);
829 if (NULL == current) {
830 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
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;
841 SLOG(LOG_DEBUG, tts_tag(), "[Player] No current playing");
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;
851 free(current->paused_data);
852 current->paused_data = NULL;
856 current->event = TTSE_RESULT_EVENT_FINISH;
857 current->state = APP_STATE_READY;
858 current->is_paused_data = false;
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] ==========================");
866 /* The thread should be released */
867 int thread_count = ecore_thread_active_get();
869 while (0 < thread_count) {
874 SLOG(LOG_WARN, tts_tag(), "[Player WARNING!!] Thread is blocked. Player release continue.");
878 thread_count = ecore_thread_active_get();
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] ==========================");
886 SLOG(LOG_INFO, tts_tag(), "[Player SUCCESS] Stop player : uid(%d)", uid);
891 int ttsd_player_clear(int uid)
893 if (false == g_player_init) {
894 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
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);
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;
913 free(current->paused_data);
914 current->paused_data = NULL;
918 current->event = TTSE_RESULT_EVENT_FINISH;
919 current->state = APP_STATE_READY;
920 current->is_paused_data = false;
923 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Clear player : uid(%d)", uid);
928 int ttsd_player_pause(int uid)
930 SLOG(LOG_DEBUG, tts_tag(), "[Player] pause player : uid(%d)", uid);
932 if (false == g_player_init) {
933 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
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);
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;
955 current->state = APP_STATE_PAUSED;
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] ==========================");
961 /* The thread should be released */
962 int thread_count = ecore_thread_active_get();
964 while (0 < thread_count) {
969 SLOG(LOG_WARN, tts_tag(), "[Player WARNING!!] Thread is blocked. Player release continue.");
973 thread_count = ecore_thread_active_get();
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] ==========================");
980 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Pause player : uid(%d)", uid);
985 int ttsd_player_resume(int uid)
987 SLOG(LOG_DEBUG, tts_tag(), "[Player] Resume player : uid(%d)", uid);
989 if (false == g_player_init) {
990 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
996 current = __player_get_item(uid);
997 if (NULL == current) {
998 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
1002 /* check current player */
1003 if (NULL != g_playing_info)
1004 g_playing_info = NULL;
1006 current->state = APP_STATE_PLAYING;
1007 g_playing_info = current;
1009 SLOG(LOG_INFO, tts_tag(), "[Player] Resume to run thread");
1010 ecore_thread_run(__play_thread, __end_play_thread, NULL, NULL);
1015 int ttsd_player_all_stop()
1017 if (false == g_player_init) {
1018 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1022 g_playing_info = NULL;
1025 player_s *data = NULL;
1027 if (0 < g_list_length(g_player_list)) {
1028 /* Get a first item */
1029 iter = g_list_first(g_player_list);
1031 while (NULL != iter) {
1032 /* Get handle data from list */
1033 data = (player_s*)iter->data;
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);
1043 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1044 data->event = TTSE_RESULT_EVENT_FINISH;
1045 data->state = APP_STATE_READY;
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;
1054 free(data->paused_data);
1055 data->paused_data = NULL;
1059 data->is_paused_data = false;
1064 iter = g_list_next(iter);
1068 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] player all stop!!");