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 int g_focus_watch_id;
91 player_s* __player_get_item(int uid)
94 player_s *data = NULL;
96 if (0 < g_list_length(g_player_list)) {
97 /* Get a first item */
98 iter = g_list_first(g_player_list);
100 while (NULL != iter) {
101 /* Get handle data from list */
102 data = (player_s*)iter->data;
105 if (uid == data->uid)
109 iter = g_list_next(iter);
116 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,
117 sound_stream_focus_change_reason_e reason_for_change, int sound_behavior, const char *extra_info, void *user_data)
119 SLOG(LOG_DEBUG, tts_tag(), "@@@ Focus state changed cb");
121 if (stream_info != g_stream_info_h) {
122 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Invalid stream info handle");
125 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);
127 if (AUDIO_STATE_PLAY == g_audio_state && focus_mask == SOUND_STREAM_FOCUS_FOR_PLAYBACK && SOUND_STREAM_FOCUS_STATE_RELEASED == focus_state) {
128 if (TTSD_MODE_DEFAULT == ttsd_get_mode()) {
129 g_audio_state = AUDIO_STATE_READY;
131 if (NULL == g_playing_info) {
132 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] No current player");
136 if (APP_STATE_PLAYING == g_playing_info->state) {
137 int uid = g_playing_info->uid;
139 if (0 != ttsd_player_pause(uid)) {
140 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to pause the player");
144 ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
145 int pid = ttsd_data_get_pid(uid);
146 /* send message to client about changing state */
147 ttsdc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
150 SLOG(LOG_DEBUG, tts_tag(), "[Player] Ignore focus state cb - mode(%d)", ttsd_get_mode());
154 /* if (AUDIO_STATE_READY == g_audio_state && focus_mask == SOUND_STREAM_FOCUS_FOR_PLAYBACK && SOUND_STREAM_FOCUS_STATE_ACQUIRED == focus_state) {
155 if (TTSD_MODE_DEFAULT == ttsd_get_mode()) {
156 g_audio_state = AUDIO_STATE_PLAY;
158 if (NULL == g_playing_info) {
159 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] No current player");
163 if (APP_STATE_PAUSED == g_playing_info->state) {
164 int uid = g_playing_info->uid;
166 g_audio_state = AUDIO_STATE_PLAY;
167 if (0 != ttsd_player_resume(uid)) {
168 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to resume the player");
169 g_audio_state = AUDIO_STATE_READY;
173 ttsd_data_set_client_state(uid, APP_STATE_PLAYING);
174 int pid = ttsd_data_get_pid(uid);
175 ttsdc_send_set_state_message(pid, uid, APP_STATE_PLAYING);
179 SLOG(LOG_DEBUG, tts_tag(), "[Player] Ignore focus state cb - mode(%d)", ttsd_get_mode());
183 SLOG(LOG_DEBUG, tts_tag(), "@@@");
188 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,
189 const char *extra_info, void *user_data)
191 SLOG(LOG_DEBUG, tts_tag(), "@@@ Focus state watch cb");
193 ttsd_mode_e mode = ttsd_get_mode();
195 if (TTSD_MODE_SCREEN_READER != mode && TTSD_MODE_NOTIFICATION != mode) {
196 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] This is not screen-reader mode and notification mode.");
200 if (AUDIO_STATE_PLAY == g_audio_state && SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_INFORMATION == reason &&
201 NULL != extra_info && 0 == strncmp(extra_info, "TTSD_MODE_INTERRUPT", strlen(extra_info))) {
202 /* If the focus is changed by "Interrupt" mode and current players of "SR" and "Noti" modes are on going, please stop the current players. */
203 g_audio_state = AUDIO_STATE_READY;
205 if (NULL == g_playing_info) {
206 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] No current player");
210 if (APP_STATE_PLAYING == g_playing_info->state) {
211 int uid = g_playing_info->uid;
213 if (0 != ttsd_server_stop(uid)) {
214 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to stop TTS server");
217 if (0 != ttsd_player_stop(uid)) {
218 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to stop the player");
222 ttsd_data_set_client_state(uid, APP_STATE_READY);
223 int pid = ttsd_data_get_pid(uid);
224 /* send message to client about changing state */
225 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
227 SLOG(LOG_DEBUG, tts_tag(), "[Player] Not playing state");
230 SLOG(LOG_DEBUG, tts_tag(), "[Player] This is not Interrupt mode or not playing state.");
236 static int __create_audio_out(ttse_audio_type_e type, int rate)
239 audio_sample_type_e sample_type;
241 if (TTSE_AUDIO_TYPE_RAW_S16 == type) {
242 sample_type = AUDIO_SAMPLE_TYPE_S16_LE;
244 sample_type = AUDIO_SAMPLE_TYPE_U8;
247 ret = audio_out_create_new(rate, AUDIO_CHANNEL_MONO, sample_type, &g_audio_h);
248 if (AUDIO_IO_ERROR_NONE != ret) {
249 g_audio_state = AUDIO_STATE_NONE;
251 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to create audio");
254 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Create audio");
258 g_sampling_rate = rate;
260 g_audio_state = AUDIO_STATE_READY;
265 static int __destroy_audio_out()
267 if (NULL == g_audio_h) {
268 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Current handle is not valid");
273 ret = audio_out_destroy(g_audio_h);
274 if (AUDIO_IO_ERROR_NONE != ret) {
275 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to destroy audio");
278 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Destroy audio");
284 g_audio_state = AUDIO_STATE_NONE;
290 static void __end_play_thread(void *data, Ecore_Thread *thread)
292 SLOG(LOG_ERROR, tts_tag(), "@@@ End thread");
299 static void __set_policy_for_playing(int volume)
301 /* Set stream info */
303 ttsd_mode_e mode = ttsd_get_mode();
304 if (TTSD_MODE_DEFAULT == mode) {
305 ret = sound_manager_acquire_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, SOUND_BEHAVIOR_NONE, NULL);
306 if (SOUND_MANAGER_ERROR_NONE != ret) {
307 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to acquire focus");
309 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Success to acquire focus (default mode)");
311 } else if (TTSD_MODE_INTERRUPT == mode) {
312 ret = sound_manager_acquire_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, SOUND_BEHAVIOR_NONE, "TTSD_MODE_INTERRUPT");
313 if (SOUND_MANAGER_ERROR_NONE != ret) {
314 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to acquire focus");
316 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Success to acquire focus (interrupt mode)");
320 ret = audio_out_set_sound_stream_info(g_audio_h, g_stream_info_h);
321 if (AUDIO_IO_ERROR_NONE != ret) {
322 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to set stream info");
325 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] set policy for playing");
330 static void __unset_policy_for_playing()
333 ttsd_mode_e mode = ttsd_get_mode();
334 /* Unset stream info */
335 if (TTSD_MODE_DEFAULT == mode || TTSD_MODE_INTERRUPT == mode) {
336 sound_stream_focus_state_e state_for_playing = SOUND_STREAM_FOCUS_STATE_ACQUIRED;
337 ret = sound_manager_get_focus_state(g_stream_info_h, &state_for_playing, NULL);
338 if (SOUND_MANAGER_ERROR_NONE != ret) {
339 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to get focus state: %d", ret);
342 if (SOUND_STREAM_FOCUS_STATE_ACQUIRED == state_for_playing) {
343 if (TTSD_MODE_DEFAULT == mode || TTSD_MODE_INTERRUPT == mode) {
344 ret = sound_manager_release_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, SOUND_BEHAVIOR_NONE, NULL);
345 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] release focus (mode: %d)", mode);
348 if (SOUND_MANAGER_ERROR_NONE != ret) {
349 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to release focus");
354 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] unset policy for playing");
359 int ttsd_player_check_current_playback_focus(bool *is_current_interrupt)
362 ttsd_mode_e mode = ttsd_get_mode();
364 if (TTSD_MODE_INTERRUPT != mode) {
365 /* check the current playback focus */
366 sound_stream_focus_change_reason_e reason;
367 int sound_behavior = 0;
368 char *extra_info = NULL;
370 ret = sound_manager_get_current_playback_focus(&reason, &sound_behavior, &extra_info);
372 SLOG(LOG_DEBUG, tts_tag(), "[Player] current playback focus: extra_info(%s), reason(%d), sound_behavior(%d)", extra_info, reason, sound_behavior);
374 if (SOUND_MANAGER_ERROR_NONE == ret && NULL != extra_info && 0 < strlen(extra_info)) {
375 if (SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_INFORMATION == reason && 0 == strncmp(extra_info, "TTSD_MODE_INTERRUPT", strlen(extra_info))) {
376 SLOG(LOG_DEBUG, tts_tag(), "[Player] The current focus in Interrupt. Cannot play the requested sound data");
377 *is_current_interrupt = true;
382 return TTSD_ERROR_NONE;
386 if (NULL != extra_info) {
392 *is_current_interrupt = false;
394 return TTSD_ERROR_NONE;
397 static void __play_thread(void *data, Ecore_Thread *thread)
399 SLOG(LOG_DEBUG, tts_tag(), "@@@ Start thread");
405 snprintf(g_temp_file_name, sizeof(g_temp_file_name), "/tmp/tts_temp_%d_%d", getpid(), g_count);
406 int ret = access(g_temp_file_name, 0);
409 SLOG(LOG_ERROR, tts_tag(), "[Recorder ERROR] File is already exist");
410 if (0 == remove(g_temp_file_name)) {
411 SLOG(LOG_DEBUG, tts_tag(), "[Recorder] Remove file");
421 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[Recorder] Temp file name=[%s]", g_temp_file_name);
424 g_pFile = fopen(g_temp_file_name, "wb+x");
426 SLOG(LOG_ERROR, tts_tag(), "[Recorder ERROR] File not found!");
431 if (NULL == g_playing_info) {
432 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] No current player");
436 player_s* player = g_playing_info;
437 sound_data_s* sound_data = NULL;
440 int len = SOUND_BUFFER_LENGTH;
443 /* set volume policy as 40% */
444 __set_policy_for_playing(40);
446 if (true == player->is_paused_data) {
448 sound_data = player->paused_data;
449 player->paused_data = NULL;
453 player->is_paused_data = false;
456 if (NULL == sound_data) {
457 /* Request unprepare */
458 ret = audio_out_unprepare(g_audio_h);
459 if (AUDIO_IO_ERROR_NONE != ret) {
460 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
462 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
465 g_audio_state = AUDIO_STATE_READY;
467 /* unset volume policy, volume will be 100% */
468 __unset_policy_for_playing();
471 SLOG(LOG_INFO, tts_tag(), "[Player] Sound info : id(%d) data(%p) size(%d) audiotype(%d) rate(%d) event(%d)",
472 sound_data->utt_id, sound_data->data, sound_data->data_size, sound_data->audio_type, sound_data->rate, sound_data->event);
475 ret = ttsd_data_get_sound_data(player->uid, &sound_data);
476 if (0 != ret || NULL == sound_data) {
478 SLOG(LOG_DEBUG, tts_tag(), "[Player] No sound data. Waiting mode");
480 /* wait for new audio data come */
483 if (NULL == g_playing_info) {
484 /* current playing uid is replaced */
485 SLOG(LOG_INFO, tts_tag(), "[Player] Finish thread");
486 if (AUDIO_STATE_PLAY == g_audio_state) {
487 /* release audio & recover session */
488 ret = audio_out_unprepare(g_audio_h);
489 if (AUDIO_IO_ERROR_NONE != ret) {
490 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
492 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
494 g_audio_state = AUDIO_STATE_READY;
496 /* unset volume policy, volume will be 100% */
497 __unset_policy_for_playing();
499 } else if (0 < ttsd_data_get_sound_data_size(player->uid)) {
500 /* new audio data come */
501 SLOG(LOG_INFO, tts_tag(), "[Player] Resume thread");
505 /* If engine is not on processing */
506 if (TTSD_SYNTHESIS_CONTROL_DOING != ttsd_get_synth_control()) {
507 if (AUDIO_STATE_PLAY == g_audio_state) {
508 /* release audio & recover session */
509 ret = audio_out_unprepare(g_audio_h);
510 if (AUDIO_IO_ERROR_NONE != ret) {
511 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
513 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
515 g_audio_state = AUDIO_STATE_READY;
517 /* unset volume policy, volume will be 100% */
518 __unset_policy_for_playing();
523 SLOG(LOG_INFO, tts_tag(), "[Player] Finish to wait for new audio data come");
525 if (AUDIO_STATE_READY == g_audio_state) {
526 /* set volume policy as 40%, when resume play thread*/
527 __set_policy_for_playing(40);
530 /* resume play thread */
531 player->state = APP_STATE_PLAYING;
535 /* If wdata's event is 'start', current wdata is first data of engine for synthesis.
536 * If wdata's event is 'finish', player should check previous event to know whether this wdata is first or not.
537 * When previous wdata's event is 'finish' and current wdata's event is 'finish',
538 * the player should send utt started event.
540 if (TTSE_RESULT_EVENT_START == sound_data->event ||
541 (TTSE_RESULT_EVENT_FINISH == player->event && TTSE_RESULT_EVENT_FINISH == sound_data->event)) {
542 int pid = ttsd_data_get_pid(player->uid);
545 SLOG(LOG_WARN, tts_tag(), "[Send WARNIING] Current player is not valid");
546 /* unset volume policy, volume will be 100% */
547 __unset_policy_for_playing();
552 if (0 != ttsdc_send_utt_start_message(pid, player->uid, sound_data->utt_id)) {
553 SLOG(LOG_ERROR, tts_tag(), "[Send ERROR] Fail to send Utterance Start Signal : pid(%d), uid(%d), uttid(%d)",
554 pid, player->uid, sound_data->utt_id);
556 SLOG(LOG_INFO, tts_tag(), "[Player] Start utterance : uid(%d), uttid(%d)", player->uid, sound_data->utt_id);
559 /* Save last event to check utterance start */
560 player->event = sound_data->event;
563 if (NULL == sound_data->data || 0 >= sound_data->data_size) {
564 if (TTSE_RESULT_EVENT_FINISH == sound_data->event) {
565 SLOG(LOG_DEBUG, tts_tag(), "No sound data");
566 /* send utterence finish signal */
567 int pid = ttsd_data_get_pid(player->uid);
570 SLOG(LOG_WARN, tts_tag(), "[Send WARNIING] Current player is not valid");
571 /* unset volume policy, volume will be 100% */
572 __unset_policy_for_playing();
576 __unset_policy_for_playing();
578 if (0 != ttsdc_send_utt_finish_message(pid, player->uid, sound_data->utt_id)) {
579 SLOG(LOG_ERROR, tts_tag(), "[Send ERROR] Fail to send Utterance Completed Signal : pid(%d), uid(%d), uttid(%d)",
580 pid, player->uid, sound_data->utt_id);
582 SLOG(LOG_INFO, tts_tag(), "[Player] Finish utterance : uid(%d), uttid(%d)", player->uid, sound_data->utt_id);
585 SLOG(LOG_INFO, tts_tag(), "[Player] Event(%d) utterance : uid(%d), uttid(%d)", sound_data->event, player->uid, sound_data->utt_id);
586 ttsd_data_clear_sound_data(player->uid, &sound_data);
591 if (g_sampling_rate != sound_data->rate || g_audio_type != sound_data->audio_type) {
592 SLOG(LOG_INFO, tts_tag(), "[Player] Change audio handle : org type(%d) org rate(%d)", g_audio_type, g_sampling_rate);
593 if (NULL != g_audio_h) {
594 __destroy_audio_out();
597 if (0 > __create_audio_out(sound_data->audio_type, sound_data->rate)) {
598 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to create audio out");
599 /* unset volume policy, volume will be 100% */
600 __unset_policy_for_playing();
602 ttsd_data_clear_sound_data(player->uid, &sound_data);
607 SLOG(LOG_INFO, tts_tag(), "[Player INFO] Success to destroy and recreate audio out");
608 __set_policy_for_playing(40);
611 while (APP_STATE_PLAYING == player->state || APP_STATE_PAUSED == player->state) {
612 if ((unsigned int)idx >= sound_data->data_size)
615 if ((unsigned int)idx + SOUND_BUFFER_LENGTH > sound_data->data_size) {
616 len = sound_data->data_size - idx;
618 len = SOUND_BUFFER_LENGTH;
621 if (AUDIO_STATE_READY == g_audio_state) {
622 /* Request prepare */
623 ret = audio_out_prepare(g_audio_h);
624 if (AUDIO_IO_ERROR_NONE != ret) {
625 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to prepare audio : %d", ret);
626 g_playing_info = NULL;
627 /* unset volume policy, volume will be 100% */
628 __unset_policy_for_playing();
630 ttsd_data_clear_sound_data(player->uid, &sound_data);
634 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Prepare audio");
635 g_audio_state = AUDIO_STATE_PLAY;
638 char* temp_data = sound_data->data;
639 SLOG(LOG_INFO, tts_tag(), "[Player INFO] Before audio_out_write. data(%p), data[%d](%p), uid(%d), utt_id(%d), len(%d)",
640 temp_data, idx, &temp_data[idx], player->uid, sound_data->utt_id, len);
642 /* write pcm buffer */
643 fwrite(&temp_data[idx], 1, len, g_pFile);
645 ret = audio_out_write(g_audio_h, &temp_data[idx], len);
647 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to audio write - %d", ret);
650 SLOG(LOG_INFO, tts_tag(), "[Player INFO] After audio_out_write");
653 if (NULL == g_playing_info && APP_STATE_PAUSED != player->state) {
654 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Current player is NULL");
655 g_audio_state = AUDIO_STATE_READY;
656 ret = audio_out_unprepare(g_audio_h);
657 if (AUDIO_IO_ERROR_NONE != ret) {
658 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
660 /* unset volume policy, volume will be 100% */
661 __unset_policy_for_playing();
663 ttsd_data_clear_sound_data(player->uid, &sound_data);
668 if (APP_STATE_PAUSED == player->state) {
670 SLOG(LOG_DEBUG, tts_tag(), "[Player] player(%p)", player);
671 player->paused_data = sound_data;
673 player->is_paused_data = true;
676 g_audio_state = AUDIO_STATE_READY;
678 SLOG(LOG_INFO, tts_tag(), "[Player] Stop player thread by pause");
680 /* Request prepare */
681 ret = audio_out_unprepare(g_audio_h);
682 if (AUDIO_IO_ERROR_NONE != ret) {
683 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
685 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
687 /* unset volume policy, volume will be 100% */
688 __unset_policy_for_playing();
693 if (NULL == g_playing_info && APP_STATE_READY == player->state) {
695 g_audio_state = AUDIO_STATE_READY;
696 SLOG(LOG_DEBUG, tts_tag(), "[Player] Stop player thread");
698 /* Request prepare */
699 ret = audio_out_unprepare(g_audio_h);
700 if (AUDIO_IO_ERROR_NONE != ret) {
701 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
703 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
706 ttsd_data_clear_sound_data(player->uid, &sound_data);
707 /* unset volume policy, volume will be 100% */
708 __unset_policy_for_playing();
712 if ((APP_STATE_PLAYING == player->state || APP_STATE_PAUSED == player->state) &&
713 (TTSE_RESULT_EVENT_FINISH == sound_data->event)) {
714 /* send utterence finish signal */
715 int pid = ttsd_data_get_pid(player->uid);
718 SLOG(LOG_WARN, tts_tag(), "[Send WARNIING] Current player is not valid");
719 /* unset volume policy, volume will be 100% */
720 __unset_policy_for_playing();
725 if (0 != ttsdc_send_utt_finish_message(pid, player->uid, sound_data->utt_id)) {
726 SLOG(LOG_ERROR, tts_tag(), "[Send ERROR] Fail to send Utterance Completed Signal : pid(%d), uid(%d), uttid(%d)",
727 pid, player->uid, sound_data->utt_id);
728 /* unset volume policy, volume will be 100% */
729 __unset_policy_for_playing();
731 ttsd_data_clear_sound_data(player->uid, &sound_data);
735 SLOG(LOG_INFO, tts_tag(), "[Player] Finish utterance : uid(%d), uttid(%d)", player->uid, sound_data->utt_id);
738 ttsd_data_clear_sound_data(player->uid, &sound_data);
740 if (NULL == g_playing_info) {
741 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Current player is NULL");
742 g_audio_state = AUDIO_STATE_READY;
743 ret = audio_out_unprepare(g_audio_h);
744 if (AUDIO_IO_ERROR_NONE != ret) {
745 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
747 /* unset volume policy, volume will be 100% */
748 __unset_policy_for_playing();
758 int ttsd_player_init()
760 g_playing_info = NULL;
761 g_audio_state = AUDIO_STATE_NONE;
766 ecore_thread_max_set(1);
770 if (0 == access(FOCUS_SERVER_READY, F_OK)) {
771 SLOG(LOG_ERROR, tts_tag(), "[Player SUCCESS] focus server is available");
775 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] focus server is not available");
780 ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_VOICE_INFORMATION, __player_focus_state_cb, NULL, &g_stream_info_h);
781 if (SOUND_MANAGER_ERROR_NONE != ret) {
782 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to create stream info");
785 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Create stream info");
788 /* add sound focus state watch callback */
789 ret = sound_manager_add_focus_state_watch_cb(SOUND_STREAM_FOCUS_FOR_PLAYBACK, __player_focus_state_watch_cb, NULL, &g_focus_watch_id);
790 if (SOUND_MANAGER_ERROR_NONE != ret) {
791 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to add sound focus watch callback");
792 sound_manager_destroy_stream_information(g_stream_info_h);
795 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Add sound focus watch callback");
798 ecore_thread_max_set(1);
800 ret = __create_audio_out(TTSE_AUDIO_TYPE_RAW_S16, 16000);
802 sound_manager_destroy_stream_information(g_stream_info_h);
803 sound_manager_remove_focus_state_watch_cb(g_focus_watch_id);
807 g_player_init = true;
812 int ttsd_player_release(void)
814 if (false == g_player_init) {
815 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
816 return TTSD_ERROR_OPERATION_FAILED;
821 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] @@@@@");
822 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Active thread count : %d", ecore_thread_active_get());
823 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] @@@@@");
825 /* The thread should be released */
826 int thread_count = ecore_thread_active_get();
828 while (0 < thread_count) {
833 SLOG(LOG_WARN, tts_tag(), "[Player WARNING!!] Thread is blocked. Player release continue.");
837 thread_count = ecore_thread_active_get();
840 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Thread is released");
842 ret = __destroy_audio_out();
846 ret = sound_manager_destroy_stream_information(g_stream_info_h);
847 if (SOUND_MANAGER_ERROR_NONE != ret) {
848 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to destroy stream info");
850 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Destroy stream info");
853 /* remove focus state watch callback */
854 ret = sound_manager_remove_focus_state_watch_cb(g_focus_watch_id);
855 if (SOUND_MANAGER_ERROR_NONE != ret) {
856 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to remove the focus state watch cb");
858 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Remove the focus state watch cb");
861 /* clear g_player_list */
862 g_playing_info = NULL;
863 g_player_init = false;
868 int ttsd_player_create_instance(int uid)
870 if (false == g_player_init) {
871 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
875 /* Check uid is duplicated */
876 if (NULL != __player_get_item(uid)) {
877 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is already registered", uid);
881 player_s* new_client = (player_s*)calloc(1, sizeof(player_s));
882 if (NULL == new_client) {
883 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to allocate memory");
884 return TTSE_ERROR_OUT_OF_MEMORY;
887 new_client->uid = uid;
888 new_client->event = TTSE_RESULT_EVENT_FINISH;
889 new_client->state = APP_STATE_READY;
890 new_client->is_paused_data = false;
892 new_client->paused_data = NULL;
894 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[Player] Create player : uid(%d)", uid);
896 g_player_list = g_list_append(g_player_list, new_client);
901 int ttsd_player_destroy_instance(int uid)
903 if (false == g_player_init) {
904 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
909 current = __player_get_item(uid);
910 if (NULL == current) {
911 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
915 if (NULL != g_playing_info) {
916 if (uid == g_playing_info->uid) {
917 g_playing_info = NULL;
922 player_s *data = NULL;
924 if (0 < g_list_length(g_player_list)) {
925 /* Get a first item */
926 iter = g_list_first(g_player_list);
928 while (NULL != iter) {
929 /* Get handle data from list */
930 data = (player_s*)iter->data;
934 if (uid == data->uid) {
935 g_player_list = g_list_remove_link(g_player_list, iter);
943 iter = g_list_next(iter);
947 SLOG(LOG_DEBUG, tts_tag(), "[PLAYER Success] Destroy instance");
952 int ttsd_player_play(int uid)
954 if (false == g_player_init) {
955 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
959 if (NULL != g_playing_info) {
960 if (uid == g_playing_info->uid) {
961 SLOG(LOG_DEBUG, tts_tag(), "[Player] uid(%d) has already played", g_playing_info->uid);
964 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] stop old player (%d)", g_playing_info->uid);
965 ttsd_player_stop(g_playing_info->uid);
969 SLOG(LOG_DEBUG, tts_tag(), "[Player] start play : uid(%d)", uid);
971 /* Check sound queue size */
972 if (0 == ttsd_data_get_sound_data_size(uid)) {
973 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] A sound queue of current player(%d) is empty", uid);
979 current = __player_get_item(uid);
980 if (NULL == current) {
981 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
985 current->state = APP_STATE_PLAYING;
987 g_playing_info = current;
989 SLOG(LOG_INFO, tts_tag(), "[Player DEBUG] Active thread count : %d", ecore_thread_active_get());
991 if (0 < ttsd_data_get_sound_data_size(current->uid)) {
992 SLOG(LOG_INFO, tts_tag(), "[Player] Run thread");
993 ecore_thread_run(__play_thread, __end_play_thread, NULL, NULL);
999 int ttsd_player_stop(int uid)
1001 if (false == g_player_init) {
1002 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1008 current = __player_get_item(uid);
1009 if (NULL == current) {
1010 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
1014 /* check whether uid is current playing or not */
1015 if (NULL != g_playing_info) {
1016 if (uid == g_playing_info->uid) {
1017 /* release current playing info */
1018 g_playing_info = NULL;
1021 SLOG(LOG_DEBUG, tts_tag(), "[Player] No current playing");
1024 if (true == current->is_paused_data) {
1025 if (NULL != current->paused_data) {
1026 if (NULL != current->paused_data->data) {
1027 free(current->paused_data->data);
1028 current->paused_data->data = NULL;
1031 free(current->paused_data);
1032 current->paused_data = NULL;
1036 current->event = TTSE_RESULT_EVENT_FINISH;
1037 current->state = APP_STATE_READY;
1038 current->is_paused_data = false;
1041 if (NULL == g_playing_info) {
1042 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1043 SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
1044 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1046 /* The thread should be released */
1047 int thread_count = ecore_thread_active_get();
1049 while (0 < thread_count) {
1054 SLOG(LOG_WARN, tts_tag(), "[Player WARNING!!] Thread is blocked. Player release continue.");
1058 thread_count = ecore_thread_active_get();
1061 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1062 SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
1063 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1066 SLOG(LOG_INFO, tts_tag(), "[Player SUCCESS] Stop player : uid(%d)", uid);
1071 int ttsd_player_clear(int uid)
1073 if (false == g_player_init) {
1074 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1080 current = __player_get_item(uid);
1081 if (NULL == current) {
1082 SECURE_SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
1086 if (true == current->is_paused_data) {
1087 if (NULL != current->paused_data) {
1088 if (NULL != current->paused_data->data) {
1089 free(current->paused_data->data);
1090 current->paused_data->data = NULL;
1093 free(current->paused_data);
1094 current->paused_data = NULL;
1098 current->event = TTSE_RESULT_EVENT_FINISH;
1099 current->state = APP_STATE_READY;
1100 current->is_paused_data = false;
1103 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Clear player : uid(%d)", uid);
1108 int ttsd_player_pause(int uid)
1110 SLOG(LOG_DEBUG, tts_tag(), "[Player] pause player : uid(%d)", uid);
1112 if (false == g_player_init) {
1113 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1119 current = __player_get_item(uid);
1120 if (NULL == current) {
1121 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] ttsd_player_pause() : uid(%d) is not valid", uid);
1125 /* check whether uid is current playing or not */
1126 if (NULL != g_playing_info) {
1127 if (uid == g_playing_info->uid) {
1128 /* release current playing info */
1129 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] release current playing info (%d)", uid);
1130 g_playing_info = NULL;
1136 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] current player (%p), g_playing_info(%p)", current, g_playing_info);
1138 current->state = APP_STATE_PAUSED;
1140 if (NULL == g_playing_info) {
1141 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1142 SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
1143 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1145 /* The thread should be released */
1146 int thread_count = ecore_thread_active_get();
1148 while (0 < thread_count) {
1152 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] current(%p), state(%d)", current, current->state);
1155 SLOG(LOG_WARN, tts_tag(), "[Player WARNING!!] Thread is blocked. Player release continue. current(%p) current state(%d)", current, current->state);
1159 thread_count = ecore_thread_active_get();
1162 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1163 SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
1164 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1168 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Pause player : uid(%d)", uid);
1173 int ttsd_player_resume(int uid)
1175 SLOG(LOG_DEBUG, tts_tag(), "[Player] Resume player : uid(%d)", uid);
1177 if (false == g_player_init) {
1178 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1184 current = __player_get_item(uid);
1185 if (NULL == current) {
1186 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
1190 /* check current player */
1191 if (NULL != g_playing_info)
1192 g_playing_info = NULL;
1194 current->state = APP_STATE_PLAYING;
1195 g_playing_info = current;
1197 SLOG(LOG_INFO, tts_tag(), "[Player] Resume to run thread");
1198 ecore_thread_run(__play_thread, __end_play_thread, NULL, NULL);
1203 int ttsd_player_all_stop()
1205 if (false == g_player_init) {
1206 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1210 g_playing_info = NULL;
1213 player_s *data = NULL;
1215 if (0 < g_list_length(g_player_list)) {
1216 /* Get a first item */
1217 iter = g_list_first(g_player_list);
1219 while (NULL != iter) {
1220 /* Get handle data from list */
1221 data = (player_s*)iter->data;
1223 app_tts_state_e state;
1224 if (0 > ttsd_data_get_client_state(data->uid, &state)) {
1225 SLOG(LOG_ERROR, tts_tag(), "[player ERROR] uid(%d) is not valid", data->uid);
1226 ttsd_player_destroy_instance(data->uid);
1227 iter = g_list_next(iter);
1231 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1232 data->event = TTSE_RESULT_EVENT_FINISH;
1233 data->state = APP_STATE_READY;
1235 if (true == data->is_paused_data) {
1236 if (NULL != data->paused_data) {
1237 if (NULL != data->paused_data->data) {
1238 free(data->paused_data->data);
1239 data->paused_data->data = NULL;
1242 free(data->paused_data);
1243 data->paused_data = NULL;
1247 data->is_paused_data = false;
1252 iter = g_list_next(iter);
1256 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] player all stop!!");
1261 int ttsd_player_play_pcm(int uid)
1263 if (false == g_player_init) {
1264 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1268 if (NULL != g_playing_info) {
1269 if (uid == g_playing_info->uid) {
1270 SLOG(LOG_DEBUG, tts_tag(), "[Player] uid(%d) has already played", g_playing_info->uid);
1273 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] stop old player (%d)", g_playing_info->uid);
1274 ttsd_player_stop(g_playing_info->uid);
1278 SLOG(LOG_DEBUG, tts_tag(), "[Player] start play : uid(%d)", uid);
1280 /* Check sound queue size */
1281 if (0 == ttsd_data_get_sound_data_size(uid)) {
1282 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] A sound queue of current player(%d) is empty", uid);
1287 current = __player_get_item(uid);
1288 if (NULL == current) {
1289 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
1293 current->state = APP_STATE_PLAYING;
1295 g_playing_info = current;
1297 SLOG(LOG_INFO, tts_tag(), "[Player DEBUG] Active thread count : %d", ecore_thread_active_get());
1299 if (0 <= ttsd_data_get_sound_data_size(current->uid)) {
1300 SLOG(LOG_INFO, tts_tag(), "[Player] Run thread");
1301 ecore_thread_run(__play_thread, __end_play_thread, NULL, NULL);