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_stream_ducking;
86 static bool ducking_flag;
88 #define SND_MGR_DUCKING_DURATION 500
90 static int g_focus_watch_id;
92 static double g_bg_volume_ratio;
98 player_s* __player_get_item(int uid)
101 player_s *data = NULL;
103 if (0 < g_list_length(g_player_list)) {
104 /* Get a first item */
105 iter = g_list_first(g_player_list);
107 while (NULL != iter) {
108 /* Get handle data from list */
109 data = (player_s*)iter->data;
112 if (uid == data->uid)
116 iter = g_list_next(iter);
123 void __player_focus_state_cb(sound_stream_info_h stream_info, sound_stream_focus_mask_e focus_mask, sound_stream_focus_state_e focus_state,
124 sound_stream_focus_change_reason_e reason_for_change, int sound_behavior, const char *extra_info, void *user_data)
126 SLOG(LOG_DEBUG, tts_tag(), "@@@ Focus state changed cb");
128 if (stream_info != g_stream_info_h) {
129 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Invalid stream info handle");
132 SLOG(LOG_WARN, tts_tag(), "[Player] focus state changed to (%d) with reason(%d) and extra info(%s)", (int)focus_state, (int)reason_for_change, extra_info);
134 if (AUDIO_STATE_PLAY == g_audio_state && focus_mask == SOUND_STREAM_FOCUS_FOR_PLAYBACK && SOUND_STREAM_FOCUS_STATE_RELEASED == focus_state) {
135 if (TTSD_MODE_DEFAULT == ttsd_get_mode()) {
136 g_audio_state = AUDIO_STATE_READY;
138 if (NULL == g_playing_info) {
139 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] No current player");
143 if (APP_STATE_PLAYING == g_playing_info->state) {
144 int uid = g_playing_info->uid;
146 if (0 != ttsd_player_pause(uid)) {
147 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to pause the player");
151 ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
152 int pid = ttsd_data_get_pid(uid);
153 /* send message to client about changing state */
154 ttsdc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
157 SLOG(LOG_DEBUG, tts_tag(), "[Player] Ignore focus state cb - mode(%d)", ttsd_get_mode());
161 /* if (AUDIO_STATE_READY == g_audio_state && focus_mask == SOUND_STREAM_FOCUS_FOR_PLAYBACK && SOUND_STREAM_FOCUS_STATE_ACQUIRED == focus_state) {
162 if (TTSD_MODE_DEFAULT == ttsd_get_mode()) {
163 g_audio_state = AUDIO_STATE_PLAY;
165 if (NULL == g_playing_info) {
166 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] No current player");
170 if (APP_STATE_PAUSED == g_playing_info->state) {
171 int uid = g_playing_info->uid;
173 g_audio_state = AUDIO_STATE_PLAY;
174 if (0 != ttsd_player_resume(uid)) {
175 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to resume the player");
176 g_audio_state = AUDIO_STATE_READY;
180 ttsd_data_set_client_state(uid, APP_STATE_PLAYING);
181 int pid = ttsd_data_get_pid(uid);
182 ttsdc_send_set_state_message(pid, uid, APP_STATE_PLAYING);
186 SLOG(LOG_DEBUG, tts_tag(), "[Player] Ignore focus state cb - mode(%d)", ttsd_get_mode());
190 SLOG(LOG_DEBUG, tts_tag(), "@@@");
195 void __player_focus_state_watch_cb(int id, sound_stream_focus_mask_e focus_mask, sound_stream_focus_state_e focus_state, sound_stream_focus_change_reason_e reason,
196 const char *extra_info, void *user_data)
198 SLOG(LOG_DEBUG, tts_tag(), "@@@ Focus state watch cb");
200 ttsd_mode_e mode = ttsd_get_mode();
202 if (TTSD_MODE_SCREEN_READER != mode && TTSD_MODE_NOTIFICATION != mode) {
203 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] This is not screen-reader mode and notification mode.");
207 if (AUDIO_STATE_PLAY == g_audio_state && SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_INFORMATION == reason &&
208 NULL != extra_info && 0 == strncmp(extra_info, "TTSD_MODE_INTERRUPT", strlen(extra_info))) {
209 /* If the focus is changed by "Interrupt" mode and current players of "SR" and "Noti" modes are on going, please stop the current players. */
210 g_audio_state = AUDIO_STATE_READY;
212 if (NULL == g_playing_info) {
213 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] No current player");
217 if (APP_STATE_PLAYING == g_playing_info->state) {
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] Not playing state");
237 SLOG(LOG_DEBUG, tts_tag(), "[Player] This is not Interrupt mode or not playing state.");
243 void __sound_stream_ducking_state_changed_cb(sound_stream_ducking_h stream_ducking, bool is_ducked, void *user_data)
245 SLOG(LOG_DEBUG, tts_tag(), "@@@ ducking state changed cb");
247 SLOG(LOG_DEBUG, tts_tag(), "[Player] is ducked : %d", is_ducked);
253 static void __change_background_volume()
255 int ret = SOUND_MANAGER_ERROR_NONE;
256 bool is_ducked = false;
257 ret = sound_manager_is_ducked(g_stream_ducking, &is_ducked);
259 SLOG(LOG_DEBUG, tts_tag(), "[Player] The background volume is already ducked");
263 ducking_flag = false;
264 ret = sound_manager_activate_ducking(g_stream_ducking, SND_MGR_DUCKING_DURATION, g_bg_volume_ratio);
265 if (SOUND_MANAGER_ERROR_NONE != ret) {
266 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to activate ducking");
269 SLOG(LOG_DEBUG, tts_tag(), "[Player] Activate ducking");
272 while (ducking_flag == false) {
274 SLOG(LOG_DEBUG, tts_tag(), "ducking waiting");
278 static void __recover_background_volume()
280 int ret = SOUND_MANAGER_ERROR_NONE;
281 bool is_ducked = false;
282 ret = sound_manager_is_ducked(g_stream_ducking, &is_ducked);
284 SLOG(LOG_DEBUG, tts_tag(), "[Player] The background volume is already recovered");
288 ret = sound_manager_deactivate_ducking(g_stream_ducking);
289 if (SOUND_MANAGER_ERROR_NONE != ret)
290 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to deactivate ducking");
292 SLOG(LOG_WARN, tts_tag(), "[Player] Deactivate ducking");
295 static int __create_audio_out(ttse_audio_type_e type, int rate)
298 audio_sample_type_e sample_type;
300 if (TTSE_AUDIO_TYPE_RAW_S16 == type) {
301 sample_type = AUDIO_SAMPLE_TYPE_S16_LE;
303 sample_type = AUDIO_SAMPLE_TYPE_U8;
306 ret = audio_out_create_new(rate, AUDIO_CHANNEL_MONO, sample_type, &g_audio_h);
307 if (AUDIO_IO_ERROR_NONE != ret) {
308 g_audio_state = AUDIO_STATE_NONE;
310 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to create audio");
313 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Create audio");
317 g_sampling_rate = rate;
319 g_audio_state = AUDIO_STATE_READY;
324 static int __destroy_audio_out()
326 if (NULL == g_audio_h) {
327 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Current handle is not valid");
332 ret = audio_out_destroy(g_audio_h);
333 if (AUDIO_IO_ERROR_NONE != ret) {
334 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to destroy audio");
337 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Destroy audio");
343 g_audio_state = AUDIO_STATE_NONE;
349 static void __end_play_thread(void *data, Ecore_Thread *thread)
351 SLOG(LOG_ERROR, tts_tag(), "@@@ End thread");
358 static void __set_policy_for_playing(int volume)
360 /* Set stream info */
362 ttsd_mode_e mode = ttsd_get_mode();
363 if (TTSD_MODE_DEFAULT == mode) {
364 ret = sound_manager_acquire_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, SOUND_BEHAVIOR_NONE, NULL);
365 if (SOUND_MANAGER_ERROR_NONE != ret) {
366 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to acquire focus");
368 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Success to acquire focus (default mode)");
370 } else if (TTSD_MODE_INTERRUPT == mode) {
371 ret = sound_manager_acquire_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, SOUND_BEHAVIOR_NONE, "TTSD_MODE_INTERRUPT");
372 if (SOUND_MANAGER_ERROR_NONE != ret) {
373 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to acquire focus");
375 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Success to acquire focus (interrupt mode)");
379 ret = audio_out_set_sound_stream_info(g_audio_h, g_stream_info_h);
380 if (AUDIO_IO_ERROR_NONE != ret) {
381 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to set stream info");
384 __change_background_volume();
386 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] set policy for playing");
391 static void __unset_policy_for_playing()
394 ttsd_mode_e mode = ttsd_get_mode();
395 /* Unset stream info */
396 if (TTSD_MODE_DEFAULT == mode || TTSD_MODE_INTERRUPT == mode) {
397 sound_stream_focus_state_e state_for_playing = SOUND_STREAM_FOCUS_STATE_ACQUIRED;
398 ret = sound_manager_get_focus_state(g_stream_info_h, &state_for_playing, NULL);
399 if (SOUND_MANAGER_ERROR_NONE != ret) {
400 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to get focus state: %d", ret);
403 if (SOUND_STREAM_FOCUS_STATE_ACQUIRED == state_for_playing) {
404 if (TTSD_MODE_DEFAULT == mode || TTSD_MODE_INTERRUPT == mode) {
405 ret = sound_manager_release_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, SOUND_BEHAVIOR_NONE, NULL);
406 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] release focus (mode: %d)", mode);
409 if (SOUND_MANAGER_ERROR_NONE != ret) {
410 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to release focus");
415 __recover_background_volume();
417 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] unset policy for playing");
422 int ttsd_player_check_current_playback_focus(bool *is_current_interrupt)
425 ttsd_mode_e mode = ttsd_get_mode();
427 if (TTSD_MODE_INTERRUPT != mode) {
428 /* check the current playback focus */
429 sound_stream_focus_change_reason_e reason;
430 int sound_behavior = 0;
431 char *extra_info = NULL;
433 ret = sound_manager_get_current_playback_focus(&reason, &sound_behavior, &extra_info);
435 SLOG(LOG_DEBUG, tts_tag(), "[Player] current playback focus: extra_info(%s), reason(%d), sound_behavior(%d)", extra_info, reason, sound_behavior);
437 if (SOUND_MANAGER_ERROR_NONE == ret && NULL != extra_info && 0 < strlen(extra_info)) {
438 if (SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_INFORMATION == reason && 0 == strncmp(extra_info, "TTSD_MODE_INTERRUPT", strlen(extra_info))) {
439 SLOG(LOG_DEBUG, tts_tag(), "[Player] The current focus in Interrupt. Cannot play the requested sound data");
440 *is_current_interrupt = true;
445 return TTSD_ERROR_NONE;
449 if (NULL != extra_info) {
455 *is_current_interrupt = false;
457 return TTSD_ERROR_NONE;
460 static void __play_thread(void *data, Ecore_Thread *thread)
462 SLOG(LOG_DEBUG, tts_tag(), "@@@ Start thread");
468 snprintf(g_temp_file_name, sizeof(g_temp_file_name), "/tmp/tts_temp_%d_%d", getpid(), g_count);
469 int ret = access(g_temp_file_name, 0);
472 SLOG(LOG_ERROR, tts_tag(), "[Recorder ERROR] File is already exist");
473 if (0 == remove(g_temp_file_name)) {
474 SLOG(LOG_DEBUG, tts_tag(), "[Recorder] Remove file");
484 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[Recorder] Temp file name=[%s]", g_temp_file_name);
487 g_pFile = fopen(g_temp_file_name, "wb+x");
489 SLOG(LOG_ERROR, tts_tag(), "[Recorder ERROR] File not found!");
494 if (NULL == g_playing_info) {
495 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] No current player");
499 player_s* player = g_playing_info;
500 sound_data_s* sound_data = NULL;
503 int len = SOUND_BUFFER_LENGTH;
506 /* set volume policy as 40% */
507 __set_policy_for_playing(40);
509 if (true == player->is_paused_data) {
511 sound_data = player->paused_data;
512 player->paused_data = NULL;
516 player->is_paused_data = false;
519 if (NULL == sound_data) {
520 /* Request unprepare */
521 ret = audio_out_unprepare(g_audio_h);
522 if (AUDIO_IO_ERROR_NONE != ret) {
523 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
525 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
528 g_audio_state = AUDIO_STATE_READY;
530 /* unset volume policy, volume will be 100% */
531 __unset_policy_for_playing();
534 SLOG(LOG_INFO, tts_tag(), "[Player] Sound info : id(%d) data(%p) size(%d) audiotype(%d) rate(%d) event(%d)",
535 sound_data->utt_id, sound_data->data, sound_data->data_size, sound_data->audio_type, sound_data->rate, sound_data->event);
538 ret = ttsd_data_get_sound_data(player->uid, &sound_data);
539 if (0 != ret || NULL == sound_data) {
541 SLOG(LOG_DEBUG, tts_tag(), "[Player] No sound data. Waiting mode");
543 /* wait for new audio data come */
546 if (NULL == g_playing_info) {
547 /* current playing uid is replaced */
548 SLOG(LOG_INFO, tts_tag(), "[Player] Finish thread");
549 if (AUDIO_STATE_PLAY == g_audio_state) {
550 /* release audio & recover session */
551 ret = audio_out_unprepare(g_audio_h);
552 if (AUDIO_IO_ERROR_NONE != ret) {
553 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
555 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
557 g_audio_state = AUDIO_STATE_READY;
559 /* unset volume policy, volume will be 100% */
560 __unset_policy_for_playing();
562 } else if (0 < ttsd_data_get_sound_data_size(player->uid)) {
563 /* new audio data come */
564 SLOG(LOG_INFO, tts_tag(), "[Player] Resume thread");
568 /* If engine is not on processing */
569 if (TTSD_SYNTHESIS_CONTROL_DOING != ttsd_get_synth_control()) {
570 if (AUDIO_STATE_PLAY == g_audio_state) {
571 /* release audio & recover session */
572 ret = audio_out_unprepare(g_audio_h);
573 if (AUDIO_IO_ERROR_NONE != ret) {
574 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
576 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
578 g_audio_state = AUDIO_STATE_READY;
580 /* unset volume policy, volume will be 100% */
581 __unset_policy_for_playing();
586 SLOG(LOG_INFO, tts_tag(), "[Player] Finish to wait for new audio data come");
588 if (AUDIO_STATE_READY == g_audio_state) {
589 /* set volume policy as 40%, when resume play thread*/
590 __set_policy_for_playing(40);
593 /* resume play thread */
594 player->state = APP_STATE_PLAYING;
598 /* If wdata's event is 'start', current wdata is first data of engine for synthesis.
599 * If wdata's event is 'finish', player should check previous event to know whether this wdata is first or not.
600 * When previous wdata's event is 'finish' and current wdata's event is 'finish',
601 * the player should send utt started event.
603 if (TTSE_RESULT_EVENT_START == sound_data->event ||
604 (TTSE_RESULT_EVENT_FINISH == player->event && TTSE_RESULT_EVENT_FINISH == sound_data->event)) {
605 int pid = ttsd_data_get_pid(player->uid);
608 SLOG(LOG_WARN, tts_tag(), "[Send WARNIING] Current player is not valid");
609 /* unset volume policy, volume will be 100% */
610 __unset_policy_for_playing();
615 if (0 != ttsdc_send_utt_start_message(pid, player->uid, sound_data->utt_id)) {
616 SLOG(LOG_ERROR, tts_tag(), "[Send ERROR] Fail to send Utterance Start Signal : pid(%d), uid(%d), uttid(%d)",
617 pid, player->uid, sound_data->utt_id);
619 SLOG(LOG_INFO, tts_tag(), "[Player] Start utterance : uid(%d), uttid(%d)", player->uid, sound_data->utt_id);
622 /* Save last event to check utterance start */
623 player->event = sound_data->event;
626 if (NULL == sound_data->data || 0 >= sound_data->data_size) {
627 if (TTSE_RESULT_EVENT_FINISH == sound_data->event) {
628 SLOG(LOG_DEBUG, tts_tag(), "No sound data");
629 /* send utterence finish signal */
630 int pid = ttsd_data_get_pid(player->uid);
633 SLOG(LOG_WARN, tts_tag(), "[Send WARNIING] Current player is not valid");
634 /* unset volume policy, volume will be 100% */
635 __unset_policy_for_playing();
639 __unset_policy_for_playing();
641 if (0 != ttsdc_send_utt_finish_message(pid, player->uid, sound_data->utt_id)) {
642 SLOG(LOG_ERROR, tts_tag(), "[Send ERROR] Fail to send Utterance Completed Signal : pid(%d), uid(%d), uttid(%d)",
643 pid, player->uid, sound_data->utt_id);
645 SLOG(LOG_INFO, tts_tag(), "[Player] Finish utterance : uid(%d), uttid(%d)", player->uid, sound_data->utt_id);
648 SLOG(LOG_INFO, tts_tag(), "[Player] Event(%d) utterance : uid(%d), uttid(%d)", sound_data->event, player->uid, sound_data->utt_id);
649 ttsd_data_clear_sound_data(player->uid, &sound_data);
654 if (g_sampling_rate != sound_data->rate || g_audio_type != sound_data->audio_type) {
655 SLOG(LOG_INFO, tts_tag(), "[Player] Change audio handle : org type(%d) org rate(%d)", g_audio_type, g_sampling_rate);
656 if (NULL != g_audio_h) {
657 __destroy_audio_out();
660 if (0 > __create_audio_out(sound_data->audio_type, sound_data->rate)) {
661 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to create audio out");
662 /* unset volume policy, volume will be 100% */
663 __unset_policy_for_playing();
665 ttsd_data_clear_sound_data(player->uid, &sound_data);
670 SLOG(LOG_INFO, tts_tag(), "[Player INFO] Success to destroy and recreate audio out");
671 __set_policy_for_playing(40);
674 while (APP_STATE_PLAYING == player->state || APP_STATE_PAUSED == player->state) {
675 if ((unsigned int)idx >= sound_data->data_size)
678 if ((unsigned int)idx + SOUND_BUFFER_LENGTH > sound_data->data_size) {
679 len = sound_data->data_size - idx;
681 len = SOUND_BUFFER_LENGTH;
684 if (AUDIO_STATE_READY == g_audio_state) {
685 /* Request prepare */
686 ret = audio_out_prepare(g_audio_h);
687 if (AUDIO_IO_ERROR_NONE != ret) {
688 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to prepare audio : %d", ret);
689 g_playing_info = NULL;
690 /* unset volume policy, volume will be 100% */
691 __unset_policy_for_playing();
693 ttsd_data_clear_sound_data(player->uid, &sound_data);
697 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Prepare audio");
698 g_audio_state = AUDIO_STATE_PLAY;
701 char* temp_data = sound_data->data;
702 SLOG(LOG_INFO, tts_tag(), "[Player INFO] Before audio_out_write. data(%p), data[%d](%p), uid(%d), utt_id(%d), len(%d)",
703 temp_data, idx, &temp_data[idx], player->uid, sound_data->utt_id, len);
705 /* write pcm buffer */
706 fwrite(&temp_data[idx], 1, len, g_pFile);
708 ret = audio_out_write(g_audio_h, &temp_data[idx], len);
710 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to audio write - %d", ret);
713 SLOG(LOG_INFO, tts_tag(), "[Player INFO] After audio_out_write");
716 if (NULL == g_playing_info && APP_STATE_PAUSED != player->state) {
717 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Current player is NULL");
718 g_audio_state = AUDIO_STATE_READY;
719 ret = audio_out_unprepare(g_audio_h);
720 if (AUDIO_IO_ERROR_NONE != ret) {
721 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
723 /* unset volume policy, volume will be 100% */
724 __unset_policy_for_playing();
726 ttsd_data_clear_sound_data(player->uid, &sound_data);
731 if (APP_STATE_PAUSED == player->state) {
733 SLOG(LOG_DEBUG, tts_tag(), "[Player] player(%p)", player);
734 player->paused_data = sound_data;
736 player->is_paused_data = true;
739 g_audio_state = AUDIO_STATE_READY;
741 SLOG(LOG_INFO, tts_tag(), "[Player] Stop player thread by pause");
743 /* Request prepare */
744 ret = audio_out_unprepare(g_audio_h);
745 if (AUDIO_IO_ERROR_NONE != ret) {
746 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
748 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
750 /* unset volume policy, volume will be 100% */
751 __unset_policy_for_playing();
756 if (NULL == g_playing_info && APP_STATE_READY == player->state) {
758 g_audio_state = AUDIO_STATE_READY;
759 SLOG(LOG_DEBUG, tts_tag(), "[Player] Stop player thread");
761 /* Request prepare */
762 ret = audio_out_unprepare(g_audio_h);
763 if (AUDIO_IO_ERROR_NONE != ret) {
764 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
766 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
769 ttsd_data_clear_sound_data(player->uid, &sound_data);
770 /* unset volume policy, volume will be 100% */
771 __unset_policy_for_playing();
775 if ((APP_STATE_PLAYING == player->state || APP_STATE_PAUSED == player->state) &&
776 (TTSE_RESULT_EVENT_FINISH == sound_data->event)) {
777 /* send utterence finish signal */
778 int pid = ttsd_data_get_pid(player->uid);
781 SLOG(LOG_WARN, tts_tag(), "[Send WARNIING] Current player is not valid");
782 /* unset volume policy, volume will be 100% */
783 __unset_policy_for_playing();
788 if (0 != ttsdc_send_utt_finish_message(pid, player->uid, sound_data->utt_id)) {
789 SLOG(LOG_ERROR, tts_tag(), "[Send ERROR] Fail to send Utterance Completed Signal : pid(%d), uid(%d), uttid(%d)",
790 pid, player->uid, sound_data->utt_id);
791 /* unset volume policy, volume will be 100% */
792 __unset_policy_for_playing();
794 ttsd_data_clear_sound_data(player->uid, &sound_data);
798 SLOG(LOG_INFO, tts_tag(), "[Player] Finish utterance : uid(%d), uttid(%d)", player->uid, sound_data->utt_id);
801 ttsd_data_clear_sound_data(player->uid, &sound_data);
803 if (NULL == g_playing_info) {
804 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Current player is NULL");
805 g_audio_state = AUDIO_STATE_READY;
806 ret = audio_out_unprepare(g_audio_h);
807 if (AUDIO_IO_ERROR_NONE != ret) {
808 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
810 /* unset volume policy, volume will be 100% */
811 __unset_policy_for_playing();
821 int ttsd_player_init()
823 g_playing_info = NULL;
824 g_audio_state = AUDIO_STATE_NONE;
829 ecore_thread_max_set(1);
833 if (0 == access(FOCUS_SERVER_READY, F_OK)) {
834 SLOG(LOG_ERROR, tts_tag(), "[Player SUCCESS] focus server is available");
838 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] focus server is not available");
843 ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_VOICE_INFORMATION, __player_focus_state_cb, NULL, &g_stream_info_h);
844 if (SOUND_MANAGER_ERROR_NONE != ret) {
845 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to create stream info");
848 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Create stream info");
851 /* add sound focus state watch callback */
852 ret = sound_manager_add_focus_state_watch_cb(SOUND_STREAM_FOCUS_FOR_PLAYBACK, __player_focus_state_watch_cb, NULL, &g_focus_watch_id);
853 if (SOUND_MANAGER_ERROR_NONE != ret) {
854 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to add sound focus watch callback");
855 sound_manager_destroy_stream_information(g_stream_info_h);
858 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Add sound focus watch callback");
861 ret = sound_manager_create_stream_ducking(SOUND_STREAM_TYPE_MEDIA, __sound_stream_ducking_state_changed_cb, NULL, &g_stream_ducking);
862 if (SOUND_MANAGER_ERROR_NONE != ret)
863 SLOG(LOG_ERROR, tts_tag(), "[Player WARNING] Fail to create stream ducking");
865 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Create stream ducking");
867 ecore_thread_max_set(1);
869 ret = __create_audio_out(TTSE_AUDIO_TYPE_RAW_S16, 16000);
871 sound_manager_destroy_stream_information(g_stream_info_h);
872 sound_manager_remove_focus_state_watch_cb(g_focus_watch_id);
876 g_player_init = true;
881 int ttsd_player_release(void)
883 if (false == g_player_init) {
884 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
885 return TTSD_ERROR_OPERATION_FAILED;
890 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] @@@@@");
891 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Active thread count : %d", ecore_thread_active_get());
892 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] @@@@@");
894 /* The thread should be released */
895 int thread_count = ecore_thread_active_get();
897 while (0 < thread_count) {
902 SLOG(LOG_WARN, tts_tag(), "[Player WARNING!!] Thread is blocked. Player release continue.");
906 thread_count = ecore_thread_active_get();
909 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Thread is released");
911 ret = __destroy_audio_out();
915 ret = sound_manager_destroy_stream_information(g_stream_info_h);
916 if (SOUND_MANAGER_ERROR_NONE != ret) {
917 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to destroy stream info");
919 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Destroy stream info");
922 /* remove focus state watch callback */
923 ret = sound_manager_remove_focus_state_watch_cb(g_focus_watch_id);
924 if (SOUND_MANAGER_ERROR_NONE != ret) {
925 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to remove the focus state watch cb");
927 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Remove the focus state watch cb");
930 ret = sound_manager_destroy_stream_ducking(g_stream_ducking);
931 if (SOUND_MANAGER_ERROR_NONE != ret)
932 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to destroy stream ducking");
934 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Destroy stream ducking");
936 /* clear g_player_list */
937 g_playing_info = NULL;
938 g_player_init = false;
943 int ttsd_player_create_instance(int uid)
945 if (false == g_player_init) {
946 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
950 /* Check uid is duplicated */
951 if (NULL != __player_get_item(uid)) {
952 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is already registered", uid);
956 player_s* new_client = (player_s*)calloc(1, sizeof(player_s));
957 if (NULL == new_client) {
958 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to allocate memory");
959 return TTSE_ERROR_OUT_OF_MEMORY;
962 new_client->uid = uid;
963 new_client->event = TTSE_RESULT_EVENT_FINISH;
964 new_client->state = APP_STATE_READY;
965 new_client->is_paused_data = false;
967 new_client->paused_data = NULL;
969 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[Player] Create player : uid(%d)", uid);
971 g_player_list = g_list_append(g_player_list, new_client);
976 int ttsd_player_destroy_instance(int uid)
978 if (false == g_player_init) {
979 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
984 current = __player_get_item(uid);
985 if (NULL == current) {
986 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
990 if (NULL != g_playing_info) {
991 if (uid == g_playing_info->uid) {
992 g_playing_info = NULL;
997 player_s *data = NULL;
999 if (0 < g_list_length(g_player_list)) {
1000 /* Get a first item */
1001 iter = g_list_first(g_player_list);
1003 while (NULL != iter) {
1004 /* Get handle data from list */
1005 data = (player_s*)iter->data;
1009 if (uid == data->uid) {
1010 g_player_list = g_list_remove_link(g_player_list, iter);
1018 iter = g_list_next(iter);
1022 SLOG(LOG_DEBUG, tts_tag(), "[PLAYER Success] Destroy instance");
1027 int ttsd_player_play(int uid)
1029 if (false == g_player_init) {
1030 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1034 if (NULL != g_playing_info) {
1035 if (uid == g_playing_info->uid) {
1036 SLOG(LOG_DEBUG, tts_tag(), "[Player] uid(%d) has already played", g_playing_info->uid);
1039 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] stop old player (%d)", g_playing_info->uid);
1040 ttsd_player_stop(g_playing_info->uid);
1044 SLOG(LOG_DEBUG, tts_tag(), "[Player] start play : uid(%d)", uid);
1046 /* Check sound queue size */
1047 if (0 == ttsd_data_get_sound_data_size(uid)) {
1048 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] A sound queue of current player(%d) is empty", uid);
1054 current = __player_get_item(uid);
1055 if (NULL == current) {
1056 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
1060 current->state = APP_STATE_PLAYING;
1062 g_playing_info = current;
1064 SLOG(LOG_INFO, tts_tag(), "[Player DEBUG] Active thread count : %d", ecore_thread_active_get());
1066 if (0 < ttsd_data_get_sound_data_size(current->uid)) {
1067 SLOG(LOG_INFO, tts_tag(), "[Player] Run thread");
1068 ecore_thread_run(__play_thread, __end_play_thread, NULL, NULL);
1074 int ttsd_player_stop(int uid)
1076 if (false == g_player_init) {
1077 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 /* check whether uid is current playing or not */
1090 if (NULL != g_playing_info) {
1091 if (uid == g_playing_info->uid) {
1092 /* release current playing info */
1093 g_playing_info = NULL;
1096 SLOG(LOG_DEBUG, tts_tag(), "[Player] No current playing");
1099 if (true == current->is_paused_data) {
1100 if (NULL != current->paused_data) {
1101 if (NULL != current->paused_data->data) {
1102 free(current->paused_data->data);
1103 current->paused_data->data = NULL;
1106 free(current->paused_data);
1107 current->paused_data = NULL;
1111 current->event = TTSE_RESULT_EVENT_FINISH;
1112 current->state = APP_STATE_READY;
1113 current->is_paused_data = false;
1116 if (NULL == g_playing_info) {
1117 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1118 SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
1119 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1121 /* The thread should be released */
1122 int thread_count = ecore_thread_active_get();
1124 while (0 < thread_count) {
1129 SLOG(LOG_WARN, tts_tag(), "[Player WARNING!!] Thread is blocked. Player release continue.");
1133 thread_count = ecore_thread_active_get();
1136 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1137 SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
1138 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1141 SLOG(LOG_INFO, tts_tag(), "[Player SUCCESS] Stop player : uid(%d)", uid);
1146 int ttsd_player_clear(int uid)
1148 if (false == g_player_init) {
1149 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1155 current = __player_get_item(uid);
1156 if (NULL == current) {
1157 SECURE_SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
1161 if (true == current->is_paused_data) {
1162 if (NULL != current->paused_data) {
1163 if (NULL != current->paused_data->data) {
1164 free(current->paused_data->data);
1165 current->paused_data->data = NULL;
1168 free(current->paused_data);
1169 current->paused_data = NULL;
1173 current->event = TTSE_RESULT_EVENT_FINISH;
1174 current->state = APP_STATE_READY;
1175 current->is_paused_data = false;
1178 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Clear player : uid(%d)", uid);
1183 int ttsd_player_pause(int uid)
1185 SLOG(LOG_DEBUG, tts_tag(), "[Player] pause player : uid(%d)", uid);
1187 if (false == g_player_init) {
1188 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1194 current = __player_get_item(uid);
1195 if (NULL == current) {
1196 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] ttsd_player_pause() : uid(%d) is not valid", uid);
1200 /* check whether uid is current playing or not */
1201 if (NULL != g_playing_info) {
1202 if (uid == g_playing_info->uid) {
1203 /* release current playing info */
1204 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] release current playing info (%d)", uid);
1205 g_playing_info = NULL;
1211 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] current player (%p), g_playing_info(%p)", current, g_playing_info);
1213 current->state = APP_STATE_PAUSED;
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) {
1227 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] current(%p), state(%d)", current, current->state);
1230 SLOG(LOG_WARN, tts_tag(), "[Player WARNING!!] Thread is blocked. Player release continue. current(%p) current state(%d)", current, current->state);
1234 thread_count = ecore_thread_active_get();
1237 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1238 SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
1239 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1243 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Pause player : uid(%d)", uid);
1248 int ttsd_player_resume(int uid)
1250 SLOG(LOG_DEBUG, tts_tag(), "[Player] Resume player : uid(%d)", uid);
1252 if (false == g_player_init) {
1253 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1259 current = __player_get_item(uid);
1260 if (NULL == current) {
1261 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
1265 /* check current player */
1266 if (NULL != g_playing_info)
1267 g_playing_info = NULL;
1269 current->state = APP_STATE_PLAYING;
1270 g_playing_info = current;
1272 SLOG(LOG_INFO, tts_tag(), "[Player] Resume to run thread");
1273 ecore_thread_run(__play_thread, __end_play_thread, NULL, NULL);
1278 int ttsd_player_all_stop()
1280 if (false == g_player_init) {
1281 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1285 g_playing_info = NULL;
1288 player_s *data = NULL;
1290 if (0 < g_list_length(g_player_list)) {
1291 /* Get a first item */
1292 iter = g_list_first(g_player_list);
1294 while (NULL != iter) {
1295 /* Get handle data from list */
1296 data = (player_s*)iter->data;
1298 app_tts_state_e state;
1299 if (0 > ttsd_data_get_client_state(data->uid, &state)) {
1300 SLOG(LOG_ERROR, tts_tag(), "[player ERROR] uid(%d) is not valid", data->uid);
1301 ttsd_player_destroy_instance(data->uid);
1302 iter = g_list_next(iter);
1306 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1307 data->event = TTSE_RESULT_EVENT_FINISH;
1308 data->state = APP_STATE_READY;
1310 if (true == data->is_paused_data) {
1311 if (NULL != data->paused_data) {
1312 if (NULL != data->paused_data->data) {
1313 free(data->paused_data->data);
1314 data->paused_data->data = NULL;
1317 free(data->paused_data);
1318 data->paused_data = NULL;
1322 data->is_paused_data = false;
1327 iter = g_list_next(iter);
1331 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] player all stop!!");
1336 int ttsd_player_play_pcm(int uid)
1338 if (false == g_player_init) {
1339 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1343 if (NULL != g_playing_info) {
1344 if (uid == g_playing_info->uid) {
1345 SLOG(LOG_DEBUG, tts_tag(), "[Player] uid(%d) has already played", g_playing_info->uid);
1348 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] stop old player (%d)", g_playing_info->uid);
1349 ttsd_player_stop(g_playing_info->uid);
1353 SLOG(LOG_DEBUG, tts_tag(), "[Player] start play : uid(%d)", uid);
1355 /* Check sound queue size */
1356 if (0 == ttsd_data_get_sound_data_size(uid)) {
1357 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] A sound queue of current player(%d) is empty", uid);
1362 current = __player_get_item(uid);
1363 if (NULL == current) {
1364 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
1368 current->state = APP_STATE_PLAYING;
1370 g_playing_info = current;
1372 SLOG(LOG_INFO, tts_tag(), "[Player DEBUG] Active thread count : %d", ecore_thread_active_get());
1374 if (0 <= ttsd_data_get_sound_data_size(current->uid)) {
1375 SLOG(LOG_INFO, tts_tag(), "[Player] Run thread");
1376 ecore_thread_run(__play_thread, __end_play_thread, NULL, NULL);
1382 int ttsd_player_get_background_volume_ratio(double* ratio)
1387 *ratio = g_bg_volume_ratio;
1389 return TTSD_ERROR_NONE;
1392 int ttsd_player_set_background_volume_ratio(double ratio)
1394 SLOG(LOG_INFO, tts_tag(), "[Player DEBUG] ttsd_player_set_background_volume_ratio : %lf", ratio);
1396 g_bg_volume_ratio = ratio;
1397 return TTSD_ERROR_NONE;