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"
24 #include "tts_internal.h"
25 #include "ttsd_server.h"
28 * Internal data structure
38 int uid; /** client id */
39 app_tts_state_e state; /** client state */
41 /* Current utterance information */
42 ttse_result_event_e event; /** event of last utterance */
46 sound_data_s* paused_data;
49 #define SOUND_BUFFER_LENGTH 2048
50 #define FOCUS_SERVER_READY "/tmp/.sound_server_ready"
52 /* Sound buf save for test */
57 static char g_temp_file_name[128] = {'\0',};
61 static int g_count = 1;
64 /** player init info */
65 static bool g_player_init = false;
68 static GList *g_player_list;
70 /** current player information */
71 static player_s* g_playing_info;
74 static audio_state_e g_audio_state;
76 static ttse_audio_type_e g_audio_type;
78 static int g_sampling_rate;
80 static audio_out_h g_audio_h;
82 static sound_stream_info_h g_stream_info_h;
84 static sound_stream_ducking_h g_media_stream_ducking;
85 static sound_stream_ducking_h g_system_stream_ducking;
86 static sound_stream_ducking_h g_notification_stream_ducking;
87 static sound_stream_ducking_h g_alarm_stream_ducking;
89 // static bool ducking_flag;
91 #define SND_MGR_DUCKING_DURATION 500
93 static int g_focus_watch_id;
95 static double g_bg_volume_ratio;
101 player_s* __player_get_item(int uid)
104 player_s *data = NULL;
106 if (0 < g_list_length(g_player_list)) {
107 /* Get a first item */
108 iter = g_list_first(g_player_list);
110 while (NULL != iter) {
111 /* Get handle data from list */
112 data = (player_s*)iter->data;
115 if (uid == data->uid)
119 iter = g_list_next(iter);
126 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,
127 sound_stream_focus_change_reason_e reason_for_change, int sound_behavior, const char *extra_info, void *user_data)
129 SLOG(LOG_DEBUG, tts_tag(), "@@@ Focus state changed cb");
131 if (stream_info != g_stream_info_h) {
132 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Invalid stream info handle");
135 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);
137 if (NULL == g_playing_info) {
138 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] No current player");
142 if (APP_STATE_PLAYING == g_playing_info->state && focus_mask == SOUND_STREAM_FOCUS_FOR_PLAYBACK && SOUND_STREAM_FOCUS_STATE_RELEASED == focus_state) {
143 if (TTSD_MODE_DEFAULT == ttsd_get_mode()) {
144 g_audio_state = AUDIO_STATE_READY;
146 int uid = g_playing_info->uid;
148 if (0 != ttsd_player_pause(uid)) {
149 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to pause the player");
153 ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
154 int pid = ttsd_data_get_pid(uid);
155 /* send message to client about changing state */
156 ttsdc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
158 SLOG(LOG_DEBUG, tts_tag(), "[Player] Ignore focus state cb - mode(%d)", ttsd_get_mode());
162 /* if (AUDIO_STATE_READY == g_audio_state && focus_mask == SOUND_STREAM_FOCUS_FOR_PLAYBACK && SOUND_STREAM_FOCUS_STATE_ACQUIRED == focus_state) {
163 if (TTSD_MODE_DEFAULT == ttsd_get_mode()) {
164 g_audio_state = AUDIO_STATE_PLAY;
166 if (NULL == g_playing_info) {
167 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] No current player");
171 if (APP_STATE_PAUSED == g_playing_info->state) {
172 int uid = g_playing_info->uid;
174 g_audio_state = AUDIO_STATE_PLAY;
175 if (0 != ttsd_player_resume(uid)) {
176 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to resume the player");
177 g_audio_state = AUDIO_STATE_READY;
181 ttsd_data_set_client_state(uid, APP_STATE_PLAYING);
182 int pid = ttsd_data_get_pid(uid);
183 ttsdc_send_set_state_message(pid, uid, APP_STATE_PLAYING);
187 SLOG(LOG_DEBUG, tts_tag(), "[Player] Ignore focus state cb - mode(%d)", ttsd_get_mode());
191 SLOG(LOG_DEBUG, tts_tag(), "@@@");
196 void __player_focus_state_watch_cb(int id, sound_stream_focus_mask_e focus_mask, sound_stream_focus_state_e focus_state, sound_stream_focus_change_reason_e reason,
197 const char *extra_info, void *user_data)
199 SLOG(LOG_DEBUG, tts_tag(), "@@@ Focus state watch cb");
201 ttsd_mode_e mode = ttsd_get_mode();
203 if (TTSD_MODE_SCREEN_READER != mode && TTSD_MODE_NOTIFICATION != mode) {
204 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] This is not screen-reader mode and notification mode.");
208 if (NULL == g_playing_info) {
209 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] No current player");
213 if (APP_STATE_PLAYING == g_playing_info->state && SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_INFORMATION == reason &&
214 NULL != extra_info && 0 == strncmp(extra_info, "TTSD_MODE_INTERRUPT", strlen(extra_info))) {
215 /* If the focus is changed by "Interrupt" mode and current players of "SR" and "Noti" modes are on going, please stop the current players. */
216 g_audio_state = AUDIO_STATE_READY;
218 int uid = g_playing_info->uid;
220 if (0 != ttsd_server_stop(uid)) {
221 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to stop TTS server");
224 if (0 != ttsd_player_stop(uid)) {
225 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to stop the player");
229 ttsd_data_set_client_state(uid, APP_STATE_READY);
230 int pid = ttsd_data_get_pid(uid);
231 /* send message to client about changing state */
232 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
234 SLOG(LOG_DEBUG, tts_tag(), "[Player] Extra info is not Interrupt mode(%s) or not playing state(%d).", extra_info, g_playing_info->state);
240 void __sound_stream_ducking_state_changed_cb(sound_stream_ducking_h stream_ducking, bool is_ducked, void *user_data)
242 SLOG(LOG_DEBUG, tts_tag(), "@@@ ducking state changed cb");
243 SLOG(LOG_DEBUG, tts_tag(), "[Player] is ducked : %d", is_ducked);
244 // ducking_flag = true;
248 static char* __get_ducking_stream(sound_stream_type_e stream_type)
250 if (SOUND_STREAM_TYPE_MEDIA == stream_type)
251 return "Media stream";
252 else if (SOUND_STREAM_TYPE_SYSTEM == stream_type)
253 return "System stream";
254 else if (SOUND_STREAM_TYPE_NOTIFICATION == stream_type)
255 return "Notification stream";
256 else if (SOUND_STREAM_TYPE_ALARM == stream_type)
257 return "Alarm stream";
258 return "Non matched stream";
261 static int __activate_ducking_sound_stream(sound_stream_type_e stream_type, sound_stream_ducking_h stream_ducking_h)
263 bool is_ducked = false;
264 int ret = sound_manager_is_ducked(stream_ducking_h, &is_ducked);
266 SLOG(LOG_DEBUG, tts_tag(), "[Player] The %s is already ducked", __get_ducking_stream(stream_type));
268 ret = sound_manager_activate_ducking(stream_ducking_h, SND_MGR_DUCKING_DURATION, g_bg_volume_ratio);
269 if (SOUND_MANAGER_ERROR_NONE != ret) {
270 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to activate ducking for %s", __get_ducking_stream(stream_type));
272 SLOG(LOG_INFO, tts_tag(), "[Player SUCCESS] Activate ducking for %s", __get_ducking_stream(stream_type));
278 static void __change_background_volume()
280 SLOG(LOG_INFO, tts_tag(), "[Player] current ducking stream(%s), volume ratio(%d)", __get_ducking_stream(stream_type), g_bg_volume_ratio);
281 if (1.0 > g_bg_volume_ratio) {
282 __activate_ducking_sound_stream(SOUND_STREAM_TYPE_MEDIA, g_media_stream_ducking);
283 __activate_ducking_sound_stream(SOUND_STREAM_TYPE_SYSTEM, g_system_stream_ducking);
284 __activate_ducking_sound_stream(SOUND_STREAM_TYPE_NOTIFICATION, g_notification_stream_ducking);
285 __activate_ducking_sound_stream(SOUND_STREAM_TYPE_ALARM, g_alarm_stream_ducking);
289 static int __deactivate_ducking_sound_stream(sound_stream_type_e stream_type, sound_stream_ducking_h stream_ducking_h)
291 bool is_ducked = false;
292 int ret = sound_manager_is_ducked(stream_ducking_h, &is_ducked);
294 SLOG(LOG_DEBUG, tts_tag(), "[Player] The %s is already recovered from ducking", __get_ducking_stream(stream_type));
296 ret = sound_manager_deactivate_ducking(stream_ducking_h);
297 if (SOUND_MANAGER_ERROR_NONE != ret) {
298 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to deactivate ducking for %s", __get_ducking_stream(stream_type));
300 SLOG(LOG_INFO, tts_tag(), "[Player SUCCESS] Deactivate ducking for %s", __get_ducking_stream(stream_type));
306 static void __recover_background_volume()
308 __deactivate_ducking_sound_stream(SOUND_STREAM_TYPE_MEDIA, g_media_stream_ducking);
309 __deactivate_ducking_sound_stream(SOUND_STREAM_TYPE_SYSTEM, g_system_stream_ducking);
310 __deactivate_ducking_sound_stream(SOUND_STREAM_TYPE_NOTIFICATION, g_notification_stream_ducking);
311 __deactivate_ducking_sound_stream(SOUND_STREAM_TYPE_ALARM, g_alarm_stream_ducking);
314 static int __create_audio_out(ttse_audio_type_e type, int rate)
317 audio_sample_type_e sample_type;
319 if (TTSE_AUDIO_TYPE_RAW_S16 == type) {
320 sample_type = AUDIO_SAMPLE_TYPE_S16_LE;
322 sample_type = AUDIO_SAMPLE_TYPE_U8;
325 ret = audio_out_create_new(rate, AUDIO_CHANNEL_MONO, sample_type, &g_audio_h);
326 if (AUDIO_IO_ERROR_NONE != ret) {
327 g_audio_state = AUDIO_STATE_NONE;
329 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to create audio");
332 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Create audio");
336 g_sampling_rate = rate;
338 g_audio_state = AUDIO_STATE_READY;
343 static int __destroy_audio_out()
345 if (NULL == g_audio_h) {
346 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Current handle is not valid");
351 ret = audio_out_destroy(g_audio_h);
352 if (AUDIO_IO_ERROR_NONE != ret) {
353 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to destroy audio");
356 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Destroy audio");
362 g_audio_state = AUDIO_STATE_NONE;
368 static void __end_play_thread(void *data, Ecore_Thread *thread)
370 SLOG(LOG_ERROR, tts_tag(), "@@@ End thread");
377 static void __set_policy_for_playing(int volume)
380 __change_background_volume();
382 /* Set stream info */
384 ttsd_mode_e mode = ttsd_get_mode();
385 if (TTSD_MODE_DEFAULT == mode) {
386 ret = sound_manager_acquire_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, SOUND_BEHAVIOR_NONE, NULL);
387 if (SOUND_MANAGER_ERROR_NONE != ret) {
388 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to acquire focus");
390 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Success to acquire focus (default mode)");
392 } else if (TTSD_MODE_INTERRUPT == mode) {
393 ret = sound_manager_acquire_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, SOUND_BEHAVIOR_NONE, "TTSD_MODE_INTERRUPT");
394 if (SOUND_MANAGER_ERROR_NONE != ret) {
395 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to acquire focus");
397 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Success to acquire focus (interrupt mode)");
401 ret = audio_out_set_sound_stream_info(g_audio_h, g_stream_info_h);
402 if (AUDIO_IO_ERROR_NONE != ret) {
403 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to set stream info");
406 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] set policy for playing");
411 static void __unset_policy_for_playing()
414 ttsd_mode_e mode = ttsd_get_mode();
415 /* Unset stream info */
416 if (TTSD_MODE_DEFAULT == mode || TTSD_MODE_INTERRUPT == mode) {
417 sound_stream_focus_state_e state_for_playing = SOUND_STREAM_FOCUS_STATE_ACQUIRED;
418 ret = sound_manager_get_focus_state(g_stream_info_h, &state_for_playing, NULL);
419 if (SOUND_MANAGER_ERROR_NONE != ret) {
420 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to get focus state: %d", ret);
423 if (SOUND_STREAM_FOCUS_STATE_ACQUIRED == state_for_playing) {
424 if (TTSD_MODE_DEFAULT == mode || TTSD_MODE_INTERRUPT == mode) {
425 ret = sound_manager_release_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, SOUND_BEHAVIOR_NONE, NULL);
426 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] release focus (mode: %d)", mode);
429 if (SOUND_MANAGER_ERROR_NONE != ret) {
430 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to release focus");
435 __recover_background_volume();
437 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] unset policy for playing");
442 int ttsd_player_check_current_playback_focus(bool *is_current_interrupt)
445 ttsd_mode_e mode = ttsd_get_mode();
447 if (TTSD_MODE_INTERRUPT != mode) {
448 /* check the current playback focus */
449 sound_stream_focus_change_reason_e reason;
450 int sound_behavior = 0;
451 char *extra_info = NULL;
453 ret = sound_manager_get_current_playback_focus(&reason, &sound_behavior, &extra_info);
455 SLOG(LOG_DEBUG, tts_tag(), "[Player] current playback focus: extra_info(%s), reason(%d), sound_behavior(%d)", extra_info, reason, sound_behavior);
457 if (SOUND_MANAGER_ERROR_NONE == ret && NULL != extra_info && 0 < strlen(extra_info)) {
458 if (SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_INFORMATION == reason && 0 == strncmp(extra_info, "TTSD_MODE_INTERRUPT", strlen(extra_info))) {
459 SLOG(LOG_DEBUG, tts_tag(), "[Player] The current focus in Interrupt. Cannot play the requested sound data");
460 *is_current_interrupt = true;
465 return TTSD_ERROR_NONE;
469 if (NULL != extra_info) {
475 *is_current_interrupt = false;
477 return TTSD_ERROR_NONE;
480 static void __play_thread(void *data, Ecore_Thread *thread)
482 SLOG(LOG_DEBUG, tts_tag(), "@@@ Start thread");
488 snprintf(g_temp_file_name, sizeof(g_temp_file_name), "/tmp/tts_temp_%d_%d", getpid(), g_count);
489 int ret = access(g_temp_file_name, 0);
492 SLOG(LOG_ERROR, tts_tag(), "[Recorder ERROR] File is already exist");
493 if (0 == remove(g_temp_file_name)) {
494 SLOG(LOG_DEBUG, tts_tag(), "[Recorder] Remove file");
504 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[Recorder] Temp file name=[%s]", g_temp_file_name);
507 g_pFile = fopen(g_temp_file_name, "wb+x");
509 SLOG(LOG_ERROR, tts_tag(), "[Recorder ERROR] File not found!");
514 if (NULL == g_playing_info) {
515 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] No current player");
519 player_s* player = g_playing_info;
520 sound_data_s* sound_data = NULL;
523 int len = SOUND_BUFFER_LENGTH;
526 /* set volume policy as 40% */
527 __set_policy_for_playing(40);
529 if (true == player->is_paused_data) {
531 sound_data = player->paused_data;
532 player->paused_data = NULL;
536 player->is_paused_data = false;
539 if (NULL == sound_data) {
540 /* Request unprepare */
541 ret = audio_out_unprepare(g_audio_h);
542 if (AUDIO_IO_ERROR_NONE != ret) {
543 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
545 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
548 g_audio_state = AUDIO_STATE_READY;
550 /* unset volume policy, volume will be 100% */
551 __unset_policy_for_playing();
554 SLOG(LOG_INFO, tts_tag(), "[Player] Sound info : id(%d) data(%p) size(%d) audiotype(%d) rate(%d) event(%d)",
555 sound_data->utt_id, sound_data->data, sound_data->data_size, sound_data->audio_type, sound_data->rate, sound_data->event);
558 ret = ttsd_data_get_sound_data(player->uid, &sound_data);
559 if (0 != ret || NULL == sound_data) {
561 SLOG(LOG_DEBUG, tts_tag(), "[Player] No sound data. Waiting mode");
563 /* wait for new audio data come */
566 if (NULL == g_playing_info) {
567 /* current playing uid is replaced */
568 SLOG(LOG_INFO, tts_tag(), "[Player] Finish thread");
569 if (AUDIO_STATE_PLAY == g_audio_state) {
570 /* release audio & recover session */
571 ret = audio_out_unprepare(g_audio_h);
572 if (AUDIO_IO_ERROR_NONE != ret) {
573 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
575 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
577 g_audio_state = AUDIO_STATE_READY;
579 /* unset volume policy, volume will be 100% */
580 __unset_policy_for_playing();
582 } else if (0 < ttsd_data_get_sound_data_size(player->uid)) {
583 /* new audio data come */
584 SLOG(LOG_INFO, tts_tag(), "[Player] Resume thread");
588 /* If engine is not on processing */
589 if (TTSD_SYNTHESIS_CONTROL_DOING != ttsd_get_synth_control()) {
590 if (AUDIO_STATE_PLAY == g_audio_state) {
591 /* release audio & recover session */
592 ret = audio_out_unprepare(g_audio_h);
593 if (AUDIO_IO_ERROR_NONE != ret) {
594 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
596 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
598 g_audio_state = AUDIO_STATE_READY;
600 /* unset volume policy, volume will be 100% */
601 __unset_policy_for_playing();
606 SLOG(LOG_INFO, tts_tag(), "[Player] Finish to wait for new audio data come");
608 if (AUDIO_STATE_READY == g_audio_state) {
609 /* set volume policy as 40%, when resume play thread*/
610 __set_policy_for_playing(40);
613 /* resume play thread */
614 player->state = APP_STATE_PLAYING;
618 /* If wdata's event is 'start', current wdata is first data of engine for synthesis.
619 * If wdata's event is 'finish', player should check previous event to know whether this wdata is first or not.
620 * When previous wdata's event is 'finish' and current wdata's event is 'finish',
621 * the player should send utt started event.
623 if (TTSE_RESULT_EVENT_START == sound_data->event ||
624 (TTSE_RESULT_EVENT_FINISH == player->event && TTSE_RESULT_EVENT_FINISH == sound_data->event)) {
625 int pid = ttsd_data_get_pid(player->uid);
628 SLOG(LOG_WARN, tts_tag(), "[Send WARNIING] Current player is not valid");
629 /* unset volume policy, volume will be 100% */
630 __unset_policy_for_playing();
635 if (0 != ttsdc_send_utt_start_message(pid, player->uid, sound_data->utt_id)) {
636 SLOG(LOG_ERROR, tts_tag(), "[Send ERROR] Fail to send Utterance Start Signal : pid(%d), uid(%d), uttid(%d)",
637 pid, player->uid, sound_data->utt_id);
639 SLOG(LOG_INFO, tts_tag(), "[Player] Start utterance : uid(%d), uttid(%d)", player->uid, sound_data->utt_id);
642 /* Save last event to check utterance start */
643 player->event = sound_data->event;
646 if (NULL == sound_data->data || 0 >= sound_data->data_size) {
647 if (TTSE_RESULT_EVENT_FINISH == sound_data->event) {
648 SLOG(LOG_DEBUG, tts_tag(), "No sound data");
649 /* send utterence finish signal */
650 int pid = ttsd_data_get_pid(player->uid);
653 SLOG(LOG_WARN, tts_tag(), "[Send WARNIING] Current player is not valid");
654 /* unset volume policy, volume will be 100% */
655 __unset_policy_for_playing();
659 __unset_policy_for_playing();
661 if (0 != ttsdc_send_utt_finish_message(pid, player->uid, sound_data->utt_id)) {
662 SLOG(LOG_ERROR, tts_tag(), "[Send ERROR] Fail to send Utterance Completed Signal : pid(%d), uid(%d), uttid(%d)",
663 pid, player->uid, sound_data->utt_id);
665 SLOG(LOG_INFO, tts_tag(), "[Player] Finish utterance : uid(%d), uttid(%d)", player->uid, sound_data->utt_id);
668 SLOG(LOG_INFO, tts_tag(), "[Player] Event(%d) utterance : uid(%d), uttid(%d)", sound_data->event, player->uid, sound_data->utt_id);
669 ttsd_data_clear_sound_data(player->uid, &sound_data);
674 if (g_sampling_rate != sound_data->rate || g_audio_type != sound_data->audio_type) {
675 SLOG(LOG_INFO, tts_tag(), "[Player] Change audio handle : org type(%d) org rate(%d)", g_audio_type, g_sampling_rate);
676 if (NULL != g_audio_h) {
677 __destroy_audio_out();
680 if (0 > __create_audio_out(sound_data->audio_type, sound_data->rate)) {
681 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to create audio out");
682 /* unset volume policy, volume will be 100% */
683 __unset_policy_for_playing();
685 ttsd_data_clear_sound_data(player->uid, &sound_data);
690 SLOG(LOG_INFO, tts_tag(), "[Player INFO] Success to destroy and recreate audio out");
691 __set_policy_for_playing(40);
694 while (APP_STATE_PLAYING == player->state || APP_STATE_PAUSED == player->state) {
695 if ((unsigned int)idx >= sound_data->data_size)
698 if ((unsigned int)idx + SOUND_BUFFER_LENGTH > sound_data->data_size) {
699 len = sound_data->data_size - idx;
701 len = SOUND_BUFFER_LENGTH;
704 if (AUDIO_STATE_READY == g_audio_state) {
705 /* Request prepare */
706 ret = audio_out_prepare(g_audio_h);
707 if (AUDIO_IO_ERROR_NONE != ret) {
708 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to prepare audio : %d", ret);
709 g_playing_info = NULL;
710 /* unset volume policy, volume will be 100% */
711 __unset_policy_for_playing();
713 ttsd_data_clear_sound_data(player->uid, &sound_data);
717 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Prepare audio");
718 g_audio_state = AUDIO_STATE_PLAY;
721 char* temp_data = sound_data->data;
722 SLOG(LOG_INFO, tts_tag(), "[Player INFO] Before audio_out_write. data(%p), data[%d](%p), uid(%d), utt_id(%d), len(%d)",
723 temp_data, idx, &temp_data[idx], player->uid, sound_data->utt_id, len);
725 /* write pcm buffer */
726 fwrite(&temp_data[idx], 1, len, g_pFile);
728 ret = audio_out_write(g_audio_h, &temp_data[idx], len);
730 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to audio write - %d", ret);
733 SLOG(LOG_INFO, tts_tag(), "[Player INFO] After audio_out_write");
736 if (NULL == g_playing_info && APP_STATE_PAUSED != player->state) {
737 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Current player is NULL");
738 g_audio_state = AUDIO_STATE_READY;
739 ret = audio_out_unprepare(g_audio_h);
740 if (AUDIO_IO_ERROR_NONE != ret) {
741 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
743 /* unset volume policy, volume will be 100% */
744 __unset_policy_for_playing();
746 ttsd_data_clear_sound_data(player->uid, &sound_data);
751 if (APP_STATE_PAUSED == player->state) {
753 SLOG(LOG_DEBUG, tts_tag(), "[Player] player(%p)", player);
754 player->paused_data = sound_data;
756 player->is_paused_data = true;
759 g_audio_state = AUDIO_STATE_READY;
761 SLOG(LOG_INFO, tts_tag(), "[Player] Stop player thread by pause");
763 /* Request prepare */
764 ret = audio_out_unprepare(g_audio_h);
765 if (AUDIO_IO_ERROR_NONE != ret) {
766 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
768 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
770 /* unset volume policy, volume will be 100% */
771 __unset_policy_for_playing();
776 if (NULL == g_playing_info && APP_STATE_READY == player->state) {
778 g_audio_state = AUDIO_STATE_READY;
779 SLOG(LOG_DEBUG, tts_tag(), "[Player] Stop player thread");
781 /* Request prepare */
782 ret = audio_out_unprepare(g_audio_h);
783 if (AUDIO_IO_ERROR_NONE != ret) {
784 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
786 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
789 ttsd_data_clear_sound_data(player->uid, &sound_data);
790 /* unset volume policy, volume will be 100% */
791 __unset_policy_for_playing();
795 if ((APP_STATE_PLAYING == player->state || APP_STATE_PAUSED == player->state) &&
796 (TTSE_RESULT_EVENT_FINISH == sound_data->event)) {
797 /* send utterence finish signal */
798 int pid = ttsd_data_get_pid(player->uid);
801 SLOG(LOG_WARN, tts_tag(), "[Send WARNIING] Current player is not valid");
802 /* unset volume policy, volume will be 100% */
803 __unset_policy_for_playing();
808 if (0 != ttsdc_send_utt_finish_message(pid, player->uid, sound_data->utt_id)) {
809 SLOG(LOG_ERROR, tts_tag(), "[Send ERROR] Fail to send Utterance Completed Signal : pid(%d), uid(%d), uttid(%d)",
810 pid, player->uid, sound_data->utt_id);
811 /* unset volume policy, volume will be 100% */
812 __unset_policy_for_playing();
814 ttsd_data_clear_sound_data(player->uid, &sound_data);
818 SLOG(LOG_INFO, tts_tag(), "[Player] Finish utterance : uid(%d), uttid(%d)", player->uid, sound_data->utt_id);
821 ttsd_data_clear_sound_data(player->uid, &sound_data);
823 if (NULL == g_playing_info) {
824 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Current player is NULL");
825 g_audio_state = AUDIO_STATE_READY;
826 ret = audio_out_unprepare(g_audio_h);
827 if (AUDIO_IO_ERROR_NONE != ret) {
828 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
830 /* unset volume policy, volume will be 100% */
831 __unset_policy_for_playing();
838 int __create_ducking_handle(void)
841 if (NULL == g_media_stream_ducking) {
842 ret = sound_manager_create_stream_ducking(SOUND_STREAM_TYPE_MEDIA, __sound_stream_ducking_state_changed_cb, NULL, &g_media_stream_ducking);
843 if (SOUND_MANAGER_ERROR_NONE != ret)
844 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to create stream ducking for type media, ret(%d)", ret);
846 SLOG(LOG_INFO, tts_tag(), "[Player INFO] Ducking handle for media stream is already created");
848 if (NULL == g_system_stream_ducking) {
849 ret = sound_manager_create_stream_ducking(SOUND_TYPE_SYSTEM, __sound_stream_ducking_state_changed_cb, NULL, &g_system_stream_ducking);
850 if (SOUND_MANAGER_ERROR_NONE != ret)
851 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to create stream ducking for system type, ret(%d)", ret);
853 SLOG(LOG_INFO, tts_tag(), "[Player INFO] Ducking handle for system stream is already created");
856 if (NULL == g_notification_stream_ducking) {
857 ret = sound_manager_create_stream_ducking(SOUND_TYPE_NOTIFICATION, __sound_stream_ducking_state_changed_cb, NULL, &g_notification_stream_ducking);
858 if (SOUND_MANAGER_ERROR_NONE != ret)
859 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to create stream ducking for notification type, ret(%d)", ret);
861 SLOG(LOG_INFO, tts_tag(), "[Player INFO] Ducking handle for notification stream is already created");
864 if (NULL == g_alarm_stream_ducking) {
865 ret = sound_manager_create_stream_ducking(SOUND_TYPE_ALARM, __sound_stream_ducking_state_changed_cb, NULL, &g_alarm_stream_ducking);
866 if (SOUND_MANAGER_ERROR_NONE != ret)
867 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to create stream ducking for alarm type, ret(%d)", ret);
869 SLOG(LOG_INFO, tts_tag(), "[Player INFO] Ducking handle for alarm stream is already created");
878 int ttsd_player_init()
880 g_playing_info = NULL;
881 g_audio_state = AUDIO_STATE_NONE;
886 ecore_thread_max_set(1);
890 if (0 == access(FOCUS_SERVER_READY, F_OK)) {
891 SLOG(LOG_ERROR, tts_tag(), "[Player SUCCESS] focus server is available");
895 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] focus server is not available");
900 ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_VOICE_INFORMATION, __player_focus_state_cb, NULL, &g_stream_info_h);
901 if (SOUND_MANAGER_ERROR_NONE != ret) {
902 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to create stream info");
905 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Create stream info");
908 /* add sound focus state watch callback */
909 ret = sound_manager_add_focus_state_watch_cb(SOUND_STREAM_FOCUS_FOR_PLAYBACK, __player_focus_state_watch_cb, NULL, &g_focus_watch_id);
910 if (SOUND_MANAGER_ERROR_NONE != ret) {
911 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to add sound focus watch callback");
912 sound_manager_destroy_stream_information(g_stream_info_h);
915 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Add sound focus watch callback");
918 ret = __create_ducking_handle();
919 if (SOUND_MANAGER_ERROR_NONE != ret) {
920 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to create ducking handle, ret(%d)", ret);
922 SLOG(LOG_INFO, tts_tag(), "[Player SUCCESS] Create ducking handle");
925 ecore_thread_max_set(1);
927 ret = __create_audio_out(TTSE_AUDIO_TYPE_RAW_S16, 16000);
929 sound_manager_destroy_stream_information(g_stream_info_h);
930 sound_manager_remove_focus_state_watch_cb(g_focus_watch_id);
934 g_player_init = true;
939 int __destroy_ducking_handle(void)
942 if (g_media_stream_ducking) {
943 ret = sound_manager_destroy_stream_ducking(g_media_stream_ducking);
944 if (SOUND_MANAGER_ERROR_NONE != ret)
945 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to destroy media stream ducking, ret(%d)", ret);
946 g_media_stream_ducking = NULL;
948 SLOG(LOG_INFO, tts_tag(), "[Player INFO] Ducking handle for media stream is already created");
951 if (g_system_stream_ducking) {
952 ret = sound_manager_destroy_stream_ducking(g_system_stream_ducking);
953 if (SOUND_MANAGER_ERROR_NONE != ret)
954 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to destroy system stream ducking, ret(%d)", ret);
955 g_system_stream_ducking = NULL;
957 SLOG(LOG_INFO, tts_tag(), "[Player INFO] Ducking handle for system stream is already created");
960 if (g_notification_stream_ducking) {
961 ret = sound_manager_destroy_stream_ducking(g_notification_stream_ducking);
962 if (SOUND_MANAGER_ERROR_NONE != ret)
963 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to destroy notification stream ducking, ret(%d)", ret);
964 g_notification_stream_ducking = NULL;
966 SLOG(LOG_INFO, tts_tag(), "[Player INFO] Ducking handle for notification stream is already created");
969 if (g_alarm_stream_ducking) {
970 ret = sound_manager_destroy_stream_ducking(g_alarm_stream_ducking);
971 if (SOUND_MANAGER_ERROR_NONE != ret)
972 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to destroy alarm stream ducking, ret(%d)", ret);
973 g_alarm_stream_ducking = NULL;
975 SLOG(LOG_INFO, tts_tag(), "[Player INFO] Ducking handle for alarm stream is already created");
980 int ttsd_player_release(void)
982 if (false == g_player_init) {
983 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
984 return TTSD_ERROR_OPERATION_FAILED;
989 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] @@@@@");
990 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Active thread count : %d", ecore_thread_active_get());
991 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] @@@@@");
993 /* The thread should be released */
994 int thread_count = ecore_thread_active_get();
996 while (0 < thread_count) {
1001 SLOG(LOG_WARN, tts_tag(), "[Player WARNING!!] Thread is blocked. Player release continue.");
1005 thread_count = ecore_thread_active_get();
1008 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Thread is released");
1010 ret = __destroy_audio_out();
1014 ret = sound_manager_destroy_stream_information(g_stream_info_h);
1015 if (SOUND_MANAGER_ERROR_NONE != ret) {
1016 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to destroy stream info");
1018 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Destroy stream info");
1021 /* remove focus state watch callback */
1022 ret = sound_manager_remove_focus_state_watch_cb(g_focus_watch_id);
1023 if (SOUND_MANAGER_ERROR_NONE != ret) {
1024 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to remove the focus state watch cb");
1026 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Remove the focus state watch cb");
1029 ret = __destroy_ducking_handle();
1030 if (SOUND_MANAGER_ERROR_NONE != ret)
1031 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to destroy stream ducking, ret(%d)", ret);
1033 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Destroy stream ducking");
1035 /* clear g_player_list */
1036 g_playing_info = NULL;
1037 g_player_init = false;
1042 int ttsd_player_create_instance(int uid)
1044 if (false == g_player_init) {
1045 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1049 /* Check uid is duplicated */
1050 if (NULL != __player_get_item(uid)) {
1051 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is already registered", uid);
1055 player_s* new_client = (player_s*)calloc(1, sizeof(player_s));
1056 if (NULL == new_client) {
1057 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to allocate memory");
1058 return TTSE_ERROR_OUT_OF_MEMORY;
1061 new_client->uid = uid;
1062 new_client->event = TTSE_RESULT_EVENT_FINISH;
1063 new_client->state = APP_STATE_READY;
1064 new_client->is_paused_data = false;
1065 new_client->idx = 0;
1066 new_client->paused_data = NULL;
1068 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[Player] Create player : uid(%d)", uid);
1070 g_player_list = g_list_append(g_player_list, new_client);
1075 int ttsd_player_destroy_instance(int uid)
1077 if (false == g_player_init) {
1078 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1083 current = __player_get_item(uid);
1084 if (NULL == current) {
1085 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
1089 if (NULL != g_playing_info) {
1090 if (uid == g_playing_info->uid) {
1091 g_playing_info = NULL;
1096 player_s *data = NULL;
1098 if (0 < g_list_length(g_player_list)) {
1099 /* Get a first item */
1100 iter = g_list_first(g_player_list);
1102 while (NULL != iter) {
1103 /* Get handle data from list */
1104 data = (player_s*)iter->data;
1108 if (uid == data->uid) {
1109 g_player_list = g_list_remove_link(g_player_list, iter);
1117 iter = g_list_next(iter);
1121 SLOG(LOG_DEBUG, tts_tag(), "[PLAYER Success] Destroy instance");
1126 int ttsd_player_play(int uid)
1128 if (false == g_player_init) {
1129 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1133 if (NULL != g_playing_info) {
1134 if (uid == g_playing_info->uid) {
1135 SLOG(LOG_DEBUG, tts_tag(), "[Player] uid(%d) has already played", g_playing_info->uid);
1138 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] stop old player (%d)", g_playing_info->uid);
1139 ttsd_player_stop(g_playing_info->uid);
1143 SLOG(LOG_DEBUG, tts_tag(), "[Player] start play : uid(%d)", uid);
1145 /* Check sound queue size */
1146 if (0 == ttsd_data_get_sound_data_size(uid)) {
1147 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] A sound queue of current player(%d) is empty", uid);
1153 current = __player_get_item(uid);
1154 if (NULL == current) {
1155 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
1159 current->state = APP_STATE_PLAYING;
1161 g_playing_info = current;
1163 SLOG(LOG_INFO, tts_tag(), "[Player DEBUG] Active thread count : %d", ecore_thread_active_get());
1165 if (0 < ttsd_data_get_sound_data_size(current->uid)) {
1166 SLOG(LOG_INFO, tts_tag(), "[Player] Run thread");
1167 ecore_thread_run(__play_thread, __end_play_thread, NULL, NULL);
1173 int ttsd_player_stop(int uid)
1175 if (false == g_player_init) {
1176 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1182 current = __player_get_item(uid);
1183 if (NULL == current) {
1184 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
1188 /* check whether uid is current playing or not */
1189 if (NULL != g_playing_info) {
1190 if (uid == g_playing_info->uid) {
1191 /* release current playing info */
1192 g_playing_info = NULL;
1195 SLOG(LOG_DEBUG, tts_tag(), "[Player] No current playing");
1198 if (true == current->is_paused_data) {
1199 if (NULL != current->paused_data) {
1200 if (NULL != current->paused_data->data) {
1201 free(current->paused_data->data);
1202 current->paused_data->data = NULL;
1205 free(current->paused_data);
1206 current->paused_data = NULL;
1210 current->event = TTSE_RESULT_EVENT_FINISH;
1211 current->state = APP_STATE_READY;
1212 current->is_paused_data = false;
1215 if (NULL == g_playing_info) {
1216 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1217 SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
1218 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1220 /* The thread should be released */
1221 int thread_count = ecore_thread_active_get();
1223 while (0 < thread_count) {
1228 SLOG(LOG_WARN, tts_tag(), "[Player WARNING!!] Thread is blocked. Player release continue.");
1232 thread_count = ecore_thread_active_get();
1235 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1236 SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
1237 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1240 SLOG(LOG_INFO, tts_tag(), "[Player SUCCESS] Stop player : uid(%d)", uid);
1245 int ttsd_player_clear(int uid)
1247 if (false == g_player_init) {
1248 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1254 current = __player_get_item(uid);
1255 if (NULL == current) {
1256 SECURE_SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
1260 if (true == current->is_paused_data) {
1261 if (NULL != current->paused_data) {
1262 if (NULL != current->paused_data->data) {
1263 free(current->paused_data->data);
1264 current->paused_data->data = NULL;
1267 free(current->paused_data);
1268 current->paused_data = NULL;
1272 current->event = TTSE_RESULT_EVENT_FINISH;
1273 current->state = APP_STATE_READY;
1274 current->is_paused_data = false;
1277 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Clear player : uid(%d)", uid);
1282 int ttsd_player_pause(int uid)
1284 SLOG(LOG_DEBUG, tts_tag(), "[Player] pause player : uid(%d)", uid);
1286 if (false == g_player_init) {
1287 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1293 current = __player_get_item(uid);
1294 if (NULL == current) {
1295 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] ttsd_player_pause() : uid(%d) is not valid", uid);
1299 /* check whether uid is current playing or not */
1300 if (NULL != g_playing_info) {
1301 if (uid == g_playing_info->uid) {
1302 /* release current playing info */
1303 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] release current playing info (%d)", uid);
1304 g_playing_info = NULL;
1310 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] current player (%p), g_playing_info(%p)", current, g_playing_info);
1312 current->state = APP_STATE_PAUSED;
1314 if (NULL == g_playing_info) {
1315 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1316 SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
1317 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1319 /* The thread should be released */
1320 int thread_count = ecore_thread_active_get();
1322 while (0 < thread_count) {
1326 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] current(%p), state(%d)", current, current->state);
1329 SLOG(LOG_WARN, tts_tag(), "[Player WARNING!!] Thread is blocked. Player release continue. current(%p) current state(%d)", current, current->state);
1333 thread_count = ecore_thread_active_get();
1336 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1337 SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
1338 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1342 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Pause player : uid(%d)", uid);
1347 int ttsd_player_resume(int uid)
1349 SLOG(LOG_DEBUG, tts_tag(), "[Player] Resume player : uid(%d)", uid);
1351 if (false == g_player_init) {
1352 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1358 current = __player_get_item(uid);
1359 if (NULL == current) {
1360 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
1364 /* check current player */
1365 if (NULL != g_playing_info)
1366 g_playing_info = NULL;
1368 current->state = APP_STATE_PLAYING;
1369 g_playing_info = current;
1371 SLOG(LOG_INFO, tts_tag(), "[Player] Resume to run thread");
1372 ecore_thread_run(__play_thread, __end_play_thread, NULL, NULL);
1377 int ttsd_player_all_stop()
1379 if (false == g_player_init) {
1380 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1384 g_playing_info = NULL;
1387 player_s *data = NULL;
1389 if (0 < g_list_length(g_player_list)) {
1390 /* Get a first item */
1391 iter = g_list_first(g_player_list);
1393 while (NULL != iter) {
1394 /* Get handle data from list */
1395 data = (player_s*)iter->data;
1397 app_tts_state_e state;
1398 if (0 > ttsd_data_get_client_state(data->uid, &state)) {
1399 SLOG(LOG_ERROR, tts_tag(), "[player ERROR] uid(%d) is not valid", data->uid);
1400 ttsd_player_destroy_instance(data->uid);
1401 iter = g_list_next(iter);
1405 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1406 data->event = TTSE_RESULT_EVENT_FINISH;
1407 data->state = APP_STATE_READY;
1409 if (true == data->is_paused_data) {
1410 if (NULL != data->paused_data) {
1411 if (NULL != data->paused_data->data) {
1412 free(data->paused_data->data);
1413 data->paused_data->data = NULL;
1416 free(data->paused_data);
1417 data->paused_data = NULL;
1421 data->is_paused_data = false;
1426 iter = g_list_next(iter);
1430 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] player all stop!!");
1435 int ttsd_player_play_pcm(int uid)
1437 if (false == g_player_init) {
1438 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1442 if (NULL != g_playing_info) {
1443 if (uid == g_playing_info->uid) {
1444 SLOG(LOG_DEBUG, tts_tag(), "[Player] uid(%d) has already played", g_playing_info->uid);
1447 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] stop old player (%d)", g_playing_info->uid);
1448 ttsd_player_stop(g_playing_info->uid);
1452 SLOG(LOG_DEBUG, tts_tag(), "[Player] start play : uid(%d)", uid);
1454 /* Check sound queue size */
1455 if (0 == ttsd_data_get_sound_data_size(uid)) {
1456 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] A sound queue of current player(%d) is empty", uid);
1461 current = __player_get_item(uid);
1462 if (NULL == current) {
1463 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
1467 current->state = APP_STATE_PLAYING;
1469 g_playing_info = current;
1471 SLOG(LOG_INFO, tts_tag(), "[Player DEBUG] Active thread count : %d", ecore_thread_active_get());
1473 if (0 <= ttsd_data_get_sound_data_size(current->uid)) {
1474 SLOG(LOG_INFO, tts_tag(), "[Player] Run thread");
1475 ecore_thread_run(__play_thread, __end_play_thread, NULL, NULL);
1481 int ttsd_player_get_background_volume_ratio(double* ratio)
1486 *ratio = g_bg_volume_ratio;
1488 return TTSD_ERROR_NONE;
1491 int ttsd_player_set_background_volume_ratio(double ratio)
1493 SLOG(LOG_INFO, tts_tag(), "[Player DEBUG] ttsd_player_set_background_volume_ratio : %lf", ratio);
1495 g_bg_volume_ratio = ratio;
1496 return TTSD_ERROR_NONE;