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 /** player init info */
53 static bool g_player_init = false;
56 static GList *g_player_list;
58 /** current player information */
59 static player_s* g_playing_info;
62 static audio_state_e g_audio_state;
64 static ttse_audio_type_e g_audio_type;
66 static int g_sampling_rate;
68 static audio_out_h g_audio_h;
70 static sound_stream_info_h g_stream_info_h;
72 static int g_focus_watch_id;
79 player_s* __player_get_item(int uid)
82 player_s *data = NULL;
84 if (0 < g_list_length(g_player_list)) {
85 /* Get a first item */
86 iter = g_list_first(g_player_list);
88 while (NULL != iter) {
89 /* Get handle data from list */
90 data = (player_s*)iter->data;
97 iter = g_list_next(iter);
104 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,
105 sound_stream_focus_change_reason_e reason_for_change, int sound_behavior, const char *extra_info, void *user_data)
107 SLOG(LOG_DEBUG, tts_tag(), "@@@ Focus state changed cb");
109 if (stream_info != g_stream_info_h) {
110 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Invalid stream info handle");
113 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);
115 if (AUDIO_STATE_PLAY == g_audio_state && focus_mask == SOUND_STREAM_FOCUS_FOR_PLAYBACK && SOUND_STREAM_FOCUS_STATE_RELEASED == focus_state) {
116 if (TTSD_MODE_DEFAULT == ttsd_get_mode()) {
117 g_audio_state = AUDIO_STATE_READY;
119 if (NULL == g_playing_info) {
120 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] No current player");
124 if (APP_STATE_PLAYING == g_playing_info->state) {
125 int uid = g_playing_info->uid;
127 if (0 != ttsd_player_pause(uid)) {
128 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to pause the player");
132 ttsd_data_set_client_state(uid, APP_STATE_PAUSED);
133 int pid = ttsd_data_get_pid(uid);
134 /* send message to client about changing state */
135 ttsdc_send_set_state_message(pid, uid, APP_STATE_PAUSED);
138 SLOG(LOG_DEBUG, tts_tag(), "[Player] Ignore focus state cb - mode(%d)", ttsd_get_mode());
142 /* if (AUDIO_STATE_READY == g_audio_state && focus_mask == SOUND_STREAM_FOCUS_FOR_PLAYBACK && SOUND_STREAM_FOCUS_STATE_ACQUIRED == focus_state) {
143 if (TTSD_MODE_DEFAULT == ttsd_get_mode()) {
144 g_audio_state = AUDIO_STATE_PLAY;
146 if (NULL == g_playing_info) {
147 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] No current player");
151 if (APP_STATE_PAUSED == g_playing_info->state) {
152 int uid = g_playing_info->uid;
154 g_audio_state = AUDIO_STATE_PLAY;
155 if (0 != ttsd_player_resume(uid)) {
156 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to resume the player");
157 g_audio_state = AUDIO_STATE_READY;
161 ttsd_data_set_client_state(uid, APP_STATE_PLAYING);
162 int pid = ttsd_data_get_pid(uid);
163 ttsdc_send_set_state_message(pid, uid, APP_STATE_PLAYING);
167 SLOG(LOG_DEBUG, tts_tag(), "[Player] Ignore focus state cb - mode(%d)", ttsd_get_mode());
171 SLOG(LOG_DEBUG, tts_tag(), "@@@");
176 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,
177 const char *extra_info, void *user_data)
179 SLOG(LOG_DEBUG, tts_tag(), "@@@ Focus state watch cb");
181 ttsd_mode_e mode = ttsd_get_mode();
183 if (TTSD_MODE_SCREEN_READER != mode && TTSD_MODE_NOTIFICATION != mode) {
184 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] This is not screen-reader mode and notification mode.");
188 if (AUDIO_STATE_PLAY == g_audio_state && SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_INFORMATION == reason &&
189 NULL != extra_info && 0 == strncmp(extra_info, "TTSD_MODE_INTERRUPT", strlen(extra_info))) {
190 /* If the focus is changed by "Interrupt" mode and current players of "SR" and "Noti" modes are on going, please stop the current players. */
191 g_audio_state = AUDIO_STATE_READY;
193 if (NULL == g_playing_info) {
194 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] No current player");
198 if (APP_STATE_PLAYING == g_playing_info->state) {
199 int uid = g_playing_info->uid;
201 if (0 != ttsd_server_stop(uid)) {
202 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to stop TTS server");
205 if (0 != ttsd_player_stop(uid)) {
206 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to stop the player");
210 ttsd_data_set_client_state(uid, APP_STATE_READY);
211 int pid = ttsd_data_get_pid(uid);
212 /* send message to client about changing state */
213 ttsdc_send_set_state_message(pid, uid, APP_STATE_READY);
215 SLOG(LOG_DEBUG, tts_tag(), "[Player] Not playing state");
218 SLOG(LOG_DEBUG, tts_tag(), "[Player] This is not Interrupt mode or not playing state.");
224 static int __create_audio_out(ttse_audio_type_e type, int rate)
227 audio_sample_type_e sample_type;
229 if (TTSE_AUDIO_TYPE_RAW_S16 == type) {
230 sample_type = AUDIO_SAMPLE_TYPE_S16_LE;
232 sample_type = AUDIO_SAMPLE_TYPE_U8;
235 ret = audio_out_create_new(rate, AUDIO_CHANNEL_MONO, sample_type, &g_audio_h);
236 if (AUDIO_IO_ERROR_NONE != ret) {
237 g_audio_state = AUDIO_STATE_NONE;
239 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to create audio");
242 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Create audio");
246 g_sampling_rate = rate;
248 g_audio_state = AUDIO_STATE_READY;
253 static int __destroy_audio_out()
255 if (NULL == g_audio_h) {
256 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Current handle is not valid");
261 ret = audio_out_destroy(g_audio_h);
262 if (AUDIO_IO_ERROR_NONE != ret) {
263 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to destroy audio");
266 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Destroy audio");
272 g_audio_state = AUDIO_STATE_NONE;
278 static void __end_play_thread(void *data, Ecore_Thread *thread)
280 SLOG(LOG_ERROR, tts_tag(), "@@@ End thread");
283 static void __set_policy_for_playing(int volume)
285 /* Set stream info */
287 ttsd_mode_e mode = ttsd_get_mode();
288 if (TTSD_MODE_DEFAULT == mode) {
289 ret = sound_manager_acquire_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, SOUND_BEHAVIOR_NONE, NULL);
290 if (SOUND_MANAGER_ERROR_NONE != ret) {
291 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to acquire focus");
293 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Success to acquire focus (default mode)");
295 } else if (TTSD_MODE_INTERRUPT == mode) {
296 ret = sound_manager_acquire_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, SOUND_BEHAVIOR_NONE, "TTSD_MODE_INTERRUPT");
297 if (SOUND_MANAGER_ERROR_NONE != ret) {
298 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to acquire focus");
300 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Success to acquire focus (interrupt mode)");
304 ret = audio_out_set_sound_stream_info(g_audio_h, g_stream_info_h);
305 if (AUDIO_IO_ERROR_NONE != ret) {
306 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to set stream info");
309 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] set policy for playing");
314 static void __unset_policy_for_playing()
317 ttsd_mode_e mode = ttsd_get_mode();
318 /* Unset stream info */
319 if (TTSD_MODE_DEFAULT == mode || TTSD_MODE_INTERRUPT == mode) {
320 sound_stream_focus_state_e state_for_playing = SOUND_STREAM_FOCUS_STATE_ACQUIRED;
321 ret = sound_manager_get_focus_state(g_stream_info_h, &state_for_playing, NULL);
322 if (SOUND_MANAGER_ERROR_NONE != ret) {
323 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to get focus state: %d", ret);
326 if (SOUND_STREAM_FOCUS_STATE_ACQUIRED == state_for_playing) {
327 if (TTSD_MODE_DEFAULT == mode || TTSD_MODE_INTERRUPT == mode) {
328 ret = sound_manager_release_focus(g_stream_info_h, SOUND_STREAM_FOCUS_FOR_PLAYBACK, SOUND_BEHAVIOR_NONE, NULL);
329 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] release focus (mode: %d)", mode);
332 if (SOUND_MANAGER_ERROR_NONE != ret) {
333 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to release focus");
338 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] unset policy for playing");
343 int ttsd_player_check_current_playback_focus(bool *is_current_interrupt)
346 ttsd_mode_e mode = ttsd_get_mode();
348 if (TTSD_MODE_INTERRUPT != mode) {
349 /* check the current playback focus */
350 sound_stream_focus_change_reason_e reason;
351 int sound_behavior = 0;
352 char *extra_info = NULL;
354 ret = sound_manager_get_current_playback_focus(&reason, &sound_behavior, &extra_info);
356 SLOG(LOG_DEBUG, tts_tag(), "[Player] current playback focus: extra_info(%s), reason(%d), sound_behavior(%d)", extra_info, reason, sound_behavior);
358 if (SOUND_MANAGER_ERROR_NONE == ret && NULL != extra_info && 0 < strlen(extra_info)) {
359 if (SOUND_STREAM_FOCUS_CHANGED_BY_VOICE_INFORMATION == reason && 0 == strncmp(extra_info, "TTSD_MODE_INTERRUPT", strlen(extra_info))) {
360 SLOG(LOG_DEBUG, tts_tag(), "[Player] The current focus in Interrupt. Cannot play the requested sound data");
361 *is_current_interrupt = true;
366 return TTSD_ERROR_NONE;
370 if (NULL != extra_info) {
376 *is_current_interrupt = false;
378 return TTSD_ERROR_NONE;
381 static void __play_thread(void *data, Ecore_Thread *thread)
383 SLOG(LOG_DEBUG, tts_tag(), "@@@ Start thread");
385 if (NULL == g_playing_info) {
386 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] No current player");
390 player_s* player = g_playing_info;
391 sound_data_s* sound_data = NULL;
394 int len = SOUND_BUFFER_LENGTH;
397 /* set volume policy as 40% */
398 __set_policy_for_playing(40);
400 if (true == player->is_paused_data) {
402 sound_data = player->paused_data;
403 player->paused_data = NULL;
407 player->is_paused_data = false;
410 if (NULL == sound_data) {
411 /* Request unprepare */
412 ret = audio_out_unprepare(g_audio_h);
413 if (AUDIO_IO_ERROR_NONE != ret) {
414 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
416 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
419 g_audio_state = AUDIO_STATE_READY;
421 /* unset volume policy, volume will be 100% */
422 __unset_policy_for_playing();
425 SLOG(LOG_INFO, tts_tag(), "[Player] Sound info : id(%d) data(%p) size(%d) audiotype(%d) rate(%d) event(%d)",
426 sound_data->utt_id, sound_data->data, sound_data->data_size, sound_data->audio_type, sound_data->rate, sound_data->event);
429 ret = ttsd_data_get_sound_data(player->uid, &sound_data);
430 if (0 != ret || NULL == sound_data) {
432 SLOG(LOG_DEBUG, tts_tag(), "[Player] No sound data. Waiting mode");
434 /* wait for new audio data come */
437 if (NULL == g_playing_info) {
438 /* current playing uid is replaced */
439 SLOG(LOG_INFO, tts_tag(), "[Player] Finish thread");
440 if (AUDIO_STATE_PLAY == g_audio_state) {
441 /* release audio & recover session */
442 ret = audio_out_unprepare(g_audio_h);
443 if (AUDIO_IO_ERROR_NONE != ret) {
444 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
446 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
448 g_audio_state = AUDIO_STATE_READY;
450 /* unset volume policy, volume will be 100% */
451 __unset_policy_for_playing();
453 } else if (0 < ttsd_data_get_sound_data_size(player->uid)) {
454 /* new audio data come */
455 SLOG(LOG_INFO, tts_tag(), "[Player] Resume thread");
459 /* If engine is not on processing */
460 if (TTSD_SYNTHESIS_CONTROL_DOING != ttsd_get_synth_control()) {
461 if (AUDIO_STATE_PLAY == g_audio_state) {
462 /* release audio & recover session */
463 ret = audio_out_unprepare(g_audio_h);
464 if (AUDIO_IO_ERROR_NONE != ret) {
465 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
467 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
469 g_audio_state = AUDIO_STATE_READY;
471 /* unset volume policy, volume will be 100% */
472 __unset_policy_for_playing();
477 SLOG(LOG_INFO, tts_tag(), "[Player] Finish to wait for new audio data come");
479 if (AUDIO_STATE_READY == g_audio_state) {
480 /* set volume policy as 40%, when resume play thread*/
481 __set_policy_for_playing(40);
484 /* resume play thread */
485 player->state = APP_STATE_PLAYING;
489 /* If wdata's event is 'start', current wdata is first data of engine for synthesis.
490 * If wdata's event is 'finish', player should check previous event to know whether this wdata is first or not.
491 * When previous wdata's event is 'finish' and current wdata's event is 'finish',
492 * the player should send utt started event.
494 if (TTSE_RESULT_EVENT_START == sound_data->event ||
495 (TTSE_RESULT_EVENT_FINISH == player->event && TTSE_RESULT_EVENT_FINISH == sound_data->event)) {
496 int pid = ttsd_data_get_pid(player->uid);
499 SLOG(LOG_WARN, tts_tag(), "[Send WARNIING] Current player is not valid");
500 /* unset volume policy, volume will be 100% */
501 __unset_policy_for_playing();
506 if (0 != ttsdc_send_utt_start_message(pid, player->uid, sound_data->utt_id)) {
507 SLOG(LOG_ERROR, tts_tag(), "[Send ERROR] Fail to send Utterance Start Signal : pid(%d), uid(%d), uttid(%d)",
508 pid, player->uid, sound_data->utt_id);
510 SLOG(LOG_INFO, tts_tag(), "[Player] Start utterance : uid(%d), uttid(%d)", player->uid, sound_data->utt_id);
513 /* Save last event to check utterance start */
514 player->event = sound_data->event;
517 if (NULL == sound_data->data || 0 >= sound_data->data_size) {
518 if (TTSE_RESULT_EVENT_FINISH == sound_data->event) {
519 SLOG(LOG_DEBUG, tts_tag(), "No sound data");
520 /* send utterence finish signal */
521 int pid = ttsd_data_get_pid(player->uid);
524 SLOG(LOG_WARN, tts_tag(), "[Send WARNIING] Current player is not valid");
525 /* unset volume policy, volume will be 100% */
526 __unset_policy_for_playing();
530 __unset_policy_for_playing();
532 if (0 != ttsdc_send_utt_finish_message(pid, player->uid, sound_data->utt_id)) {
533 SLOG(LOG_ERROR, tts_tag(), "[Send ERROR] Fail to send Utterance Completed Signal : pid(%d), uid(%d), uttid(%d)",
534 pid, player->uid, sound_data->utt_id);
536 SLOG(LOG_INFO, tts_tag(), "[Player] Finish utterance : uid(%d), uttid(%d)", player->uid, sound_data->utt_id);
539 SLOG(LOG_INFO, tts_tag(), "[Player] Event(%d) utterance : uid(%d), uttid(%d)", sound_data->event, player->uid, sound_data->utt_id);
540 ttsd_data_clear_sound_data(player->uid, &sound_data);
545 if (g_sampling_rate != sound_data->rate || g_audio_type != sound_data->audio_type) {
546 SLOG(LOG_INFO, tts_tag(), "[Player] Change audio handle : org type(%d) org rate(%d)", g_audio_type, g_sampling_rate);
547 if (NULL != g_audio_h) {
548 __destroy_audio_out();
551 if (0 > __create_audio_out(sound_data->audio_type, sound_data->rate)) {
552 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to create audio out");
553 /* unset volume policy, volume will be 100% */
554 __unset_policy_for_playing();
556 ttsd_data_clear_sound_data(player->uid, &sound_data);
561 SLOG(LOG_INFO, tts_tag(), "[Player INFO] Success to destroy and recreate audio out");
562 __set_policy_for_playing(40);
565 while (APP_STATE_PLAYING == player->state || APP_STATE_PAUSED == player->state) {
566 if ((unsigned int)idx >= sound_data->data_size)
569 if ((unsigned int)idx + SOUND_BUFFER_LENGTH > sound_data->data_size) {
570 len = sound_data->data_size - idx;
572 len = SOUND_BUFFER_LENGTH;
575 if (AUDIO_STATE_READY == g_audio_state) {
576 /* Request prepare */
577 ret = audio_out_prepare(g_audio_h);
578 if (AUDIO_IO_ERROR_NONE != ret) {
579 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to prepare audio : %d", ret);
580 g_playing_info = NULL;
581 /* unset volume policy, volume will be 100% */
582 __unset_policy_for_playing();
584 ttsd_data_clear_sound_data(player->uid, &sound_data);
588 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Prepare audio");
589 g_audio_state = AUDIO_STATE_PLAY;
592 char* temp_data = sound_data->data;
593 SLOG(LOG_INFO, tts_tag(), "[Player INFO] Before audio_out_write. data(%p), data[%d](%p), uid(%d), utt_id(%d), len(%d)",
594 temp_data, idx, &temp_data[idx], player->uid, sound_data->utt_id, len);
595 ret = audio_out_write(g_audio_h, &temp_data[idx], len);
597 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to audio write - %d", ret);
600 SLOG(LOG_INFO, tts_tag(), "[Player INFO] After audio_out_write");
603 if (NULL == g_playing_info && APP_STATE_PAUSED != player->state) {
604 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Current player is NULL");
605 g_audio_state = AUDIO_STATE_READY;
606 ret = audio_out_unprepare(g_audio_h);
607 if (AUDIO_IO_ERROR_NONE != ret) {
608 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
610 /* unset volume policy, volume will be 100% */
611 __unset_policy_for_playing();
613 ttsd_data_clear_sound_data(player->uid, &sound_data);
618 if (APP_STATE_PAUSED == player->state) {
620 SLOG(LOG_DEBUG, tts_tag(), "[Player] player(%p)", player);
621 player->paused_data = sound_data;
623 player->is_paused_data = true;
626 g_audio_state = AUDIO_STATE_READY;
628 SLOG(LOG_INFO, tts_tag(), "[Player] Stop player thread by pause");
630 /* Request prepare */
631 ret = audio_out_unprepare(g_audio_h);
632 if (AUDIO_IO_ERROR_NONE != ret) {
633 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
635 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
637 /* unset volume policy, volume will be 100% */
638 __unset_policy_for_playing();
643 if (NULL == g_playing_info && APP_STATE_READY == player->state) {
645 g_audio_state = AUDIO_STATE_READY;
646 SLOG(LOG_DEBUG, tts_tag(), "[Player] Stop player thread");
648 /* Request prepare */
649 ret = audio_out_unprepare(g_audio_h);
650 if (AUDIO_IO_ERROR_NONE != ret) {
651 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
653 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Unprepare audio");
656 ttsd_data_clear_sound_data(player->uid, &sound_data);
657 /* unset volume policy, volume will be 100% */
658 __unset_policy_for_playing();
662 if ((APP_STATE_PLAYING == player->state || APP_STATE_PAUSED == player->state) &&
663 (TTSE_RESULT_EVENT_FINISH == sound_data->event)) {
664 /* send utterence finish signal */
665 int pid = ttsd_data_get_pid(player->uid);
668 SLOG(LOG_WARN, tts_tag(), "[Send WARNIING] Current player is not valid");
669 /* unset volume policy, volume will be 100% */
670 __unset_policy_for_playing();
675 if (0 != ttsdc_send_utt_finish_message(pid, player->uid, sound_data->utt_id)) {
676 SLOG(LOG_ERROR, tts_tag(), "[Send ERROR] Fail to send Utterance Completed Signal : pid(%d), uid(%d), uttid(%d)",
677 pid, player->uid, sound_data->utt_id);
678 /* unset volume policy, volume will be 100% */
679 __unset_policy_for_playing();
681 ttsd_data_clear_sound_data(player->uid, &sound_data);
685 SLOG(LOG_INFO, tts_tag(), "[Player] Finish utterance : uid(%d), uttid(%d)", player->uid, sound_data->utt_id);
688 ttsd_data_clear_sound_data(player->uid, &sound_data);
690 if (NULL == g_playing_info) {
691 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Current player is NULL");
692 g_audio_state = AUDIO_STATE_READY;
693 ret = audio_out_unprepare(g_audio_h);
694 if (AUDIO_IO_ERROR_NONE != ret) {
695 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to unprepare audio : %d", ret);
697 /* unset volume policy, volume will be 100% */
698 __unset_policy_for_playing();
708 int ttsd_player_init()
710 g_playing_info = NULL;
711 g_audio_state = AUDIO_STATE_NONE;
716 ecore_thread_max_set(1);
720 if (0 == access(FOCUS_SERVER_READY, F_OK)) {
721 SLOG(LOG_ERROR, tts_tag(), "[Player SUCCESS] focus server is available");
725 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] focus server is not available");
730 ret = sound_manager_create_stream_information(SOUND_STREAM_TYPE_VOICE_INFORMATION, __player_focus_state_cb, NULL, &g_stream_info_h);
731 if (SOUND_MANAGER_ERROR_NONE != ret) {
732 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to create stream info");
735 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Create stream info");
738 /* add sound focus state watch callback */
739 ret = sound_manager_add_focus_state_watch_cb(SOUND_STREAM_FOCUS_FOR_PLAYBACK, __player_focus_state_watch_cb, NULL, &g_focus_watch_id);
740 if (SOUND_MANAGER_ERROR_NONE != ret) {
741 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to add sound focus watch callback");
742 sound_manager_destroy_stream_information(g_stream_info_h);
745 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Add sound focus watch callback");
748 ecore_thread_max_set(1);
750 ret = __create_audio_out(TTSE_AUDIO_TYPE_RAW_S16, 16000);
752 sound_manager_destroy_stream_information(g_stream_info_h);
753 sound_manager_remove_focus_state_watch_cb(g_focus_watch_id);
757 g_player_init = true;
762 int ttsd_player_release(void)
764 if (false == g_player_init) {
765 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
766 return TTSD_ERROR_OPERATION_FAILED;
771 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] @@@@@");
772 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Active thread count : %d", ecore_thread_active_get());
773 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] @@@@@");
775 /* The thread should be released */
776 int thread_count = ecore_thread_active_get();
778 while (0 < thread_count) {
783 SLOG(LOG_WARN, tts_tag(), "[Player WARNING!!] Thread is blocked. Player release continue.");
787 thread_count = ecore_thread_active_get();
790 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] Thread is released");
792 ret = __destroy_audio_out();
796 ret = sound_manager_destroy_stream_information(g_stream_info_h);
797 if (SOUND_MANAGER_ERROR_NONE != ret) {
798 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to destroy stream info");
800 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Destroy stream info");
803 /* remove focus state watch callback */
804 ret = sound_manager_remove_focus_state_watch_cb(g_focus_watch_id);
805 if (SOUND_MANAGER_ERROR_NONE != ret) {
806 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] Fail to remove the focus state watch cb");
808 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Remove the focus state watch cb");
811 /* clear g_player_list */
812 g_playing_info = NULL;
813 g_player_init = false;
818 int ttsd_player_create_instance(int uid)
820 if (false == g_player_init) {
821 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
825 /* Check uid is duplicated */
826 if (NULL != __player_get_item(uid)) {
827 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is already registered", uid);
831 player_s* new_client = (player_s*)calloc(1, sizeof(player_s));
832 if (NULL == new_client) {
833 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Fail to allocate memory");
834 return TTSE_ERROR_OUT_OF_MEMORY;
837 new_client->uid = uid;
838 new_client->event = TTSE_RESULT_EVENT_FINISH;
839 new_client->state = APP_STATE_READY;
840 new_client->is_paused_data = false;
842 new_client->paused_data = NULL;
844 SECURE_SLOG(LOG_DEBUG, tts_tag(), "[Player] Create player : uid(%d)", uid);
846 g_player_list = g_list_append(g_player_list, new_client);
851 int ttsd_player_destroy_instance(int uid)
853 if (false == g_player_init) {
854 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
859 current = __player_get_item(uid);
860 if (NULL == current) {
861 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
865 if (NULL != g_playing_info) {
866 if (uid == g_playing_info->uid) {
867 g_playing_info = NULL;
872 player_s *data = NULL;
874 if (0 < g_list_length(g_player_list)) {
875 /* Get a first item */
876 iter = g_list_first(g_player_list);
878 while (NULL != iter) {
879 /* Get handle data from list */
880 data = (player_s*)iter->data;
884 if (uid == data->uid) {
885 g_player_list = g_list_remove_link(g_player_list, iter);
893 iter = g_list_next(iter);
897 SLOG(LOG_DEBUG, tts_tag(), "[PLAYER Success] Destroy instance");
902 int ttsd_player_play(int uid)
904 if (false == g_player_init) {
905 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
909 if (NULL != g_playing_info) {
910 if (uid == g_playing_info->uid) {
911 SLOG(LOG_DEBUG, tts_tag(), "[Player] uid(%d) has already played", g_playing_info->uid);
914 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] stop old player (%d)", g_playing_info->uid);
915 ttsd_player_stop(g_playing_info->uid);
919 SLOG(LOG_DEBUG, tts_tag(), "[Player] start play : uid(%d)", uid);
921 /* Check sound queue size */
922 if (0 == ttsd_data_get_sound_data_size(uid)) {
923 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] A sound queue of current player(%d) is empty", uid);
929 current = __player_get_item(uid);
930 if (NULL == current) {
931 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
935 current->state = APP_STATE_PLAYING;
937 g_playing_info = current;
939 SLOG(LOG_INFO, tts_tag(), "[Player DEBUG] Active thread count : %d", ecore_thread_active_get());
941 if (0 < ttsd_data_get_sound_data_size(current->uid)) {
942 SLOG(LOG_INFO, tts_tag(), "[Player] Run thread");
943 ecore_thread_run(__play_thread, __end_play_thread, NULL, NULL);
949 int ttsd_player_stop(int uid)
951 if (false == g_player_init) {
952 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
958 current = __player_get_item(uid);
959 if (NULL == current) {
960 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
964 /* check whether uid is current playing or not */
965 if (NULL != g_playing_info) {
966 if (uid == g_playing_info->uid) {
967 /* release current playing info */
968 g_playing_info = NULL;
971 SLOG(LOG_DEBUG, tts_tag(), "[Player] No current playing");
974 if (true == current->is_paused_data) {
975 if (NULL != current->paused_data) {
976 if (NULL != current->paused_data->data) {
977 free(current->paused_data->data);
978 current->paused_data->data = NULL;
981 free(current->paused_data);
982 current->paused_data = NULL;
986 current->event = TTSE_RESULT_EVENT_FINISH;
987 current->state = APP_STATE_READY;
988 current->is_paused_data = false;
991 if (NULL == g_playing_info) {
992 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
993 SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
994 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
996 /* The thread should be released */
997 int thread_count = ecore_thread_active_get();
999 while (0 < thread_count) {
1004 SLOG(LOG_WARN, tts_tag(), "[Player WARNING!!] Thread is blocked. Player release continue.");
1008 thread_count = ecore_thread_active_get();
1011 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1012 SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
1013 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1016 SLOG(LOG_INFO, tts_tag(), "[Player SUCCESS] Stop player : uid(%d)", uid);
1021 int ttsd_player_clear(int uid)
1023 if (false == g_player_init) {
1024 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1030 current = __player_get_item(uid);
1031 if (NULL == current) {
1032 SECURE_SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
1036 if (true == current->is_paused_data) {
1037 if (NULL != current->paused_data) {
1038 if (NULL != current->paused_data->data) {
1039 free(current->paused_data->data);
1040 current->paused_data->data = NULL;
1043 free(current->paused_data);
1044 current->paused_data = NULL;
1048 current->event = TTSE_RESULT_EVENT_FINISH;
1049 current->state = APP_STATE_READY;
1050 current->is_paused_data = false;
1053 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Clear player : uid(%d)", uid);
1058 int ttsd_player_pause(int uid)
1060 SLOG(LOG_DEBUG, tts_tag(), "[Player] pause player : uid(%d)", uid);
1062 if (false == g_player_init) {
1063 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1069 current = __player_get_item(uid);
1070 if (NULL == current) {
1071 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] ttsd_player_pause() : uid(%d) is not valid", uid);
1075 /* check whether uid is current playing or not */
1076 if (NULL != g_playing_info) {
1077 if (uid == g_playing_info->uid) {
1078 /* release current playing info */
1079 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] release current playing info (%d)", uid);
1080 g_playing_info = NULL;
1086 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] current player (%p), g_playing_info(%p)", current, g_playing_info);
1088 current->state = APP_STATE_PAUSED;
1090 if (NULL == g_playing_info) {
1091 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1092 SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
1093 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1095 /* The thread should be released */
1096 int thread_count = ecore_thread_active_get();
1098 while (0 < thread_count) {
1102 SLOG(LOG_DEBUG, tts_tag(), "[Player DEBUG] current(%p), state(%d)", current, current->state);
1105 SLOG(LOG_WARN, tts_tag(), "[Player WARNING!!] Thread is blocked. Player release continue. current(%p) current state(%d)", current, current->state);
1109 thread_count = ecore_thread_active_get();
1112 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1113 SLOG(LOG_ERROR, tts_tag(), "[Player] Active thread count : %d", ecore_thread_active_get());
1114 SLOG(LOG_DEBUG, tts_tag(), "[Player] @@@@@");
1118 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] Pause player : uid(%d)", uid);
1123 int ttsd_player_resume(int uid)
1125 SLOG(LOG_DEBUG, tts_tag(), "[Player] Resume player : uid(%d)", uid);
1127 if (false == g_player_init) {
1128 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1134 current = __player_get_item(uid);
1135 if (NULL == current) {
1136 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
1140 /* check current player */
1141 if (NULL != g_playing_info)
1142 g_playing_info = NULL;
1144 current->state = APP_STATE_PLAYING;
1145 g_playing_info = current;
1147 SLOG(LOG_INFO, tts_tag(), "[Player] Resume to run thread");
1148 ecore_thread_run(__play_thread, __end_play_thread, NULL, NULL);
1153 int ttsd_player_all_stop()
1155 if (false == g_player_init) {
1156 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1160 g_playing_info = NULL;
1163 player_s *data = NULL;
1165 if (0 < g_list_length(g_player_list)) {
1166 /* Get a first item */
1167 iter = g_list_first(g_player_list);
1169 while (NULL != iter) {
1170 /* Get handle data from list */
1171 data = (player_s*)iter->data;
1173 app_tts_state_e state;
1174 if (0 > ttsd_data_get_client_state(data->uid, &state)) {
1175 SLOG(LOG_ERROR, tts_tag(), "[player ERROR] uid(%d) is not valid", data->uid);
1176 ttsd_player_destroy_instance(data->uid);
1177 iter = g_list_next(iter);
1181 if (APP_STATE_PLAYING == state || APP_STATE_PAUSED == state) {
1182 data->event = TTSE_RESULT_EVENT_FINISH;
1183 data->state = APP_STATE_READY;
1185 if (true == data->is_paused_data) {
1186 if (NULL != data->paused_data) {
1187 if (NULL != data->paused_data->data) {
1188 free(data->paused_data->data);
1189 data->paused_data->data = NULL;
1192 free(data->paused_data);
1193 data->paused_data = NULL;
1197 data->is_paused_data = false;
1202 iter = g_list_next(iter);
1206 SLOG(LOG_DEBUG, tts_tag(), "[Player SUCCESS] player all stop!!");
1211 int ttsd_player_play_pcm(int uid)
1213 if (false == g_player_init) {
1214 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] Not Initialized");
1218 if (NULL != g_playing_info) {
1219 if (uid == g_playing_info->uid) {
1220 SLOG(LOG_DEBUG, tts_tag(), "[Player] uid(%d) has already played", g_playing_info->uid);
1223 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] stop old player (%d)", g_playing_info->uid);
1224 ttsd_player_stop(g_playing_info->uid);
1228 SLOG(LOG_DEBUG, tts_tag(), "[Player] start play : uid(%d)", uid);
1230 /* Check sound queue size */
1231 if (0 == ttsd_data_get_sound_data_size(uid)) {
1232 SLOG(LOG_WARN, tts_tag(), "[Player WARNING] A sound queue of current player(%d) is empty", uid);
1237 current = __player_get_item(uid);
1238 if (NULL == current) {
1239 SLOG(LOG_ERROR, tts_tag(), "[Player ERROR] uid(%d) is not valid", uid);
1243 current->state = APP_STATE_PLAYING;
1245 g_playing_info = current;
1247 SLOG(LOG_INFO, tts_tag(), "[Player DEBUG] Active thread count : %d", ecore_thread_active_get());
1249 if (0 <= ttsd_data_get_sound_data_size(current->uid)) {
1250 SLOG(LOG_INFO, tts_tag(), "[Player] Run thread");
1251 ecore_thread_run(__play_thread, __end_play_thread, NULL, NULL);